rivetkit 0.0.0-skip-ready-wait-flatten-client-options.1cae221 → 0.0.0-sqlite-uds.4e59a38

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 (253) hide show
  1. package/dist/browser/client.d.ts +561 -89
  2. package/dist/browser/client.js +1063 -430
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +22 -2
  5. package/dist/browser/inspector/client.js +1036 -232
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +6 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -74
  10. package/dist/tsup/actor/errors.d.ts +1 -74
  11. package/dist/tsup/actor/errors.js +5 -1
  12. package/dist/tsup/agent-os/index.cjs +2218 -2082
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +556 -85
  15. package/dist/tsup/agent-os/index.d.ts +556 -85
  16. package/dist/tsup/agent-os/index.js +2220 -2084
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-NFC5VU36.cjs → chunk-3GC3EP6D.cjs} +18 -10
  19. package/dist/tsup/chunk-3GC3EP6D.cjs.map +1 -0
  20. package/dist/tsup/{chunk-7MRGNVSR.js → chunk-3GLNEWGY.js} +78 -6
  21. package/dist/tsup/chunk-3GLNEWGY.js.map +1 -0
  22. package/dist/tsup/{chunk-V3QNBJ7N.cjs → chunk-4FC7TVS6.cjs} +35 -10
  23. package/dist/tsup/chunk-4FC7TVS6.cjs.map +1 -0
  24. package/dist/tsup/{chunk-POTPHU3P.cjs → chunk-62LSMTP2.cjs} +3 -3
  25. package/dist/tsup/{chunk-POTPHU3P.cjs.map → chunk-62LSMTP2.cjs.map} +1 -1
  26. package/dist/tsup/{chunk-OIKOPSXQ.js → chunk-7IVUT2JT.js} +12 -4
  27. package/dist/tsup/chunk-7IVUT2JT.js.map +1 -0
  28. package/dist/tsup/{chunk-22XU3BVY.cjs → chunk-GEYQLGXC.cjs} +925 -328
  29. package/dist/tsup/chunk-GEYQLGXC.cjs.map +1 -0
  30. package/dist/tsup/{chunk-D2TLG75Y.cjs → chunk-GMN45CQ6.cjs} +10 -10
  31. package/dist/tsup/chunk-GMN45CQ6.cjs.map +1 -0
  32. package/dist/tsup/{chunk-3BFYSYRU.js → chunk-HXUEHHJF.js} +3 -2
  33. package/dist/tsup/chunk-HXUEHHJF.js.map +1 -0
  34. package/dist/tsup/{chunk-IOYKCB6Y.cjs → chunk-I336PMWT.cjs} +44 -9
  35. package/dist/tsup/chunk-I336PMWT.cjs.map +1 -0
  36. package/dist/tsup/{chunk-RZEJJJKU.js → chunk-IB73AMGJ.js} +42 -7
  37. package/dist/tsup/chunk-IB73AMGJ.js.map +1 -0
  38. package/dist/tsup/{chunk-4ZIWB4CQ.js → chunk-ITWJ6Y7G.js} +2 -2
  39. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JZ7TWV65.js} +1 -1
  40. package/dist/tsup/chunk-JZ7TWV65.js.map +1 -0
  41. package/dist/tsup/{chunk-W54QCENI.js → chunk-KTNBYGRU.js} +134 -20
  42. package/dist/tsup/chunk-KTNBYGRU.js.map +1 -0
  43. package/dist/tsup/{chunk-AK57AL7S.cjs → chunk-OAPVVVTX.cjs} +79 -7
  44. package/dist/tsup/chunk-OAPVVVTX.cjs.map +1 -0
  45. package/dist/tsup/{chunk-TPBHXFB2.cjs → chunk-OUDV3ITX.cjs} +138 -24
  46. package/dist/tsup/chunk-OUDV3ITX.cjs.map +1 -0
  47. package/dist/tsup/{chunk-XNZR7UD2.cjs → chunk-PRXTIERO.cjs} +90 -65
  48. package/dist/tsup/chunk-PRXTIERO.cjs.map +1 -0
  49. package/dist/tsup/{chunk-LLD2D5LO.cjs → chunk-VE2X4KMG.cjs} +3 -2
  50. package/dist/tsup/chunk-VE2X4KMG.cjs.map +1 -0
  51. package/dist/tsup/{chunk-KSG2COSQ.js → chunk-WRZEVVLL.js} +6 -6
  52. package/dist/tsup/chunk-WRZEVVLL.js.map +1 -0
  53. package/dist/tsup/{chunk-LNMB25HW.js → chunk-XAIMHUPG.js} +780 -183
  54. package/dist/tsup/chunk-XAIMHUPG.js.map +1 -0
  55. package/dist/tsup/{chunk-D4TSTM6C.js → chunk-XZ62SWSC.js} +83 -58
  56. package/dist/tsup/chunk-XZ62SWSC.js.map +1 -0
  57. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-ZA7FLHKH.cjs} +1 -1
  58. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +1 -0
  59. package/dist/tsup/{chunk-K34B3OVG.js → chunk-ZZ3WBRPD.js} +34 -9
  60. package/dist/tsup/chunk-ZZ3WBRPD.js.map +1 -0
  61. package/dist/tsup/client/mod.cjs +9 -9
  62. package/dist/tsup/client/mod.cjs.map +1 -1
  63. package/dist/tsup/client/mod.d.cts +8 -9
  64. package/dist/tsup/client/mod.d.ts +8 -9
  65. package/dist/tsup/client/mod.js +8 -8
  66. package/dist/tsup/common/log.cjs +5 -3
  67. package/dist/tsup/common/log.cjs.map +1 -1
  68. package/dist/tsup/common/log.d.cts +5 -1
  69. package/dist/tsup/common/log.d.ts +5 -1
  70. package/dist/tsup/common/log.js +6 -4
  71. package/dist/tsup/common/websocket.cjs +4 -4
  72. package/dist/tsup/common/websocket.js +3 -3
  73. package/dist/tsup/{config-CRdGwwbr.d.cts → config-CxG53eQo.d.cts} +577 -36
  74. package/dist/tsup/{config-kxXbtrJU.d.ts → config-DLOxy81E.d.cts} +5 -5
  75. package/dist/tsup/{config-kxXbtrJU.d.cts → config-DLOxy81E.d.ts} +5 -5
  76. package/dist/tsup/{config-DIpBWm5C.d.ts → config-Ds6DCRhV.d.ts} +577 -36
  77. package/dist/tsup/context-8KlcOYIs.d.cts +129 -0
  78. package/dist/tsup/context-Cfbm4QLr.d.ts +129 -0
  79. package/dist/tsup/db/drizzle.cjs +151 -79
  80. package/dist/tsup/db/drizzle.cjs.map +1 -1
  81. package/dist/tsup/db/drizzle.d.cts +2 -2
  82. package/dist/tsup/db/drizzle.d.ts +2 -2
  83. package/dist/tsup/db/drizzle.js +151 -79
  84. package/dist/tsup/db/drizzle.js.map +1 -1
  85. package/dist/tsup/db/mod.cjs +79 -24
  86. package/dist/tsup/db/mod.cjs.map +1 -1
  87. package/dist/tsup/db/mod.d.cts +2 -2
  88. package/dist/tsup/db/mod.d.ts +2 -2
  89. package/dist/tsup/db/mod.js +78 -23
  90. package/dist/tsup/db/mod.js.map +1 -1
  91. package/dist/tsup/dynamic/mod.cjs +24 -0
  92. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  93. package/dist/tsup/dynamic/mod.d.cts +37 -0
  94. package/dist/tsup/dynamic/mod.d.ts +37 -0
  95. package/dist/tsup/dynamic/mod.js +24 -0
  96. package/dist/tsup/dynamic/mod.js.map +1 -0
  97. package/dist/tsup/inspector/mod.cjs +47 -12
  98. package/dist/tsup/inspector/mod.cjs.map +1 -1
  99. package/dist/tsup/inspector/mod.d.cts +37 -15
  100. package/dist/tsup/inspector/mod.d.ts +37 -15
  101. package/dist/tsup/inspector/mod.js +46 -11
  102. package/dist/tsup/inspector/mod.js.map +1 -1
  103. package/dist/tsup/inspector-tab/mod.cjs +162 -0
  104. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  105. package/dist/tsup/inspector-tab/mod.d.cts +240 -0
  106. package/dist/tsup/inspector-tab/mod.d.ts +240 -0
  107. package/dist/tsup/inspector-tab/mod.js +162 -0
  108. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  109. package/dist/tsup/mod.cjs +1078 -479
  110. package/dist/tsup/mod.cjs.map +1 -1
  111. package/dist/tsup/mod.d.cts +5 -6
  112. package/dist/tsup/mod.d.ts +5 -6
  113. package/dist/tsup/mod.js +996 -397
  114. package/dist/tsup/mod.js.map +1 -1
  115. package/dist/tsup/test/mod.cjs +21 -18
  116. package/dist/tsup/test/mod.cjs.map +1 -1
  117. package/dist/tsup/test/mod.d.cts +4 -5
  118. package/dist/tsup/test/mod.d.ts +4 -5
  119. package/dist/tsup/test/mod.js +18 -15
  120. package/dist/tsup/test/mod.js.map +1 -1
  121. package/dist/tsup/utils-CqDnC_PS.d.cts +104 -0
  122. package/dist/tsup/utils-CqDnC_PS.d.ts +104 -0
  123. package/dist/tsup/utils.cjs +3 -3
  124. package/dist/tsup/utils.d.cts +1 -1
  125. package/dist/tsup/utils.d.ts +1 -1
  126. package/dist/tsup/utils.js +2 -2
  127. package/dist/tsup/workflow/mod.cjs +386 -322
  128. package/dist/tsup/workflow/mod.cjs.map +1 -1
  129. package/dist/tsup/workflow/mod.d.cts +8 -9
  130. package/dist/tsup/workflow/mod.d.ts +8 -9
  131. package/dist/tsup/workflow/mod.js +363 -299
  132. package/dist/tsup/workflow/mod.js.map +1 -1
  133. package/package.json +35 -14
  134. package/src/actor/config.ts +188 -59
  135. package/src/actor/contexts/index.ts +7 -2
  136. package/src/actor/definition.ts +17 -19
  137. package/src/actor/driver.ts +3 -3
  138. package/src/actor/errors.ts +70 -7
  139. package/src/actor/instance/mod.ts +39 -56
  140. package/src/actor/keys.ts +2 -1
  141. package/src/actor/mod.ts +22 -20
  142. package/src/actor/schema.ts +2 -2
  143. package/src/agent-os/actor/index.ts +38 -18
  144. package/src/agent-os/actor/preview.ts +1 -2
  145. package/src/agent-os/actor/session.ts +2 -2
  146. package/src/agent-os/config.ts +1 -1
  147. package/src/agent-os/fs/database-vfs.ts +1 -1
  148. package/src/agent-os/index.ts +16 -15
  149. package/src/client/actor-common.ts +88 -62
  150. package/src/client/actor-conn.ts +58 -63
  151. package/src/client/actor-handle.ts +125 -71
  152. package/src/client/actor-query.ts +5 -5
  153. package/src/client/config.ts +3 -3
  154. package/src/client/errors.ts +2 -1
  155. package/src/client/lifecycle-errors.ts +2 -4
  156. package/src/client/query.ts +1 -1
  157. package/src/client/queue.ts +8 -3
  158. package/src/client/raw-utils.ts +10 -10
  159. package/src/client/resolve-gateway-target.ts +1 -1
  160. package/src/client/utils.ts +34 -10
  161. package/src/common/actor-router-consts.ts +7 -4
  162. package/src/common/actor-websocket.ts +3 -1
  163. package/src/common/bare/actor-persist/v1.ts +205 -163
  164. package/src/common/bare/actor-persist/v2.ts +265 -213
  165. package/src/common/bare/actor-persist/v3.ts +176 -172
  166. package/src/common/bare/actor-persist/v4.ts +254 -253
  167. package/src/common/bare/generated/client-protocol/v4.ts +599 -0
  168. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  169. package/src/common/bare/transport/v1.ts +694 -542
  170. package/src/common/client-protocol-versioned.ts +176 -67
  171. package/src/common/client-protocol-zod.ts +7 -0
  172. package/src/common/client-protocol.ts +1 -1
  173. package/src/common/database/config.ts +16 -5
  174. package/src/common/database/mod.test.ts +83 -16
  175. package/src/common/database/mod.ts +86 -30
  176. package/src/common/database/native-database.test.ts +35 -22
  177. package/src/common/database/native-database.ts +26 -10
  178. package/src/common/database/shared.ts +1 -0
  179. package/src/common/encoding.ts +250 -16
  180. package/src/common/engine.ts +28 -1
  181. package/src/common/eventsource.ts +1 -1
  182. package/src/common/inline-websocket-adapter.ts +14 -13
  183. package/src/common/log.ts +15 -8
  184. package/src/common/router.ts +49 -23
  185. package/src/common/utils.ts +10 -202
  186. package/src/common/websocket-interface.ts +1 -1
  187. package/src/db/drizzle.ts +173 -96
  188. package/src/db/mod.ts +1 -1
  189. package/src/devtools-loader/index.ts +4 -7
  190. package/src/devtools-loader/serve-devtools.ts +26 -0
  191. package/src/drivers/engine/actor-driver.ts +76 -73
  192. package/src/dynamic/instance.ts +32 -0
  193. package/src/dynamic/internal.ts +50 -0
  194. package/src/dynamic/isolate-runtime.ts +66 -0
  195. package/src/dynamic/mod.ts +32 -0
  196. package/src/engine-client/actor-http-client.ts +6 -9
  197. package/src/engine-client/actor-websocket-client.ts +14 -13
  198. package/src/engine-client/api-endpoints.ts +51 -2
  199. package/src/engine-client/api-utils.ts +2 -2
  200. package/src/engine-client/driver.ts +3 -4
  201. package/src/engine-client/mod.ts +17 -10
  202. package/src/engine-client/ws-proxy.ts +9 -4
  203. package/src/inspector/client.browser.ts +66 -15
  204. package/src/inspector/mod.ts +2 -4
  205. package/src/inspector-tab/mod.ts +300 -0
  206. package/src/registry/config/envoy.ts +1 -2
  207. package/src/registry/config/index.ts +46 -20
  208. package/src/registry/index.ts +364 -87
  209. package/src/registry/napi-runtime.ts +97 -8
  210. package/src/registry/native-validation.ts +10 -12
  211. package/src/registry/native.ts +592 -343
  212. package/src/registry/process-metrics.ts +250 -0
  213. package/src/registry/runtime.ts +85 -6
  214. package/src/registry/wasm-runtime.ts +72 -3
  215. package/src/registry/write-through-proxy.ts +40 -0
  216. package/src/serde.ts +2 -2
  217. package/src/serverless/configure.ts +19 -8
  218. package/src/test/mod.ts +11 -8
  219. package/src/utils/crypto.ts +14 -0
  220. package/src/utils/endpoint-parser.ts +1 -1
  221. package/src/utils/env-vars.ts +37 -0
  222. package/src/utils/router.ts +1 -1
  223. package/src/utils.ts +1 -2
  224. package/src/workflow/context.ts +703 -240
  225. package/src/workflow/driver.ts +23 -12
  226. package/src/workflow/inspector.ts +12 -3
  227. package/src/workflow/mod.ts +37 -23
  228. package/dist/tsup/chunk-22XU3BVY.cjs.map +0 -1
  229. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  230. package/dist/tsup/chunk-3BFYSYRU.js.map +0 -1
  231. package/dist/tsup/chunk-7MRGNVSR.js.map +0 -1
  232. package/dist/tsup/chunk-AK57AL7S.cjs.map +0 -1
  233. package/dist/tsup/chunk-D2TLG75Y.cjs.map +0 -1
  234. package/dist/tsup/chunk-D4TSTM6C.js.map +0 -1
  235. package/dist/tsup/chunk-IOYKCB6Y.cjs.map +0 -1
  236. package/dist/tsup/chunk-K34B3OVG.js.map +0 -1
  237. package/dist/tsup/chunk-KSG2COSQ.js.map +0 -1
  238. package/dist/tsup/chunk-LLD2D5LO.cjs.map +0 -1
  239. package/dist/tsup/chunk-LNMB25HW.js.map +0 -1
  240. package/dist/tsup/chunk-NFC5VU36.cjs.map +0 -1
  241. package/dist/tsup/chunk-OIKOPSXQ.js.map +0 -1
  242. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  243. package/dist/tsup/chunk-RZEJJJKU.js.map +0 -1
  244. package/dist/tsup/chunk-TPBHXFB2.cjs.map +0 -1
  245. package/dist/tsup/chunk-V3QNBJ7N.cjs.map +0 -1
  246. package/dist/tsup/chunk-W54QCENI.js.map +0 -1
  247. package/dist/tsup/chunk-XNZR7UD2.cjs.map +0 -1
  248. package/dist/tsup/context-B7G6sk7x.d.ts +0 -92
  249. package/dist/tsup/context-DzMRHkjC.d.cts +0 -92
  250. package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
  251. package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
  252. package/src/utils/serve.ts +0 -217
  253. /package/dist/tsup/{chunk-4ZIWB4CQ.js.map → chunk-ITWJ6Y7G.js.map} +0 -0
@@ -7,9 +7,7 @@ interface DatabaseFactoryConfig {
7
7
  onMigrate?: (db: RawAccess) => Promise<void> | void;
8
8
  }
9
9
 
10
- type RawAccessWithWriteMode = RawAccess & {
11
- __rivetWriteMode: <T>(callback: () => Promise<T> | T) => Promise<T>;
12
- };
10
+ const MIGRATION_LEASE_TIMEOUT_MS = 60_000;
13
11
 
14
12
  function hasMultipleStatements(query: string): boolean {
15
13
  const trimmed = query.trim().replace(/;+$/, "").trimEnd();
@@ -38,7 +36,10 @@ export function db({
38
36
  }
39
37
  };
40
38
 
41
- const client: RawAccessWithWriteMode = {
39
+ const createClient = (lease?: {
40
+ key: string;
41
+ timeoutMs?: number;
42
+ }): RawAccess => ({
42
43
  execute: async <
43
44
  TRow extends Record<string, unknown> = Record<
44
45
  string,
@@ -64,6 +65,7 @@ export function db({
64
65
  const { rows, columns } = await db.execute(
65
66
  query,
66
67
  bindings,
68
+ lease,
67
69
  );
68
70
  return rows.map((row) =>
69
71
  rowToObject<TRow>(row, columns),
@@ -71,13 +73,17 @@ export function db({
71
73
  }
72
74
 
73
75
  if (!hasMultipleStatements(query)) {
74
- const { rows, columns } = await db.execute(query);
76
+ const { rows, columns } = await db.execute(
77
+ query,
78
+ undefined,
79
+ lease,
80
+ );
75
81
  return rows.map((row) =>
76
82
  rowToObject<TRow>(row, columns),
77
83
  );
78
84
  }
79
85
 
80
- return await execMultiStatement<TRow>(db, query);
86
+ return await execMultiStatement<TRow>(db, query, lease);
81
87
  } finally {
82
88
  const durationMs = performance.now() - start;
83
89
  ctx.metrics?.trackSql(query, durationMs);
@@ -96,6 +102,40 @@ export function db({
96
102
  }
97
103
  }
98
104
  },
105
+ transaction: async <T>(
106
+ callback: (tx: RawAccess) => Promise<T> | T,
107
+ ): Promise<T> => {
108
+ const transactionLease = { key: crypto.randomUUID() };
109
+ const tx = createClient(transactionLease);
110
+ await db.execute("BEGIN", undefined, transactionLease);
111
+ try {
112
+ const result = await callback(tx);
113
+ await db.execute("COMMIT", undefined, transactionLease);
114
+ return result;
115
+ } catch (error) {
116
+ try {
117
+ await db.execute(
118
+ "ROLLBACK",
119
+ undefined,
120
+ transactionLease,
121
+ );
122
+ } catch {
123
+ // A deadline poisons the lease key, so its cleanup rollback is
124
+ // expected to fail. Preserve the callback or commit error.
125
+ }
126
+ throw error;
127
+ }
128
+ },
129
+ __withSqliteLease: async <T>(
130
+ timeoutMs: number,
131
+ callback: (leased: RawAccess) => Promise<T> | T,
132
+ ): Promise<T> => {
133
+ const internalLease = {
134
+ key: crypto.randomUUID(),
135
+ timeoutMs,
136
+ };
137
+ return await callback(createClient(internalLease));
138
+ },
99
139
  close: async () => {
100
140
  if (!closed) {
101
141
  closed = true;
@@ -103,17 +143,15 @@ export function db({
103
143
  }
104
144
  },
105
145
  nativeMetrics: () => db.nativeMetrics?.() ?? null,
106
- __rivetWriteMode: async <T>(
107
- callback: () => Promise<T> | T,
108
- ): Promise<T> => {
109
- return await db.writeMode(async () => await callback());
110
- },
111
- };
146
+ });
147
+ const client = createClient();
112
148
  return client;
113
149
  },
114
150
  onMigrate: async (client) => {
115
151
  if (onMigrate) {
116
- await dbWriteMode(client, () => onMigrate(client));
152
+ await withMigrationSavepoint(client, (leased) =>
153
+ onMigrate(leased),
154
+ );
117
155
  }
118
156
  },
119
157
  };
@@ -133,29 +171,47 @@ function rowToObject<TRow extends Record<string, unknown>>(
133
171
  async function execMultiStatement<TRow extends Record<string, unknown>>(
134
172
  db: SqliteDatabase,
135
173
  query: string,
174
+ lease?: { key: string; timeoutMs?: number },
136
175
  ): Promise<TRow[]> {
137
176
  const results: Record<string, unknown>[] = [];
138
177
  let columnNames: string[] | null = null;
139
- await db.exec(query, (row: unknown[], columns: string[]) => {
140
- if (!columnNames) {
141
- columnNames = columns;
142
- }
143
- results.push(rowToObject(row, columnNames));
144
- });
178
+ await db.exec(
179
+ query,
180
+ (row: unknown[], columns: string[]) => {
181
+ if (!columnNames) {
182
+ columnNames = columns;
183
+ }
184
+ results.push(rowToObject(row, columnNames));
185
+ },
186
+ lease,
187
+ );
145
188
  return results as TRow[];
146
189
  }
147
190
 
148
- async function dbWriteMode<T>(
191
+ async function withMigrationSavepoint<T>(
149
192
  client: RawAccess,
150
- callback: () => Promise<T> | T,
193
+ callback: (leased: RawAccess) => Promise<T> | T,
151
194
  ): Promise<T> {
152
- const maybeClient = client as RawAccess & {
153
- __rivetWriteMode?: <TInner>(
154
- callback: () => Promise<TInner> | TInner,
155
- ) => Promise<TInner>;
156
- };
157
- if (maybeClient.__rivetWriteMode) {
158
- return await maybeClient.__rivetWriteMode(callback);
159
- }
160
- return await callback();
195
+ return await client.__withSqliteLease(
196
+ MIGRATION_LEASE_TIMEOUT_MS,
197
+ async (leased) => {
198
+ await leased.execute("SAVEPOINT __rivet_on_migrate");
199
+ try {
200
+ const result = await callback(leased);
201
+ await leased.execute("RELEASE SAVEPOINT __rivet_on_migrate");
202
+ return result;
203
+ } catch (error) {
204
+ try {
205
+ await leased.execute(
206
+ "ROLLBACK TO SAVEPOINT __rivet_on_migrate",
207
+ );
208
+ } finally {
209
+ await leased.execute(
210
+ "RELEASE SAVEPOINT __rivet_on_migrate",
211
+ );
212
+ }
213
+ throw error;
214
+ }
215
+ },
216
+ );
161
217
  }
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, test } from "vitest";
2
+ import { BRIDGE_RIVET_ERROR_PREFIX } from "@/actor/errors";
2
3
  import {
3
4
  type JsNativeDatabaseLike,
4
5
  wrapJsNativeDatabase,
@@ -64,6 +65,14 @@ class FakeNativeDatabase implements JsNativeDatabaseLike {
64
65
  });
65
66
  }
66
67
 
68
+ rejectNext(error: unknown) {
69
+ const pending = this.#pending.shift();
70
+ if (!pending) {
71
+ throw new Error("no pending native execute call");
72
+ }
73
+ pending.reject(error);
74
+ }
75
+
67
76
  async #startExecute(
68
77
  sql: string,
69
78
  params: NativeParams,
@@ -104,28 +113,6 @@ describe("wrapJsNativeDatabase", () => {
104
113
  });
105
114
  });
106
115
 
107
- test("keeps write mode on the normal native execute lane", async () => {
108
- const native = new FakeNativeDatabase();
109
- const db = wrapJsNativeDatabase(native);
110
-
111
- const query = db.writeMode(async () => {
112
- const promise = db.query("SELECT 1");
113
- expect(native.executeCalls).toMatchObject([
114
- { sql: "SELECT 1", write: false },
115
- ]);
116
- native.resolveNext({
117
- columns: ["value"],
118
- rows: [[1]],
119
- });
120
- return await promise;
121
- });
122
-
123
- await expect(query).resolves.toEqual({
124
- columns: ["value"],
125
- rows: [[1]],
126
- });
127
- });
128
-
129
116
  test("normalizes supported sqlite bind values", async () => {
130
117
  const native = new FakeNativeDatabase();
131
118
  const db = wrapJsNativeDatabase(native);
@@ -195,4 +182,30 @@ describe("wrapJsNativeDatabase", () => {
195
182
  "Database is closed",
196
183
  );
197
184
  });
185
+
186
+ test("decodes sanitized native bridge errors without logging", async () => {
187
+ const native = new FakeNativeDatabase();
188
+ const db = wrapJsNativeDatabase(native);
189
+ const query = db.execute("SELECT broken", [1, "two"]);
190
+ const bridgeReason = `${BRIDGE_RIVET_ERROR_PREFIX}${JSON.stringify({
191
+ group: "rivetkit",
192
+ code: "internal_error",
193
+ message: "An internal error occurred",
194
+ statusCode: 500,
195
+ })}`;
196
+
197
+ native.rejectNext(new Error(bridgeReason));
198
+
199
+ await expect(query).rejects.toMatchObject({
200
+ name: "RivetError",
201
+ group: "rivetkit",
202
+ code: "internal_error",
203
+ message: "An internal error occurred",
204
+ });
205
+
206
+ await query.catch((error) => {
207
+ expect(error.stack).toContain("decodeBridgeRivetError");
208
+ expect(error.stack).toContain("enrichNativeDatabaseError");
209
+ });
210
+ });
198
211
  });
@@ -3,6 +3,7 @@ import type {
3
3
  SqliteBindings,
4
4
  SqliteDatabase,
5
5
  SqliteExecuteResult,
6
+ SqliteLeaseOptions,
6
7
  SqliteNativeMetrics,
7
8
  } from "./config";
8
9
 
@@ -66,10 +67,16 @@ interface NativeExecuteResult {
66
67
  }
67
68
 
68
69
  export interface JsNativeDatabaseLike {
69
- exec(sql: string): Promise<NativeExecResult>;
70
+ exec(
71
+ sql: string,
72
+ leaseKey?: string,
73
+ leaseTimeoutMs?: number,
74
+ ): Promise<NativeExecResult>;
70
75
  execute(
71
76
  sql: string,
72
77
  params?: NativeBindParam[] | null,
78
+ leaseKey?: string,
79
+ leaseTimeoutMs?: number,
73
80
  ): Promise<NativeExecuteResult>;
74
81
  query(
75
82
  sql: string,
@@ -92,12 +99,16 @@ function enrichNativeDatabaseError(
92
99
  database: JsNativeDatabaseLike,
93
100
  error: unknown,
94
101
  ): never {
95
- const bridged =
102
+ const bridgeReason =
96
103
  typeof error === "string"
97
- ? decodeBridgeRivetError(error)
104
+ ? error
98
105
  : error instanceof Error
99
- ? decodeBridgeRivetError(error.message)
106
+ ? error.message
100
107
  : undefined;
108
+ const bridged =
109
+ bridgeReason === undefined
110
+ ? undefined
111
+ : decodeBridgeRivetError(bridgeReason);
101
112
  if (bridged) {
102
113
  throw bridged;
103
114
  }
@@ -286,6 +297,7 @@ export function wrapJsNativeDatabase(
286
297
  const executeNative = async (
287
298
  sql: string,
288
299
  params?: SqliteBindings,
300
+ lease?: SqliteLeaseOptions,
289
301
  ): Promise<SqliteExecuteResult> => {
290
302
  const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
291
303
  if (lastInsertRowIdColumn) {
@@ -300,7 +312,12 @@ export function wrapJsNativeDatabase(
300
312
  const release = gate.enter();
301
313
  try {
302
314
  const nativeParams = toNativeBindings(sql, params);
303
- const result = await database.execute(sql, nativeParams);
315
+ const result = await database.execute(
316
+ sql,
317
+ nativeParams,
318
+ lease?.key,
319
+ lease?.timeoutMs,
320
+ );
304
321
  if (result.lastInsertRowId !== undefined) {
305
322
  lastInsertRowId = result.lastInsertRowId;
306
323
  }
@@ -316,11 +333,12 @@ export function wrapJsNativeDatabase(
316
333
  async exec(
317
334
  sql: string,
318
335
  callback?: (row: unknown[], columns: string[]) => void,
336
+ lease?: SqliteLeaseOptions,
319
337
  ): Promise<void> {
320
338
  const release = gate.enter();
321
339
  let result: NativeExecResult;
322
340
  try {
323
- result = await database.exec(sql);
341
+ result = await database.exec(sql, lease?.key, lease?.timeoutMs);
324
342
  } catch (error) {
325
343
  enrichNativeDatabaseError(database, error);
326
344
  } finally {
@@ -336,8 +354,9 @@ export function wrapJsNativeDatabase(
336
354
  async execute(
337
355
  sql: string,
338
356
  params?: SqliteBindings,
357
+ lease?: SqliteLeaseOptions,
339
358
  ): Promise<SqliteExecuteResult> {
340
- return await executeNative(sql, params);
359
+ return await executeNative(sql, params, lease);
341
360
  },
342
361
  async run(sql: string, params?: SqliteBindings): Promise<void> {
343
362
  await executeNative(sql, params);
@@ -346,9 +365,6 @@ export function wrapJsNativeDatabase(
346
365
  const { columns, rows } = await executeNative(sql, params);
347
366
  return { columns, rows };
348
367
  },
349
- async writeMode<T>(callback: () => Promise<T>): Promise<T> {
350
- return await callback();
351
- },
352
368
  nativeMetrics(): SqliteNativeMetrics | null {
353
369
  return normalizeNativeMetrics(database.metrics?.());
354
370
  },
@@ -1,4 +1,5 @@
1
1
  import type { SqliteBindings } from "./config";
2
+
2
3
  type SqliteBindingObject = Record<string, unknown>;
3
4
 
4
5
  function isSqliteBindingValue(value: unknown): boolean {
@@ -1,5 +1,5 @@
1
- import { z } from "zod/v4";
2
1
  import type { VersionedDataHandler } from "vbare";
2
+ import { z } from "zod/v4";
3
3
  import { serializeWithEncoding } from "@/serde";
4
4
  import { assertUnreachable } from "./utils";
5
5
 
@@ -13,6 +13,171 @@ const JSON_COMPAT_BIGINT = "$BigInt";
13
13
  const JSON_COMPAT_ARRAY_BUFFER = "$ArrayBuffer";
14
14
  const JSON_COMPAT_UINT8_ARRAY = "$Uint8Array";
15
15
  const JSON_COMPAT_UNDEFINED = "$Undefined";
16
+ const JSON_COMPAT_SET = "$Set";
17
+
18
+ /**
19
+ * Types that cbor-x encodes natively without any compat transforms.
20
+ */
21
+ export type CborSerializable =
22
+ | string
23
+ | number
24
+ | boolean
25
+ | null
26
+ | undefined
27
+ | bigint
28
+ | Date
29
+ | RegExp
30
+ | Error
31
+ | ArrayBuffer
32
+ | Uint8Array
33
+ | Uint8ClampedArray
34
+ | Uint16Array
35
+ | Uint32Array
36
+ | BigUint64Array
37
+ | Int8Array
38
+ | Int16Array
39
+ | Int32Array
40
+ | BigInt64Array
41
+ | Float32Array
42
+ | Float64Array
43
+ | CborSerializable[]
44
+ | Map<CborSerializable, CborSerializable>
45
+ | { [key: string]: CborSerializable };
46
+
47
+ /**
48
+ * User-facing serializable type. Extends CborSerializable with Set (encoded
49
+ * as a `$Set` tagged array by the JSON compat layer).
50
+ */
51
+ export type JsonCompatValue =
52
+ | CborSerializable
53
+ | Set<JsonCompatValue>
54
+ | JsonCompatValue[]
55
+ | Map<JsonCompatValue, JsonCompatValue>
56
+ | { [key: string]: JsonCompatValue };
57
+
58
+ function isTypedArray(value: unknown): boolean {
59
+ return (
60
+ value instanceof Uint8ClampedArray ||
61
+ value instanceof Uint16Array ||
62
+ value instanceof Uint32Array ||
63
+ value instanceof BigUint64Array ||
64
+ value instanceof Int8Array ||
65
+ value instanceof Int16Array ||
66
+ value instanceof Int32Array ||
67
+ value instanceof BigInt64Array ||
68
+ value instanceof Float32Array ||
69
+ value instanceof Float64Array
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Recursively validates that a value is CBOR serializable. Throws TypeError
75
+ * with a descriptive message for non-serializable values.
76
+ */
77
+ export function assertJsonCompatValue(
78
+ value: unknown,
79
+ path = "",
80
+ ): asserts value is JsonCompatValue {
81
+ if (
82
+ value === null ||
83
+ value === undefined ||
84
+ typeof value === "string" ||
85
+ typeof value === "number" ||
86
+ typeof value === "boolean" ||
87
+ typeof value === "bigint"
88
+ ) {
89
+ return;
90
+ }
91
+
92
+ if (typeof value === "function") {
93
+ throw new TypeError(
94
+ `Value at ${path || "root"} is a function and is not CBOR serializable`,
95
+ );
96
+ }
97
+
98
+ if (typeof value === "symbol") {
99
+ throw new TypeError(
100
+ `Value at ${path || "root"} is a symbol and is not CBOR serializable`,
101
+ );
102
+ }
103
+
104
+ if (
105
+ value instanceof Date ||
106
+ value instanceof RegExp ||
107
+ value instanceof Error ||
108
+ value instanceof ArrayBuffer ||
109
+ value instanceof Uint8Array ||
110
+ isTypedArray(value)
111
+ ) {
112
+ return;
113
+ }
114
+
115
+ if (value instanceof WeakMap) {
116
+ throw new TypeError(
117
+ `Value at ${path || "root"} is a WeakMap and is not CBOR serializable`,
118
+ );
119
+ }
120
+
121
+ if (value instanceof WeakSet) {
122
+ throw new TypeError(
123
+ `Value at ${path || "root"} is a WeakSet and is not CBOR serializable`,
124
+ );
125
+ }
126
+
127
+ if (value instanceof WeakRef) {
128
+ throw new TypeError(
129
+ `Value at ${path || "root"} is a WeakRef and is not CBOR serializable`,
130
+ );
131
+ }
132
+
133
+ if (value instanceof Promise) {
134
+ throw new TypeError(
135
+ `Value at ${path || "root"} is a Promise and is not CBOR serializable`,
136
+ );
137
+ }
138
+
139
+ if (value instanceof Map) {
140
+ for (const [k, v] of value.entries()) {
141
+ assertJsonCompatValue(k, `${path || "root"}.key(${String(k)})`);
142
+ assertJsonCompatValue(v, `${path || "root"}.value(${String(k)})`);
143
+ }
144
+ return;
145
+ }
146
+
147
+ if (value instanceof Set) {
148
+ let index = 0;
149
+ for (const item of value.values()) {
150
+ assertJsonCompatValue(item, `${path || "root"}.set[${index}]`);
151
+ index++;
152
+ }
153
+ return;
154
+ }
155
+
156
+ if (Array.isArray(value)) {
157
+ for (let i = 0; i < value.length; i++) {
158
+ assertJsonCompatValue(value[i], `${path || "root"}[${i}]`);
159
+ }
160
+ return;
161
+ }
162
+
163
+ if (isPlainObject(value)) {
164
+ for (const key in value) {
165
+ assertJsonCompatValue(
166
+ value[key as keyof typeof value],
167
+ path ? `${path}.${key}` : key,
168
+ );
169
+ }
170
+ return;
171
+ }
172
+
173
+ const typeName =
174
+ typeof value === "object" && value !== null
175
+ ? (value.constructor?.name ?? typeof value)
176
+ : typeof value;
177
+ throw new TypeError(
178
+ `Value at ${path || "root"} of type "${typeName}" is not CBOR serializable`,
179
+ );
180
+ }
16
181
 
17
182
  export const EncodingSchema = z.enum(["json", "cbor", "bare"]);
18
183
 
@@ -112,38 +277,97 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
112
277
  return proto === Object.prototype || proto === null;
113
278
  }
114
279
 
115
- export function encodeJsonCompatValue(input: any): any {
280
+ export function encodeJsonCompatValue(input: JsonCompatValue): unknown {
281
+ // Primitives
282
+ if (input === null) {
283
+ return input;
284
+ }
116
285
  if (input === undefined) {
117
286
  return [JSON_COMPAT_UNDEFINED, 0];
118
287
  }
288
+ if (
289
+ typeof input === "string" ||
290
+ typeof input === "number" ||
291
+ typeof input === "boolean"
292
+ ) {
293
+ return input;
294
+ }
119
295
  if (typeof input === "bigint") {
120
296
  return [JSON_COMPAT_BIGINT, input.toString()];
121
297
  }
298
+
299
+ // Binary types with custom encoding
122
300
  if (input instanceof ArrayBuffer) {
123
301
  return [JSON_COMPAT_ARRAY_BUFFER, base64EncodeArrayBuffer(input)];
124
302
  }
125
303
  if (input instanceof Uint8Array) {
126
304
  return [JSON_COMPAT_UINT8_ARRAY, base64EncodeUint8Array(input)];
127
305
  }
306
+
307
+ // TypedArrays pass through for cbor-x native handling
308
+ if (isTypedArray(input)) {
309
+ return input;
310
+ }
311
+
312
+ // Date, RegExp, and Error pass through for cbor-x native handling
313
+ if (
314
+ input instanceof Date ||
315
+ input instanceof RegExp ||
316
+ input instanceof Error
317
+ ) {
318
+ return input;
319
+ }
320
+
321
+ // Set uses custom tag encoding
322
+ if (input instanceof Set) {
323
+ const encoded = [...input.values()].map((v) =>
324
+ encodeJsonCompatValue(v as JsonCompatValue),
325
+ );
326
+ return [JSON_COMPAT_SET, encoded];
327
+ }
328
+
329
+ // Map recurses into keys and values
330
+ if (input instanceof Map) {
331
+ const encoded = new Map<unknown, unknown>();
332
+ for (const [k, v] of input.entries()) {
333
+ encoded.set(
334
+ encodeJsonCompatValue(k as JsonCompatValue),
335
+ encodeJsonCompatValue(v as JsonCompatValue),
336
+ );
337
+ }
338
+ return encoded;
339
+ }
340
+
341
+ // Arrays
128
342
  if (Array.isArray(input)) {
129
- const encoded = input.map((value) => encodeJsonCompatValue(value));
343
+ const encoded = input.map((value) =>
344
+ encodeJsonCompatValue(value as JsonCompatValue),
345
+ );
130
346
  if (
131
347
  encoded.length === 2 &&
132
348
  typeof encoded[0] === "string" &&
133
- encoded[0].startsWith("$")
349
+ (encoded[0] as string).startsWith("$")
134
350
  ) {
135
- return ["$" + encoded[0], encoded[1]];
351
+ return [`$${encoded[0]}`, encoded[1]];
136
352
  }
137
353
  return encoded;
138
354
  }
355
+
356
+ // Plain objects
139
357
  if (isPlainObject(input)) {
140
358
  const encoded: Record<string, unknown> = {};
141
359
  for (const [key, value] of Object.entries(input)) {
142
- encoded[key] = encodeJsonCompatValue(value);
360
+ encoded[key] = encodeJsonCompatValue(value as JsonCompatValue);
143
361
  }
144
362
  return encoded;
145
363
  }
146
- return input;
364
+
365
+ // Not serializable
366
+ const typeName =
367
+ typeof input === "object" && input !== null
368
+ ? ((input as object).constructor?.name ?? typeof input)
369
+ : typeof input;
370
+ throw new TypeError(`Value of type "${typeName}" is not CBOR serializable`);
147
371
  }
148
372
 
149
373
  export interface JsonCompatReviveOptions {
@@ -164,6 +388,16 @@ export function reviveJsonCompatValue(
164
388
  }
165
389
  return input;
166
390
  }
391
+ if (input instanceof Map) {
392
+ const revived = new Map();
393
+ for (const [k, v] of input.entries()) {
394
+ revived.set(
395
+ reviveJsonCompatValue(k, options),
396
+ reviveJsonCompatValue(v, options),
397
+ );
398
+ }
399
+ return revived;
400
+ }
167
401
  if (Array.isArray(input)) {
168
402
  if (
169
403
  input.length === 2 &&
@@ -182,6 +416,12 @@ export function reviveJsonCompatValue(
182
416
  if (input[0] === JSON_COMPAT_UNDEFINED) {
183
417
  return undefined;
184
418
  }
419
+ if (input[0] === JSON_COMPAT_SET) {
420
+ const items = (input[1] as unknown[]).map((v) =>
421
+ reviveJsonCompatValue(v, options),
422
+ );
423
+ return new Set(items);
424
+ }
185
425
  if (input[0].startsWith("$$")) {
186
426
  return [
187
427
  input[0].substring(1),
@@ -239,16 +479,10 @@ export function jsonStringifyCompat(input: any): string {
239
479
  return [JSON_COMPAT_BIGINT, value.toString()];
240
480
  }
241
481
  if (value instanceof ArrayBuffer) {
242
- return [
243
- JSON_COMPAT_ARRAY_BUFFER,
244
- base64EncodeArrayBuffer(value),
245
- ];
482
+ return [JSON_COMPAT_ARRAY_BUFFER, base64EncodeArrayBuffer(value)];
246
483
  }
247
484
  if (value instanceof Uint8Array) {
248
- return [
249
- JSON_COMPAT_UINT8_ARRAY,
250
- base64EncodeUint8Array(value),
251
- ];
485
+ return [JSON_COMPAT_UINT8_ARRAY, base64EncodeUint8Array(value)];
252
486
  }
253
487
  if (
254
488
  Array.isArray(value) &&
@@ -256,7 +490,7 @@ export function jsonStringifyCompat(input: any): string {
256
490
  typeof value[0] === "string" &&
257
491
  value[0].startsWith("$")
258
492
  ) {
259
- return ["$" + value[0], value[1]];
493
+ return [`$${value[0]}`, value[1]];
260
494
  }
261
495
  return value;
262
496
  });