rivetkit 0.0.0-active-actors-fix-active-count.253e406 → 0.0.0-bisect-sync-sqlite-1.910d1c7

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 (303) hide show
  1. package/dist/browser/client.d.ts +1286 -89
  2. package/dist/browser/client.js +1699 -704
  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 +2083 -236
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +8 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -1
  10. package/dist/tsup/actor/errors.d.ts +1 -1
  11. package/dist/tsup/actor/errors.js +7 -1
  12. package/dist/tsup/agent-os/index.cjs +3 -2289
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +1 -1813
  15. package/dist/tsup/agent-os/index.d.ts +1 -1813
  16. package/dist/tsup/agent-os/index.js +3 -2289
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/chunk-2JJU7IQ7.cjs +14 -0
  19. package/dist/tsup/chunk-2JJU7IQ7.cjs.map +1 -0
  20. package/dist/tsup/{chunk-DZJZLZ4R.js → chunk-2MXBZGUP.js} +2 -2
  21. package/dist/tsup/{chunk-L4FVS7GY.js → chunk-47IS5ZFI.js} +6 -6
  22. package/dist/tsup/chunk-47IS5ZFI.js.map +1 -0
  23. package/dist/tsup/{chunk-DXWOGCZY.js → chunk-5XWY5CBA.js} +70 -8
  24. package/dist/tsup/chunk-5XWY5CBA.js.map +1 -0
  25. package/dist/tsup/{chunk-OQEEZDIK.cjs → chunk-6XVNZKKP.cjs} +506 -31
  26. package/dist/tsup/chunk-6XVNZKKP.cjs.map +1 -0
  27. package/dist/tsup/{chunk-3YY5S6TV.js → chunk-A2CF6PFH.js} +20 -12
  28. package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
  29. package/dist/tsup/{chunk-WQ4HNA4W.cjs → chunk-AQJFKHBK.cjs} +276 -27
  30. package/dist/tsup/chunk-AQJFKHBK.cjs.map +1 -0
  31. package/dist/tsup/chunk-DPWLASMC.cjs +244 -0
  32. package/dist/tsup/chunk-DPWLASMC.cjs.map +1 -0
  33. package/dist/tsup/{chunk-VUF2YEF4.cjs → chunk-DYJOU3MF.cjs} +10 -10
  34. package/dist/tsup/chunk-DYJOU3MF.cjs.map +1 -0
  35. package/dist/tsup/{chunk-7WXCRV25.cjs → chunk-ELZ6MT3V.cjs} +18 -11
  36. package/dist/tsup/chunk-ELZ6MT3V.cjs.map +1 -0
  37. package/dist/tsup/chunk-HYIVSH5X.js +244 -0
  38. package/dist/tsup/chunk-HYIVSH5X.js.map +1 -0
  39. package/dist/tsup/{chunk-T6YVRM4K.js → chunk-IHEO5OT3.js} +275 -26
  40. package/dist/tsup/chunk-IHEO5OT3.js.map +1 -0
  41. package/dist/tsup/chunk-IWACVNN5.js +14 -0
  42. package/dist/tsup/chunk-IWACVNN5.js.map +1 -0
  43. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JTHHCZCZ.js} +24 -1
  44. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  45. package/dist/tsup/{chunk-V4JUWAT6.js → chunk-K3AJOPSF.js} +498 -23
  46. package/dist/tsup/chunk-K3AJOPSF.js.map +1 -0
  47. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-KVFIONKQ.cjs} +25 -2
  48. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  49. package/dist/tsup/{chunk-QAZLM4WT.cjs → chunk-MGI2VVF6.cjs} +45 -9
  50. package/dist/tsup/chunk-MGI2VVF6.cjs.map +1 -0
  51. package/dist/tsup/{chunk-QNLGOTQP.js → chunk-NPHTIUUJ.js} +1291 -420
  52. package/dist/tsup/chunk-NPHTIUUJ.js.map +1 -0
  53. package/dist/tsup/{chunk-7GKQJFSB.cjs → chunk-POCSZXPQ.cjs} +72 -10
  54. package/dist/tsup/chunk-POCSZXPQ.cjs.map +1 -0
  55. package/dist/tsup/{chunk-LD5YASJU.cjs → chunk-RTWH3AX3.cjs} +19 -11
  56. package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
  57. package/dist/tsup/{chunk-ARRBQTAR.cjs → chunk-X76BM5JS.cjs} +1420 -549
  58. package/dist/tsup/chunk-X76BM5JS.cjs.map +1 -0
  59. package/dist/tsup/{chunk-AGBP757I.js → chunk-X7SYKFPM.js} +16 -9
  60. package/dist/tsup/chunk-X7SYKFPM.js.map +1 -0
  61. package/dist/tsup/{chunk-GKAMEBTZ.cjs → chunk-XLDJSZ3B.cjs} +3 -3
  62. package/dist/tsup/{chunk-GKAMEBTZ.cjs.map → chunk-XLDJSZ3B.cjs.map} +1 -1
  63. package/dist/tsup/{chunk-KIWH5H3K.js → chunk-ZOVJDAOW.js} +44 -8
  64. package/dist/tsup/chunk-ZOVJDAOW.js.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 +33 -7
  68. package/dist/tsup/client/mod.d.ts +33 -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.js +3 -3
  77. package/dist/tsup/{config-nlTlOKaE.d.cts → config-BMGUEKMZ.d.cts} +1252 -75
  78. package/dist/tsup/config-CBTr4IhE.d.cts +224 -0
  79. package/dist/tsup/config-CBTr4IhE.d.ts +224 -0
  80. package/dist/tsup/{config-D7ykAntK.d.ts → config-iBpXyITj.d.ts} +1252 -75
  81. package/dist/tsup/context-hyVakgnU.d.ts +136 -0
  82. package/dist/tsup/context-qfvLuF0P.d.cts +136 -0
  83. package/dist/tsup/db/drizzle.cjs +127 -67
  84. package/dist/tsup/db/drizzle.cjs.map +1 -1
  85. package/dist/tsup/db/drizzle.d.cts +8 -4
  86. package/dist/tsup/db/drizzle.d.ts +8 -4
  87. package/dist/tsup/db/drizzle.js +127 -67
  88. package/dist/tsup/db/drizzle.js.map +1 -1
  89. package/dist/tsup/db/mod.cjs +6 -124
  90. package/dist/tsup/db/mod.cjs.map +1 -1
  91. package/dist/tsup/db/mod.d.cts +12 -4
  92. package/dist/tsup/db/mod.d.ts +12 -4
  93. package/dist/tsup/db/mod.js +5 -123
  94. package/dist/tsup/db/mod.js.map +1 -1
  95. package/dist/tsup/dynamic/mod.cjs +24 -0
  96. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  97. package/dist/tsup/dynamic/mod.d.cts +39 -0
  98. package/dist/tsup/dynamic/mod.d.ts +39 -0
  99. package/dist/tsup/dynamic/mod.js +24 -0
  100. package/dist/tsup/dynamic/mod.js.map +1 -0
  101. package/dist/tsup/inspector/mod.cjs +47 -12
  102. package/dist/tsup/inspector/mod.cjs.map +1 -1
  103. package/dist/tsup/inspector/mod.d.cts +26 -136
  104. package/dist/tsup/inspector/mod.d.ts +26 -136
  105. package/dist/tsup/inspector/mod.js +46 -11
  106. package/dist/tsup/inspector/mod.js.map +1 -1
  107. package/dist/tsup/inspector/workflow.cjs +21 -0
  108. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  109. package/dist/tsup/inspector/workflow.d.cts +144 -0
  110. package/dist/tsup/inspector/workflow.d.ts +144 -0
  111. package/dist/tsup/inspector/workflow.js +21 -0
  112. package/dist/tsup/inspector/workflow.js.map +1 -0
  113. package/dist/tsup/inspector-tab/mod.cjs +162 -0
  114. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  115. package/dist/tsup/inspector-tab/mod.d.cts +242 -0
  116. package/dist/tsup/inspector-tab/mod.d.ts +242 -0
  117. package/dist/tsup/inspector-tab/mod.js +162 -0
  118. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  119. package/dist/tsup/mod.cjs +3527 -753
  120. package/dist/tsup/mod.cjs.map +1 -1
  121. package/dist/tsup/mod.d.cts +11 -7
  122. package/dist/tsup/mod.d.ts +11 -7
  123. package/dist/tsup/mod.js +3468 -694
  124. package/dist/tsup/mod.js.map +1 -1
  125. package/dist/tsup/test/mod.cjs +26 -21
  126. package/dist/tsup/test/mod.cjs.map +1 -1
  127. package/dist/tsup/test/mod.d.cts +7 -5
  128. package/dist/tsup/test/mod.d.ts +7 -5
  129. package/dist/tsup/test/mod.js +25 -20
  130. package/dist/tsup/test/mod.js.map +1 -1
  131. package/dist/tsup/unstable/migrations.cjs +101 -0
  132. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  133. package/dist/tsup/unstable/migrations.d.cts +29 -0
  134. package/dist/tsup/unstable/migrations.d.ts +29 -0
  135. package/dist/tsup/unstable/migrations.js +101 -0
  136. package/dist/tsup/unstable/migrations.js.map +1 -0
  137. package/dist/tsup/{utils-DVekpm4I.d.ts → utils-3rsE0JvI.d.cts} +26 -2
  138. package/dist/tsup/{utils-DVekpm4I.d.cts → utils-3rsE0JvI.d.ts} +26 -2
  139. package/dist/tsup/utils-C1O-SklO.d.ts +162 -0
  140. package/dist/tsup/utils-C6b0aYUM.d.cts +162 -0
  141. package/dist/tsup/utils.cjs +7 -3
  142. package/dist/tsup/utils.cjs.map +1 -1
  143. package/dist/tsup/utils.d.cts +4 -147
  144. package/dist/tsup/utils.d.ts +4 -147
  145. package/dist/tsup/utils.js +6 -2
  146. package/dist/tsup/workflow/mod.cjs +557 -410
  147. package/dist/tsup/workflow/mod.cjs.map +1 -1
  148. package/dist/tsup/workflow/mod.d.cts +11 -9
  149. package/dist/tsup/workflow/mod.d.ts +11 -9
  150. package/dist/tsup/workflow/mod.js +551 -404
  151. package/dist/tsup/workflow/mod.js.map +1 -1
  152. package/package.json +67 -18
  153. package/src/actor/actions.ts +155 -0
  154. package/src/actor/config.ts +679 -101
  155. package/src/actor/contexts/index.ts +9 -2
  156. package/src/actor/definition.ts +75 -26
  157. package/src/actor/errors.ts +107 -8
  158. package/src/actor/keys.ts +87 -1
  159. package/src/actor/mod.ts +36 -20
  160. package/src/actor/schema.ts +6 -2
  161. package/src/actor/tracing.ts +28 -0
  162. package/src/agent-os/index.ts +5 -64
  163. package/src/client/actor-common.ts +84 -54
  164. package/src/client/actor-conn.ts +231 -68
  165. package/src/client/actor-handle.ts +283 -89
  166. package/src/client/actor-query.ts +13 -6
  167. package/src/client/auth.ts +37 -0
  168. package/src/client/client.ts +182 -21
  169. package/src/client/config.ts +28 -3
  170. package/src/client/errors.ts +15 -2
  171. package/src/client/lifecycle-errors.ts +63 -5
  172. package/src/client/mod.ts +10 -0
  173. package/src/client/outbound-telemetry.ts +66 -0
  174. package/src/client/query.ts +3 -1
  175. package/src/client/queue.ts +13 -3
  176. package/src/client/raw-utils.ts +59 -14
  177. package/src/client/resolve-gateway-target.ts +3 -1
  178. package/src/client/retry-budget.ts +81 -0
  179. package/src/client/token-provider.ts +93 -0
  180. package/src/client/utils.ts +22 -9
  181. package/src/common/actor-router-consts.ts +6 -0
  182. package/src/common/actor-telemetry-context.ts +19 -0
  183. package/src/common/actor-websocket.ts +3 -1
  184. package/src/common/bare/actor-persist/v1.ts +205 -163
  185. package/src/common/bare/actor-persist/v2.ts +265 -213
  186. package/src/common/bare/actor-persist/v3.ts +176 -172
  187. package/src/common/bare/actor-persist/v4.ts +254 -253
  188. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  189. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  190. package/src/common/bare/transport/v1.ts +694 -542
  191. package/src/common/client-protocol-versioned.ts +66 -64
  192. package/src/common/database/config.ts +175 -9
  193. package/src/common/database/mod.ts +262 -77
  194. package/src/common/database/native-database.ts +442 -20
  195. package/src/common/database/shared.ts +25 -0
  196. package/src/common/encoding.ts +278 -34
  197. package/src/common/engine.ts +28 -1
  198. package/src/common/eventsource.ts +1 -1
  199. package/src/common/fetch-like.ts +63 -0
  200. package/src/common/inline-websocket-adapter.ts +14 -13
  201. package/src/common/inspector-transport.ts +12 -2
  202. package/src/common/log.ts +15 -8
  203. package/src/common/otel-context.ts +99 -0
  204. package/src/common/router.ts +14 -18
  205. package/src/common/utils.ts +8 -169
  206. package/src/common/websocket-interface.ts +1 -1
  207. package/src/db/drizzle.ts +182 -80
  208. package/src/db/mod.ts +5 -1
  209. package/src/devtools-loader/index.ts +4 -7
  210. package/src/devtools-loader/serve-devtools.ts +26 -0
  211. package/src/dynamic/instance.ts +32 -0
  212. package/src/dynamic/internal.ts +50 -0
  213. package/src/dynamic/isolate-runtime.ts +64 -0
  214. package/src/dynamic/mod.ts +32 -0
  215. package/src/engine-api/actors.ts +15 -3
  216. package/src/engine-client/actor-http-client.ts +37 -22
  217. package/src/engine-client/actor-websocket-client.ts +37 -9
  218. package/src/engine-client/api-endpoints.ts +128 -5
  219. package/src/engine-client/api-utils.ts +2 -2
  220. package/src/engine-client/driver.ts +21 -16
  221. package/src/engine-client/mod.ts +280 -111
  222. package/src/engine-client/ws-proxy.ts +9 -4
  223. package/src/inspector/client.browser.ts +127 -15
  224. package/src/inspector/mod.ts +2 -4
  225. package/src/inspector/workflow.ts +61 -0
  226. package/src/inspector-tab/mod.ts +301 -0
  227. package/src/registry/config/envoy.ts +3 -20
  228. package/src/registry/config/index.ts +91 -74
  229. package/src/registry/index.ts +702 -80
  230. package/src/registry/napi-runtime.ts +643 -42
  231. package/src/registry/native-http.ts +225 -0
  232. package/src/registry/native-validation.ts +10 -12
  233. package/src/registry/native.ts +1881 -582
  234. package/src/registry/run-handler-coordinator.ts +219 -0
  235. package/src/registry/runtime.ts +441 -7
  236. package/src/registry/wasm-runtime.ts +516 -23
  237. package/src/registry/write-through-proxy.ts +141 -0
  238. package/src/serde.ts +2 -2
  239. package/src/serverless/configure.ts +18 -7
  240. package/src/test/mod.ts +2 -17
  241. package/src/test/ready.ts +22 -0
  242. package/src/unstable/migrations.ts +141 -0
  243. package/src/utils/endpoint-parser.ts +1 -1
  244. package/src/utils/env-vars.ts +50 -2
  245. package/src/utils/node.ts +44 -28
  246. package/src/utils/router.ts +1 -1
  247. package/src/utils.ts +4 -2
  248. package/src/workflow/context.ts +703 -242
  249. package/src/workflow/driver.ts +284 -125
  250. package/src/workflow/inspector.ts +24 -24
  251. package/src/workflow/mod.ts +76 -84
  252. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  253. package/dist/tsup/chunk-3YY5S6TV.js.map +0 -1
  254. package/dist/tsup/chunk-6NT2LIFN.cjs +0 -268
  255. package/dist/tsup/chunk-6NT2LIFN.cjs.map +0 -1
  256. package/dist/tsup/chunk-7GKQJFSB.cjs.map +0 -1
  257. package/dist/tsup/chunk-7WXCRV25.cjs.map +0 -1
  258. package/dist/tsup/chunk-AGBP757I.js.map +0 -1
  259. package/dist/tsup/chunk-ARRBQTAR.cjs.map +0 -1
  260. package/dist/tsup/chunk-DXWOGCZY.js.map +0 -1
  261. package/dist/tsup/chunk-JGIPV3QU.js +0 -268
  262. package/dist/tsup/chunk-JGIPV3QU.js.map +0 -1
  263. package/dist/tsup/chunk-KIWH5H3K.js.map +0 -1
  264. package/dist/tsup/chunk-L4FVS7GY.js.map +0 -1
  265. package/dist/tsup/chunk-LD5YASJU.cjs.map +0 -1
  266. package/dist/tsup/chunk-OQEEZDIK.cjs.map +0 -1
  267. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  268. package/dist/tsup/chunk-QAZLM4WT.cjs.map +0 -1
  269. package/dist/tsup/chunk-QNLGOTQP.js.map +0 -1
  270. package/dist/tsup/chunk-T6YVRM4K.js.map +0 -1
  271. package/dist/tsup/chunk-V4JUWAT6.js.map +0 -1
  272. package/dist/tsup/chunk-VUF2YEF4.cjs.map +0 -1
  273. package/dist/tsup/chunk-WQ4HNA4W.cjs.map +0 -1
  274. package/dist/tsup/config-CxjGYf4K.d.cts +0 -121
  275. package/dist/tsup/config-CxjGYf4K.d.ts +0 -121
  276. package/dist/tsup/context-88opQsfZ.d.cts +0 -92
  277. package/dist/tsup/context-CVEG5zfg.d.ts +0 -92
  278. package/src/actor/driver.ts +0 -141
  279. package/src/actor/instance/mod.ts +0 -2492
  280. package/src/actor/keys.test.ts +0 -275
  281. package/src/actor/utils.test.ts +0 -48
  282. package/src/agent-os/actor/cron.ts +0 -84
  283. package/src/agent-os/actor/db.ts +0 -54
  284. package/src/agent-os/actor/filesystem.ts +0 -131
  285. package/src/agent-os/actor/index.ts +0 -265
  286. package/src/agent-os/actor/network.ts +0 -58
  287. package/src/agent-os/actor/preview.ts +0 -191
  288. package/src/agent-os/actor/process.ts +0 -177
  289. package/src/agent-os/actor/session.ts +0 -518
  290. package/src/agent-os/actor/shell.ts +0 -60
  291. package/src/agent-os/config.ts +0 -79
  292. package/src/agent-os/fs/database-vfs.ts +0 -624
  293. package/src/agent-os/types.ts +0 -148
  294. package/src/client/test.ts +0 -44
  295. package/src/common/database/mod.test.ts +0 -137
  296. package/src/common/database/native-database.test.ts +0 -211
  297. package/src/common/inline-websocket-adapter.test.ts +0 -51
  298. package/src/drivers/engine/actor-driver.ts +0 -2460
  299. package/src/registry/runtime.test.ts +0 -50
  300. package/src/utils/endpoint-parser.test.ts +0 -206
  301. package/src/utils/serve.ts +0 -217
  302. package/src/utils.test.ts +0 -34
  303. /package/dist/tsup/{chunk-DZJZLZ4R.js.map → chunk-2MXBZGUP.js.map} +0 -0
@@ -0,0 +1,141 @@
1
+ import onChange from "@rivetkit/on-change";
2
+
3
+ /**
4
+ * Creates a proxy that tracks deep mutations on an object and calls `commit`
5
+ * after every change. Uses `@rivetkit/on-change` internally, which correctly
6
+ * detects mutations via methods on Map, Set, Date, TypedArrays, and arrays.
7
+ *
8
+ * If the value is not an object (primitive, null, undefined), it is returned
9
+ * as-is since primitives cannot be proxied or mutated.
10
+ *
11
+ * @param value - The root value to watch.
12
+ * @param commit - Called after every detected mutation with the root object.
13
+ * @param beforeChange - Called before every mutation with the new value being
14
+ * assigned. Throw to reject the change.
15
+ */
16
+ export function createWriteThroughProxy<T>(
17
+ value: T,
18
+ commit: (next: T) => void,
19
+ beforeChange?: (newValue: unknown) => void,
20
+ ): T {
21
+ if (!value || typeof value !== "object") {
22
+ return value;
23
+ }
24
+
25
+ return onChange(
26
+ value as T & Record<string, any>,
27
+ () => {
28
+ commit(value);
29
+ },
30
+ {
31
+ // Rejection is throw-based: beforeChange throws to prevent the
32
+ // mutation. We always return true so on-change applies the change
33
+ // if beforeChange did not throw.
34
+ onValidate(_path: string, newValue: unknown) {
35
+ beforeChange?.(newValue);
36
+ return true;
37
+ },
38
+ },
39
+ ) as T;
40
+ }
41
+
42
+ /**
43
+ * Returns the raw target behind an `@rivetkit/on-change` proxy, following
44
+ * chains of proxies wrapping proxies until reaching a plain value.
45
+ */
46
+ function unwrapProxy(value: unknown): unknown {
47
+ let current = value;
48
+ while (current !== null && typeof current === "object") {
49
+ const target = onChange.target(current as Record<string, any>);
50
+ if (target === current) {
51
+ break;
52
+ }
53
+ current = target;
54
+ }
55
+ return current;
56
+ }
57
+
58
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
59
+ const proto = Object.getPrototypeOf(value as object);
60
+ return proto === Object.prototype || proto === null;
61
+ }
62
+
63
+ function unwrapDeep(value: unknown, seen: Set<object>): unknown {
64
+ const unwrapped = unwrapProxy(value);
65
+ if (!unwrapped || typeof unwrapped !== "object") {
66
+ return unwrapped;
67
+ }
68
+ if (seen.has(unwrapped)) {
69
+ return unwrapped;
70
+ }
71
+ seen.add(unwrapped);
72
+
73
+ if (Array.isArray(unwrapped)) {
74
+ for (let i = 0; i < unwrapped.length; i++) {
75
+ const child = unwrapDeep(unwrapped[i], seen);
76
+ if (child !== unwrapped[i]) {
77
+ unwrapped[i] = child;
78
+ }
79
+ }
80
+ return unwrapped;
81
+ }
82
+
83
+ if (unwrapped instanceof Map) {
84
+ const replacements: [unknown, unknown, unknown][] = [];
85
+ for (const [key, child] of unwrapped.entries()) {
86
+ const nextKey = unwrapDeep(key, seen);
87
+ const nextChild = unwrapDeep(child, seen);
88
+ if (nextKey !== key || nextChild !== child) {
89
+ replacements.push([key, nextKey, nextChild]);
90
+ }
91
+ }
92
+ for (const [key, nextKey, nextChild] of replacements) {
93
+ if (nextKey !== key) {
94
+ unwrapped.delete(key);
95
+ }
96
+ unwrapped.set(nextKey, nextChild);
97
+ }
98
+ return unwrapped;
99
+ }
100
+
101
+ if (unwrapped instanceof Set) {
102
+ const replacements: [unknown, unknown][] = [];
103
+ for (const child of unwrapped.values()) {
104
+ const next = unwrapDeep(child, seen);
105
+ if (next !== child) {
106
+ replacements.push([child, next]);
107
+ }
108
+ }
109
+ for (const [child, next] of replacements) {
110
+ unwrapped.delete(child);
111
+ unwrapped.add(next);
112
+ }
113
+ return unwrapped;
114
+ }
115
+
116
+ if (isPlainObject(unwrapped)) {
117
+ for (const key of Object.keys(unwrapped)) {
118
+ const child = unwrapDeep(unwrapped[key], seen);
119
+ if (child !== unwrapped[key]) {
120
+ unwrapped[key] = child;
121
+ }
122
+ }
123
+ }
124
+
125
+ return unwrapped;
126
+ }
127
+
128
+ /**
129
+ * Strips every `@rivetkit/on-change` proxy out of a value in place, including
130
+ * proxies nested inside plain objects, arrays, `Map`s, and `Set`s.
131
+ *
132
+ * A read of `c.state` or `conn.state` hands back a deep write-through proxy,
133
+ * so an update written as `c.state = { ...c.state, foo }` produces a plain root
134
+ * object whose children are still proxies. Persisting that value as-is makes
135
+ * the next read wrap proxies in another proxy layer, and each layer multiplies
136
+ * the work of traversing the state, so repeated spread updates degrade
137
+ * exponentially. Unwrapping before persisting keeps stored state proxy-free.
138
+ */
139
+ export function unwrapWriteThroughProxy<T>(value: T): T {
140
+ return unwrapDeep(value, new Set()) as T;
141
+ }
package/src/serde.ts CHANGED
@@ -2,8 +2,8 @@ import * as cbor from "cbor-x";
2
2
  import invariant from "invariant";
3
3
  import type { VersionedDataHandler } from "vbare";
4
4
  import type { z } from "zod/v4";
5
+ import type { Encoding, JsonCompatValue } from "@/common/encoding";
5
6
  import { assertUnreachable } from "@/common/utils";
6
- import type { Encoding } from "@/common/encoding";
7
7
  import {
8
8
  encodeJsonCompatValue,
9
9
  jsonParseCompat,
@@ -46,7 +46,7 @@ export function contentTypeForEncoding(encoding: Encoding): string {
46
46
  }
47
47
  }
48
48
 
49
- export function encodeCborCompat(value: unknown): Uint8Array {
49
+ export function encodeCborCompat(value: JsonCompatValue): Uint8Array {
50
50
  return cbor.encode(encodeJsonCompatValue(value));
51
51
  }
52
52
 
@@ -1,9 +1,9 @@
1
1
  import { convertRegistryConfigToClientConfig } from "@/client/config";
2
+ import { stringifyError } from "@/common/utils";
2
3
  import {
3
4
  getDatacenters,
4
5
  updateRunnerConfig,
5
6
  } from "@/engine-client/api-endpoints";
6
- import { stringifyError } from "@/common/utils";
7
7
  import type { RegistryConfig } from "@/registry/config";
8
8
  import { logger } from "@/registry/log";
9
9
 
@@ -16,11 +16,14 @@ function sleep(ms: number): Promise<void> {
16
16
 
17
17
  function configureTimeoutMs() {
18
18
  const value = process.env.RIVET_SERVERLESS_CONFIGURE_TIMEOUT_MS;
19
- if (value === undefined || value === "") return DEFAULT_CONFIGURE_TIMEOUT_MS;
19
+ if (value === undefined || value === "")
20
+ return DEFAULT_CONFIGURE_TIMEOUT_MS;
20
21
 
21
22
  const parsed = Number(value);
22
23
  if (!Number.isFinite(parsed) || parsed < 0) {
23
- throw new Error("RIVET_SERVERLESS_CONFIGURE_TIMEOUT_MS must be a finite non-negative number");
24
+ throw new Error(
25
+ "RIVET_SERVERLESS_CONFIGURE_TIMEOUT_MS must be a finite non-negative number",
26
+ );
24
27
  }
25
28
 
26
29
  return parsed;
@@ -40,13 +43,19 @@ export async function configureServerlessPool(
40
43
  attempts += 1;
41
44
  try {
42
45
  if (!config.namespace) {
43
- throw new Error("namespace is required for serverless configuration");
46
+ throw new Error(
47
+ "namespace is required for serverless configuration",
48
+ );
44
49
  }
45
50
  if (!config.endpoint) {
46
- throw new Error("endpoint is required for serverless configuration");
51
+ throw new Error(
52
+ "endpoint is required for serverless configuration",
53
+ );
47
54
  }
48
55
  if (!config.configurePool) {
49
- throw new Error("configurePool is required for serverless configuration");
56
+ throw new Error(
57
+ "configurePool is required for serverless configuration",
58
+ );
50
59
  }
51
60
 
52
61
  const customConfig = config.configurePool;
@@ -55,7 +64,9 @@ export async function configureServerlessPool(
55
64
  const poolName = customConfig.name ?? "default";
56
65
  const serverlessToken = config.token ?? config.publicToken;
57
66
  const headers = {
58
- ...(serverlessToken ? { "x-rivet-token": serverlessToken } : {}),
67
+ ...(serverlessToken
68
+ ? { "x-rivet-token": serverlessToken }
69
+ : {}),
59
70
  ...(customConfig.headers ?? {}),
60
71
  };
61
72
  const serverlessConfig = {
package/src/test/mod.ts CHANGED
@@ -1,28 +1,13 @@
1
- import pRetry from "p-retry";
2
1
  import type { TestContext } from "vitest";
3
- import { type Client, createClient } from "@/client/mod";
4
2
  import { convertRegistryConfigToClientConfig } from "@/client/config";
5
- import { getMetadata } from "@/engine-client/api-endpoints";
3
+ import { type Client, createClient } from "@/client/mod";
6
4
  import type { Registry } from "@/registry";
5
+ import { waitForRegistryReady } from "./ready";
7
6
 
8
7
  export interface SetupTestResult<A extends Registry<any>> {
9
8
  client: Client<A>;
10
9
  }
11
10
 
12
- async function waitForRegistryReady(registry: Registry<any>): Promise<void> {
13
- const clientConfig = convertRegistryConfigToClientConfig(
14
- registry.parseConfig(),
15
- );
16
-
17
- await pRetry(async () => {
18
- await getMetadata(clientConfig);
19
- }, {
20
- retries: 20,
21
- minTimeout: 50,
22
- maxTimeout: 250,
23
- });
24
- }
25
-
26
11
  export async function setupTest<A extends Registry<any>>(
27
12
  c: TestContext,
28
13
  registry: A,
@@ -0,0 +1,22 @@
1
+ import pRetry from "p-retry";
2
+ import type { Registry } from "@/registry";
3
+
4
+ export async function waitForRegistryReady(
5
+ registry: Registry<any>,
6
+ ): Promise<void> {
7
+ await pRetry(
8
+ async () => {
9
+ const response = await registry.routes.health();
10
+ if (!response.ok) {
11
+ throw new Error(
12
+ `RivetKit registry is not ready: ${response.status} ${await response.text()}`,
13
+ );
14
+ }
15
+ },
16
+ {
17
+ retries: 120,
18
+ minTimeout: 50,
19
+ maxTimeout: 250,
20
+ },
21
+ );
22
+ }
@@ -0,0 +1,141 @@
1
+ import type { RawAccess } from "@/common/database/config";
2
+
3
+ export const DEFAULT_MIGRATION_TABLE_NAME = "rivetkit_schema_version";
4
+
5
+ export type MigrationDatabase = Pick<RawAccess, "execute">;
6
+
7
+ export type Migration =
8
+ | {
9
+ version: number;
10
+ sql: string;
11
+ up?: never;
12
+ }
13
+ | {
14
+ version: number;
15
+ sql?: never;
16
+ up: (db: MigrationDatabase) => Promise<void> | void;
17
+ };
18
+
19
+ export interface MigrationsOptions {
20
+ /** A table owned by this schema. Override this when multiple schemas share a database. */
21
+ tableName?: string;
22
+ /** Detect the version of a schema that predates its migration table. */
23
+ adopt?: (db: MigrationDatabase) => Promise<number> | number;
24
+ /** Contiguous migrations numbered from 1. */
25
+ migrations: readonly Migration[];
26
+ }
27
+
28
+ /**
29
+ * Creates an `onMigrate` callback that applies each unapplied migration in order.
30
+ * RivetKit runs the callback inside its migration savepoint, making the complete
31
+ * ladder atomic when used with `db({ onMigrate })`.
32
+ */
33
+ export function migrations({
34
+ tableName = DEFAULT_MIGRATION_TABLE_NAME,
35
+ adopt,
36
+ migrations: ladder,
37
+ }: MigrationsOptions): (db: MigrationDatabase) => Promise<void> {
38
+ validateTableName(tableName);
39
+ validateLadder(ladder);
40
+
41
+ return async (database) => {
42
+ const quotedTableName = `"${tableName}"`;
43
+ await database.execute(`
44
+ CREATE TABLE IF NOT EXISTS ${quotedTableName} (
45
+ singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
46
+ schema_version INTEGER NOT NULL CHECK (
47
+ schema_version BETWEEN 0 AND ${Number.MAX_SAFE_INTEGER}
48
+ )
49
+ ) STRICT
50
+ `);
51
+
52
+ const rows = await database.execute<{ schema_version: unknown }>(
53
+ `SELECT schema_version FROM ${quotedTableName} WHERE singleton = 1`,
54
+ );
55
+ if (rows.length > 1) {
56
+ throw new Error(
57
+ `invalid migration table "${tableName}": expected at most one version row, received ${rows.length}`,
58
+ );
59
+ }
60
+
61
+ const rawCurrent = rows[0]?.schema_version;
62
+ let current = rows.length === 0 ? 0 : rawCurrent;
63
+ if (rows.length === 0 && adopt) {
64
+ current = await adopt(database);
65
+ }
66
+ if (
67
+ typeof current !== "number" ||
68
+ !Number.isSafeInteger(current) ||
69
+ current < 0
70
+ ) {
71
+ throw new Error(
72
+ `invalid schema version ${String(current)} in migration table "${tableName}"`,
73
+ );
74
+ }
75
+ if (current > ladder.length) {
76
+ throw new Error(
77
+ `migration version ${current} in "${tableName}" is newer than supported version ${ladder.length}`,
78
+ );
79
+ }
80
+ if (rows.length === 0 && current > 0) {
81
+ await writeVersion(database, quotedTableName, current);
82
+ }
83
+
84
+ for (const migration of ladder.slice(current)) {
85
+ if (migration.sql !== undefined) {
86
+ await database.execute(migration.sql);
87
+ } else {
88
+ await migration.up(database);
89
+ }
90
+ await writeVersion(database, quotedTableName, migration.version);
91
+ }
92
+ };
93
+ }
94
+
95
+ async function writeVersion(
96
+ database: MigrationDatabase,
97
+ quotedTableName: string,
98
+ version: number,
99
+ ): Promise<void> {
100
+ await database.execute(
101
+ `INSERT INTO ${quotedTableName} (singleton, schema_version)
102
+ VALUES (1, ?)
103
+ ON CONFLICT(singleton) DO UPDATE SET schema_version = excluded.schema_version`,
104
+ version,
105
+ );
106
+ }
107
+
108
+ function validateTableName(tableName: string): void {
109
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(tableName)) {
110
+ throw new Error(
111
+ `invalid migration table name "${tableName}": expected a SQLite identifier`,
112
+ );
113
+ }
114
+ }
115
+
116
+ function validateLadder(ladder: readonly Migration[]): void {
117
+ if (ladder.length === 0) {
118
+ throw new Error("migration ladder must not be empty");
119
+ }
120
+
121
+ for (const [index, migration] of ladder.entries()) {
122
+ const expectedVersion = index + 1;
123
+ if (migration.version !== expectedVersion) {
124
+ throw new Error(
125
+ `invalid migration ladder: expected version ${expectedVersion}, received ${migration.version}`,
126
+ );
127
+ }
128
+ const hasSql = typeof migration.sql === "string";
129
+ const hasUp = typeof migration.up === "function";
130
+ if (hasSql === hasUp) {
131
+ throw new Error(
132
+ `invalid migration ${migration.version}: expected exactly one of sql or up`,
133
+ );
134
+ }
135
+ if (hasSql && migration.sql.trim().length === 0) {
136
+ throw new Error(
137
+ `invalid migration ${migration.version}: SQL is empty`,
138
+ );
139
+ }
140
+ }
141
+ }
@@ -1,4 +1,4 @@
1
- import { z } from "zod/v4";
1
+ import type { z } from "zod/v4";
2
2
 
3
3
  export interface ParsedEndpoint {
4
4
  endpoint: string;
@@ -22,12 +22,25 @@ export const getRivetTotalSlots = (): number | undefined => {
22
22
  };
23
23
  export const getRivetRunEngine = (): boolean =>
24
24
  getEnvUniversal("RIVET_RUN_ENGINE") === "1";
25
+ export const getRivetRunEngineHost = (): string | undefined =>
26
+ getEnvUniversal("RIVET_RUN_ENGINE_HOST");
27
+ export const getRivetRunEnginePort = (): number | undefined => {
28
+ const value = getEnvUniversal("RIVET_RUN_ENGINE_PORT");
29
+ return value !== undefined ? parseInt(value, 10) : undefined;
30
+ };
25
31
  export const getRivetRunEngineVersion = (): string | undefined =>
26
32
  getEnvUniversal("RIVET_RUN_ENGINE_VERSION");
33
+ export const getRivetRunServices = (): boolean | undefined => {
34
+ const value = getEnvUniversal("RIVET_RUN_SERVICES");
35
+ return value === undefined ? undefined : value === "1";
36
+ };
27
37
  export const getRivetEnvoyKind = (): string | undefined =>
28
- getEnvUniversal("RIVET_ENVOY_KIND");
38
+ getEnvUniversal("RIVET_NODE_KIND") ?? getEnvUniversal("RIVET_ENVOY_KIND");
29
39
  export const getRivetEnvoyVersion = (): number | undefined => {
30
- const value = getEnvUniversal("RIVET_ENVOY_VERSION");
40
+ // The legacy platform deployment still sets the old name.
41
+ const value =
42
+ getEnvUniversal("RIVET_NODE_VERSION") ??
43
+ getEnvUniversal("RIVET_ENVOY_VERSION");
31
44
  return value !== undefined ? parseInt(value, 10) : undefined;
32
45
  };
33
46
  export const getRivetPublicEndpoint = (): string | undefined =>
@@ -44,6 +57,41 @@ export const getRivetkitStoragePath = (): string | undefined =>
44
57
  getEnvUniversal("RIVETKIT_STORAGE_PATH");
45
58
  export const getRivetkitRuntime = (): string | undefined =>
46
59
  getEnvUniversal("RIVETKIT_RUNTIME");
60
+ export type RuntimeMode = "node" | "serverless";
61
+
62
+ export const getRivetkitRuntimeMode = (): RuntimeMode => {
63
+ const value = getEnvUniversal("RIVETKIT_RUNTIME_MODE");
64
+ if (value === undefined || value === "envoy") return "node";
65
+ // Serverless is disabled for v3.
66
+ if (value === "serverless") {
67
+ throw new Error("serverless is disabled in this version");
68
+ }
69
+ if (value === "node") return value;
70
+ throw new Error(
71
+ `RIVETKIT_RUNTIME_MODE env var must be "node" or "serverless"; got "${value}"`,
72
+ );
73
+ };
74
+
75
+ export const getRivetkitPublicDir = (): string | undefined => {
76
+ const value = getEnvUniversal("RIVETKIT_PUBLIC_DIR");
77
+ return value === undefined || value === "" ? undefined : value;
78
+ };
79
+
80
+ export function parsePortEnv(raw: string | undefined): number | undefined {
81
+ if (raw === undefined || raw === "") return undefined;
82
+ const parsed = Number.parseInt(raw, 10);
83
+ if (
84
+ !Number.isFinite(parsed) ||
85
+ parsed < 1 ||
86
+ parsed > 65535 ||
87
+ String(parsed) !== raw.trim()
88
+ ) {
89
+ throw new Error(
90
+ `RIVET_PORT env var must be an integer between 1 and 65535; got "${raw}"`,
91
+ );
92
+ }
93
+ return parsed;
94
+ }
47
95
 
48
96
  // Logging configuration
49
97
  // DEPRECATED: LOG_LEVEL will be removed in a future version
package/src/utils/node.ts CHANGED
@@ -19,20 +19,35 @@ let nodeStream: typeof import("node:stream/promises") | undefined;
19
19
  let nodeUrl: typeof import("node:url") | undefined;
20
20
 
21
21
  let hasImportedDependencies = false;
22
+ type NodeBuiltin = `node:${string}`;
23
+ let fallbackRequire: ReturnType<typeof createRequire> | undefined;
22
24
 
23
- // Helper to get a require function that works in both CommonJS and ESM.
24
- // We use require() instead of await import() because registry.start() cannot
25
- // be async and needs immediate access to Node.js modules during setup.
26
- export function getRequireFn() {
27
- // TODO: This causes issues in tsup
28
- // CommonJS context - use global require
29
- // if (typeof require !== "undefined") {
30
- // console.log("existing require");
31
- // return require;
32
- // }
33
-
34
- // ESM context - use createRequire with import.meta.url
35
- return createRequire(import.meta.url);
25
+ /**
26
+ * Loads a Node.js built-in synchronously without relying on `import.meta`,
27
+ * which esbuild cannot preserve in CommonJS output. Prefer Node's dedicated
28
+ * built-in loader and retain `createRequire` for Node 22.0-22.2 compatibility.
29
+ *
30
+ * @see https://nodejs.org/api/process.html#processgetbuiltinmoduleid
31
+ * @see https://github.com/evanw/esbuild/issues/1492
32
+ */
33
+ export function loadNodeBuiltin<T>(id: NodeBuiltin): T {
34
+ const nodeProcess = globalThis.process;
35
+ const builtin = nodeProcess?.getBuiltinModule?.(id);
36
+
37
+ if (builtin !== undefined) {
38
+ return builtin as T;
39
+ }
40
+
41
+ if (!nodeProcess?.execPath) {
42
+ throw new Error("Node.js built-ins are not available");
43
+ }
44
+
45
+ // Node 22.0-22.2 does not provide getBuiltinModule(). Avoid import.meta here:
46
+ // esbuild replaces it with an empty shim in CommonJS output, leaving
47
+ // import_meta.url undefined. This loader only resolves built-ins, so the
48
+ // fallback base does not affect package resolution.
49
+ fallbackRequire ??= createRequire(nodeProcess.execPath);
50
+ return fallbackRequire(id) as T;
36
51
  }
37
52
 
38
53
  /**
@@ -48,22 +63,23 @@ export function importNodeDependencies(): void {
48
63
  if (hasImportedDependencies) return;
49
64
 
50
65
  try {
51
- // Get a require function that works in both CommonJS and ESM
52
- const requireFn = getRequireFn();
53
-
54
- // Use requireFn with webpack ignore comment to prevent bundling
55
- nodeCrypto = requireFn(/* webpackIgnore: true */ "node:crypto");
56
- nodeFsSync = requireFn(/* webpackIgnore: true */ "node:fs");
57
- nodeFs = requireFn(/* webpackIgnore: true */ "node:fs/promises");
58
- nodePath = requireFn(/* webpackIgnore: true */ "node:path");
59
- nodeOs = requireFn(/* webpackIgnore: true */ "node:os");
60
- nodeChildProcess = requireFn(
61
- /* webpackIgnore: true */ "node:child_process",
62
- );
63
- nodeStream = requireFn(
64
- /* webpackIgnore: true */ "node:stream/promises",
66
+ nodeCrypto =
67
+ loadNodeBuiltin<typeof import("node:crypto")>("node:crypto");
68
+ nodeFsSync = loadNodeBuiltin<typeof import("node:fs")>("node:fs");
69
+ nodeFs =
70
+ loadNodeBuiltin<typeof import("node:fs/promises")>(
71
+ "node:fs/promises",
72
+ );
73
+ nodePath = loadNodeBuiltin<typeof import("node:path")>("node:path");
74
+ nodeOs = loadNodeBuiltin<typeof import("node:os")>("node:os");
75
+ nodeChildProcess =
76
+ loadNodeBuiltin<typeof import("node:child_process")>(
77
+ "node:child_process",
78
+ );
79
+ nodeStream = loadNodeBuiltin<typeof import("node:stream/promises")>(
80
+ "node:stream/promises",
65
81
  );
66
- nodeUrl = requireFn(/* webpackIgnore: true */ "node:url");
82
+ nodeUrl = loadNodeBuiltin<typeof import("node:url")>("node:url");
67
83
  hasImportedDependencies = true;
68
84
  } catch (err) {
69
85
  console.warn(
@@ -2,12 +2,12 @@ import { OpenAPIHono } from "@hono/zod-openapi";
2
2
  import type { Hono } from "hono";
3
3
  import { createMiddleware } from "hono/factory";
4
4
  import { cors } from "@/common/cors";
5
+ import { getLogger } from "@/common/log";
5
6
  import {
6
7
  handleRouteError,
7
8
  handleRouteNotFound,
8
9
  loggerMiddleware,
9
10
  } from "@/common/router";
10
- import { getLogger } from "@/common/log";
11
11
 
12
12
  export function logger() {
13
13
  return getLogger("router");
package/src/utils.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { stringifyError } from "@/common/utils";
2
1
  import type { Context as HonoContext, Handler as HonoHandler } from "hono";
3
- import { stringify as uuidstringify } from "uuid";
2
+ import { stringifyError } from "@/common/utils";
4
3
  import pkgJson from "../package.json" with { type: "json" };
5
4
  import { getLogger } from "./common/log";
6
5
  import { assertUnreachable } from "./common/utils";
@@ -11,6 +10,9 @@ export { stringifyError };
11
10
  /** @experimental */
12
11
  export { assertUnreachable };
13
12
 
13
+ /** @experimental */
14
+ export { jsonParseCompat, jsonStringifyCompat } from "./common/encoding";
15
+
14
16
  /**
15
17
  * Joins multiple abort signals into one.
16
18
  *