rivetkit 0.0.0-sqlite-query-metrics-v3.1dff84f → 0.0.0-sqlite-sync-3-0-6-final.cf8679c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist/browser/client.d.ts +95 -20
  2. package/dist/browser/client.js +2 -2
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +1 -1
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/{chunk-HXJ5U2ZI.cjs → chunk-275ZGRSG.cjs} +3 -3
  7. package/dist/tsup/{chunk-HXJ5U2ZI.cjs.map → chunk-275ZGRSG.cjs.map} +1 -1
  8. package/dist/tsup/{chunk-HFGXV2CC.cjs → chunk-E54YPM4V.cjs} +10 -10
  9. package/dist/tsup/{chunk-HFGXV2CC.cjs.map → chunk-E54YPM4V.cjs.map} +1 -1
  10. package/dist/tsup/{chunk-Y4QLVCCS.cjs → chunk-I26AW6BG.cjs} +2 -2
  11. package/dist/tsup/{chunk-Y4QLVCCS.cjs.map → chunk-I26AW6BG.cjs.map} +1 -1
  12. package/dist/tsup/{chunk-CWN7N5AF.cjs → chunk-JHHUCWBC.cjs} +7 -7
  13. package/dist/tsup/{chunk-CWN7N5AF.cjs.map → chunk-JHHUCWBC.cjs.map} +1 -1
  14. package/dist/tsup/{chunk-22S5RCCJ.cjs → chunk-LYXPQB6H.cjs} +3 -3
  15. package/dist/tsup/{chunk-22S5RCCJ.cjs.map → chunk-LYXPQB6H.cjs.map} +1 -1
  16. package/dist/tsup/{chunk-NNCL4IBC.js → chunk-M72ZKQ7R.js} +2 -2
  17. package/dist/tsup/{chunk-NNCL4IBC.js.map → chunk-M72ZKQ7R.js.map} +1 -1
  18. package/dist/tsup/{chunk-QQIFRR3B.js → chunk-ME74LHJM.js} +7 -7
  19. package/dist/tsup/chunk-ME74LHJM.js.map +1 -0
  20. package/dist/tsup/{chunk-UXDASKP6.js → chunk-PBIQPYE5.js} +12 -13
  21. package/dist/tsup/chunk-PBIQPYE5.js.map +1 -0
  22. package/dist/tsup/{chunk-2CTZU7GL.js → chunk-R4QCQBB4.js} +2 -2
  23. package/dist/tsup/{chunk-SW33HMM5.js → chunk-RJ7WDGOJ.js} +3 -3
  24. package/dist/tsup/{chunk-NG5CUXMM.cjs → chunk-SJJWJU26.cjs} +145 -145
  25. package/dist/tsup/chunk-SJJWJU26.cjs.map +1 -0
  26. package/dist/tsup/{chunk-HRKFVQPS.js → chunk-SSMFNH4N.js} +4 -4
  27. package/dist/tsup/{chunk-Y5CFDGPA.cjs → chunk-TKCX6KNZ.cjs} +4 -4
  28. package/dist/tsup/{chunk-Y5CFDGPA.cjs.map → chunk-TKCX6KNZ.cjs.map} +1 -1
  29. package/dist/tsup/{chunk-JJUSO7GJ.cjs → chunk-TVBQQXQE.cjs} +3 -3
  30. package/dist/tsup/{chunk-JJUSO7GJ.cjs.map → chunk-TVBQQXQE.cjs.map} +1 -1
  31. package/dist/tsup/{chunk-F5PWAGS7.js → chunk-VXVJHTW6.js} +2 -2
  32. package/dist/tsup/{chunk-FT6VRK4C.js → chunk-XATHAIZX.js} +2 -2
  33. package/dist/tsup/{chunk-D7DWNYEO.js → chunk-Y5TV6V5Z.js} +2 -2
  34. package/dist/tsup/{chunk-SSF2XC33.cjs → chunk-YWWDUC6D.cjs} +12 -13
  35. package/dist/tsup/chunk-YWWDUC6D.cjs.map +1 -0
  36. package/dist/tsup/client/mod.cjs +5 -5
  37. package/dist/tsup/client/mod.d.cts +3 -3
  38. package/dist/tsup/client/mod.d.ts +3 -3
  39. package/dist/tsup/client/mod.js +4 -4
  40. package/dist/tsup/common/log.cjs +2 -2
  41. package/dist/tsup/common/log.js +1 -1
  42. package/dist/tsup/common/websocket.cjs +3 -3
  43. package/dist/tsup/common/websocket.js +2 -2
  44. package/dist/tsup/{config-BnMhcPHl.d.cts → config-5K0rzjv1.d.cts} +72 -25
  45. package/dist/tsup/{config-CBTr4IhE.d.cts → config-Df5jOcde.d.cts} +26 -0
  46. package/dist/tsup/{config-CBTr4IhE.d.ts → config-Df5jOcde.d.ts} +26 -0
  47. package/dist/tsup/{config-BiiS5T5M.d.ts → config-DsAjIyfZ.d.ts} +72 -25
  48. package/dist/tsup/{context-iMxXq_Lt.d.ts → context-B19Rx5Zg.d.cts} +5 -2
  49. package/dist/tsup/{context-DVE8XMm1.d.cts → context-BEh8x4jn.d.ts} +5 -2
  50. package/dist/tsup/db/drizzle.cjs +2 -2
  51. package/dist/tsup/db/drizzle.d.cts +1 -1
  52. package/dist/tsup/db/drizzle.d.ts +1 -1
  53. package/dist/tsup/db/drizzle.js +1 -1
  54. package/dist/tsup/db/mod.cjs +3 -3
  55. package/dist/tsup/db/mod.d.cts +2 -2
  56. package/dist/tsup/db/mod.d.ts +2 -2
  57. package/dist/tsup/db/mod.js +2 -2
  58. package/dist/tsup/dynamic/mod.cjs +2 -2
  59. package/dist/tsup/dynamic/mod.d.cts +2 -2
  60. package/dist/tsup/dynamic/mod.d.ts +2 -2
  61. package/dist/tsup/dynamic/mod.js +1 -1
  62. package/dist/tsup/inspector/mod.cjs +5 -5
  63. package/dist/tsup/inspector/mod.js +4 -4
  64. package/dist/tsup/inspector/workflow.cjs +4 -4
  65. package/dist/tsup/inspector/workflow.js +3 -3
  66. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  67. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  68. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  69. package/dist/tsup/inspector-tab/mod.js +1 -1
  70. package/dist/tsup/mod.cjs +548 -169
  71. package/dist/tsup/mod.cjs.map +1 -1
  72. package/dist/tsup/mod.d.cts +6 -4
  73. package/dist/tsup/mod.d.ts +6 -4
  74. package/dist/tsup/mod.js +447 -68
  75. package/dist/tsup/mod.js.map +1 -1
  76. package/dist/tsup/test/mod.cjs +6 -6
  77. package/dist/tsup/test/mod.d.cts +2 -2
  78. package/dist/tsup/test/mod.d.ts +2 -2
  79. package/dist/tsup/test/mod.js +4 -4
  80. package/dist/tsup/unstable/migrations.d.cts +1 -1
  81. package/dist/tsup/unstable/migrations.d.ts +1 -1
  82. package/dist/tsup/utils.cjs +2 -2
  83. package/dist/tsup/utils.js +1 -1
  84. package/dist/tsup/workflow/mod.cjs +13 -10
  85. package/dist/tsup/workflow/mod.cjs.map +1 -1
  86. package/dist/tsup/workflow/mod.d.cts +4 -4
  87. package/dist/tsup/workflow/mod.d.ts +4 -4
  88. package/dist/tsup/workflow/mod.js +8 -5
  89. package/dist/tsup/workflow/mod.js.map +1 -1
  90. package/package.json +9 -9
  91. package/src/actor/config.ts +41 -29
  92. package/src/actor/contexts/index.ts +2 -0
  93. package/src/actor/definition.ts +33 -1
  94. package/src/actor/schema.ts +4 -0
  95. package/src/client/actor-common.ts +4 -0
  96. package/src/client/actor-conn.ts +2 -0
  97. package/src/client/actor-handle.ts +2 -0
  98. package/src/client/config.ts +1 -1
  99. package/src/client/queue.ts +2 -0
  100. package/src/common/database/config.ts +39 -0
  101. package/src/common/database/native-database.ts +230 -21
  102. package/src/registry/index.ts +142 -1
  103. package/src/registry/napi-runtime.ts +93 -4
  104. package/src/registry/native.ts +61 -28
  105. package/src/registry/runtime.ts +58 -8
  106. package/src/registry/wasm-runtime.ts +88 -14
  107. package/src/workflow/context.ts +3 -0
  108. package/dist/tsup/chunk-NG5CUXMM.cjs.map +0 -1
  109. package/dist/tsup/chunk-QQIFRR3B.js.map +0 -1
  110. package/dist/tsup/chunk-SSF2XC33.cjs.map +0 -1
  111. package/dist/tsup/chunk-UXDASKP6.js.map +0 -1
  112. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +0 -848
  113. /package/dist/tsup/{chunk-2CTZU7GL.js.map → chunk-R4QCQBB4.js.map} +0 -0
  114. /package/dist/tsup/{chunk-SW33HMM5.js.map → chunk-RJ7WDGOJ.js.map} +0 -0
  115. /package/dist/tsup/{chunk-HRKFVQPS.js.map → chunk-SSMFNH4N.js.map} +0 -0
  116. /package/dist/tsup/{chunk-F5PWAGS7.js.map → chunk-VXVJHTW6.js.map} +0 -0
  117. /package/dist/tsup/{chunk-FT6VRK4C.js.map → chunk-XATHAIZX.js.map} +0 -0
  118. /package/dist/tsup/{chunk-D7DWNYEO.js.map → chunk-Y5TV6V5Z.js.map} +0 -0
@@ -10,6 +10,7 @@ import {
10
10
  type ActorConfig,
11
11
  type ActorConfigInput,
12
12
  ActorConfigSchema,
13
+ getRunInspectorKind,
13
14
  } from "./config";
14
15
  import { loggerWithoutContext } from "./log";
15
16
  import type { EventSchemaConfig, QueueSchemaConfig } from "./schema";
@@ -34,12 +35,42 @@ function warnDeprecatedOptionKeys(options: unknown) {
34
35
  if (opts[key] !== undefined && !warnedDeprecatedOptionKeys.has(key)) {
35
36
  warnedDeprecatedOptionKeys.add(key);
36
37
  loggerWithoutContext().warn({
37
- msg: `actor option \`${key}\` is deprecated and is now ignored. ${detail} Will be removed in 2.2.0.`,
38
+ msg: `actor option \`${key}\` is deprecated and is now ignored. ${detail} Will be removed in 3.2.0.`,
38
39
  });
39
40
  }
40
41
  }
41
42
  }
42
43
 
44
+ let warnedDeprecatedRun = false;
45
+
46
+ /**
47
+ * Warns once per process when `run` is a plain function. `run: workflow(...)`
48
+ * is the supported way to use `run` and does not warn.
49
+ */
50
+ function warnDeprecatedRun(input: unknown) {
51
+ if (warnedDeprecatedRun || !input || typeof input !== "object") return;
52
+
53
+ const run = (input as { run?: unknown }).run;
54
+ if (run === undefined) return;
55
+
56
+ const runFunction =
57
+ typeof run === "object" && run !== null
58
+ ? (run as { run?: unknown }).run
59
+ : run;
60
+ if (
61
+ typeof runFunction === "function" &&
62
+ getRunInspectorKind(runFunction as (...args: any[]) => any) ===
63
+ "workflow"
64
+ ) {
65
+ return;
66
+ }
67
+
68
+ warnedDeprecatedRun = true;
69
+ loggerWithoutContext().warn({
70
+ msg: "actor option `run` with a plain function was deprecated in 3.0 and will be removed in a future version. Start background work from `onWake` with `c.keepAwake` and track it in `c.vars`. `run: workflow(...)` is not deprecated. See https://rivet.dev/actors/docs/deprecated/run",
71
+ });
72
+ }
73
+
43
74
  export interface BaseActorDefinition<
44
75
  S,
45
76
  CP,
@@ -200,6 +231,7 @@ export function actor<
200
231
  warnDeprecatedOptionKeys(
201
232
  (input as { options?: unknown } | undefined)?.options,
202
233
  );
234
+ warnDeprecatedRun(input);
203
235
  const config = ActorConfigSchema.parse(input) as ActorConfig<
204
236
  TState,
205
237
  TConnParams,
@@ -14,6 +14,7 @@ export interface EventTypeToken<T, TContext = any> {
14
14
  export interface QueueTypeToken<TMessage, TComplete = never, TContext = any> {
15
15
  readonly _queueMessage?: TMessage;
16
16
  readonly _queueComplete?: TComplete;
17
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
17
18
  readonly canPublish?: SchemaHook<TContext>;
18
19
  readonly message?: PrimitiveSchema;
19
20
  readonly complete?: PrimitiveSchema;
@@ -28,6 +29,7 @@ interface EventOptions<TContext = any> {
28
29
  }
29
30
 
30
31
  interface QueueOptions<TContext = any> {
32
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
31
33
  canPublish?: SchemaHook<TContext>;
32
34
  message?: PrimitiveSchema;
33
35
  complete?: PrimitiveSchema;
@@ -39,6 +41,7 @@ export function event<T, TContext = any>(
39
41
  return (options ?? {}) as EventTypeToken<T, TContext>;
40
42
  }
41
43
 
44
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
42
45
  export function queue<TMessage, TComplete = never, TContext = any>(
43
46
  options?: QueueOptions<TContext>,
44
47
  ): QueueTypeToken<TMessage, TComplete, TContext> {
@@ -55,6 +58,7 @@ export interface EventSchemaDefinition<TContext = any> {
55
58
  export interface QueueSchemaDefinition<TContext = any> {
56
59
  message: PrimitiveSchema;
57
60
  complete?: PrimitiveSchema;
61
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
58
62
  canPublish?: SchemaHook<TContext>;
59
63
  }
60
64
 
@@ -92,21 +92,25 @@ export type ActorDefinitionActions<AD extends AnyActorDefinition> =
92
92
  : ActorActionMap<ActionsOf<AD>>;
93
93
 
94
94
  type ActorQueueSend<TQueues extends QueueSchemaConfig> = {
95
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
95
96
  <K extends keyof TQueues & string>(
96
97
  name: K,
97
98
  body: InferSchemaMap<TQueues>[K],
98
99
  options: QueueSendWaitOptions,
99
100
  ): Promise<QueueSendResult<InferQueueCompleteMap<TQueues>[K]>>;
101
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
100
102
  <K extends keyof TQueues & string>(
101
103
  name: K,
102
104
  body: InferSchemaMap<TQueues>[K],
103
105
  options?: QueueSendNoWaitOptions,
104
106
  ): Promise<void>;
107
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
105
108
  (
106
109
  name: keyof TQueues extends never ? string : never,
107
110
  body: unknown,
108
111
  options: QueueSendWaitOptions,
109
112
  ): Promise<QueueSendResult>;
113
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
110
114
  (
111
115
  name: keyof TQueues extends never ? string : never,
112
116
  body: unknown,
@@ -304,11 +304,13 @@ export class ActorConnRaw {
304
304
  );
305
305
  }
306
306
 
307
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
307
308
  send(
308
309
  name: string,
309
310
  body: unknown,
310
311
  options: QueueSendWaitOptions,
311
312
  ): Promise<QueueSendResult>;
313
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
312
314
  send(
313
315
  name: string,
314
316
  body: unknown,
@@ -138,11 +138,13 @@ export class ActorHandleRaw {
138
138
  return this.#params;
139
139
  }
140
140
 
141
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
141
142
  send(
142
143
  name: string,
143
144
  body: unknown,
144
145
  options: QueueSendWaitOptions,
145
146
  ): Promise<QueueSendResult>;
147
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
146
148
  send(
147
149
  name: string,
148
150
  body: unknown,
@@ -42,7 +42,7 @@ export const ClientConfigSchemaBase = z.object({
42
42
  hasWarnedMissingEndpoint = true;
43
43
  console.warn(
44
44
  `[rivetkit] No endpoint provided to client. Defaulting to ${DEFAULT_ENDPOINT}. ` +
45
- `Starting in 2.2.0, an explicit endpoint will be required. ` +
45
+ `Starting in 3.2.0, an explicit endpoint will be required. ` +
46
46
  `Pass an endpoint to createClient() or createRivetKit(), ` +
47
47
  `or set the RIVET_ENDPOINT environment variable.`,
48
48
  );
@@ -20,11 +20,13 @@ import { bufferToArrayBuffer } from "@/utils";
20
20
  import { sendHttpRequest } from "./utils";
21
21
 
22
22
  export interface QueueSender {
23
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
23
24
  send(
24
25
  name: string,
25
26
  body: unknown,
26
27
  options: QueueSendWaitOptions,
27
28
  ): Promise<QueueSendResult>;
29
+ /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */
28
30
  send(
29
31
  name: string,
30
32
  body: unknown,
@@ -133,7 +133,12 @@ export interface SqliteDatabase {
133
133
  sql: string,
134
134
  callback?: (row: unknown[], columns: string[]) => void,
135
135
  ): Promise<void>;
136
+ execSync?(
137
+ sql: string,
138
+ callback?: (row: unknown[], columns: string[]) => void,
139
+ ): void;
136
140
  execute(sql: string, params?: SqliteBindings): Promise<SqliteExecuteResult>;
141
+ executeSync?(sql: string, params?: SqliteBindings): SqliteExecuteResult;
137
142
  executeBatch(
138
143
  statements: SqliteBatchStatement[],
139
144
  ): Promise<SqliteExecuteResult[]>;
@@ -141,12 +146,31 @@ export interface SqliteDatabase {
141
146
  timeoutMs?: number,
142
147
  name?: string,
143
148
  ): Promise<SqliteTransactionDatabase>;
149
+ beginTransactionSync?(
150
+ timeoutMs?: number,
151
+ name?: string,
152
+ ): SynchronousSqliteTransactionDatabase;
144
153
  run(sql: string, params?: SqliteBindings): Promise<void>;
145
154
  query(sql: string, params?: SqliteBindings): Promise<SqliteQueryResult>;
146
155
  nativeMetrics?():
147
156
  | SqliteNativeMetrics
148
157
  | Promise<SqliteNativeMetrics | null>
149
158
  | null;
159
+ /**
160
+ * Returns the ticket of the latest local commit. Synchronous writes return before they are
161
+ * durable; pass the ticket to `whenDurable` before releasing output that depends on them.
162
+ */
163
+ commitTicket?(): number;
164
+ /**
165
+ * Resolves once the commit `ticket` names and every earlier commit are durable. Rejects with
166
+ * `sqlite.durability_failed` once the database broke and the actor is restarting.
167
+ */
168
+ whenDurable?(ticket: number): Promise<void>;
169
+ /**
170
+ * Restarts the owning actor from durable state. Every later write fails, including the final
171
+ * state save. For native database providers whose in-memory state can no longer be trusted.
172
+ */
173
+ abortGeneration?(reason: string): void;
150
174
  close(): Promise<void>;
151
175
  }
152
176
 
@@ -155,11 +179,26 @@ export interface SqliteTransactionDatabase {
155
179
  sql: string,
156
180
  callback?: (row: unknown[], columns: string[]) => void,
157
181
  ): Promise<void>;
182
+ execSync?(
183
+ sql: string,
184
+ callback?: (row: unknown[], columns: string[]) => void,
185
+ ): void;
158
186
  execute(sql: string, params?: SqliteBindings): Promise<SqliteExecuteResult>;
187
+ executeSync?(sql: string, params?: SqliteBindings): SqliteExecuteResult;
159
188
  commit(): Promise<void>;
160
189
  rollback(): Promise<void>;
161
190
  }
162
191
 
192
+ export type SynchronousSqliteTransactionDatabase = SqliteTransactionDatabase & {
193
+ execSync(
194
+ sql: string,
195
+ callback?: (row: unknown[], columns: string[]) => void,
196
+ ): void;
197
+ executeSync(sql: string, params?: SqliteBindings): SqliteExecuteResult;
198
+ commitSync(): void;
199
+ rollbackSync(): void;
200
+ };
201
+
163
202
  /**
164
203
  * Provider for opening native databases from the active runtime.
165
204
  */
@@ -6,6 +6,7 @@ import type {
6
6
  SqliteExecuteResult,
7
7
  SqliteNativeMetrics,
8
8
  SqliteTransactionDatabase,
9
+ SynchronousSqliteTransactionDatabase,
9
10
  } from "./config";
10
11
 
11
12
  type NativeBindNoValues = {
@@ -74,10 +75,15 @@ interface NativeBatchStatement {
74
75
 
75
76
  export interface JsNativeDatabaseLike {
76
77
  exec(sql: string): Promise<NativeExecResult>;
78
+ execSync(sql: string): NativeExecResult;
77
79
  execute(
78
80
  sql: string,
79
81
  params?: NativeBindParam[] | null,
80
82
  ): Promise<NativeExecuteResult>;
83
+ executeSync(
84
+ sql: string,
85
+ params?: NativeBindParam[] | null,
86
+ ): NativeExecuteResult;
81
87
  executeBatch?(
82
88
  statements: NativeBatchStatement[],
83
89
  ): Promise<NativeExecuteResult[]>;
@@ -89,6 +95,10 @@ export interface JsNativeDatabaseLike {
89
95
  timeoutMs?: number,
90
96
  name?: string,
91
97
  ): Promise<JsNativeTransactionLike>;
98
+ beginTransactionSync(
99
+ timeoutMs?: number,
100
+ name?: string,
101
+ ): JsNativeSynchronousTransactionLike;
92
102
  query(
93
103
  sql: string,
94
104
  params?: NativeBindParam[] | null,
@@ -99,9 +109,16 @@ export interface JsNativeDatabaseLike {
99
109
  ): Promise<NativeRunResult>;
100
110
  metrics?(): SqliteNativeMetrics | null;
101
111
  takeLastKvError?(): string | null;
112
+ open(): Promise<void>;
113
+ commitTicket(): number;
114
+ whenDurable(ticket: number): Promise<void>;
115
+ abortGeneration(reason: string): void;
102
116
  close(): Promise<void>;
103
117
  }
104
118
  export type StateAwareSqliteDatabase = SqliteDatabase & {
119
+ open(): Promise<void>;
120
+ commitTicket(): number;
121
+ whenDurable(ticket: number): Promise<void>;
105
122
  beginStateTransaction(
106
123
  timeoutMs?: number,
107
124
  context?: unknown,
@@ -123,6 +140,28 @@ export interface JsNativeTransactionLike {
123
140
  rollback(): Promise<void>;
124
141
  }
125
142
 
143
+ export interface JsNativeSynchronousTransactionLike
144
+ extends JsNativeTransactionLike {
145
+ execSync(sql: string): NativeExecResult;
146
+ executeSync(
147
+ sql: string,
148
+ params?: NativeBindParam[] | null,
149
+ ): NativeExecuteResult;
150
+ commitSync(): void;
151
+ rollbackSync(): void;
152
+ }
153
+
154
+ function isSynchronousTransaction(
155
+ transaction: JsNativeTransactionLike,
156
+ ): transaction is JsNativeSynchronousTransactionLike {
157
+ const candidate =
158
+ transaction as Partial<JsNativeSynchronousTransactionLike>;
159
+ return (
160
+ typeof candidate.commitSync === "function" &&
161
+ typeof candidate.rollbackSync === "function"
162
+ );
163
+ }
164
+
126
165
  function shouldAttachNativeKvError(message: string): boolean {
127
166
  return /i\/o error|unable to open database file/i.test(message);
128
167
  }
@@ -161,13 +200,19 @@ function toNativeBinding(arg: unknown): NativeBindParam {
161
200
  if (arg === null || arg === undefined) {
162
201
  return { kind: "null" };
163
202
  }
164
- if (typeof arg === "bigint") {
165
- return { kind: "int", intValue: Number(arg) };
203
+ if (
204
+ typeof arg === "bigint" ||
205
+ (typeof arg === "number" && Number.isInteger(arg))
206
+ ) {
207
+ const value = Number(arg);
208
+ if (!Number.isSafeInteger(value)) {
209
+ throw new RangeError(
210
+ "SQLite integer bindings must be within the JavaScript safe integer range.",
211
+ );
212
+ }
213
+ return { kind: "int", intValue: value };
166
214
  }
167
215
  if (typeof arg === "number") {
168
- if (Number.isInteger(arg)) {
169
- return { kind: "int", intValue: arg };
170
- }
171
216
  return { kind: "float", floatValue: arg };
172
217
  }
173
218
  if (typeof arg === "string") {
@@ -185,9 +230,13 @@ function toNativeBinding(arg: unknown): NativeBindParam {
185
230
  function extractNamedSqliteParameters(sql: string): string[] {
186
231
  const orderedNames: string[] = [];
187
232
  const seen = new Set<string>();
188
- const pattern = /([:@$][A-Za-z_][A-Za-z0-9_]*)/g;
233
+ // SQLite identifiers may contain "$" and any non-ASCII character. Consume
234
+ // whole identifiers so their suffixes cannot be mistaken for parameters.
235
+ const pattern =
236
+ /'(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`|\[[^\]]*\]|--[^\n]*|\/\*[\s\S]*?(?:\*\/|$)|([:@$][\w$\u0080-\u{10FFFF}]+(?:::[\w$\u0080-\u{10FFFF}]+)*(?:\([^)]*\))?)|[A-Za-z_\u0080-\u{10FFFF}][\w$\u0080-\u{10FFFF}]*/gu;
189
237
  for (const match of sql.matchAll(pattern)) {
190
238
  const name = match[1];
239
+ if (!name) continue;
191
240
  if (seen.has(name)) {
192
241
  continue;
193
242
  }
@@ -341,21 +390,49 @@ export function wrapJsNativeDatabase(
341
390
  sql: string,
342
391
  params?: SqliteBindings,
343
392
  ): Promise<SqliteExecuteResult> => {
344
- const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
345
- if (lastInsertRowIdColumn) {
346
- return {
347
- columns: [lastInsertRowIdColumn],
348
- rows: [[lastInsertRowId ?? 0]],
349
- changes: 0,
350
- lastInsertRowId,
351
- };
352
- }
353
-
354
393
  const release = gate.enter();
355
394
  try {
395
+ const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
396
+ if (lastInsertRowIdColumn) {
397
+ return {
398
+ columns: [lastInsertRowIdColumn],
399
+ rows: [[lastInsertRowId ?? 0]],
400
+ changes: 0,
401
+ lastInsertRowId,
402
+ };
403
+ }
404
+
356
405
  const nativeParams = toNativeBindings(sql, params);
357
406
  const result = await database.execute(sql, nativeParams);
358
- if (result.lastInsertRowId !== undefined) {
407
+ if (result.lastInsertRowId != null) {
408
+ lastInsertRowId = result.lastInsertRowId;
409
+ }
410
+ return result;
411
+ } catch (error) {
412
+ enrichNativeDatabaseError(database, error);
413
+ } finally {
414
+ release();
415
+ }
416
+ };
417
+ const executeNativeSync = (
418
+ sql: string,
419
+ params?: SqliteBindings,
420
+ ): SqliteExecuteResult => {
421
+ const release = gate.enter();
422
+ try {
423
+ const nativeParams = toNativeBindings(sql, params);
424
+ const result = database.executeSync(sql, nativeParams);
425
+ // Execute first so runtime availability and transaction admission are enforced.
426
+ const rowIdColumn = lastInsertRowIdColumnName(sql);
427
+ if (rowIdColumn) {
428
+ return {
429
+ columns: [rowIdColumn],
430
+ rows: [[lastInsertRowId ?? 0]],
431
+ changes: 0,
432
+ lastInsertRowId,
433
+ };
434
+ }
435
+ if (result.lastInsertRowId != null) {
359
436
  lastInsertRowId = result.lastInsertRowId;
360
437
  }
361
438
  return result;
@@ -387,12 +464,34 @@ export function wrapJsNativeDatabase(
387
464
  callback(row, result.columns);
388
465
  }
389
466
  },
467
+ execSync(
468
+ sql: string,
469
+ callback?: (row: unknown[], columns: string[]) => void,
470
+ ): void {
471
+ const release = gate.enter();
472
+ let result: NativeExecResult;
473
+ try {
474
+ result = database.execSync(sql);
475
+ } catch (error) {
476
+ enrichNativeDatabaseError(database, error);
477
+ } finally {
478
+ release();
479
+ }
480
+ if (callback) {
481
+ for (const row of result.rows) {
482
+ callback(row, result.columns);
483
+ }
484
+ }
485
+ },
390
486
  async execute(
391
487
  sql: string,
392
488
  params?: SqliteBindings,
393
489
  ): Promise<SqliteExecuteResult> {
394
490
  return await executeNative(sql, params);
395
491
  },
492
+ executeSync(sql: string, params?: SqliteBindings): SqliteExecuteResult {
493
+ return executeNativeSync(sql, params);
494
+ },
396
495
  async executeBatch(
397
496
  statements: SqliteBatchStatement[],
398
497
  ): Promise<SqliteExecuteResult[]> {
@@ -419,7 +518,7 @@ export function wrapJsNativeDatabase(
419
518
  return transactionResults;
420
519
  })();
421
520
  for (const result of results) {
422
- if (result.lastInsertRowId !== undefined) {
521
+ if (result.lastInsertRowId != null) {
423
522
  lastInsertRowId = result.lastInsertRowId;
424
523
  }
425
524
  }
@@ -451,7 +550,26 @@ export function wrapJsNativeDatabase(
451
550
  release();
452
551
  }
453
552
  return wrapTransaction(database, transaction, gate, (result) => {
454
- if (result.lastInsertRowId !== undefined) {
553
+ if (result.lastInsertRowId != null) {
554
+ lastInsertRowId = result.lastInsertRowId;
555
+ }
556
+ });
557
+ },
558
+ beginTransactionSync(
559
+ timeoutMs?: number,
560
+ name?: string,
561
+ ): SynchronousSqliteTransactionDatabase {
562
+ const release = gate.enter();
563
+ let transaction: JsNativeSynchronousTransactionLike;
564
+ try {
565
+ transaction = database.beginTransactionSync(timeoutMs, name);
566
+ } catch (error) {
567
+ enrichNativeDatabaseError(database, error);
568
+ } finally {
569
+ release();
570
+ }
571
+ return wrapTransaction(database, transaction, gate, (result) => {
572
+ if (result.lastInsertRowId != null) {
455
573
  lastInsertRowId = result.lastInsertRowId;
456
574
  }
457
575
  });
@@ -476,7 +594,7 @@ export function wrapJsNativeDatabase(
476
594
  release();
477
595
  }
478
596
  return wrapTransaction(database, transaction, gate, (result) => {
479
- if (result.lastInsertRowId !== undefined) {
597
+ if (result.lastInsertRowId != null) {
480
598
  lastInsertRowId = result.lastInsertRowId;
481
599
  }
482
600
  });
@@ -491,6 +609,25 @@ export function wrapJsNativeDatabase(
491
609
  nativeMetrics(): SqliteNativeMetrics | null {
492
610
  return normalizeNativeMetrics(database.metrics?.());
493
611
  },
612
+ async open(): Promise<void> {
613
+ const release = gate.enter();
614
+ try {
615
+ await database.open();
616
+ } catch (error) {
617
+ enrichNativeDatabaseError(database, error);
618
+ } finally {
619
+ release();
620
+ }
621
+ },
622
+ commitTicket(): number {
623
+ return database.commitTicket();
624
+ },
625
+ async whenDurable(ticket: number): Promise<void> {
626
+ await database.whenDurable(ticket);
627
+ },
628
+ abortGeneration(reason: string): void {
629
+ database.abortGeneration(reason);
630
+ },
494
631
  async close(): Promise<void> {
495
632
  terminalPromise ??= gate.close(() => database.close());
496
633
  await terminalPromise;
@@ -502,13 +639,25 @@ export function wrapJsNativeDatabase(
502
639
  };
503
640
  }
504
641
 
642
+ function wrapTransaction(
643
+ database: JsNativeDatabaseLike,
644
+ transaction: JsNativeSynchronousTransactionLike,
645
+ gate: NativeCloseGate,
646
+ onExecute: (result: NativeExecuteResult) => void,
647
+ ): SynchronousSqliteTransactionDatabase;
648
+ function wrapTransaction(
649
+ database: JsNativeDatabaseLike,
650
+ transaction: JsNativeTransactionLike,
651
+ gate: NativeCloseGate,
652
+ onExecute: (result: NativeExecuteResult) => void,
653
+ ): SqliteTransactionDatabase;
505
654
  function wrapTransaction(
506
655
  database: JsNativeDatabaseLike,
507
656
  transaction: JsNativeTransactionLike,
508
657
  gate: NativeCloseGate,
509
658
  onExecute: (result: NativeExecuteResult) => void,
510
659
  ): SqliteTransactionDatabase {
511
- return {
660
+ const wrapped: SqliteTransactionDatabase = {
512
661
  async exec(sql, callback) {
513
662
  const release = gate.enter();
514
663
  let result: NativeExecResult;
@@ -559,6 +708,66 @@ function wrapTransaction(
559
708
  }
560
709
  },
561
710
  };
711
+
712
+ if (isSynchronousTransaction(transaction)) {
713
+ return Object.assign(wrapped, {
714
+ execSync(
715
+ sql: string,
716
+ callback?: (row: unknown[], columns: string[]) => void,
717
+ ) {
718
+ const release = gate.enter();
719
+ let result: NativeExecResult;
720
+ try {
721
+ result = transaction.execSync(sql);
722
+ } catch (error) {
723
+ enrichNativeDatabaseError(database, error);
724
+ } finally {
725
+ release();
726
+ }
727
+ if (callback) {
728
+ for (const row of result.rows)
729
+ callback(row, result.columns);
730
+ }
731
+ },
732
+ executeSync(sql: string, params?: SqliteBindings) {
733
+ const release = gate.enter();
734
+ try {
735
+ const result = transaction.executeSync(
736
+ sql,
737
+ toNativeBindings(sql, params),
738
+ );
739
+ onExecute(result);
740
+ return result;
741
+ } catch (error) {
742
+ enrichNativeDatabaseError(database, error);
743
+ } finally {
744
+ release();
745
+ }
746
+ },
747
+ commitSync() {
748
+ const release = gate.enter();
749
+ try {
750
+ transaction.commitSync();
751
+ } catch (error) {
752
+ enrichNativeDatabaseError(database, error);
753
+ } finally {
754
+ release();
755
+ }
756
+ },
757
+ rollbackSync() {
758
+ const release = gate.enter();
759
+ try {
760
+ transaction.rollbackSync();
761
+ } catch (error) {
762
+ enrichNativeDatabaseError(database, error);
763
+ } finally {
764
+ release();
765
+ }
766
+ },
767
+ }) as SynchronousSqliteTransactionDatabase;
768
+ }
769
+
770
+ return wrapped;
562
771
  }
563
772
 
564
773
  function lastInsertRowIdColumnName(sql: string): string | undefined {