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,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
 
@@ -54,6 +54,12 @@ export function decodeCborCompat<T>(buffer: Uint8Array): T {
54
54
  return reviveJsonCompatValue(cbor.decode(buffer)) as T;
55
55
  }
56
56
 
57
+ export function decodeCborJsonCompat<T>(buffer: Uint8Array): T {
58
+ return reviveJsonCompatValue(cbor.decode(buffer), {
59
+ coerceSafeIntegerBigInts: true,
60
+ }) as T;
61
+ }
62
+
57
63
  export function wsBinaryTypeForEncoding(
58
64
  encoding: Encoding,
59
65
  ): "arraybuffer" | "blob" {
@@ -1,4 +1,5 @@
1
1
  import { convertRegistryConfigToClientConfig } from "@/client/config";
2
+ import { stringifyError } from "@/common/utils";
2
3
  import {
3
4
  getDatacenters,
4
5
  updateRunnerConfig,
@@ -6,62 +7,114 @@ import {
6
7
  import type { RegistryConfig } from "@/registry/config";
7
8
  import { logger } from "@/registry/log";
8
9
 
10
+ const DEFAULT_CONFIGURE_TIMEOUT_MS = 60_000;
11
+ const CONFIGURE_RETRY_DELAY_MS = 1_000;
12
+
13
+ function sleep(ms: number): Promise<void> {
14
+ return new Promise((resolve) => setTimeout(resolve, ms));
15
+ }
16
+
17
+ function configureTimeoutMs() {
18
+ const value = process.env.RIVET_SERVERLESS_CONFIGURE_TIMEOUT_MS;
19
+ if (value === undefined || value === "")
20
+ return DEFAULT_CONFIGURE_TIMEOUT_MS;
21
+
22
+ const parsed = Number(value);
23
+ if (!Number.isFinite(parsed) || parsed < 0) {
24
+ throw new Error(
25
+ "RIVET_SERVERLESS_CONFIGURE_TIMEOUT_MS must be a finite non-negative number",
26
+ );
27
+ }
28
+
29
+ return parsed;
30
+ }
31
+
9
32
  export async function configureServerlessPool(
10
33
  config: RegistryConfig,
11
34
  ): Promise<void> {
12
35
  logger().debug({ msg: "configuring serverless pool" });
13
36
 
14
- try {
15
- if (!config.namespace) {
16
- throw new Error("namespace is required for serverless configuration");
17
- }
18
- if (!config.endpoint) {
19
- throw new Error("endpoint is required for serverless configuration");
20
- }
21
- if (!config.configurePool) {
22
- throw new Error("configurePool is required for serverless configuration");
23
- }
37
+ const startedAt = Date.now();
38
+ const timeoutMs = configureTimeoutMs();
39
+ let attempts = 0;
40
+ let lastError: unknown;
24
41
 
25
- const customConfig = config.configurePool;
26
- const clientConfig = convertRegistryConfigToClientConfig(config);
27
- const dcsRes = await getDatacenters(clientConfig);
28
- const poolName = customConfig.name ?? "default";
29
- const headers = {
30
- ...(config.token ? { "x-rivet-token": config.token } : {}),
31
- ...(customConfig.headers ?? {}),
32
- };
33
- const serverlessConfig = {
34
- serverless: {
35
- url: customConfig.url,
36
- headers,
37
- request_lifespan: customConfig.requestLifespan ?? 15 * 60,
38
- metadata_poll_interval:
39
- customConfig.metadataPollInterval ?? 1000,
40
- max_runners: 100_000,
41
- min_runners: 0,
42
- runners_margin: 0,
43
- slots_per_runner: 1,
44
- },
45
- metadata: customConfig.metadata ?? {},
46
- drain_on_version_upgrade:
47
- customConfig.drainOnVersionUpgrade ?? true,
48
- };
42
+ while (Date.now() - startedAt <= timeoutMs) {
43
+ attempts += 1;
44
+ try {
45
+ if (!config.namespace) {
46
+ throw new Error(
47
+ "namespace is required for serverless configuration",
48
+ );
49
+ }
50
+ if (!config.endpoint) {
51
+ throw new Error(
52
+ "endpoint is required for serverless configuration",
53
+ );
54
+ }
55
+ if (!config.configurePool) {
56
+ throw new Error(
57
+ "configurePool is required for serverless configuration",
58
+ );
59
+ }
49
60
 
50
- await updateRunnerConfig(clientConfig, poolName, {
51
- datacenters: Object.fromEntries(
52
- dcsRes.datacenters.map((dc) => [dc.name, serverlessConfig]),
53
- ),
54
- });
61
+ const customConfig = config.configurePool;
62
+ const clientConfig = convertRegistryConfigToClientConfig(config);
63
+ const dcsRes = await getDatacenters(clientConfig);
64
+ const poolName = customConfig.name ?? "default";
65
+ const serverlessToken = config.token ?? config.publicToken;
66
+ const headers = {
67
+ ...(serverlessToken
68
+ ? { "x-rivet-token": serverlessToken }
69
+ : {}),
70
+ ...(customConfig.headers ?? {}),
71
+ };
72
+ const serverlessConfig = {
73
+ serverless: {
74
+ url: customConfig.url,
75
+ headers,
76
+ request_lifespan: customConfig.requestLifespan ?? 60 * 60,
77
+ drain_grace_period: customConfig.drainGracePeriod,
78
+ metadata_poll_interval:
79
+ customConfig.metadataPollInterval ?? 1000,
80
+ max_runners: 100_000,
81
+ min_runners: 0,
82
+ runners_margin: 0,
83
+ slots_per_runner: 1,
84
+ },
85
+ metadata: customConfig.metadata ?? {},
86
+ drain_on_version_upgrade:
87
+ customConfig.drainOnVersionUpgrade ?? true,
88
+ };
55
89
 
56
- logger().info({
57
- msg: "serverless pool configured successfully",
58
- poolName,
59
- namespace: config.namespace,
60
- });
61
- } catch (error) {
62
- logger().error({
63
- msg: "failed to configure serverless pool, validate endpoint is configured correctly then restart this process",
64
- error,
65
- });
90
+ await updateRunnerConfig(clientConfig, poolName, {
91
+ datacenters: Object.fromEntries(
92
+ dcsRes.datacenters.map((dc) => [dc.name, serverlessConfig]),
93
+ ),
94
+ });
95
+
96
+ logger().info({
97
+ msg: "serverless pool configured successfully",
98
+ poolName,
99
+ namespace: config.namespace,
100
+ attempts,
101
+ });
102
+ return;
103
+ } catch (error) {
104
+ lastError = error;
105
+ logger().warn({
106
+ msg: "serverless pool configuration attempt failed",
107
+ attempts,
108
+ error: stringifyError(error),
109
+ });
110
+ await sleep(CONFIGURE_RETRY_DELAY_MS);
111
+ }
66
112
  }
113
+
114
+ logger().error({
115
+ msg: "failed to configure serverless pool, validate endpoint is configured correctly then restart this process",
116
+ attempts,
117
+ error: stringifyError(lastError),
118
+ });
119
+ throw lastError;
67
120
  }
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,170 @@
1
+ import { DatabaseSync } from "node:sqlite";
2
+ import { afterEach, describe, expect, test } from "vitest";
3
+ import {
4
+ DEFAULT_MIGRATION_TABLE_NAME,
5
+ migrations,
6
+ type MigrationDatabase,
7
+ } from "./migrations";
8
+
9
+ const databases: DatabaseSync[] = [];
10
+
11
+ afterEach(() => {
12
+ for (const database of databases.splice(0)) database.close();
13
+ });
14
+
15
+ function createDatabase(): { raw: DatabaseSync; db: MigrationDatabase } {
16
+ const raw = new DatabaseSync(":memory:");
17
+ databases.push(raw);
18
+ return {
19
+ raw,
20
+ db: {
21
+ execute: async <TRow extends Record<string, unknown>>(
22
+ sql: string,
23
+ ...args: unknown[]
24
+ ): Promise<TRow[]> => {
25
+ if (args.length > 0 || /^\s*SELECT\b/i.test(sql)) {
26
+ return raw.prepare(sql).all(...(args as never[])) as TRow[];
27
+ }
28
+ raw.exec(sql);
29
+ return [];
30
+ },
31
+ },
32
+ };
33
+ }
34
+
35
+ describe("migrations", () => {
36
+ test("applies SQL and function migrations once in order", async () => {
37
+ const { raw, db } = createDatabase();
38
+ const applied: number[] = [];
39
+ const migrate = migrations({
40
+ tableName: "app_schema_version",
41
+ migrations: [
42
+ {
43
+ version: 1,
44
+ sql: "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
45
+ },
46
+ {
47
+ version: 2,
48
+ up: async (database) => {
49
+ applied.push(2);
50
+ await database.execute(
51
+ "ALTER TABLE items ADD COLUMN active INTEGER NOT NULL DEFAULT 1",
52
+ );
53
+ },
54
+ },
55
+ ],
56
+ });
57
+
58
+ await migrate(db);
59
+ await migrate(db);
60
+
61
+ expect(applied).toEqual([2]);
62
+ expect(
63
+ raw
64
+ .prepare(
65
+ "SELECT schema_version FROM app_schema_version WHERE singleton = 1",
66
+ )
67
+ .get(),
68
+ ).toEqual({ schema_version: 2 });
69
+ expect(
70
+ raw
71
+ .prepare("PRAGMA table_info(items)")
72
+ .all()
73
+ .map((row) => row.name),
74
+ ).toEqual(["id", "name", "active"]);
75
+ });
76
+
77
+ test("uses the default migration table", async () => {
78
+ const { raw, db } = createDatabase();
79
+ await migrations({
80
+ migrations: [{ version: 1, sql: "CREATE TABLE item (id INTEGER)" }],
81
+ })(db);
82
+
83
+ expect(
84
+ raw
85
+ .prepare(
86
+ `SELECT schema_version FROM ${DEFAULT_MIGRATION_TABLE_NAME} WHERE singleton = 1`,
87
+ )
88
+ .get(),
89
+ ).toEqual({ schema_version: 1 });
90
+ });
91
+
92
+ test("adopts an unversioned schema before applying later migrations", async () => {
93
+ const { raw, db } = createDatabase();
94
+ raw.exec(
95
+ "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
96
+ );
97
+ const applied: number[] = [];
98
+
99
+ await migrations({
100
+ tableName: "app_schema_version",
101
+ adopt: async (database) => {
102
+ const tables = await database.execute<{ name: string }>(
103
+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'items'",
104
+ );
105
+ return tables.length === 0 ? 0 : 1;
106
+ },
107
+ migrations: [
108
+ {
109
+ version: 1,
110
+ up: () => {
111
+ throw new Error("already applied migration ran");
112
+ },
113
+ },
114
+ {
115
+ version: 2,
116
+ up: async (database) => {
117
+ applied.push(2);
118
+ await database.execute(
119
+ "ALTER TABLE items ADD COLUMN active INTEGER NOT NULL DEFAULT 1",
120
+ );
121
+ },
122
+ },
123
+ ],
124
+ })(db);
125
+
126
+ expect(applied).toEqual([2]);
127
+ expect(
128
+ raw
129
+ .prepare(
130
+ "SELECT schema_version FROM app_schema_version WHERE singleton = 1",
131
+ )
132
+ .get(),
133
+ ).toEqual({ schema_version: 2 });
134
+ });
135
+
136
+ test("rejects invalid ladders and table names before executing", () => {
137
+ expect(() => migrations({ migrations: [] })).toThrow(
138
+ "must not be empty",
139
+ );
140
+ expect(() =>
141
+ migrations({
142
+ migrations: [{ version: 2, sql: "SELECT 1" }],
143
+ }),
144
+ ).toThrow("expected version 1");
145
+ expect(() =>
146
+ migrations({
147
+ tableName: "schema; DROP TABLE items",
148
+ migrations: [{ version: 1, sql: "SELECT 1" }],
149
+ }),
150
+ ).toThrow("invalid migration table name");
151
+ });
152
+
153
+ test("rejects a database newer than the migration ladder", async () => {
154
+ const { raw, db } = createDatabase();
155
+ raw.exec(`
156
+ CREATE TABLE app_schema_version (
157
+ singleton INTEGER PRIMARY KEY,
158
+ schema_version INTEGER NOT NULL
159
+ ) STRICT;
160
+ INSERT INTO app_schema_version VALUES (1, 2);
161
+ `);
162
+
163
+ await expect(
164
+ migrations({
165
+ tableName: "app_schema_version",
166
+ migrations: [{ version: 1, sql: "SELECT 1" }],
167
+ })(db),
168
+ ).rejects.toThrow("newer than supported version 1");
169
+ });
170
+ });