rivetkit 0.0.0-main.41efcce → 0.0.0-main.49693dc

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 (299) hide show
  1. package/dist/browser/client.d.ts +939 -104
  2. package/dist/browser/client.js +1867 -900
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +252 -129
  5. package/dist/browser/inspector/client.js +2079 -233
  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 +2408 -2029
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +932 -114
  15. package/dist/tsup/agent-os/index.d.ts +932 -114
  16. package/dist/tsup/agent-os/index.js +2412 -2033
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-J27CEW6F.js → chunk-2OBVQZX6.js} +6 -6
  19. package/dist/tsup/chunk-2OBVQZX6.js.map +1 -0
  20. package/dist/tsup/chunk-3ZWHWMFJ.js +14 -0
  21. package/dist/tsup/chunk-3ZWHWMFJ.js.map +1 -0
  22. package/dist/tsup/{chunk-AK57AL7S.cjs → chunk-4KGXFC6M.cjs} +238 -22
  23. package/dist/tsup/chunk-4KGXFC6M.cjs.map +1 -0
  24. package/dist/tsup/{chunk-CVHUXINE.js → chunk-5IU3MI7X.js} +594 -72
  25. package/dist/tsup/chunk-5IU3MI7X.js.map +1 -0
  26. package/dist/tsup/{chunk-VD7AZIBR.js → chunk-6CXQMHZR.js} +1161 -361
  27. package/dist/tsup/chunk-6CXQMHZR.js.map +1 -0
  28. package/dist/tsup/{chunk-SBEFJ4IQ.js → chunk-6W2UIH3X.js} +70 -8
  29. package/dist/tsup/chunk-6W2UIH3X.js.map +1 -0
  30. package/dist/tsup/{chunk-7MRGNVSR.js → chunk-6W5VGLFT.js} +237 -21
  31. package/dist/tsup/chunk-6W5VGLFT.js.map +1 -0
  32. package/dist/tsup/{chunk-RF3MEJV6.cjs → chunk-B73TBMMG.cjs} +18 -11
  33. package/dist/tsup/chunk-B73TBMMG.cjs.map +1 -0
  34. package/dist/tsup/chunk-D6TORRA4.js +243 -0
  35. package/dist/tsup/chunk-D6TORRA4.js.map +1 -0
  36. package/dist/tsup/{chunk-PXLJOMJD.js → chunk-FXT3HN32.js} +2 -2
  37. package/dist/tsup/{chunk-ZGWSH7YV.js → chunk-IXBD7BXC.js} +48 -72
  38. package/dist/tsup/chunk-IXBD7BXC.js.map +1 -0
  39. package/dist/tsup/{chunk-3BFYSYRU.js → chunk-JI6GZ2C2.js} +4 -12
  40. package/dist/tsup/chunk-JI6GZ2C2.js.map +1 -0
  41. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JTHHCZCZ.js} +24 -1
  42. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  43. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-KVFIONKQ.cjs} +25 -2
  44. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  45. package/dist/tsup/{chunk-SPMMF6TR.cjs → chunk-LGOWPZJ5.cjs} +3 -3
  46. package/dist/tsup/chunk-LGOWPZJ5.cjs.map +1 -0
  47. package/dist/tsup/chunk-LYNWTX6R.cjs +14 -0
  48. package/dist/tsup/chunk-LYNWTX6R.cjs.map +1 -0
  49. package/dist/tsup/{chunk-LLD2D5LO.cjs → chunk-MYWZCDML.cjs} +4 -12
  50. package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
  51. package/dist/tsup/{chunk-XSC2WWXD.js → chunk-NYDZLWHZ.js} +16 -9
  52. package/dist/tsup/chunk-NYDZLWHZ.js.map +1 -0
  53. package/dist/tsup/{chunk-AV54CVAG.cjs → chunk-RB7WUDX6.cjs} +1290 -490
  54. package/dist/tsup/chunk-RB7WUDX6.cjs.map +1 -0
  55. package/dist/tsup/{chunk-JTL6Y2UN.cjs → chunk-SPJORZMM.cjs} +49 -73
  56. package/dist/tsup/chunk-SPJORZMM.cjs.map +1 -0
  57. package/dist/tsup/chunk-SSMKIYEX.cjs +243 -0
  58. package/dist/tsup/chunk-SSMKIYEX.cjs.map +1 -0
  59. package/dist/tsup/{chunk-TQSL4NJ3.cjs → chunk-TQ4HSX2U.cjs} +72 -10
  60. package/dist/tsup/chunk-TQ4HSX2U.cjs.map +1 -0
  61. package/dist/tsup/{chunk-EVADNK6Z.cjs → chunk-U6ZQELGJ.cjs} +10 -10
  62. package/dist/tsup/chunk-U6ZQELGJ.cjs.map +1 -0
  63. package/dist/tsup/chunk-XLBMKZD3.cjs +1257 -0
  64. package/dist/tsup/chunk-XLBMKZD3.cjs.map +1 -0
  65. package/dist/tsup/client/mod.cjs +8 -9
  66. package/dist/tsup/client/mod.cjs.map +1 -1
  67. package/dist/tsup/client/mod.d.cts +18 -7
  68. package/dist/tsup/client/mod.d.ts +18 -7
  69. package/dist/tsup/client/mod.js +7 -8
  70. package/dist/tsup/common/log.cjs +5 -3
  71. package/dist/tsup/common/log.cjs.map +1 -1
  72. package/dist/tsup/common/log.d.cts +5 -1
  73. package/dist/tsup/common/log.d.ts +5 -1
  74. package/dist/tsup/common/log.js +6 -4
  75. package/dist/tsup/common/websocket.cjs +4 -4
  76. package/dist/tsup/common/websocket.cjs.map +1 -1
  77. package/dist/tsup/common/websocket.js +3 -3
  78. package/dist/tsup/{config-B3RIidaJ.d.ts → config-BXSV17G7.d.cts} +987 -72
  79. package/dist/tsup/{config-D6WxBYjV.d.cts → config-Bi1r6gI7.d.ts} +987 -72
  80. package/dist/tsup/{config-B9qUIHX4.d.ts → config-ey2I2zz5.d.cts} +82 -15
  81. package/dist/tsup/{config-B9qUIHX4.d.cts → config-ey2I2zz5.d.ts} +82 -15
  82. package/dist/tsup/context-4fJemY0D.d.cts +129 -0
  83. package/dist/tsup/context-D4sNYboo.d.ts +129 -0
  84. package/dist/tsup/db/drizzle.cjs +168 -103
  85. package/dist/tsup/db/drizzle.cjs.map +1 -1
  86. package/dist/tsup/db/drizzle.d.cts +8 -4
  87. package/dist/tsup/db/drizzle.d.ts +8 -4
  88. package/dist/tsup/db/drizzle.js +170 -105
  89. package/dist/tsup/db/drizzle.js.map +1 -1
  90. package/dist/tsup/db/mod.cjs +6 -163
  91. package/dist/tsup/db/mod.cjs.map +1 -1
  92. package/dist/tsup/db/mod.d.cts +12 -4
  93. package/dist/tsup/db/mod.d.ts +12 -4
  94. package/dist/tsup/db/mod.js +5 -162
  95. package/dist/tsup/db/mod.js.map +1 -1
  96. package/dist/tsup/dynamic/mod.cjs +24 -0
  97. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  98. package/dist/tsup/dynamic/mod.d.cts +38 -0
  99. package/dist/tsup/dynamic/mod.d.ts +38 -0
  100. package/dist/tsup/dynamic/mod.js +24 -0
  101. package/dist/tsup/dynamic/mod.js.map +1 -0
  102. package/dist/tsup/inspector/mod.cjs +47 -12
  103. package/dist/tsup/inspector/mod.cjs.map +1 -1
  104. package/dist/tsup/inspector/mod.d.cts +26 -136
  105. package/dist/tsup/inspector/mod.d.ts +26 -136
  106. package/dist/tsup/inspector/mod.js +46 -11
  107. package/dist/tsup/inspector/mod.js.map +1 -1
  108. package/dist/tsup/inspector/workflow.cjs +21 -0
  109. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  110. package/dist/tsup/inspector/workflow.d.cts +144 -0
  111. package/dist/tsup/inspector/workflow.d.ts +144 -0
  112. package/dist/tsup/inspector/workflow.js +21 -0
  113. package/dist/tsup/inspector/workflow.js.map +1 -0
  114. package/dist/tsup/inspector-tab/mod.cjs +162 -0
  115. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  116. package/dist/tsup/inspector-tab/mod.d.cts +241 -0
  117. package/dist/tsup/inspector-tab/mod.d.ts +241 -0
  118. package/dist/tsup/inspector-tab/mod.js +162 -0
  119. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  120. package/dist/tsup/mod.cjs +4268 -944
  121. package/dist/tsup/mod.cjs.map +1 -1
  122. package/dist/tsup/mod.d.cts +9 -8
  123. package/dist/tsup/mod.d.ts +9 -8
  124. package/dist/tsup/mod.js +4229 -905
  125. package/dist/tsup/mod.js.map +1 -1
  126. package/dist/tsup/test/mod.cjs +26 -21
  127. package/dist/tsup/test/mod.cjs.map +1 -1
  128. package/dist/tsup/test/mod.d.cts +7 -6
  129. package/dist/tsup/test/mod.d.ts +7 -6
  130. package/dist/tsup/test/mod.js +25 -20
  131. package/dist/tsup/test/mod.js.map +1 -1
  132. package/dist/tsup/unstable/migrations.cjs +101 -0
  133. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  134. package/dist/tsup/unstable/migrations.d.cts +29 -0
  135. package/dist/tsup/unstable/migrations.d.ts +29 -0
  136. package/dist/tsup/unstable/migrations.js +101 -0
  137. package/dist/tsup/unstable/migrations.js.map +1 -0
  138. package/dist/tsup/utils-Bbz-3411.d.cts +162 -0
  139. package/dist/tsup/utils-BqdWVlzm.d.ts +162 -0
  140. package/dist/tsup/utils-vvJQyygr.d.cts +109 -0
  141. package/dist/tsup/utils-vvJQyygr.d.ts +109 -0
  142. package/dist/tsup/utils.cjs +7 -3
  143. package/dist/tsup/utils.cjs.map +1 -1
  144. package/dist/tsup/utils.d.cts +4 -147
  145. package/dist/tsup/utils.d.ts +4 -147
  146. package/dist/tsup/utils.js +6 -2
  147. package/dist/tsup/workflow/mod.cjs +517 -412
  148. package/dist/tsup/workflow/mod.cjs.map +1 -1
  149. package/dist/tsup/workflow/mod.d.cts +11 -10
  150. package/dist/tsup/workflow/mod.d.ts +11 -10
  151. package/dist/tsup/workflow/mod.js +511 -406
  152. package/dist/tsup/workflow/mod.js.map +1 -1
  153. package/package.json +62 -14
  154. package/src/actor/actions.ts +119 -0
  155. package/src/actor/config.test.ts +78 -0
  156. package/src/actor/config.ts +518 -96
  157. package/src/actor/contexts/index.ts +7 -2
  158. package/src/actor/definition.ts +38 -26
  159. package/src/actor/errors.ts +101 -89
  160. package/src/actor/keys.ts +2 -1
  161. package/src/actor/mod.ts +32 -21
  162. package/src/actor/schema.ts +2 -2
  163. package/src/agent-os/actor/cron.ts +37 -2
  164. package/src/agent-os/actor/index.ts +39 -19
  165. package/src/agent-os/actor/preview.ts +1 -2
  166. package/src/agent-os/actor/session.ts +2 -2
  167. package/src/agent-os/config.ts +1 -1
  168. package/src/agent-os/fs/database-vfs.ts +1 -1
  169. package/src/agent-os/index.ts +16 -15
  170. package/src/agent-os/types.ts +11 -0
  171. package/src/client/actor-common.ts +106 -53
  172. package/src/client/actor-conn.ts +94 -45
  173. package/src/client/actor-handle.ts +198 -79
  174. package/src/client/actor-query.ts +13 -6
  175. package/src/client/client.ts +43 -15
  176. package/src/client/config.ts +8 -0
  177. package/src/client/errors.ts +4 -2
  178. package/src/client/lifecycle-errors.ts +28 -4
  179. package/src/client/mod.ts +9 -4
  180. package/src/client/query.ts +3 -1
  181. package/src/client/queue.ts +8 -3
  182. package/src/client/raw-utils.ts +24 -16
  183. package/src/client/resolve-gateway-target.ts +3 -1
  184. package/src/client/utils.ts +36 -10
  185. package/src/common/actor-router-consts.ts +7 -0
  186. package/src/common/actor-websocket.ts +3 -1
  187. package/src/common/bare/actor-persist/v1.ts +205 -163
  188. package/src/common/bare/actor-persist/v2.ts +265 -213
  189. package/src/common/bare/actor-persist/v3.ts +176 -172
  190. package/src/common/bare/actor-persist/v4.ts +254 -253
  191. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  192. package/src/common/bare/generated/client-protocol/v4.ts +599 -0
  193. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  194. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  195. package/src/common/bare/transport/v1.ts +694 -542
  196. package/src/common/client-protocol-versioned.ts +176 -67
  197. package/src/common/client-protocol-zod.ts +7 -0
  198. package/src/common/client-protocol.ts +1 -1
  199. package/src/common/database/config.ts +103 -10
  200. package/src/common/database/mod.test.ts +364 -0
  201. package/src/common/database/mod.ts +278 -149
  202. package/src/common/database/native-database.test.ts +220 -0
  203. package/src/common/database/native-database.ts +394 -58
  204. package/src/common/database/shared.ts +25 -0
  205. package/src/common/encoding.ts +302 -38
  206. package/src/common/engine.ts +28 -1
  207. package/src/common/eventsource.ts +1 -1
  208. package/src/common/fetch-like.ts +49 -0
  209. package/src/common/inline-websocket-adapter.ts +14 -13
  210. package/src/common/inspector-transport.ts +12 -2
  211. package/src/common/log.ts +117 -14
  212. package/src/common/router.ts +49 -23
  213. package/src/common/utils.ts +23 -203
  214. package/src/common/websocket-interface.ts +1 -1
  215. package/src/db/drizzle.test.ts +164 -0
  216. package/src/db/drizzle.ts +207 -121
  217. package/src/db/mod.ts +6 -1
  218. package/src/devtools-loader/index.ts +4 -7
  219. package/src/devtools-loader/serve-devtools.ts +26 -0
  220. package/src/dynamic/instance.ts +32 -0
  221. package/src/dynamic/internal.ts +50 -0
  222. package/src/dynamic/isolate-runtime.ts +64 -0
  223. package/src/dynamic/mod.ts +32 -0
  224. package/src/engine-client/actor-http-client.ts +23 -4
  225. package/src/engine-client/actor-websocket-client.ts +32 -5
  226. package/src/engine-client/api-endpoints.ts +52 -2
  227. package/src/engine-client/api-utils.ts +2 -2
  228. package/src/engine-client/driver.ts +21 -17
  229. package/src/engine-client/mod.ts +116 -64
  230. package/src/engine-client/ws-proxy.ts +9 -4
  231. package/src/globals.d.ts +4 -0
  232. package/src/inspector/client.browser.ts +127 -15
  233. package/src/inspector/mod.ts +2 -4
  234. package/src/inspector/workflow.ts +61 -0
  235. package/src/inspector-tab/mod.ts +301 -0
  236. package/src/registry/config/envoy.ts +1 -2
  237. package/src/registry/config/index.ts +160 -53
  238. package/src/registry/config/serverless.ts +7 -1
  239. package/src/registry/index.ts +659 -96
  240. package/src/registry/napi-runtime.ts +1154 -0
  241. package/src/registry/native-validation.ts +10 -12
  242. package/src/registry/native.ts +2005 -955
  243. package/src/registry/process-metrics.ts +250 -0
  244. package/src/registry/run-handler-coordinator.test.ts +252 -0
  245. package/src/registry/run-handler-coordinator.ts +219 -0
  246. package/src/registry/runtime.test.ts +50 -0
  247. package/src/registry/runtime.ts +839 -0
  248. package/src/registry/wasm-runtime.ts +1283 -0
  249. package/src/registry/write-through-proxy.ts +141 -0
  250. package/src/serde.ts +8 -2
  251. package/src/serverless/configure.ts +102 -49
  252. package/src/test/mod.ts +2 -17
  253. package/src/test/ready.ts +22 -0
  254. package/src/unstable/migrations.test.ts +170 -0
  255. package/src/unstable/migrations.ts +141 -0
  256. package/src/utils/crypto.ts +14 -0
  257. package/src/utils/endpoint-parser.ts +1 -1
  258. package/src/utils/env-vars.ts +39 -0
  259. package/src/utils/node.ts +44 -28
  260. package/src/utils/router.ts +1 -1
  261. package/src/utils.ts +4 -2
  262. package/src/workflow/context.ts +683 -242
  263. package/src/workflow/driver.test.ts +62 -0
  264. package/src/workflow/driver.ts +246 -119
  265. package/src/workflow/inspector.ts +24 -24
  266. package/src/workflow/mod.ts +76 -84
  267. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  268. package/dist/tsup/chunk-3BFYSYRU.js.map +0 -1
  269. package/dist/tsup/chunk-6AVUJ52Q.cjs +0 -253
  270. package/dist/tsup/chunk-6AVUJ52Q.cjs.map +0 -1
  271. package/dist/tsup/chunk-7MRGNVSR.js.map +0 -1
  272. package/dist/tsup/chunk-7X7RVTFF.cjs +0 -735
  273. package/dist/tsup/chunk-7X7RVTFF.cjs.map +0 -1
  274. package/dist/tsup/chunk-AK57AL7S.cjs.map +0 -1
  275. package/dist/tsup/chunk-AV54CVAG.cjs.map +0 -1
  276. package/dist/tsup/chunk-CVHUXINE.js.map +0 -1
  277. package/dist/tsup/chunk-EVADNK6Z.cjs.map +0 -1
  278. package/dist/tsup/chunk-J27CEW6F.js.map +0 -1
  279. package/dist/tsup/chunk-JTL6Y2UN.cjs.map +0 -1
  280. package/dist/tsup/chunk-LLD2D5LO.cjs.map +0 -1
  281. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  282. package/dist/tsup/chunk-RF3MEJV6.cjs.map +0 -1
  283. package/dist/tsup/chunk-SB4MXRN2.js +0 -253
  284. package/dist/tsup/chunk-SB4MXRN2.js.map +0 -1
  285. package/dist/tsup/chunk-SBEFJ4IQ.js.map +0 -1
  286. package/dist/tsup/chunk-SPMMF6TR.cjs.map +0 -1
  287. package/dist/tsup/chunk-TQSL4NJ3.cjs.map +0 -1
  288. package/dist/tsup/chunk-VD7AZIBR.js.map +0 -1
  289. package/dist/tsup/chunk-XSC2WWXD.js.map +0 -1
  290. package/dist/tsup/chunk-ZGWSH7YV.js.map +0 -1
  291. package/dist/tsup/context-BxvHh9wi.d.cts +0 -92
  292. package/dist/tsup/context-CNIZBIKl.d.ts +0 -92
  293. package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
  294. package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
  295. package/src/actor/driver.ts +0 -141
  296. package/src/actor/instance/mod.ts +0 -2501
  297. package/src/drivers/engine/actor-driver.ts +0 -2470
  298. package/src/utils/serve.ts +0 -217
  299. /package/dist/tsup/{chunk-PXLJOMJD.js.map → chunk-FXT3HN32.js.map} +0 -0
@@ -1,14 +1,50 @@
1
1
  import { decodeBridgeRivetError } from "@/actor/errors";
2
- import { AsyncMutex } from "./shared";
3
- import type { SqliteBindings, SqliteDatabase } from "./config";
2
+ import type {
3
+ SqliteBatchStatement,
4
+ SqliteBindings,
5
+ SqliteDatabase,
6
+ SqliteExecuteResult,
7
+ SqliteNativeMetrics,
8
+ SqliteTransactionDatabase,
9
+ } from "./config";
4
10
 
5
- interface NativeBindParam {
6
- kind: "null" | "int" | "float" | "text" | "blob";
7
- intValue?: number;
8
- floatValue?: number;
9
- textValue?: string;
10
- blobValue?: Buffer;
11
- }
11
+ type NativeBindNoValues = {
12
+ intValue?: never;
13
+ floatValue?: never;
14
+ textValue?: never;
15
+ blobValue?: never;
16
+ };
17
+
18
+ type NativeBindParam =
19
+ | ({ kind: "null" } & NativeBindNoValues)
20
+ | {
21
+ kind: "int";
22
+ intValue: number;
23
+ floatValue?: never;
24
+ textValue?: never;
25
+ blobValue?: never;
26
+ }
27
+ | {
28
+ kind: "float";
29
+ intValue?: never;
30
+ floatValue: number;
31
+ textValue?: never;
32
+ blobValue?: never;
33
+ }
34
+ | {
35
+ kind: "text";
36
+ intValue?: never;
37
+ floatValue?: never;
38
+ textValue: string;
39
+ blobValue?: never;
40
+ }
41
+ | {
42
+ kind: "blob";
43
+ intValue?: never;
44
+ floatValue?: never;
45
+ textValue?: never;
46
+ blobValue: Buffer;
47
+ };
12
48
 
13
49
  interface NativeExecResult {
14
50
  columns: string[];
@@ -24,17 +60,35 @@ interface NativeRunResult {
24
60
  changes: number;
25
61
  }
26
62
 
27
- export interface SqliteVfsMetrics {
28
- requestBuildNs: number;
29
- serializeNs: number;
30
- transportNs: number;
31
- stateUpdateNs: number;
32
- totalNs: number;
33
- commitCount: number;
63
+ interface NativeExecuteResult {
64
+ columns: string[];
65
+ rows: unknown[][];
66
+ changes: number;
67
+ lastInsertRowId?: number | null;
68
+ }
69
+
70
+ interface NativeBatchStatement {
71
+ sql: string;
72
+ params?: NativeBindParam[] | null;
34
73
  }
35
74
 
36
75
  export interface JsNativeDatabaseLike {
37
76
  exec(sql: string): Promise<NativeExecResult>;
77
+ execute(
78
+ sql: string,
79
+ params?: NativeBindParam[] | null,
80
+ ): Promise<NativeExecuteResult>;
81
+ executeBatch?(
82
+ statements: NativeBatchStatement[],
83
+ ): Promise<NativeExecuteResult[]>;
84
+ beginStateTransaction?(
85
+ timeoutMs?: number,
86
+ context?: unknown,
87
+ ): Promise<JsNativeTransactionLike>;
88
+ beginTransaction(
89
+ timeoutMs?: number,
90
+ name?: string,
91
+ ): Promise<JsNativeTransactionLike>;
38
92
  query(
39
93
  sql: string,
40
94
  params?: NativeBindParam[] | null,
@@ -43,10 +97,26 @@ export interface JsNativeDatabaseLike {
43
97
  sql: string,
44
98
  params?: NativeBindParam[] | null,
45
99
  ): Promise<NativeRunResult>;
46
- getSqliteVfsMetrics?(): SqliteVfsMetrics | null;
100
+ metrics?(): SqliteNativeMetrics | null;
47
101
  takeLastKvError?(): string | null;
48
102
  close(): Promise<void>;
49
103
  }
104
+ export type StateAwareSqliteDatabase = SqliteDatabase & {
105
+ beginStateTransaction(
106
+ timeoutMs?: number,
107
+ context?: unknown,
108
+ ): Promise<SqliteTransactionDatabase>;
109
+ };
110
+
111
+ export interface JsNativeTransactionLike {
112
+ exec(sql: string): Promise<NativeExecResult>;
113
+ execute(
114
+ sql: string,
115
+ params?: NativeBindParam[] | null,
116
+ ): Promise<NativeExecuteResult>;
117
+ commit(): Promise<void>;
118
+ rollback(): Promise<void>;
119
+ }
50
120
 
51
121
  function shouldAttachNativeKvError(message: string): boolean {
52
122
  return /i\/o error|unable to open database file/i.test(message);
@@ -56,12 +126,16 @@ function enrichNativeDatabaseError(
56
126
  database: JsNativeDatabaseLike,
57
127
  error: unknown,
58
128
  ): never {
59
- const bridged =
129
+ const bridgeReason =
60
130
  typeof error === "string"
61
- ? decodeBridgeRivetError(error)
131
+ ? error
62
132
  : error instanceof Error
63
- ? decodeBridgeRivetError(error.message)
133
+ ? error.message
64
134
  : undefined;
135
+ const bridged =
136
+ bridgeReason === undefined
137
+ ? undefined
138
+ : decodeBridgeRivetError(bridgeReason);
65
139
  if (bridged) {
66
140
  throw bridged;
67
141
  }
@@ -167,18 +241,122 @@ function toNativeBindings(
167
241
  });
168
242
  }
169
243
 
170
- export function wrapJsNativeDatabase(
171
- database: JsNativeDatabaseLike,
172
- ): SqliteDatabase {
173
- const mutex = new AsyncMutex();
174
- let closed = false;
244
+ function toNativeBatchStatement(
245
+ statement: SqliteBatchStatement,
246
+ ): NativeBatchStatement {
247
+ return {
248
+ sql: statement.sql,
249
+ params: toNativeBindings(statement.sql, statement.params),
250
+ };
251
+ }
175
252
 
176
- const ensureOpen = () => {
177
- if (closed) {
253
+ function normalizeNativeMetrics(
254
+ metrics: SqliteNativeMetrics | null | undefined,
255
+ ): SqliteNativeMetrics | null {
256
+ if (!metrics) return null;
257
+ const raw = metrics as unknown as Record<string, unknown>;
258
+ const numberField = (camel: string, snake: string) =>
259
+ Number(raw[camel] ?? raw[snake] ?? 0);
260
+
261
+ return {
262
+ requestBuildNs: numberField("requestBuildNs", "request_build_ns"),
263
+ serializeNs: numberField("serializeNs", "serialize_ns"),
264
+ transportNs: numberField("transportNs", "transport_ns"),
265
+ stateUpdateNs: numberField("stateUpdateNs", "state_update_ns"),
266
+ totalNs: numberField("totalNs", "total_ns"),
267
+ commitCount: numberField("commitCount", "commit_count"),
268
+ pageCacheEntries: numberField("pageCacheEntries", "page_cache_entries"),
269
+ pageCacheWeightedSize: numberField(
270
+ "pageCacheWeightedSize",
271
+ "page_cache_weighted_size",
272
+ ),
273
+ pageCacheCapacityPages: numberField(
274
+ "pageCacheCapacityPages",
275
+ "page_cache_capacity_pages",
276
+ ),
277
+ writeBufferDirtyPages: numberField(
278
+ "writeBufferDirtyPages",
279
+ "write_buffer_dirty_pages",
280
+ ),
281
+ dbSizePages: numberField("dbSizePages", "db_size_pages"),
282
+ };
283
+ }
284
+
285
+ class NativeCloseGate {
286
+ #active = 0;
287
+ #closed = false;
288
+ #waiters: (() => void)[] = [];
289
+
290
+ enter(): () => void {
291
+ if (this.#closed) {
178
292
  throw new Error(
179
293
  "Database is closed. This usually means a background timer (setInterval, setTimeout) or a stray promise is still running after the actor stopped. Use c.abortSignal to clean up timers before the actor shuts down.",
180
294
  );
181
295
  }
296
+
297
+ this.#active++;
298
+ let released = false;
299
+ return () => {
300
+ if (released) {
301
+ return;
302
+ }
303
+ released = true;
304
+ this.#active--;
305
+ if (this.#active === 0) {
306
+ const waiters = this.#waiters.splice(0);
307
+ for (const waiter of waiters) {
308
+ waiter();
309
+ }
310
+ }
311
+ };
312
+ }
313
+
314
+ async close(callback: () => Promise<void>): Promise<void> {
315
+ if (this.#closed) {
316
+ return;
317
+ }
318
+ this.#closed = true;
319
+ if (this.#active > 0) {
320
+ await new Promise<void>((resolve) => this.#waiters.push(resolve));
321
+ }
322
+ await callback();
323
+ }
324
+ }
325
+
326
+ export function wrapJsNativeDatabase(
327
+ database: JsNativeDatabaseLike,
328
+ ): StateAwareSqliteDatabase {
329
+ const gate = new NativeCloseGate();
330
+ let closePromise: Promise<void> | undefined;
331
+ let lastInsertRowId: number | null = null;
332
+
333
+ const executeNative = async (
334
+ sql: string,
335
+ params?: SqliteBindings,
336
+ ): Promise<SqliteExecuteResult> => {
337
+ const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
338
+ if (lastInsertRowIdColumn) {
339
+ return {
340
+ columns: [lastInsertRowIdColumn],
341
+ rows: [[lastInsertRowId ?? 0]],
342
+ changes: 0,
343
+ lastInsertRowId,
344
+ };
345
+ }
346
+
347
+ const release = gate.enter();
348
+ try {
349
+ const nativeParams = toNativeBindings(sql, params);
350
+ const result = await database.execute(sql, nativeParams);
351
+ if (result.lastInsertRowId !== undefined) {
352
+ lastInsertRowId = result.lastInsertRowId;
353
+ }
354
+ return result;
355
+ } catch (error) {
356
+ enrichNativeDatabaseError(database, error);
357
+ } finally {
358
+ release();
359
+ }
182
360
  };
183
361
 
184
362
  return {
@@ -186,14 +364,15 @@ export function wrapJsNativeDatabase(
186
364
  sql: string,
187
365
  callback?: (row: unknown[], columns: string[]) => void,
188
366
  ): Promise<void> {
189
- const result = await mutex.run(async () => {
190
- ensureOpen();
191
- try {
192
- return await database.exec(sql);
193
- } catch (error) {
194
- enrichNativeDatabaseError(database, error);
195
- }
196
- });
367
+ const release = gate.enter();
368
+ let result: NativeExecResult;
369
+ try {
370
+ result = await database.exec(sql);
371
+ } catch (error) {
372
+ enrichNativeDatabaseError(database, error);
373
+ } finally {
374
+ release();
375
+ }
197
376
  if (!callback) {
198
377
  return;
199
378
  }
@@ -201,37 +380,194 @@ export function wrapJsNativeDatabase(
201
380
  callback(row, result.columns);
202
381
  }
203
382
  },
204
- async run(sql: string, params?: SqliteBindings): Promise<void> {
205
- await mutex.run(async () => {
206
- ensureOpen();
207
- try {
208
- await database.run(sql, toNativeBindings(sql, params));
209
- } catch (error) {
210
- enrichNativeDatabaseError(database, error);
383
+ async execute(
384
+ sql: string,
385
+ params?: SqliteBindings,
386
+ ): Promise<SqliteExecuteResult> {
387
+ return await executeNative(sql, params);
388
+ },
389
+ async executeBatch(
390
+ statements: SqliteBatchStatement[],
391
+ ): Promise<SqliteExecuteResult[]> {
392
+ const nativeStatements = statements.map(toNativeBatchStatement);
393
+ const release = gate.enter();
394
+ let transaction: JsNativeTransactionLike | undefined;
395
+ try {
396
+ const results = database.executeBatch
397
+ ? await database.executeBatch(nativeStatements)
398
+ : await (async () => {
399
+ transaction = await database.beginTransaction();
400
+ const transactionResults: NativeExecuteResult[] =
401
+ [];
402
+ for (const statement of nativeStatements) {
403
+ transactionResults.push(
404
+ await transaction.execute(
405
+ statement.sql,
406
+ statement.params,
407
+ ),
408
+ );
409
+ }
410
+ await transaction.commit();
411
+ transaction = undefined;
412
+ return transactionResults;
413
+ })();
414
+ for (const result of results) {
415
+ if (result.lastInsertRowId !== undefined) {
416
+ lastInsertRowId = result.lastInsertRowId;
417
+ }
418
+ }
419
+ return results;
420
+ } catch (error) {
421
+ if (transaction) {
422
+ try {
423
+ await transaction.rollback();
424
+ } catch {
425
+ // The original batch error is more actionable than cleanup failure.
426
+ }
427
+ }
428
+ enrichNativeDatabaseError(database, error);
429
+ } finally {
430
+ release();
431
+ }
432
+ },
433
+ async beginTransaction(
434
+ timeoutMs?: number,
435
+ name?: string,
436
+ ): Promise<SqliteTransactionDatabase> {
437
+ const release = gate.enter();
438
+ let transaction: JsNativeTransactionLike;
439
+ try {
440
+ transaction = await database.beginTransaction(timeoutMs, name);
441
+ } catch (error) {
442
+ enrichNativeDatabaseError(database, error);
443
+ } finally {
444
+ release();
445
+ }
446
+ return wrapTransaction(database, transaction, gate, (result) => {
447
+ if (result.lastInsertRowId !== undefined) {
448
+ lastInsertRowId = result.lastInsertRowId;
211
449
  }
212
450
  });
213
451
  },
214
- async query(sql: string, params?: SqliteBindings) {
215
- return await mutex.run(async () => {
216
- ensureOpen();
217
- try {
218
- return await database.query(sql, toNativeBindings(sql, params));
219
- } catch (error) {
220
- enrichNativeDatabaseError(database, error);
452
+ async beginStateTransaction(
453
+ timeoutMs?: number,
454
+ context?: unknown,
455
+ ): Promise<SqliteTransactionDatabase> {
456
+ if (!database.beginStateTransaction) {
457
+ throw new Error("actor state transactions are not configured");
458
+ }
459
+ const release = gate.enter();
460
+ let transaction: JsNativeTransactionLike;
461
+ try {
462
+ transaction = await database.beginStateTransaction(
463
+ timeoutMs,
464
+ context,
465
+ );
466
+ } catch (error) {
467
+ enrichNativeDatabaseError(database, error);
468
+ } finally {
469
+ release();
470
+ }
471
+ return wrapTransaction(database, transaction, gate, (result) => {
472
+ if (result.lastInsertRowId !== undefined) {
473
+ lastInsertRowId = result.lastInsertRowId;
221
474
  }
222
475
  });
223
476
  },
224
- getSqliteVfsMetrics() {
225
- return database.getSqliteVfsMetrics?.() ?? null;
477
+ async run(sql: string, params?: SqliteBindings): Promise<void> {
478
+ await executeNative(sql, params);
479
+ },
480
+ async query(sql: string, params?: SqliteBindings) {
481
+ const { columns, rows } = await executeNative(sql, params);
482
+ return { columns, rows };
483
+ },
484
+ nativeMetrics(): SqliteNativeMetrics | null {
485
+ return normalizeNativeMetrics(database.metrics?.());
226
486
  },
227
487
  async close(): Promise<void> {
228
- await mutex.run(async () => {
229
- if (closed) {
230
- return;
231
- }
232
- closed = true;
233
- await database.close();
234
- });
488
+ closePromise ??= gate.close(() => database.close());
489
+ await closePromise;
235
490
  },
236
491
  };
237
492
  }
493
+
494
+ function wrapTransaction(
495
+ database: JsNativeDatabaseLike,
496
+ transaction: JsNativeTransactionLike,
497
+ gate: NativeCloseGate,
498
+ onExecute: (result: NativeExecuteResult) => void,
499
+ ): SqliteTransactionDatabase {
500
+ return {
501
+ async exec(sql, callback) {
502
+ const release = gate.enter();
503
+ let result: NativeExecResult;
504
+ try {
505
+ result = await transaction.exec(sql);
506
+ } catch (error) {
507
+ enrichNativeDatabaseError(database, error);
508
+ } finally {
509
+ release();
510
+ }
511
+ if (callback) {
512
+ for (const row of result.rows) callback(row, result.columns);
513
+ }
514
+ },
515
+ async execute(sql, params) {
516
+ const release = gate.enter();
517
+ try {
518
+ const result = await transaction.execute(
519
+ sql,
520
+ toNativeBindings(sql, params),
521
+ );
522
+ onExecute(result);
523
+ return result;
524
+ } catch (error) {
525
+ enrichNativeDatabaseError(database, error);
526
+ } finally {
527
+ release();
528
+ }
529
+ },
530
+ async commit() {
531
+ const release = gate.enter();
532
+ try {
533
+ await transaction.commit();
534
+ } catch (error) {
535
+ enrichNativeDatabaseError(database, error);
536
+ } finally {
537
+ release();
538
+ }
539
+ },
540
+ async rollback() {
541
+ const release = gate.enter();
542
+ try {
543
+ await transaction.rollback();
544
+ } catch (error) {
545
+ enrichNativeDatabaseError(database, error);
546
+ } finally {
547
+ release();
548
+ }
549
+ },
550
+ };
551
+ }
552
+
553
+ function lastInsertRowIdColumnName(sql: string): string | undefined {
554
+ const match = sql.match(
555
+ /^\s*SELECT\s+last_insert_rowid\s*\(\s*\)\s*(?:AS\s+("[^"]+"|`[^`]+`|\[[^\]]+\]|\w+))?\s*;?\s*$/i,
556
+ );
557
+ if (!match) {
558
+ return undefined;
559
+ }
560
+
561
+ const alias = match[1];
562
+ if (!alias) {
563
+ return "last_insert_rowid()";
564
+ }
565
+ if (
566
+ (alias.startsWith('"') && alias.endsWith('"')) ||
567
+ (alias.startsWith("`") && alias.endsWith("`")) ||
568
+ (alias.startsWith("[") && alias.endsWith("]"))
569
+ ) {
570
+ return alias.slice(1, -1);
571
+ }
572
+ return alias;
573
+ }
@@ -1,4 +1,29 @@
1
1
  import type { SqliteBindings } from "./config";
2
+
3
+ /** Migrations may legitimately do substantially more work than request transactions. */
4
+ export const MIGRATION_TRANSACTION_TIMEOUT_MS = 5 * 60_000;
5
+
6
+ export function isManualTransactionControl(query: string): boolean {
7
+ return /^\s*(?:BEGIN|SAVEPOINT|COMMIT|END|ROLLBACK)\b/i.test(query);
8
+ }
9
+
10
+ export function validateTransactionTimeout(timeout: number | undefined): void {
11
+ if (timeout !== undefined && (!Number.isFinite(timeout) || timeout <= 0)) {
12
+ throw new Error(
13
+ "db.transaction() timeout must be a positive finite number of milliseconds",
14
+ );
15
+ }
16
+ }
17
+
18
+ export function validateTransactionName(name: string | undefined): void {
19
+ if (name === undefined) {
20
+ return;
21
+ }
22
+ if (name.length === 0) {
23
+ throw new Error("db.transaction() name must not be empty");
24
+ }
25
+ }
26
+
2
27
  type SqliteBindingObject = Record<string, unknown>;
3
28
 
4
29
  function isSqliteBindingValue(value: unknown): boolean {