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
@@ -0,0 +1,1154 @@
1
+ import type {
2
+ ActorContext as NativeActorContext,
3
+ NapiActorFactory as NativeActorFactory,
4
+ CancellationToken as NativeCancellationToken,
5
+ ConnHandle as NativeConnHandle,
6
+ CoreRegistry as NativeCoreRegistry,
7
+ WebSocket as NativeWebSocket,
8
+ } from "@rivetkit/rivetkit-napi";
9
+ import type {
10
+ ActorContextHandle,
11
+ ActorFactoryHandle,
12
+ ActorStateTransactionHandle,
13
+ CancellationTokenHandle,
14
+ ConnHandle,
15
+ CoreRuntime,
16
+ RegistryHandle,
17
+ RuntimeActorConfig,
18
+ RuntimeApplicationFetch,
19
+ RuntimeApplicationListenerConfig,
20
+ RuntimeBytes,
21
+ RuntimeCronFire,
22
+ RuntimeCronJobInfo,
23
+ RuntimeHttpRequest,
24
+ RuntimeKvEntry,
25
+ RuntimeKvListOptions,
26
+ RuntimeListenerConfig,
27
+ RuntimeQueueEnqueueAndWaitOptions,
28
+ RuntimeQueueMessage,
29
+ RuntimeQueueNextBatchOptions,
30
+ RuntimeQueueTryNextBatchOptions,
31
+ RuntimeQueueWaitOptions,
32
+ RuntimeRegistryRouteResponse,
33
+ RuntimeRequestSaveOpts,
34
+ RuntimeScheduledEventInfo,
35
+ RuntimeServeConfig,
36
+ RuntimeServerlessRequest,
37
+ RuntimeServerlessResponseHead,
38
+ RuntimeServerlessStreamCallback,
39
+ RuntimeSqlBatchStatement,
40
+ RuntimeSqlBindParam,
41
+ RuntimeSqlBindParams,
42
+ RuntimeSqlExecResult,
43
+ RuntimeSqlExecuteResult,
44
+ RuntimeSqlQueryResult,
45
+ RuntimeSqlRunResult,
46
+ RuntimeStateDeltaPayload,
47
+ RuntimeWebSocketEvent,
48
+ RuntimeWorkflowKvWrite,
49
+ SqliteTransactionHandle,
50
+ WebSocketHandle,
51
+ } from "./runtime";
52
+ import { normalizeRuntimeSqlExecuteResult } from "./runtime";
53
+
54
+ type NativeBindings = typeof import("@rivetkit/rivetkit-napi");
55
+ type NapiSqlDatabase = ReturnType<NativeActorContext["sql"]>;
56
+ type NapiSqlBindParams = Parameters<NapiSqlDatabase["execute"]>[1];
57
+ type NapiSqlBatchStatement = Parameters<
58
+ NapiSqlDatabase["executeBatch"]
59
+ >[0][number];
60
+ type NapiSqlTransaction = Awaited<
61
+ ReturnType<NapiSqlDatabase["beginTransaction"]>
62
+ >;
63
+ type NapiActorStateTransaction = Awaited<
64
+ ReturnType<NativeActorContext["beginStateTransaction"]>
65
+ >;
66
+
67
+ function asNativeRegistry(handle: RegistryHandle): NativeCoreRegistry {
68
+ return handle as unknown as NativeCoreRegistry;
69
+ }
70
+
71
+ function asNativeFactory(handle: ActorFactoryHandle): NativeActorFactory {
72
+ return handle as unknown as NativeActorFactory;
73
+ }
74
+
75
+ function asNativeActorContext(handle: ActorContextHandle): NativeActorContext {
76
+ return handle as unknown as NativeActorContext;
77
+ }
78
+
79
+ function asNativeConn(handle: ConnHandle): NativeConnHandle {
80
+ return handle as unknown as NativeConnHandle;
81
+ }
82
+
83
+ function asNativeWebSocket(handle: WebSocketHandle): NativeWebSocket {
84
+ return handle as unknown as NativeWebSocket;
85
+ }
86
+
87
+ function asNativeSqlTransaction(
88
+ handle: SqliteTransactionHandle,
89
+ ): NapiSqlTransaction {
90
+ return handle as unknown as NapiSqlTransaction;
91
+ }
92
+ function asNativeActorStateTransaction(
93
+ handle: ActorStateTransactionHandle,
94
+ ): NapiActorStateTransaction {
95
+ return handle as unknown as NapiActorStateTransaction;
96
+ }
97
+
98
+ function asNativeCancellationToken(
99
+ handle: CancellationTokenHandle,
100
+ ): NativeCancellationToken {
101
+ return handle as unknown as NativeCancellationToken;
102
+ }
103
+
104
+ function asRegistryHandle(handle: NativeCoreRegistry): RegistryHandle {
105
+ return handle as unknown as RegistryHandle;
106
+ }
107
+
108
+ function asActorFactoryHandle(handle: NativeActorFactory): ActorFactoryHandle {
109
+ return handle as unknown as ActorFactoryHandle;
110
+ }
111
+
112
+ function toNapiSqlBindParam(
113
+ param: RuntimeSqlBindParam,
114
+ ): NonNullable<NapiSqlBindParams>[number] {
115
+ switch (param.kind) {
116
+ case "null":
117
+ return { kind: "null" };
118
+ case "int":
119
+ return { kind: "int", intValue: param.intValue };
120
+ case "float":
121
+ return { kind: "float", floatValue: param.floatValue };
122
+ case "text":
123
+ return { kind: "text", textValue: param.textValue };
124
+ case "blob":
125
+ return { kind: "blob", blobValue: Buffer.from(param.blobValue) };
126
+ }
127
+ }
128
+
129
+ function toNapiSqlBindParams(params?: RuntimeSqlBindParams): NapiSqlBindParams {
130
+ if (params == null) {
131
+ return params;
132
+ }
133
+ return params.map((param) => toNapiSqlBindParam(param));
134
+ }
135
+
136
+ function toNapiSqlBatchStatement(
137
+ statement: RuntimeSqlBatchStatement,
138
+ ): NapiSqlBatchStatement {
139
+ return {
140
+ sql: statement.sql,
141
+ params: toNapiSqlBindParams(statement.params) ?? undefined,
142
+ };
143
+ }
144
+
145
+ function toNapiBuffer(value: RuntimeBytes): Buffer {
146
+ return Buffer.from(value);
147
+ }
148
+
149
+ function toNapiApplication(application: RuntimeApplicationFetch) {
150
+ return async (
151
+ error: unknown,
152
+ request?: {
153
+ method: string;
154
+ url: string;
155
+ headers: Record<string, string>;
156
+ body: Buffer;
157
+ },
158
+ ) => {
159
+ if (error) throw error;
160
+ if (!request) {
161
+ throw new Error("application fetch callback received no request");
162
+ }
163
+ const response = await application({
164
+ ...request,
165
+ body: new Uint8Array(request.body),
166
+ });
167
+ return {
168
+ ...response,
169
+ body: toNapiBuffer(response.body),
170
+ };
171
+ };
172
+ }
173
+
174
+ function toNapiHttpRequest(
175
+ request?: RuntimeHttpRequest | undefined | null,
176
+ ): Parameters<NativeActorContext["connectConn"]>[1] {
177
+ if (!request) {
178
+ return request;
179
+ }
180
+ return {
181
+ ...request,
182
+ body: request.body ? toNapiBuffer(request.body) : undefined,
183
+ };
184
+ }
185
+
186
+ function toNapiStateDeltaPayload(
187
+ payload: RuntimeStateDeltaPayload,
188
+ ): Parameters<NativeActorContext["saveState"]>[0] {
189
+ return {
190
+ ...payload,
191
+ state: payload.state ? toNapiBuffer(payload.state) : undefined,
192
+ connHibernation: payload.connHibernation.map((conn) => ({
193
+ ...conn,
194
+ bytes: toNapiBuffer(conn.bytes),
195
+ })),
196
+ };
197
+ }
198
+
199
+ function toNapiKvEntry(entry: RuntimeKvEntry): {
200
+ key: Buffer;
201
+ value: Buffer;
202
+ } {
203
+ return {
204
+ key: toNapiBuffer(entry.key),
205
+ value: toNapiBuffer(entry.value),
206
+ };
207
+ }
208
+
209
+ function toNapiQueueMessage(message: RuntimeQueueMessage): RuntimeQueueMessage {
210
+ return {
211
+ id: () => message.id(),
212
+ name: () => message.name(),
213
+ body: () => message.body(),
214
+ createdAt: () => message.createdAt(),
215
+ isCompletable: () => message.isCompletable(),
216
+ complete: async (response?: RuntimeBytes | undefined | null) => {
217
+ await message.complete(
218
+ response === null || response === undefined
219
+ ? response
220
+ : toNapiBuffer(response),
221
+ );
222
+ },
223
+ };
224
+ }
225
+
226
+ export class NapiCoreRuntime implements CoreRuntime {
227
+ readonly kind = "napi";
228
+
229
+ #bindings: NativeBindings;
230
+ #sql = new WeakMap<NativeActorContext, NapiSqlDatabase>();
231
+
232
+ constructor(bindings: NativeBindings) {
233
+ this.#bindings = bindings;
234
+ }
235
+
236
+ #actorSql(ctx: ActorContextHandle): NapiSqlDatabase {
237
+ const nativeCtx = asNativeActorContext(ctx);
238
+ let database = this.#sql.get(nativeCtx);
239
+ if (!database) {
240
+ database = nativeCtx.sql();
241
+ this.#sql.set(nativeCtx, database);
242
+ }
243
+ return database;
244
+ }
245
+
246
+ createRegistry(): RegistryHandle {
247
+ return asRegistryHandle(new this.#bindings.CoreRegistry());
248
+ }
249
+
250
+ registerActor(
251
+ registry: RegistryHandle,
252
+ name: string,
253
+ factory: ActorFactoryHandle,
254
+ ): void {
255
+ asNativeRegistry(registry).register(name, asNativeFactory(factory));
256
+ }
257
+
258
+ async serveRegistry(
259
+ registry: RegistryHandle,
260
+ config: RuntimeServeConfig,
261
+ ): Promise<void> {
262
+ await asNativeRegistry(registry).serve(config);
263
+ }
264
+
265
+ async waitRegistryReady(registry: RegistryHandle): Promise<void> {
266
+ await asNativeRegistry(registry).waitReady();
267
+ }
268
+
269
+ async shutdownRegistry(registry: RegistryHandle): Promise<void> {
270
+ await asNativeRegistry(registry).shutdown();
271
+ }
272
+
273
+ async registryActorStopThresholdMs(
274
+ registry: RegistryHandle,
275
+ ): Promise<number | undefined> {
276
+ return (
277
+ (await asNativeRegistry(registry).actorStopThresholdMs()) ??
278
+ undefined
279
+ );
280
+ }
281
+
282
+ async registryHealth(
283
+ registry: RegistryHandle,
284
+ ): Promise<RuntimeRegistryRouteResponse> {
285
+ const response = await asNativeRegistry(registry).health();
286
+ return {
287
+ status: response.status,
288
+ headers: response.headers,
289
+ body: response.body,
290
+ };
291
+ }
292
+
293
+ async registryMetadata(
294
+ registry: RegistryHandle,
295
+ ): Promise<RuntimeRegistryRouteResponse> {
296
+ const response = asNativeRegistry(registry).metadata();
297
+ return {
298
+ status: response.status,
299
+ headers: response.headers,
300
+ body: response.body,
301
+ };
302
+ }
303
+
304
+ async registryMetrics(
305
+ registry: RegistryHandle,
306
+ ): Promise<RuntimeRegistryRouteResponse> {
307
+ const response = asNativeRegistry(registry).metrics();
308
+ return {
309
+ status: response.status,
310
+ headers: response.headers,
311
+ body: response.body,
312
+ };
313
+ }
314
+
315
+ async handleServerlessRequest(
316
+ registry: RegistryHandle,
317
+ req: RuntimeServerlessRequest,
318
+ onStreamEvent: RuntimeServerlessStreamCallback,
319
+ cancelToken: CancellationTokenHandle,
320
+ config: RuntimeServeConfig,
321
+ ): Promise<RuntimeServerlessResponseHead> {
322
+ return await asNativeRegistry(registry).handleServerlessRequest(
323
+ { ...req, body: toNapiBuffer(req.body) },
324
+ onStreamEvent,
325
+ asNativeCancellationToken(cancelToken),
326
+ config,
327
+ );
328
+ }
329
+
330
+ async serveListener(
331
+ registry: RegistryHandle,
332
+ listener: RuntimeListenerConfig,
333
+ config: RuntimeServeConfig,
334
+ ): Promise<void> {
335
+ const application = listener.application
336
+ ? toNapiApplication(listener.application)
337
+ : undefined;
338
+ await asNativeRegistry(registry).serveListener(
339
+ {
340
+ port: listener.port,
341
+ host: listener.host,
342
+ publicDir: listener.publicDir,
343
+ },
344
+ config,
345
+ application,
346
+ );
347
+ }
348
+
349
+ async serveApplicationListener(
350
+ registry: RegistryHandle,
351
+ listener: RuntimeApplicationListenerConfig,
352
+ config: RuntimeServeConfig,
353
+ ): Promise<void> {
354
+ await asNativeRegistry(registry).serveApplicationListener(
355
+ {
356
+ port: listener.port,
357
+ host: listener.host,
358
+ publicDir: listener.publicDir,
359
+ },
360
+ toNapiApplication(listener.application),
361
+ config,
362
+ );
363
+ }
364
+
365
+ createActorFactory(
366
+ callbacks: object,
367
+ config?: RuntimeActorConfig | undefined | null,
368
+ ): ActorFactoryHandle {
369
+ return asActorFactoryHandle(
370
+ new this.#bindings.NapiActorFactory(callbacks, config),
371
+ );
372
+ }
373
+
374
+ createCancellationToken(): CancellationTokenHandle {
375
+ return new this.#bindings.CancellationToken() as unknown as CancellationTokenHandle;
376
+ }
377
+
378
+ decodeInspectorRequest(
379
+ bytes: RuntimeBytes,
380
+ advertisedVersion: number,
381
+ ): RuntimeBytes {
382
+ return this.#bindings.decodeInspectorRequest(
383
+ toNapiBuffer(bytes),
384
+ advertisedVersion,
385
+ );
386
+ }
387
+
388
+ encodeInspectorResponse(
389
+ bytes: RuntimeBytes,
390
+ targetVersion: number,
391
+ ): RuntimeBytes {
392
+ return this.#bindings.encodeInspectorResponse(
393
+ toNapiBuffer(bytes),
394
+ targetVersion,
395
+ );
396
+ }
397
+
398
+ cancellationTokenAborted(token: CancellationTokenHandle): boolean {
399
+ return asNativeCancellationToken(token).aborted();
400
+ }
401
+
402
+ cancelCancellationToken(token: CancellationTokenHandle): void {
403
+ asNativeCancellationToken(token).cancel();
404
+ }
405
+
406
+ onCancellationTokenCancelled(
407
+ token: CancellationTokenHandle,
408
+ callback: (...args: unknown[]) => unknown,
409
+ ): void {
410
+ asNativeCancellationToken(token).onCancelled(callback);
411
+ }
412
+
413
+ actorState(ctx: ActorContextHandle): Buffer {
414
+ return asNativeActorContext(ctx).state();
415
+ }
416
+
417
+ actorBeginOnStateChange(ctx: ActorContextHandle): void {
418
+ asNativeActorContext(ctx).beginOnStateChange();
419
+ }
420
+
421
+ actorEndOnStateChange(ctx: ActorContextHandle): void {
422
+ asNativeActorContext(ctx).endOnStateChange();
423
+ }
424
+
425
+ actorSetAlarm(
426
+ ctx: ActorContextHandle,
427
+ timestampMs?: number | undefined | null,
428
+ ): void {
429
+ asNativeActorContext(ctx).setAlarm(timestampMs);
430
+ }
431
+
432
+ async actorSetRunWakeAt(
433
+ ctx: ActorContextHandle,
434
+ timestampMs?: number | undefined | null,
435
+ ): Promise<void> {
436
+ await asNativeActorContext(ctx).setRunWakeAt(timestampMs);
437
+ }
438
+
439
+ actorRequestSave(
440
+ ctx: ActorContextHandle,
441
+ opts?: RuntimeRequestSaveOpts | undefined | null,
442
+ ): void {
443
+ asNativeActorContext(ctx).requestSave(opts);
444
+ }
445
+
446
+ async actorRequestSaveAndWait(
447
+ ctx: ActorContextHandle,
448
+ opts?: RuntimeRequestSaveOpts | undefined | null,
449
+ ): Promise<void> {
450
+ await asNativeActorContext(ctx).requestSaveAndWait(opts);
451
+ }
452
+
453
+ actorInspectorSnapshot(ctx: ActorContextHandle) {
454
+ return asNativeActorContext(ctx).inspectorSnapshot();
455
+ }
456
+
457
+ actorDecodeInspectorRequest(
458
+ ctx: ActorContextHandle,
459
+ bytes: RuntimeBytes,
460
+ advertisedVersion: number,
461
+ ): RuntimeBytes {
462
+ return asNativeActorContext(ctx).decodeInspectorRequest(
463
+ toNapiBuffer(bytes),
464
+ advertisedVersion,
465
+ );
466
+ }
467
+
468
+ actorEncodeInspectorResponse(
469
+ ctx: ActorContextHandle,
470
+ bytes: RuntimeBytes,
471
+ targetVersion: number,
472
+ ): RuntimeBytes {
473
+ return asNativeActorContext(ctx).encodeInspectorResponse(
474
+ toNapiBuffer(bytes),
475
+ targetVersion,
476
+ );
477
+ }
478
+
479
+ async actorVerifyInspectorAuth(
480
+ ctx: ActorContextHandle,
481
+ bearerToken?: string | undefined | null,
482
+ ): Promise<void> {
483
+ await asNativeActorContext(ctx).verifyInspectorAuth(bearerToken);
484
+ }
485
+
486
+ actorQueueHibernationRemoval(
487
+ ctx: ActorContextHandle,
488
+ connId: string,
489
+ ): void {
490
+ asNativeActorContext(ctx).queueHibernationRemoval(connId);
491
+ }
492
+
493
+ actorTakePendingHibernationChanges(ctx: ActorContextHandle): string[] {
494
+ return asNativeActorContext(ctx).takePendingHibernationChanges();
495
+ }
496
+
497
+ actorDirtyHibernatableConns(ctx: ActorContextHandle): ConnHandle[] {
498
+ return asNativeActorContext(
499
+ ctx,
500
+ ).dirtyHibernatableConns() as unknown as ConnHandle[];
501
+ }
502
+
503
+ async actorSaveState(
504
+ ctx: ActorContextHandle,
505
+ payload: RuntimeStateDeltaPayload,
506
+ ): Promise<void> {
507
+ await asNativeActorContext(ctx).saveState(
508
+ toNapiStateDeltaPayload(payload),
509
+ );
510
+ }
511
+
512
+ async actorSaveStateAndWorkflowBatch(
513
+ ctx: ActorContextHandle,
514
+ writes: RuntimeWorkflowKvWrite[],
515
+ ): Promise<void> {
516
+ await asNativeActorContext(ctx).saveStateAndWorkflowBatch(
517
+ writes.map((write) => ({
518
+ key: toNapiBuffer(write.key),
519
+ value: toNapiBuffer(write.value),
520
+ })),
521
+ );
522
+ }
523
+
524
+ actorId(ctx: ActorContextHandle): string {
525
+ return asNativeActorContext(ctx).actorId();
526
+ }
527
+
528
+ actorName(ctx: ActorContextHandle): string {
529
+ return asNativeActorContext(ctx).name();
530
+ }
531
+
532
+ actorKey(ctx: ActorContextHandle) {
533
+ return asNativeActorContext(ctx).key();
534
+ }
535
+
536
+ actorRegion(ctx: ActorContextHandle): string {
537
+ return asNativeActorContext(ctx).region();
538
+ }
539
+
540
+ actorSleep(ctx: ActorContextHandle): void {
541
+ asNativeActorContext(ctx).sleep();
542
+ }
543
+
544
+ actorDestroy(ctx: ActorContextHandle): void {
545
+ asNativeActorContext(ctx).destroy();
546
+ }
547
+
548
+ actorAbortSignal(ctx: ActorContextHandle): AbortSignal {
549
+ return asNativeActorContext(ctx).abortSignal();
550
+ }
551
+
552
+ actorConns(ctx: ActorContextHandle): ConnHandle[] {
553
+ return asNativeActorContext(ctx).conns() as unknown as ConnHandle[];
554
+ }
555
+
556
+ async actorConnectConn(
557
+ ctx: ActorContextHandle,
558
+ params: RuntimeBytes,
559
+ request?: RuntimeHttpRequest | undefined | null,
560
+ ): Promise<ConnHandle> {
561
+ return (await asNativeActorContext(ctx).connectConn(
562
+ toNapiBuffer(params),
563
+ toNapiHttpRequest(request),
564
+ )) as unknown as ConnHandle;
565
+ }
566
+
567
+ actorBroadcast(
568
+ ctx: ActorContextHandle,
569
+ name: string,
570
+ args: RuntimeBytes,
571
+ ): void {
572
+ asNativeActorContext(ctx).broadcast(name, toNapiBuffer(args));
573
+ }
574
+
575
+ actorWaitUntil(ctx: ActorContextHandle, promise: Promise<unknown>): void {
576
+ asNativeActorContext(ctx).waitUntil(promise);
577
+ }
578
+
579
+ async actorWaitForTrackedShutdownWork(
580
+ ctx: ActorContextHandle,
581
+ ): Promise<boolean> {
582
+ return await asNativeActorContext(ctx).waitForTrackedShutdownWork();
583
+ }
584
+
585
+ async actorWaitForTrackedShutdownWorkUnbounded(
586
+ ctx: ActorContextHandle,
587
+ ): Promise<void> {
588
+ await asNativeActorContext(ctx).waitForTrackedShutdownWorkUnbounded();
589
+ }
590
+
591
+ actorKeepAwake(ctx: ActorContextHandle, promise: Promise<unknown>): void {
592
+ asNativeActorContext(ctx).keepAwake(promise);
593
+ }
594
+
595
+ actorBeginKeepAwake(ctx: ActorContextHandle): number {
596
+ return asNativeActorContext(ctx).beginKeepAwake();
597
+ }
598
+
599
+ actorEndKeepAwake(ctx: ActorContextHandle, regionId: number): void {
600
+ asNativeActorContext(ctx).endKeepAwake(regionId);
601
+ }
602
+
603
+ actorRegisterTask(
604
+ ctx: ActorContextHandle,
605
+ promise: Promise<unknown>,
606
+ ): void {
607
+ asNativeActorContext(ctx).registerTask(promise);
608
+ }
609
+
610
+ actorRuntimeState(ctx: ActorContextHandle): object {
611
+ return asNativeActorContext(ctx).runtimeState();
612
+ }
613
+
614
+ actorClearRuntimeState(ctx: ActorContextHandle): void {
615
+ asNativeActorContext(ctx).clearRuntimeState();
616
+ }
617
+
618
+ actorRestartRunHandler(ctx: ActorContextHandle): void {
619
+ asNativeActorContext(ctx).restartRunHandler();
620
+ }
621
+
622
+ actorBeginWebsocketCallback(ctx: ActorContextHandle): number {
623
+ return asNativeActorContext(ctx).beginWebsocketCallback();
624
+ }
625
+
626
+ actorEndWebsocketCallback(ctx: ActorContextHandle, regionId: number): void {
627
+ asNativeActorContext(ctx).endWebsocketCallback(regionId);
628
+ }
629
+
630
+ async actorKvGet(
631
+ ctx: ActorContextHandle,
632
+ key: RuntimeBytes,
633
+ ): Promise<RuntimeBytes | null> {
634
+ return await asNativeActorContext(ctx).kv().get(toNapiBuffer(key));
635
+ }
636
+
637
+ async actorKvPut(
638
+ ctx: ActorContextHandle,
639
+ key: RuntimeBytes,
640
+ value: RuntimeBytes,
641
+ ): Promise<void> {
642
+ await asNativeActorContext(ctx)
643
+ .kv()
644
+ .put(toNapiBuffer(key), toNapiBuffer(value));
645
+ }
646
+
647
+ async actorKvDelete(
648
+ ctx: ActorContextHandle,
649
+ key: RuntimeBytes,
650
+ ): Promise<void> {
651
+ await asNativeActorContext(ctx).kv().delete(toNapiBuffer(key));
652
+ }
653
+
654
+ async actorKvDeleteRange(
655
+ ctx: ActorContextHandle,
656
+ start: RuntimeBytes,
657
+ end: RuntimeBytes,
658
+ ): Promise<void> {
659
+ await asNativeActorContext(ctx)
660
+ .kv()
661
+ .deleteRange(toNapiBuffer(start), toNapiBuffer(end));
662
+ }
663
+
664
+ async actorKvListPrefix(
665
+ ctx: ActorContextHandle,
666
+ prefix: RuntimeBytes,
667
+ options?: RuntimeKvListOptions | undefined | null,
668
+ ): Promise<RuntimeKvEntry[]> {
669
+ return await asNativeActorContext(ctx)
670
+ .kv()
671
+ .listPrefix(toNapiBuffer(prefix), options);
672
+ }
673
+
674
+ async actorKvListRange(
675
+ ctx: ActorContextHandle,
676
+ start: RuntimeBytes,
677
+ end: RuntimeBytes,
678
+ options?: RuntimeKvListOptions | undefined | null,
679
+ ): Promise<RuntimeKvEntry[]> {
680
+ return await asNativeActorContext(ctx)
681
+ .kv()
682
+ .listRange(toNapiBuffer(start), toNapiBuffer(end), options);
683
+ }
684
+
685
+ async actorKvBatchGet(
686
+ ctx: ActorContextHandle,
687
+ keys: RuntimeBytes[],
688
+ ): Promise<Array<RuntimeBytes | undefined | null>> {
689
+ return await asNativeActorContext(ctx)
690
+ .kv()
691
+ .batchGet(keys.map(toNapiBuffer));
692
+ }
693
+
694
+ async actorKvBatchPut(
695
+ ctx: ActorContextHandle,
696
+ entries: RuntimeKvEntry[],
697
+ ): Promise<void> {
698
+ await asNativeActorContext(ctx)
699
+ .kv()
700
+ .batchPut(entries.map(toNapiKvEntry));
701
+ }
702
+
703
+ async actorKvBatchDelete(
704
+ ctx: ActorContextHandle,
705
+ keys: RuntimeBytes[],
706
+ ): Promise<void> {
707
+ await asNativeActorContext(ctx)
708
+ .kv()
709
+ .batchDelete(keys.map(toNapiBuffer));
710
+ }
711
+
712
+ async actorSqlExec(
713
+ ctx: ActorContextHandle,
714
+ sql: string,
715
+ ): Promise<RuntimeSqlExecResult> {
716
+ return await this.#actorSql(ctx).exec(sql);
717
+ }
718
+
719
+ async actorSqlExecute(
720
+ ctx: ActorContextHandle,
721
+ sql: string,
722
+ params?: RuntimeSqlBindParams,
723
+ ): Promise<RuntimeSqlExecuteResult> {
724
+ const result = await this.#actorSql(ctx).execute(
725
+ sql,
726
+ toNapiSqlBindParams(params),
727
+ );
728
+ return normalizeRuntimeSqlExecuteResult(result);
729
+ }
730
+
731
+ async actorSqlExecuteBatch(
732
+ ctx: ActorContextHandle,
733
+ statements: RuntimeSqlBatchStatement[],
734
+ ): Promise<RuntimeSqlExecuteResult[]> {
735
+ const results = await this.#actorSql(ctx).executeBatch(
736
+ statements.map(toNapiSqlBatchStatement),
737
+ );
738
+ return results.map(normalizeRuntimeSqlExecuteResult);
739
+ }
740
+
741
+ async actorSqlBeginTransaction(
742
+ ctx: ActorContextHandle,
743
+ timeoutMs?: number,
744
+ name?: string,
745
+ ): Promise<SqliteTransactionHandle> {
746
+ return (await this.#actorSql(ctx).beginTransaction(
747
+ timeoutMs,
748
+ name,
749
+ )) as unknown as SqliteTransactionHandle;
750
+ }
751
+
752
+ async actorSqlTransactionExec(
753
+ transaction: SqliteTransactionHandle,
754
+ sql: string,
755
+ ): Promise<RuntimeSqlExecResult> {
756
+ return await asNativeSqlTransaction(transaction).exec(sql);
757
+ }
758
+
759
+ async actorSqlTransactionExecute(
760
+ transaction: SqliteTransactionHandle,
761
+ sql: string,
762
+ params?: RuntimeSqlBindParams,
763
+ ): Promise<RuntimeSqlExecuteResult> {
764
+ const result = await asNativeSqlTransaction(transaction).execute(
765
+ sql,
766
+ toNapiSqlBindParams(params),
767
+ );
768
+ return normalizeRuntimeSqlExecuteResult(result);
769
+ }
770
+
771
+ async actorSqlTransactionCommit(
772
+ transaction: SqliteTransactionHandle,
773
+ ): Promise<void> {
774
+ await asNativeSqlTransaction(transaction).commit();
775
+ }
776
+
777
+ async actorSqlTransactionRollback(
778
+ transaction: SqliteTransactionHandle,
779
+ ): Promise<void> {
780
+ await asNativeSqlTransaction(transaction).rollback();
781
+ }
782
+ async actorBeginStateTransaction(
783
+ ctx: ActorContextHandle,
784
+ timeoutMs?: number,
785
+ ): Promise<ActorStateTransactionHandle> {
786
+ return (await asNativeActorContext(ctx).beginStateTransaction(
787
+ timeoutMs,
788
+ )) as unknown as ActorStateTransactionHandle;
789
+ }
790
+
791
+ async actorStateTransactionExecute(
792
+ transaction: ActorStateTransactionHandle,
793
+ sql: string,
794
+ params?: RuntimeSqlBindParams,
795
+ ): Promise<RuntimeSqlExecuteResult> {
796
+ const result = await asNativeActorStateTransaction(transaction).execute(
797
+ sql,
798
+ toNapiSqlBindParams(params),
799
+ );
800
+ return normalizeRuntimeSqlExecuteResult(result);
801
+ }
802
+
803
+ async actorStateTransactionCommit(
804
+ transaction: ActorStateTransactionHandle,
805
+ payload: RuntimeStateDeltaPayload,
806
+ ): Promise<void> {
807
+ await asNativeActorStateTransaction(transaction).commit(
808
+ toNapiStateDeltaPayload(payload),
809
+ );
810
+ }
811
+
812
+ async actorStateTransactionRollback(
813
+ transaction: ActorStateTransactionHandle,
814
+ ): Promise<void> {
815
+ await asNativeActorStateTransaction(transaction).rollback();
816
+ }
817
+
818
+ async actorSqlQuery(
819
+ ctx: ActorContextHandle,
820
+ sql: string,
821
+ params?: RuntimeSqlBindParams,
822
+ ): Promise<RuntimeSqlQueryResult> {
823
+ return await this.#actorSql(ctx).query(
824
+ sql,
825
+ toNapiSqlBindParams(params),
826
+ );
827
+ }
828
+
829
+ async actorSqlRun(
830
+ ctx: ActorContextHandle,
831
+ sql: string,
832
+ params?: RuntimeSqlBindParams,
833
+ ): Promise<RuntimeSqlRunResult> {
834
+ return await this.#actorSql(ctx).run(sql, toNapiSqlBindParams(params));
835
+ }
836
+
837
+ actorSqlMetrics(ctx: ActorContextHandle) {
838
+ return this.#actorSql(ctx).metrics?.() ?? null;
839
+ }
840
+
841
+ actorSqlTakeLastKvError(ctx: ActorContextHandle): string | null {
842
+ return this.#actorSql(ctx).takeLastKvError?.() ?? null;
843
+ }
844
+
845
+ async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
846
+ const nativeCtx = asNativeActorContext(ctx);
847
+ const database = this.#sql.get(nativeCtx);
848
+ if (!database) {
849
+ return;
850
+ }
851
+
852
+ this.#sql.delete(nativeCtx);
853
+ await database.close();
854
+ }
855
+
856
+ async actorRuntimeSocketProvision(ctx: ActorContextHandle) {
857
+ return await asNativeActorContext(ctx).provisionActorRuntimeSocket();
858
+ }
859
+
860
+ async actorQueueSend(
861
+ ctx: ActorContextHandle,
862
+ name: string,
863
+ body: RuntimeBytes,
864
+ ): Promise<RuntimeQueueMessage> {
865
+ return toNapiQueueMessage(
866
+ await asNativeActorContext(ctx)
867
+ .queue()
868
+ .send(name, toNapiBuffer(body)),
869
+ );
870
+ }
871
+
872
+ async actorQueueNextBatch(
873
+ ctx: ActorContextHandle,
874
+ options?: RuntimeQueueNextBatchOptions | undefined | null,
875
+ signal?: CancellationTokenHandle | undefined | null,
876
+ ): Promise<RuntimeQueueMessage[]> {
877
+ const messages = await asNativeActorContext(ctx)
878
+ .queue()
879
+ .nextBatch(
880
+ options,
881
+ signal ? asNativeCancellationToken(signal) : signal,
882
+ );
883
+ return messages.map(toNapiQueueMessage);
884
+ }
885
+
886
+ async actorQueueWaitForNames(
887
+ ctx: ActorContextHandle,
888
+ names: string[],
889
+ options?: RuntimeQueueWaitOptions | undefined | null,
890
+ signal?: CancellationTokenHandle | undefined | null,
891
+ ): Promise<RuntimeQueueMessage> {
892
+ return toNapiQueueMessage(
893
+ await asNativeActorContext(ctx)
894
+ .queue()
895
+ .waitForNames(
896
+ names,
897
+ options,
898
+ signal ? asNativeCancellationToken(signal) : signal,
899
+ ),
900
+ );
901
+ }
902
+
903
+ async actorQueueWaitForNamesAvailable(
904
+ ctx: ActorContextHandle,
905
+ names: string[],
906
+ options?: RuntimeQueueWaitOptions | undefined | null,
907
+ signal?: CancellationTokenHandle | undefined | null,
908
+ ): Promise<void> {
909
+ await asNativeActorContext(ctx)
910
+ .queue()
911
+ .waitForNamesAvailable(
912
+ names,
913
+ options,
914
+ signal ? asNativeCancellationToken(signal) : signal,
915
+ );
916
+ }
917
+
918
+ async actorQueueCompletePersisted(
919
+ ctx: ActorContextHandle,
920
+ messageId: bigint,
921
+ expectedName: string,
922
+ response?: RuntimeBytes | undefined | null,
923
+ ): Promise<boolean> {
924
+ return await asNativeActorContext(ctx)
925
+ .queue()
926
+ .completePersisted(
927
+ messageId,
928
+ expectedName,
929
+ response == null ? response : toNapiBuffer(response),
930
+ );
931
+ }
932
+
933
+ async actorQueueEnqueueAndWait(
934
+ ctx: ActorContextHandle,
935
+ name: string,
936
+ body: RuntimeBytes,
937
+ options?: RuntimeQueueEnqueueAndWaitOptions | undefined | null,
938
+ signal?: CancellationTokenHandle | undefined | null,
939
+ ): Promise<RuntimeBytes | null> {
940
+ return await asNativeActorContext(ctx)
941
+ .queue()
942
+ .enqueueAndWait(
943
+ name,
944
+ toNapiBuffer(body),
945
+ options,
946
+ signal ? asNativeCancellationToken(signal) : signal,
947
+ );
948
+ }
949
+
950
+ actorQueueTryNextBatch(
951
+ ctx: ActorContextHandle,
952
+ options?: RuntimeQueueTryNextBatchOptions | undefined | null,
953
+ ): RuntimeQueueMessage[] {
954
+ return asNativeActorContext(ctx)
955
+ .queue()
956
+ .tryNextBatch(options)
957
+ .map(toNapiQueueMessage);
958
+ }
959
+
960
+ actorQueueMaxSize(ctx: ActorContextHandle): number {
961
+ return asNativeActorContext(ctx).queue().maxSize();
962
+ }
963
+
964
+ async actorQueueInspectMessages(ctx: ActorContextHandle) {
965
+ return await asNativeActorContext(ctx).queue().inspectMessages();
966
+ }
967
+
968
+ async actorQueueReset(ctx: ActorContextHandle): Promise<void> {
969
+ await asNativeActorContext(ctx).queue().reset();
970
+ }
971
+
972
+ async actorScheduleAfter(
973
+ ctx: ActorContextHandle,
974
+ durationMs: number,
975
+ actionName: string,
976
+ args: RuntimeBytes,
977
+ ): Promise<string> {
978
+ return await asNativeActorContext(ctx)
979
+ .schedule()
980
+ .after(durationMs, actionName, toNapiBuffer(args));
981
+ }
982
+
983
+ async actorScheduleAt(
984
+ ctx: ActorContextHandle,
985
+ timestampMs: number,
986
+ actionName: string,
987
+ args: RuntimeBytes,
988
+ ): Promise<string> {
989
+ return await asNativeActorContext(ctx)
990
+ .schedule()
991
+ .at(timestampMs, actionName, toNapiBuffer(args));
992
+ }
993
+
994
+ async actorScheduleCancel(ctx: ActorContextHandle, id: string) {
995
+ return await asNativeActorContext(ctx).schedule().cancel(id);
996
+ }
997
+
998
+ async actorScheduleGet(
999
+ ctx: ActorContextHandle,
1000
+ id: string,
1001
+ ): Promise<RuntimeScheduledEventInfo | undefined> {
1002
+ return (
1003
+ (await asNativeActorContext(ctx).schedule().get(id)) ?? undefined
1004
+ );
1005
+ }
1006
+
1007
+ async actorScheduleList(ctx: ActorContextHandle) {
1008
+ return await asNativeActorContext(ctx).schedule().list();
1009
+ }
1010
+
1011
+ async actorCronSet(
1012
+ ctx: ActorContextHandle,
1013
+ name: string,
1014
+ expression: string,
1015
+ timezone: string | undefined,
1016
+ actionName: string,
1017
+ args: RuntimeBytes,
1018
+ maxHistory: number | undefined,
1019
+ ) {
1020
+ await asNativeActorContext(ctx)
1021
+ .schedule()
1022
+ .cronSet(
1023
+ name,
1024
+ expression,
1025
+ timezone,
1026
+ actionName,
1027
+ toNapiBuffer(args),
1028
+ maxHistory,
1029
+ );
1030
+ }
1031
+
1032
+ async actorCronEvery(
1033
+ ctx: ActorContextHandle,
1034
+ name: string,
1035
+ intervalMs: number,
1036
+ actionName: string,
1037
+ args: RuntimeBytes,
1038
+ maxHistory: number | undefined,
1039
+ ) {
1040
+ await asNativeActorContext(ctx)
1041
+ .schedule()
1042
+ .cronEvery(
1043
+ name,
1044
+ intervalMs,
1045
+ actionName,
1046
+ toNapiBuffer(args),
1047
+ maxHistory,
1048
+ );
1049
+ }
1050
+
1051
+ async actorCronGet(
1052
+ ctx: ActorContextHandle,
1053
+ name: string,
1054
+ ): Promise<RuntimeCronJobInfo | undefined> {
1055
+ return ((await asNativeActorContext(ctx).schedule().cronGet(name)) ??
1056
+ undefined) as RuntimeCronJobInfo | undefined;
1057
+ }
1058
+
1059
+ async actorCronList(
1060
+ ctx: ActorContextHandle,
1061
+ ): Promise<RuntimeCronJobInfo[]> {
1062
+ return (await asNativeActorContext(ctx)
1063
+ .schedule()
1064
+ .cronList()) as RuntimeCronJobInfo[];
1065
+ }
1066
+
1067
+ async actorCronDelete(ctx: ActorContextHandle, name: string) {
1068
+ return await asNativeActorContext(ctx).schedule().cronDelete(name);
1069
+ }
1070
+
1071
+ async actorCronHistory(
1072
+ ctx: ActorContextHandle,
1073
+ name: string,
1074
+ limit: number | undefined,
1075
+ ): Promise<RuntimeCronFire[]> {
1076
+ return (await asNativeActorContext(ctx)
1077
+ .schedule()
1078
+ .cronHistory(name, limit)) as RuntimeCronFire[];
1079
+ }
1080
+
1081
+ connId(conn: ConnHandle): string {
1082
+ return asNativeConn(conn).id();
1083
+ }
1084
+
1085
+ connParams(conn: ConnHandle): RuntimeBytes {
1086
+ return asNativeConn(conn).params();
1087
+ }
1088
+
1089
+ connState(conn: ConnHandle): RuntimeBytes {
1090
+ return asNativeConn(conn).state();
1091
+ }
1092
+
1093
+ connSetState(conn: ConnHandle, state: RuntimeBytes): void {
1094
+ asNativeConn(conn).setState(toNapiBuffer(state));
1095
+ }
1096
+
1097
+ connIsHibernatable(conn: ConnHandle): boolean {
1098
+ return asNativeConn(conn).isHibernatable();
1099
+ }
1100
+
1101
+ connSend(conn: ConnHandle, name: string, args: RuntimeBytes): void {
1102
+ asNativeConn(conn).send(name, toNapiBuffer(args));
1103
+ }
1104
+
1105
+ async connDisconnect(
1106
+ conn: ConnHandle,
1107
+ reason?: string | undefined | null,
1108
+ ): Promise<void> {
1109
+ await asNativeConn(conn).disconnect(reason);
1110
+ }
1111
+
1112
+ webSocketSend(
1113
+ ws: WebSocketHandle,
1114
+ data: RuntimeBytes,
1115
+ binary: boolean,
1116
+ ): void {
1117
+ asNativeWebSocket(ws).send(toNapiBuffer(data), binary);
1118
+ }
1119
+
1120
+ async webSocketClose(
1121
+ ws: WebSocketHandle,
1122
+ code?: number | undefined | null,
1123
+ reason?: string | undefined | null,
1124
+ ): Promise<void> {
1125
+ await asNativeWebSocket(ws).close(code, reason);
1126
+ }
1127
+
1128
+ webSocketSetEventCallback(
1129
+ ws: WebSocketHandle,
1130
+ callback: (event: RuntimeWebSocketEvent) => void,
1131
+ ): void {
1132
+ asNativeWebSocket(ws).setEventCallback(callback);
1133
+ }
1134
+ }
1135
+
1136
+ export type NapiBindings = NativeBindings;
1137
+
1138
+ export async function loadNapiRuntime(): Promise<{
1139
+ bindings: NapiBindings;
1140
+ runtime: NapiCoreRuntime;
1141
+ }> {
1142
+ // LOAD-BEARING: the specifier is computed (`.join("/")`) on purpose. Do NOT
1143
+ // "simplify" it to a literal `import("@rivetkit/rivetkit-napi")`. Edge
1144
+ // adapters (e.g. @rivetkit/supabase) pre-bundle this module for Deno; a
1145
+ // literal dynamic import is statically resolvable, so Deno's eszip bundler
1146
+ // would snapshot the native `.node` addon into the deploy and 413. The
1147
+ // computed specifier keeps it opaque to static analysis so it is never
1148
+ // bundled. Enforced by scripts/ci/check-edge-native-closure.mjs.
1149
+ const bindings = await import(["@rivetkit", "rivetkit-napi"].join("/"));
1150
+ return {
1151
+ bindings,
1152
+ runtime: new NapiCoreRuntime(bindings),
1153
+ };
1154
+ }