rivetkit 3.1.1-rc.1 → 3.1.2-rc.1

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 (92) hide show
  1. package/dist/browser/client.d.ts +41 -0
  2. package/dist/browser/client.js +1 -1
  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-2IGAVNUL.js → chunk-5RE3IF3C.js} +2 -2
  7. package/dist/tsup/{chunk-5HIBMO55.js → chunk-7PUO5YRK.js} +3 -3
  8. package/dist/tsup/{chunk-LZD54EPS.js → chunk-DKVKYLXH.js} +4 -4
  9. package/dist/tsup/{chunk-WLFARJG2.cjs → chunk-GI3TPFCE.cjs} +2 -2
  10. package/dist/tsup/{chunk-WLFARJG2.cjs.map → chunk-GI3TPFCE.cjs.map} +1 -1
  11. package/dist/tsup/{chunk-BDAEZMRQ.js → chunk-J5ZNIJT3.js} +2 -2
  12. package/dist/tsup/{chunk-4TUVJYKQ.cjs → chunk-KOB3RR6D.cjs} +7 -7
  13. package/dist/tsup/{chunk-4TUVJYKQ.cjs.map → chunk-KOB3RR6D.cjs.map} +1 -1
  14. package/dist/tsup/{chunk-QBM464NJ.js → chunk-LNMODE5R.js} +2 -2
  15. package/dist/tsup/{chunk-6GQM5TX3.cjs → chunk-MM6QYEV7.cjs} +146 -146
  16. package/dist/tsup/{chunk-6GQM5TX3.cjs.map → chunk-MM6QYEV7.cjs.map} +1 -1
  17. package/dist/tsup/{chunk-XOU5GIVI.cjs → chunk-R2GD2TDD.cjs} +3 -3
  18. package/dist/tsup/{chunk-XOU5GIVI.cjs.map → chunk-R2GD2TDD.cjs.map} +1 -1
  19. package/dist/tsup/{chunk-TSSTKUHB.js → chunk-SPLLB6BX.js} +2 -2
  20. package/dist/tsup/{chunk-57I23ITV.cjs → chunk-TR5YB355.cjs} +3 -3
  21. package/dist/tsup/{chunk-57I23ITV.cjs.map → chunk-TR5YB355.cjs.map} +1 -1
  22. package/dist/tsup/{chunk-E3FNI5G7.cjs → chunk-TYNEZVC2.cjs} +3 -3
  23. package/dist/tsup/{chunk-E3FNI5G7.cjs.map → chunk-TYNEZVC2.cjs.map} +1 -1
  24. package/dist/tsup/{chunk-XA44EGFZ.cjs → chunk-VUFOIAKH.cjs} +10 -10
  25. package/dist/tsup/{chunk-XA44EGFZ.cjs.map → chunk-VUFOIAKH.cjs.map} +1 -1
  26. package/dist/tsup/{chunk-HB3RUCG5.js → chunk-WD6L6OTX.js} +2 -2
  27. package/dist/tsup/{chunk-HB3RUCG5.js.map → chunk-WD6L6OTX.js.map} +1 -1
  28. package/dist/tsup/{chunk-SQ4KBKBS.cjs → chunk-YPJ3QEDL.cjs} +4 -4
  29. package/dist/tsup/{chunk-SQ4KBKBS.cjs.map → chunk-YPJ3QEDL.cjs.map} +1 -1
  30. package/dist/tsup/{chunk-ZIIQIZLQ.js → chunk-YT77YM32.js} +3 -3
  31. package/dist/tsup/client/mod.cjs +5 -5
  32. package/dist/tsup/client/mod.d.cts +3 -3
  33. package/dist/tsup/client/mod.d.ts +3 -3
  34. package/dist/tsup/client/mod.js +4 -4
  35. package/dist/tsup/common/log.cjs +2 -2
  36. package/dist/tsup/common/log.js +1 -1
  37. package/dist/tsup/common/websocket.cjs +3 -3
  38. package/dist/tsup/common/websocket.js +2 -2
  39. package/dist/tsup/{config-BDdeBoxX.d.cts → config-CTGjM4A5.d.cts} +16 -1
  40. package/dist/tsup/{config-Ch-SMJ-n.d.ts → config-DLXQ_TVP.d.ts} +16 -1
  41. package/dist/tsup/{config-CBTr4IhE.d.ts → config-Df5jOcde.d.cts} +26 -0
  42. package/dist/tsup/{config-CBTr4IhE.d.cts → config-Df5jOcde.d.ts} +26 -0
  43. package/dist/tsup/{context-B-wRuYrf.d.ts → context-Dmz9Hd6K.d.ts} +2 -2
  44. package/dist/tsup/{context-NzsxAAoN.d.cts → context-S-Y75hj1.d.cts} +2 -2
  45. package/dist/tsup/db/drizzle.cjs +2 -2
  46. package/dist/tsup/db/drizzle.d.cts +1 -1
  47. package/dist/tsup/db/drizzle.d.ts +1 -1
  48. package/dist/tsup/db/drizzle.js +1 -1
  49. package/dist/tsup/db/mod.cjs +3 -3
  50. package/dist/tsup/db/mod.d.cts +2 -2
  51. package/dist/tsup/db/mod.d.ts +2 -2
  52. package/dist/tsup/db/mod.js +2 -2
  53. package/dist/tsup/dynamic/mod.d.cts +2 -2
  54. package/dist/tsup/dynamic/mod.d.ts +2 -2
  55. package/dist/tsup/inspector/mod.cjs +5 -5
  56. package/dist/tsup/inspector/mod.js +4 -4
  57. package/dist/tsup/inspector/workflow.cjs +4 -4
  58. package/dist/tsup/inspector/workflow.js +3 -3
  59. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  60. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  61. package/dist/tsup/mod.cjs +379 -120
  62. package/dist/tsup/mod.cjs.map +1 -1
  63. package/dist/tsup/mod.d.cts +4 -4
  64. package/dist/tsup/mod.d.ts +4 -4
  65. package/dist/tsup/mod.js +298 -39
  66. package/dist/tsup/mod.js.map +1 -1
  67. package/dist/tsup/test/mod.cjs +6 -6
  68. package/dist/tsup/test/mod.d.cts +2 -2
  69. package/dist/tsup/test/mod.d.ts +2 -2
  70. package/dist/tsup/test/mod.js +4 -4
  71. package/dist/tsup/unstable/migrations.d.cts +1 -1
  72. package/dist/tsup/unstable/migrations.d.ts +1 -1
  73. package/dist/tsup/utils.cjs +2 -2
  74. package/dist/tsup/utils.js +1 -1
  75. package/dist/tsup/workflow/mod.cjs +7 -7
  76. package/dist/tsup/workflow/mod.d.cts +4 -4
  77. package/dist/tsup/workflow/mod.d.ts +4 -4
  78. package/dist/tsup/workflow/mod.js +4 -4
  79. package/package.json +9 -9
  80. package/src/common/database/config.ts +39 -0
  81. package/src/common/database/native-database.ts +230 -21
  82. package/src/registry/napi-runtime.ts +78 -0
  83. package/src/registry/native.ts +57 -17
  84. package/src/registry/runtime.ts +52 -0
  85. package/src/registry/wasm-runtime.ts +88 -0
  86. /package/dist/tsup/{chunk-2IGAVNUL.js.map → chunk-5RE3IF3C.js.map} +0 -0
  87. /package/dist/tsup/{chunk-5HIBMO55.js.map → chunk-7PUO5YRK.js.map} +0 -0
  88. /package/dist/tsup/{chunk-LZD54EPS.js.map → chunk-DKVKYLXH.js.map} +0 -0
  89. /package/dist/tsup/{chunk-BDAEZMRQ.js.map → chunk-J5ZNIJT3.js.map} +0 -0
  90. /package/dist/tsup/{chunk-QBM464NJ.js.map → chunk-LNMODE5R.js.map} +0 -0
  91. /package/dist/tsup/{chunk-TSSTKUHB.js.map → chunk-SPLLB6BX.js.map} +0 -0
  92. /package/dist/tsup/{chunk-ZIIQIZLQ.js.map → chunk-YT77YM32.js.map} +0 -0
@@ -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 {
@@ -883,6 +883,13 @@ export class NapiCoreRuntime implements CoreRuntime {
883
883
  return await this.#actorSql(ctx).exec(sql);
884
884
  }
885
885
 
886
+ actorSqlExecSync(
887
+ ctx: ActorContextHandle,
888
+ sql: string,
889
+ ): RuntimeSqlExecResult {
890
+ return this.#actorSql(ctx).execSync(sql);
891
+ }
892
+
886
893
  async actorSqlExecute(
887
894
  ctx: ActorContextHandle,
888
895
  sql: string,
@@ -895,6 +902,18 @@ export class NapiCoreRuntime implements CoreRuntime {
895
902
  return normalizeRuntimeSqlExecuteResult(result);
896
903
  }
897
904
 
905
+ actorSqlExecuteSync(
906
+ ctx: ActorContextHandle,
907
+ sql: string,
908
+ params?: RuntimeSqlBindParams,
909
+ ): RuntimeSqlExecuteResult {
910
+ const result = this.#actorSql(ctx).executeSync(
911
+ sql,
912
+ toNapiSqlBindParams(params),
913
+ );
914
+ return normalizeRuntimeSqlExecuteResult(result);
915
+ }
916
+
898
917
  async actorSqlExecuteBatch(
899
918
  ctx: ActorContextHandle,
900
919
  statements: RuntimeSqlBatchStatement[],
@@ -916,6 +935,17 @@ export class NapiCoreRuntime implements CoreRuntime {
916
935
  )) as unknown as SqliteTransactionHandle;
917
936
  }
918
937
 
938
+ actorSqlBeginTransactionSync(
939
+ ctx: ActorContextHandle,
940
+ timeoutMs?: number,
941
+ name?: string,
942
+ ): SqliteTransactionHandle {
943
+ return this.#actorSql(ctx).beginTransactionSync(
944
+ timeoutMs,
945
+ name,
946
+ ) as unknown as SqliteTransactionHandle;
947
+ }
948
+
919
949
  async actorSqlTransactionExec(
920
950
  transaction: SqliteTransactionHandle,
921
951
  sql: string,
@@ -923,6 +953,13 @@ export class NapiCoreRuntime implements CoreRuntime {
923
953
  return await asNativeSqlTransaction(transaction).exec(sql);
924
954
  }
925
955
 
956
+ actorSqlTransactionExecSync(
957
+ transaction: SqliteTransactionHandle,
958
+ sql: string,
959
+ ): RuntimeSqlExecResult {
960
+ return asNativeSqlTransaction(transaction).execSync(sql);
961
+ }
962
+
926
963
  async actorSqlTransactionExecute(
927
964
  transaction: SqliteTransactionHandle,
928
965
  sql: string,
@@ -935,17 +972,39 @@ export class NapiCoreRuntime implements CoreRuntime {
935
972
  return normalizeRuntimeSqlExecuteResult(result);
936
973
  }
937
974
 
975
+ actorSqlTransactionExecuteSync(
976
+ transaction: SqliteTransactionHandle,
977
+ sql: string,
978
+ params?: RuntimeSqlBindParams,
979
+ ): RuntimeSqlExecuteResult {
980
+ const result = asNativeSqlTransaction(transaction).executeSync(
981
+ sql,
982
+ toNapiSqlBindParams(params),
983
+ );
984
+ return normalizeRuntimeSqlExecuteResult(result);
985
+ }
986
+
938
987
  async actorSqlTransactionCommit(
939
988
  transaction: SqliteTransactionHandle,
940
989
  ): Promise<void> {
941
990
  await asNativeSqlTransaction(transaction).commit();
942
991
  }
943
992
 
993
+ actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void {
994
+ asNativeSqlTransaction(transaction).commitSync();
995
+ }
996
+
944
997
  async actorSqlTransactionRollback(
945
998
  transaction: SqliteTransactionHandle,
946
999
  ): Promise<void> {
947
1000
  await asNativeSqlTransaction(transaction).rollback();
948
1001
  }
1002
+
1003
+ actorSqlTransactionRollbackSync(
1004
+ transaction: SqliteTransactionHandle,
1005
+ ): void {
1006
+ asNativeSqlTransaction(transaction).rollbackSync();
1007
+ }
949
1008
  async actorBeginStateTransaction(
950
1009
  ctx: ActorContextHandle,
951
1010
  timeoutMs?: number,
@@ -1009,6 +1068,25 @@ export class NapiCoreRuntime implements CoreRuntime {
1009
1068
  return this.#actorSql(ctx).takeLastKvError?.() ?? null;
1010
1069
  }
1011
1070
 
1071
+ async actorSqlOpen(ctx: ActorContextHandle): Promise<void> {
1072
+ await this.#actorSql(ctx).open();
1073
+ }
1074
+
1075
+ actorSqlCommitTicket(ctx: ActorContextHandle): number {
1076
+ return this.#actorSql(ctx).commitTicket();
1077
+ }
1078
+
1079
+ actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void {
1080
+ this.#actorSql(ctx).abortGeneration(reason);
1081
+ }
1082
+
1083
+ async actorSqlWhenDurable(
1084
+ ctx: ActorContextHandle,
1085
+ ticket: number,
1086
+ ): Promise<void> {
1087
+ await this.#actorSql(ctx).whenDurable(ticket);
1088
+ }
1089
+
1012
1090
  async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
1013
1091
  const nativeCtx = asNativeActorContext(ctx);
1014
1092
  if (!this.#sqlOpened.delete(nativeCtx)) {
@@ -62,7 +62,11 @@ import {
62
62
  db as defaultDb,
63
63
  registerNativeStateTransactionOpener,
64
64
  } from "@/common/database/mod";
65
- import { wrapJsNativeDatabase } from "@/common/database/native-database";
65
+ import {
66
+ type JsNativeDatabaseLike,
67
+ type JsNativeSynchronousTransactionLike,
68
+ wrapJsNativeDatabase,
69
+ } from "@/common/database/native-database";
66
70
  import { assertJsonCompatValue, type JsonCompatValue } from "@/common/encoding";
67
71
  import { isResponseLike } from "@/common/fetch-like";
68
72
  import {
@@ -127,6 +131,7 @@ import type {
127
131
  RuntimeScheduledFireInfo,
128
132
  RuntimeServeConfig,
129
133
  RuntimeStateDeltaPayload,
134
+ SqliteTransactionHandle,
130
135
  WebSocketHandle,
131
136
  } from "./runtime";
132
137
  import { servicesBinaryUnavailable } from "./runtime";
@@ -657,6 +662,26 @@ async function closeNativeDatabaseClient(
657
662
  }
658
663
  }
659
664
 
665
+ function createNativeSqlTransaction(
666
+ runtime: CoreRuntime,
667
+ transaction: SqliteTransactionHandle,
668
+ ): JsNativeSynchronousTransactionLike {
669
+ return {
670
+ exec: (sql) => runtime.actorSqlTransactionExec(transaction, sql),
671
+ execSync: (sql) =>
672
+ runtime.actorSqlTransactionExecSync(transaction, sql),
673
+ execute: (sql, params) =>
674
+ runtime.actorSqlTransactionExecute(transaction, sql, params),
675
+ executeSync: (sql, params) =>
676
+ runtime.actorSqlTransactionExecuteSync(transaction, sql, params),
677
+ commit: () => runtime.actorSqlTransactionCommit(transaction),
678
+ commitSync: () => runtime.actorSqlTransactionCommitSync(transaction),
679
+ rollback: () => runtime.actorSqlTransactionRollback(transaction),
680
+ rollbackSync: () =>
681
+ runtime.actorSqlTransactionRollbackSync(transaction),
682
+ };
683
+ }
684
+
660
685
  function getOrCreateNativeSqlDatabase(
661
686
  runtime: CoreRuntime,
662
687
  ctx: ActorContextHandle,
@@ -667,9 +692,12 @@ function getOrCreateNativeSqlDatabase(
667
692
  return cachedDatabase;
668
693
  }
669
694
 
670
- const database = wrapJsNativeDatabase({
695
+ const bindings: JsNativeDatabaseLike = {
671
696
  exec: (sql) => runtime.actorSqlExec(ctx, sql),
697
+ execSync: (sql) => runtime.actorSqlExecSync(ctx, sql),
672
698
  execute: (sql, params) => runtime.actorSqlExecute(ctx, sql, params),
699
+ executeSync: (sql, params) =>
700
+ runtime.actorSqlExecuteSync(ctx, sql, params),
673
701
  executeBatch: (statements) =>
674
702
  runtime.actorSqlExecuteBatch(ctx, statements),
675
703
  beginTransaction: async (timeoutMs, name) => {
@@ -678,19 +706,15 @@ function getOrCreateNativeSqlDatabase(
678
706
  timeoutMs,
679
707
  name,
680
708
  );
681
- return {
682
- exec: (sql) =>
683
- runtime.actorSqlTransactionExec(transaction, sql),
684
- execute: (sql, params) =>
685
- runtime.actorSqlTransactionExecute(
686
- transaction,
687
- sql,
688
- params,
689
- ),
690
- commit: () => runtime.actorSqlTransactionCommit(transaction),
691
- rollback: () =>
692
- runtime.actorSqlTransactionRollback(transaction),
693
- };
709
+ return createNativeSqlTransaction(runtime, transaction);
710
+ },
711
+ beginTransactionSync: (timeoutMs, name) => {
712
+ const transaction = runtime.actorSqlBeginTransactionSync(
713
+ ctx,
714
+ timeoutMs,
715
+ name,
716
+ );
717
+ return createNativeSqlTransaction(runtime, transaction);
694
718
  },
695
719
  beginStateTransaction: async (timeoutMs, context) => {
696
720
  const scope = context as NativeStateTransactionScope | undefined;
@@ -730,8 +754,14 @@ function getOrCreateNativeSqlDatabase(
730
754
  run: (sql, params) => runtime.actorSqlRun(ctx, sql, params),
731
755
  metrics: () => runtime.actorSqlMetrics(ctx),
732
756
  takeLastKvError: () => runtime.actorSqlTakeLastKvError(ctx),
757
+ open: () => runtime.actorSqlOpen(ctx),
758
+ commitTicket: () => runtime.actorSqlCommitTicket(ctx),
759
+ whenDurable: (ticket) => runtime.actorSqlWhenDurable(ctx, ticket),
760
+ abortGeneration: (reason) =>
761
+ runtime.actorSqlAbortGeneration(ctx, reason),
733
762
  close: () => runtime.actorSqlClose(ctx),
734
- });
763
+ };
764
+ const database = wrapJsNativeDatabase(bindings);
735
765
  runtimeState.sql = database;
736
766
  return database;
737
767
  }
@@ -3195,10 +3225,12 @@ export class ActorContextHandleAdapter {
3195
3225
  {
3196
3226
  open: async (requestedActorId) => {
3197
3227
  void requestedActorId;
3198
- return getOrCreateNativeSqlDatabase(
3228
+ const database = getOrCreateNativeSqlDatabase(
3199
3229
  this.#runtime,
3200
3230
  this.#ctx,
3201
3231
  );
3232
+ await database.open();
3233
+ return database;
3202
3234
  },
3203
3235
  },
3204
3236
  async (timeoutMs, context) =>
@@ -3995,6 +4027,8 @@ function buildActorTracingConfig(
3995
4027
  };
3996
4028
  }
3997
4029
 
4030
+ const SQLITE_DEFERRED_COMMITS = Symbol.for("rivetkit.sqlite.deferredCommits");
4031
+
3998
4032
  function buildActorConfig(
3999
4033
  definition: AnyActorDefinition,
4000
4034
  registryConfig: RegistryConfig,
@@ -4008,12 +4042,18 @@ function buildActorConfig(
4008
4042
  const sqliteProfiling = (
4009
4043
  config.db as { sqliteProfiling?: SqliteProfilingOptions } | undefined
4010
4044
  )?.sqliteProfiling;
4045
+ // Internal opt-in for embedders that gate every output on `whenDurable`.
4046
+ const sqliteDeferredCommits =
4047
+ (config.db as Record<symbol, unknown> | undefined)?.[
4048
+ SQLITE_DEFERRED_COMMITS
4049
+ ] === true;
4011
4050
 
4012
4051
  return {
4013
4052
  name: options.name as string | undefined,
4014
4053
  icon: options.icon as string | undefined,
4015
4054
  hasDatabase: true,
4016
4055
  remoteSqlite: usesRemoteSqlite,
4056
+ sqliteDeferredCommits: sqliteDeferredCommits || undefined,
4017
4057
  sqliteProfiling,
4018
4058
  tracing: buildActorTracingConfig(
4019
4059
  definition.config.actions,