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
@@ -2,18 +2,25 @@ import type { Context as HonoContext } from "hono";
2
2
  import invariant from "invariant";
3
3
  import { deserializeActorKey, serializeActorKey } from "@/actor/keys";
4
4
  import type { ClientConfig } from "@/client/client";
5
+ import type { IssuedToken, IssueTokenOptions } from "@/client/auth";
6
+ import {
7
+ isInvalidToken,
8
+ isInvalidTokenResponse,
9
+ TokenProvider,
10
+ } from "@/client/token-provider";
5
11
  import {
6
12
  PATH_CONNECT,
7
13
  PATH_WEBSOCKET_BASE,
8
14
  PATH_WEBSOCKET_PREFIX,
9
15
  } from "@/common/actor-router-consts";
16
+ import type { JsonCompatValue } from "@/common/encoding";
10
17
  import { noopNext } from "@/common/utils";
11
18
  import type { Actor as ApiActor } from "@/engine-api/actors";
12
- import { shouldSkipReadyWait } from "@/engine-client/driver";
13
19
  import type {
14
20
  ActorOutput,
15
21
  CreateInput,
16
22
  EngineControlClient,
23
+ ForkInput,
17
24
  GatewayRequestOptions,
18
25
  GatewayTarget,
19
26
  GetForIdInput,
@@ -22,6 +29,7 @@ import type {
22
29
  ListActorsInput,
23
30
  RuntimeDisplayInformation,
24
31
  } from "@/engine-client/driver";
32
+ import { shouldSkipReadyWait } from "@/engine-client/driver";
25
33
  import type { Encoding, UniversalWebSocket } from "@/mod";
26
34
  import { encodeCborCompat, uint8ArrayToBase64 } from "@/serde";
27
35
  import { combineUrlPath, type GetUpgradeWebSocket } from "@/utils";
@@ -36,10 +44,11 @@ import {
36
44
  import {
37
45
  createActor,
38
46
  destroyActor,
47
+ forkActor,
39
48
  getActor,
40
49
  getActorByKey,
41
50
  getOrCreateActor,
42
- kvGet,
51
+ issueToken as issueEngineToken,
43
52
  listActorsByName,
44
53
  } from "./api-endpoints";
45
54
  import { EngineApiError, getEndpoint } from "./api-utils";
@@ -50,6 +59,13 @@ import { createWebSocketProxy } from "./ws-proxy";
50
59
  export class RemoteEngineControlClient implements EngineControlClient {
51
60
  #config: ClientConfig;
52
61
  #metadataPromise: Promise<void> | undefined;
62
+ #tokenProvider?: TokenProvider;
63
+ #webSocketTokens = new WeakMap<
64
+ UniversalWebSocket,
65
+ {
66
+ refresh?: Promise<string>;
67
+ }
68
+ >();
53
69
 
54
70
  constructor(runConfig: ClientConfig) {
55
71
  // Disable health check if in Next.js build phase since there is no `/metadata` endpoint
@@ -65,6 +81,8 @@ export class RemoteEngineControlClient implements EngineControlClient {
65
81
  // Clone config so we can mutate the endpoint in #metadataPromise
66
82
  // NOTE: This is a shallow clone, so mutating nested properties will not do anything
67
83
  this.#config = { ...runConfig };
84
+ if (runConfig.getToken)
85
+ this.#tokenProvider = new TokenProvider(runConfig.getToken);
68
86
 
69
87
  // Perform metadata check if enabled
70
88
  if (!runConfig.disableMetadataLookup) {
@@ -80,7 +98,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
80
98
  this.#config.namespace =
81
99
  metadataData.clientNamespace;
82
100
  }
83
- if (metadataData.clientToken) {
101
+ if (metadataData.clientToken && !this.#tokenProvider) {
84
102
  this.#config.token = metadataData.clientToken;
85
103
  }
86
104
 
@@ -88,7 +106,6 @@ export class RemoteEngineControlClient implements EngineControlClient {
88
106
  msg: "overriding client endpoint",
89
107
  endpoint: metadataData.clientEndpoint,
90
108
  namespace: metadataData.clientNamespace,
91
- token: metadataData.clientToken,
92
109
  });
93
110
  }
94
111
 
@@ -103,6 +120,51 @@ export class RemoteEngineControlClient implements EngineControlClient {
103
120
  }
104
121
  }
105
122
 
123
+ async #authorizedConfig(): Promise<ClientConfig> {
124
+ if (!this.#tokenProvider) return this.#config;
125
+ return { ...this.#config, token: await this.#tokenProvider.current() };
126
+ }
127
+
128
+ async #withCredential<T>(
129
+ request: (config: ClientConfig) => Promise<T>,
130
+ readOnly = false,
131
+ ): Promise<T> {
132
+ const config = await this.#authorizedConfig();
133
+ try {
134
+ return await request(config);
135
+ } catch (error) {
136
+ if (
137
+ this.#tokenProvider &&
138
+ config.token &&
139
+ error instanceof EngineApiError &&
140
+ error.statusCode === 401 &&
141
+ isInvalidToken(error.group, error.code)
142
+ ) {
143
+ if (readOnly) {
144
+ const token = await this.#tokenProvider.refreshIfCurrent(
145
+ config.token,
146
+ );
147
+ return request({ ...this.#config, token });
148
+ }
149
+ // Mutations may have executed; refresh the next call, not this one.
150
+ await this.#tokenProvider.refreshIfCurrent(config.token);
151
+ }
152
+ throw error;
153
+ }
154
+ }
155
+
156
+ async issueToken(options: IssueTokenOptions): Promise<IssuedToken> {
157
+ await this.#metadataPromise;
158
+ const response = await this.#withCredential((config) =>
159
+ issueEngineToken(config, options),
160
+ );
161
+ return {
162
+ token: response.token,
163
+ issuedAt: response.issued_ts,
164
+ expiresAt: response.expires_ts,
165
+ };
166
+ }
167
+
106
168
  async getForId({
107
169
  name,
108
170
  actorId,
@@ -110,7 +172,10 @@ export class RemoteEngineControlClient implements EngineControlClient {
110
172
  await this.#metadataPromise;
111
173
 
112
174
  // Fetch from API if not in cache
113
- const response = await getActor(this.#config, name, actorId);
175
+ const response = await this.#withCredential(
176
+ (config) => getActor(config, name, actorId),
177
+ true,
178
+ );
114
179
  const actor = response.actors[0];
115
180
  if (!actor) return undefined;
116
181
 
@@ -138,7 +203,10 @@ export class RemoteEngineControlClient implements EngineControlClient {
138
203
 
139
204
  // If not in local cache, fetch by key from API
140
205
  try {
141
- const response = await getActorByKey(this.#config, name, key);
206
+ const response = await this.#withCredential(
207
+ (config) => getActorByKey(config, name, key),
208
+ true,
209
+ );
142
210
  const actor = response.actors[0];
143
211
  if (!actor) return undefined;
144
212
 
@@ -167,7 +235,14 @@ export class RemoteEngineControlClient implements EngineControlClient {
167
235
  ): Promise<ActorOutput> {
168
236
  await this.#metadataPromise;
169
237
 
170
- const { name, key, input: actorInput, region, crashPolicy } = input;
238
+ const {
239
+ name,
240
+ key,
241
+ input: actorInput,
242
+ region,
243
+ crashPolicy,
244
+ poolName,
245
+ } = input;
171
246
 
172
247
  logger().info({
173
248
  msg: "getOrCreateWithKey: getting or creating actor via engine api",
@@ -175,26 +250,64 @@ export class RemoteEngineControlClient implements EngineControlClient {
175
250
  key,
176
251
  });
177
252
 
178
- const { actor, created } = await getOrCreateActor(this.#config, {
179
- datacenter: region,
180
- name,
181
- key: serializeActorKey(key),
182
- runner_name_selector: this.#config.poolName,
183
- input: actorInput
184
- ? uint8ArrayToBase64(encodeCborCompat(actorInput))
185
- : undefined,
186
- crash_policy: crashPolicy ?? "sleep",
187
- });
253
+ try {
254
+ const { actor, created } = await this.#withCredential((config) =>
255
+ getOrCreateActor(config, {
256
+ datacenter: region,
257
+ name,
258
+ key: serializeActorKey(key),
259
+ pool: poolName ?? this.#config.poolName,
260
+ input: actorInput
261
+ ? uint8ArrayToBase64(
262
+ encodeCborCompat(actorInput as JsonCompatValue),
263
+ )
264
+ : undefined,
265
+ crash_policy: crashPolicy ?? "sleep",
266
+ }),
267
+ );
188
268
 
189
- logger().info({
190
- msg: "getOrCreateWithKey: actor ready",
191
- actorId: actor.actor_id,
192
- name,
193
- key,
194
- created,
195
- });
269
+ logger().info({
270
+ msg: "getOrCreateWithKey: actor ready",
271
+ actorId: actor.actor_id,
272
+ name,
273
+ key,
274
+ created,
275
+ });
196
276
 
197
- return apiActorToOutput(actor);
277
+ return apiActorToOutput(actor);
278
+ } catch (error) {
279
+ // The key is reserved in a different datacenter, which means the
280
+ // actor already exists there. get-by-key forwards to the reserved
281
+ // datacenter, so retry as a get to resolve the existing actor.
282
+ if (
283
+ error instanceof EngineApiError &&
284
+ error.group === "actor" &&
285
+ error.code === "key_reserved_in_different_datacenter"
286
+ ) {
287
+ logger().warn({
288
+ msg: "getOrCreateWithKey: key reserved in different datacenter, retrying as get",
289
+ name,
290
+ key,
291
+ });
292
+
293
+ const response = await this.#withCredential(
294
+ (config) => getActorByKey(config, name, key),
295
+ true,
296
+ );
297
+ const existing = response.actors[0];
298
+ if (!existing) throw error;
299
+
300
+ logger().info({
301
+ msg: "getOrCreateWithKey: resolved existing actor via get",
302
+ actorId: existing.actor_id,
303
+ name,
304
+ key,
305
+ });
306
+
307
+ return apiActorToOutput(existing);
308
+ }
309
+ throw error;
310
+ }
198
311
  }
199
312
 
200
313
  async createActor({
@@ -203,22 +316,27 @@ export class RemoteEngineControlClient implements EngineControlClient {
203
316
  input,
204
317
  region,
205
318
  crashPolicy,
319
+ poolName,
206
320
  }: CreateInput): Promise<ActorOutput> {
207
321
  await this.#metadataPromise;
208
322
 
209
323
  logger().info({ msg: "creating actor via engine api", name, key });
210
324
 
211
325
  // Create actor via engine API
212
- const result = await createActor(this.#config, {
213
- datacenter: region,
214
- name,
215
- runner_name_selector: this.#config.poolName,
216
- key: serializeActorKey(key),
217
- input: input
218
- ? uint8ArrayToBase64(encodeCborCompat(input))
219
- : undefined,
220
- crash_policy: crashPolicy ?? "sleep",
221
- });
326
+ const result = await this.#withCredential((config) =>
327
+ createActor(config, {
328
+ datacenter: region,
329
+ name,
330
+ pool: poolName ?? this.#config.poolName,
331
+ key: serializeActorKey(key),
332
+ input: input
333
+ ? uint8ArrayToBase64(
334
+ encodeCborCompat(input as JsonCompatValue),
335
+ )
336
+ : undefined,
337
+ crash_policy: crashPolicy ?? "sleep",
338
+ }),
339
+ );
222
340
 
223
341
  logger().info({
224
342
  msg: "actor created",
@@ -230,12 +348,41 @@ export class RemoteEngineControlClient implements EngineControlClient {
230
348
  return apiActorToOutput(result.actor);
231
349
  }
232
350
 
351
+ async forkActor({ actorId, key, input }: ForkInput): Promise<ActorOutput> {
352
+ await this.#metadataPromise;
353
+
354
+ logger().info({ msg: "forking actor via engine api", actorId, key });
355
+
356
+ const result = await this.#withCredential((config) =>
357
+ forkActor(config, actorId, {
358
+ key: key ? serializeActorKey(key) : undefined,
359
+ input:
360
+ input !== undefined
361
+ ? uint8ArrayToBase64(
362
+ encodeCborCompat(input as JsonCompatValue),
363
+ )
364
+ : undefined,
365
+ }),
366
+ );
367
+
368
+ logger().info({
369
+ msg: "actor forked",
370
+ actorId: result.actor.actor_id,
371
+ sourceActorId: actorId,
372
+ });
373
+
374
+ return apiActorToOutput(result.actor);
375
+ }
376
+
233
377
  async listActors({ name }: ListActorsInput): Promise<ActorOutput[]> {
234
378
  await this.#metadataPromise;
235
379
 
236
380
  logger().debug({ msg: "listing actors via engine api", name });
237
381
 
238
- const response = await listActorsByName(this.#config, name);
382
+ const response = await this.#withCredential(
383
+ (config) => listActorsByName(config, name),
384
+ true,
385
+ );
239
386
 
240
387
  return response.actors.map(apiActorToOutput);
241
388
  }
@@ -245,7 +392,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
245
392
 
246
393
  logger().info({ msg: "destroying actor via engine api", actorId });
247
394
 
248
- await destroyActor(this.#config, actorId);
395
+ await this.#withCredential((config) => destroyActor(config, actorId));
249
396
 
250
397
  logger().info({ msg: "actor destroyed", actorId });
251
398
  }
@@ -256,13 +403,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
256
403
  options: GatewayRequestOptions = {},
257
404
  ): Promise<Response> {
258
405
  await this.#metadataPromise;
259
-
260
- const path = requestPath(actorRequest);
261
- const gatewayUrl = this.#buildGatewayUrlForTarget(
262
- target,
263
- path,
264
- options,
265
- );
406
+ const config = await this.#authorizedConfig();
266
407
  const httpOptions = {
267
408
  ...options,
268
409
  directActorId: shouldSkipReadyWait(options)
@@ -270,12 +411,37 @@ export class RemoteEngineControlClient implements EngineControlClient {
270
411
  : undefined,
271
412
  };
272
413
 
273
- return sendHttpRequestToGateway(
274
- this.#config,
275
- gatewayUrl,
276
- actorRequest,
277
- httpOptions,
278
- );
414
+ const send = (current: ClientConfig) =>
415
+ sendHttpRequestToGateway(
416
+ current,
417
+ this.#buildGatewayUrlForTarget(
418
+ current,
419
+ target,
420
+ requestPath(actorRequest),
421
+ options,
422
+ ),
423
+ actorRequest,
424
+ httpOptions,
425
+ );
426
+ const response = await send(config);
427
+ if (
428
+ !this.#tokenProvider ||
429
+ !config.token ||
430
+ !isInvalidTokenResponse(response)
431
+ )
432
+ return response;
433
+ if (
434
+ (actorRequest.method === "GET" || actorRequest.method === "HEAD") &&
435
+ !("getOrCreateForKey" in target)
436
+ ) {
437
+ const token = await this.#tokenProvider.refreshIfCurrent(
438
+ config.token,
439
+ );
440
+ await response.body?.cancel();
441
+ return send({ ...this.#config, token });
442
+ }
443
+ await this.#tokenProvider.refreshIfCurrent(config.token);
444
+ return response;
279
445
  }
280
446
 
281
447
  async openWebSocket(
@@ -286,15 +452,17 @@ export class RemoteEngineControlClient implements EngineControlClient {
286
452
  options: GatewayRequestOptions = {},
287
453
  ): Promise<UniversalWebSocket> {
288
454
  await this.#metadataPromise;
455
+ const config = await this.#authorizedConfig();
289
456
 
290
457
  const gatewayUrl = this.#buildGatewayUrlForTarget(
458
+ config,
291
459
  target,
292
460
  path,
293
461
  options,
294
462
  );
295
463
 
296
- return openWebSocketToGateway(
297
- this.#config,
464
+ const ws = await openWebSocketToGateway(
465
+ config,
298
466
  gatewayUrl,
299
467
  encoding,
300
468
  params,
@@ -305,6 +473,45 @@ export class RemoteEngineControlClient implements EngineControlClient {
305
473
  : undefined,
306
474
  },
307
475
  );
476
+ this.#watchWebSocketAuth(ws, config.token);
477
+ return ws;
478
+ }
479
+
480
+ async refreshAuthToken(ws: UniversalWebSocket): Promise<boolean> {
481
+ const context = this.#webSocketTokens.get(ws);
482
+ if (!context?.refresh) return false;
483
+ await context.refresh;
484
+ return true;
485
+ }
486
+
487
+ #watchWebSocketAuth(ws: UniversalWebSocket, token?: string): void {
488
+ const tokenProvider = this.#tokenProvider;
489
+ if (!tokenProvider || !token) return;
490
+ const context: { refresh?: Promise<string> } = {};
491
+ this.#webSocketTokens.set(ws, context);
492
+ ws.addEventListener(
493
+ "close",
494
+ (event: { code?: number; reason?: string }) => {
495
+ if (event.code !== 1008 || !event.reason) return;
496
+ const separator = event.reason.indexOf(".");
497
+ if (separator < 0) return;
498
+ const code = event.reason
499
+ .slice(separator + 1)
500
+ .split(/[\s:#]/, 1)[0];
501
+ if (isInvalidToken(event.reason.slice(0, separator), code)) {
502
+ context.refresh = tokenProvider
503
+ .refreshIfCurrent(token)
504
+ .catch((error) => {
505
+ logger().warn(
506
+ "failed to renew authentication token",
507
+ );
508
+ throw error;
509
+ });
510
+ // Raw websockets have no built-in reconnection; preempt unhandled rejection.
511
+ context.refresh.catch(() => {});
512
+ }
513
+ },
514
+ );
308
515
  }
309
516
 
310
517
  async buildGatewayUrl(
@@ -312,7 +519,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
312
519
  options: GatewayRequestOptions = {},
313
520
  ): Promise<string> {
314
521
  await this.#metadataPromise;
315
- return this.#buildGatewayUrlForTarget(target, "", options);
522
+ return this.#buildGatewayUrlForTarget(
523
+ await this.#authorizedConfig(),
524
+ target,
525
+ "",
526
+ options,
527
+ );
316
528
  }
317
529
 
318
530
  async proxyRequest(
@@ -320,14 +532,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
320
532
  actorRequest: Request,
321
533
  actorId: string,
322
534
  ): Promise<Response> {
323
- await this.#metadataPromise;
324
-
325
- const gatewayUrl = this.#buildGatewayUrlForTarget(
326
- { directId: actorId },
327
- requestPath(actorRequest),
328
- );
329
-
330
- return sendHttpRequestToGateway(this.#config, gatewayUrl, actorRequest);
535
+ return this.sendRequest({ directId: actorId }, actorRequest);
331
536
  }
332
537
 
333
538
  async proxyWebSocket(
@@ -338,11 +543,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
338
543
  params: unknown,
339
544
  ): Promise<Response> {
340
545
  await this.#metadataPromise;
546
+ const config = await this.#authorizedConfig();
341
547
 
342
- const upgradeWebSocket = this.#config.getUpgradeWebSocket?.();
548
+ const upgradeWebSocket = config.getUpgradeWebSocket?.();
343
549
  invariant(upgradeWebSocket, "missing getUpgradeWebSocket");
344
550
 
345
- const endpoint = getEndpoint(this.#config);
551
+ const endpoint = getEndpoint(config);
346
552
  const guardUrl = combineUrlPath(endpoint, path);
347
553
  const wsGuardUrl = guardUrl.replace("http://", "ws://");
348
554
 
@@ -355,7 +561,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
355
561
 
356
562
  // Build protocols
357
563
  const protocols = buildWebSocketProtocols(
358
- this.#config,
564
+ config,
359
565
  encoding,
360
566
  params,
361
567
  undefined,
@@ -369,46 +575,6 @@ export class RemoteEngineControlClient implements EngineControlClient {
369
575
  return await upgradeWebSocket(() => args)(c, noopNext());
370
576
  }
371
577
 
372
- async kvGet(actorId: string, key: Uint8Array): Promise<string | null> {
373
- await this.#metadataPromise;
374
-
375
- logger().debug({ msg: "getting kv value via engine api", key });
376
-
377
- const response = await kvGet(
378
- this.#config,
379
- actorId,
380
- new TextDecoder("utf8").decode(key),
381
- );
382
-
383
- return response.value;
384
- }
385
-
386
- async kvBatchGet(
387
- _actorId: string,
388
- _keys: Uint8Array[],
389
- ): Promise<(Uint8Array | null)[]> {
390
- throw new Error("kvBatchGet not supported on remote engine client");
391
- }
392
-
393
- async kvBatchPut(
394
- _actorId: string,
395
- _entries: [Uint8Array, Uint8Array][],
396
- ): Promise<void> {
397
- throw new Error("kvBatchPut not supported on remote engine client");
398
- }
399
-
400
- async kvBatchDelete(_actorId: string, _keys: Uint8Array[]): Promise<void> {
401
- throw new Error("kvBatchDelete not supported on remote engine client");
402
- }
403
-
404
- async kvDeleteRange(
405
- _actorId: string,
406
- _start: Uint8Array,
407
- _end: Uint8Array,
408
- ): Promise<void> {
409
- throw new Error("kvDeleteRange not supported on remote engine client");
410
- }
411
-
412
578
  displayInformation(): RuntimeDisplayInformation {
413
579
  return { properties: {} };
414
580
  }
@@ -418,11 +584,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
418
584
  }
419
585
 
420
586
  #buildGatewayUrlForTarget(
587
+ config: ClientConfig,
421
588
  target: GatewayTarget,
422
589
  path: string,
423
590
  options: GatewayRequestOptions = {},
424
591
  ): string {
425
- const endpoint = getEndpoint(this.#config);
592
+ const endpoint = getEndpoint(config);
426
593
 
427
594
  if (
428
595
  shouldSkipReadyWait(options) &&
@@ -435,8 +602,9 @@ export class RemoteEngineControlClient implements EngineControlClient {
435
602
  if ("directId" in target) {
436
603
  return buildActorGatewayUrl(
437
604
  endpoint,
605
+ config.namespace,
438
606
  target.directId,
439
- this.#config.token,
607
+ config.token,
440
608
  path,
441
609
  );
442
610
  }
@@ -444,8 +612,9 @@ export class RemoteEngineControlClient implements EngineControlClient {
444
612
  if ("getForId" in target) {
445
613
  return buildActorGatewayUrl(
446
614
  endpoint,
615
+ config.namespace,
447
616
  target.getForId.actorId,
448
- this.#config.token,
617
+ config.token,
449
618
  path,
450
619
  );
451
620
  }
@@ -453,14 +622,14 @@ export class RemoteEngineControlClient implements EngineControlClient {
453
622
  if ("getForKey" in target || "getOrCreateForKey" in target) {
454
623
  return buildActorQueryGatewayUrl(
455
624
  endpoint,
456
- this.#config.namespace,
625
+ config.namespace,
457
626
  target,
458
- this.#config.token,
627
+ config.token,
459
628
  path,
460
- this.#config.maxInputSize,
629
+ config.maxInputSize,
461
630
  undefined,
462
631
  "getOrCreateForKey" in target
463
- ? this.#config.poolName
632
+ ? (target.getOrCreateForKey.poolName ?? config.poolName)
464
633
  : undefined,
465
634
  options,
466
635
  );
@@ -9,7 +9,7 @@ import { logger } from "./log";
9
9
  * Returns Hono `upgradeWebSocket` args that will proxy requests from the client to a destination address.
10
10
  */
11
11
  export async function createWebSocketProxy(
12
- c: HonoContext,
12
+ _c: HonoContext,
13
13
  targetUrl: string,
14
14
  protocols: string[],
15
15
  ): Promise<UpgradeWebSocketArgs> {
@@ -23,7 +23,7 @@ export async function createWebSocketProxy(
23
23
  const state: WsState = {};
24
24
 
25
25
  return {
26
- onOpen: async (event: any, clientWs: WSContext) => {
26
+ onOpen: async (_event: any, clientWs: WSContext) => {
27
27
  logger().debug({ msg: "client websocket connected", targetUrl });
28
28
 
29
29
  if (clientWs.readyState !== 1) {
@@ -68,6 +68,11 @@ export async function createWebSocketProxy(
68
68
  reject(error);
69
69
  });
70
70
  });
71
+ // Attach a no-op rejection handler so Node.js does not treat this as
72
+ // an unhandled rejection if onMessage never runs (e.g. the client
73
+ // disconnects before sending a message). The rejection still propagates
74
+ // to any caller that awaits connectPromise directly.
75
+ state.connectPromise.catch(() => {});
71
76
 
72
77
  // Setup bidirectional forwarding
73
78
  state.targetWs.addEventListener("message", (event) => {
@@ -137,7 +142,7 @@ export async function createWebSocketProxy(
137
142
  }
138
143
  },
139
144
 
140
- onClose: (event: any, clientWs: WSContext) => {
145
+ onClose: (event: any, _clientWs: WSContext) => {
141
146
  logger().debug({
142
147
  msg: "client websocket closed",
143
148
  targetUrl,
@@ -159,7 +164,7 @@ export async function createWebSocketProxy(
159
164
  }
160
165
  },
161
166
 
162
- onError: (event: any, clientWs: WSContext) => {
167
+ onError: (event: any, _clientWs: WSContext) => {
163
168
  logger().error({ msg: "client websocket error", targetUrl, event });
164
169
 
165
170
  if (state.targetWs) {