rivetkit 2.0.3 → 2.0.4

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 (233) hide show
  1. package/README.md +11 -0
  2. package/dist/schemas/actor-persist/v1.ts +21 -24
  3. package/dist/schemas/client-protocol/v1.ts +6 -0
  4. package/dist/tsup/actor/errors.cjs +10 -2
  5. package/dist/tsup/actor/errors.cjs.map +1 -1
  6. package/dist/tsup/actor/errors.d.cts +17 -4
  7. package/dist/tsup/actor/errors.d.ts +17 -4
  8. package/dist/tsup/actor/errors.js +11 -3
  9. package/dist/tsup/{chunk-6PDXBYI5.js → chunk-3F2YSRJL.js} +8 -23
  10. package/dist/tsup/chunk-3F2YSRJL.js.map +1 -0
  11. package/dist/tsup/chunk-4CXBCT26.cjs +250 -0
  12. package/dist/tsup/chunk-4CXBCT26.cjs.map +1 -0
  13. package/dist/tsup/chunk-4R73YDN3.cjs +20 -0
  14. package/dist/tsup/chunk-4R73YDN3.cjs.map +1 -0
  15. package/dist/tsup/{chunk-OGAPU3UG.cjs → chunk-6LJT3QRL.cjs} +39 -25
  16. package/dist/tsup/chunk-6LJT3QRL.cjs.map +1 -0
  17. package/dist/tsup/{chunk-6WKQDDUD.cjs → chunk-GICQ3YCU.cjs} +143 -141
  18. package/dist/tsup/chunk-GICQ3YCU.cjs.map +1 -0
  19. package/dist/tsup/{chunk-FLMTTN27.js → chunk-H26RP6GD.js} +15 -8
  20. package/dist/tsup/chunk-H26RP6GD.js.map +1 -0
  21. package/dist/tsup/chunk-HI3HWJRC.js +20 -0
  22. package/dist/tsup/chunk-HI3HWJRC.js.map +1 -0
  23. package/dist/tsup/{chunk-4NSUQZ2H.js → chunk-HLLF4B4Q.js} +116 -114
  24. package/dist/tsup/chunk-HLLF4B4Q.js.map +1 -0
  25. package/dist/tsup/{chunk-FCCPJNMA.cjs → chunk-IH6CKNDW.cjs} +12 -27
  26. package/dist/tsup/chunk-IH6CKNDW.cjs.map +1 -0
  27. package/dist/tsup/chunk-LV2S3OU3.js +250 -0
  28. package/dist/tsup/chunk-LV2S3OU3.js.map +1 -0
  29. package/dist/tsup/{chunk-R2OPSKIV.cjs → chunk-LWNKVZG5.cjs} +20 -13
  30. package/dist/tsup/chunk-LWNKVZG5.cjs.map +1 -0
  31. package/dist/tsup/{chunk-INGJP237.js → chunk-NFU2BBT5.js} +102 -43
  32. package/dist/tsup/chunk-NFU2BBT5.js.map +1 -0
  33. package/dist/tsup/{chunk-3H7O2A7I.js → chunk-PQY7KKTL.js} +33 -19
  34. package/dist/tsup/chunk-PQY7KKTL.js.map +1 -0
  35. package/dist/tsup/{chunk-PO4VLDWA.js → chunk-QK72M5JB.js} +3 -5
  36. package/dist/tsup/chunk-QK72M5JB.js.map +1 -0
  37. package/dist/tsup/{chunk-TZJKSBUQ.cjs → chunk-QNNXFOQV.cjs} +3 -5
  38. package/dist/tsup/chunk-QNNXFOQV.cjs.map +1 -0
  39. package/dist/tsup/{chunk-GIR3AFFI.cjs → chunk-SBHHJ6QS.cjs} +102 -43
  40. package/dist/tsup/chunk-SBHHJ6QS.cjs.map +1 -0
  41. package/dist/tsup/chunk-TQ62L3X7.js +325 -0
  42. package/dist/tsup/chunk-TQ62L3X7.js.map +1 -0
  43. package/dist/tsup/chunk-VO7ZRVVD.cjs +6293 -0
  44. package/dist/tsup/chunk-VO7ZRVVD.cjs.map +1 -0
  45. package/dist/tsup/chunk-WHBPJNGW.cjs +325 -0
  46. package/dist/tsup/chunk-WHBPJNGW.cjs.map +1 -0
  47. package/dist/tsup/chunk-XJQHKJ4P.js +6293 -0
  48. package/dist/tsup/chunk-XJQHKJ4P.js.map +1 -0
  49. package/dist/tsup/client/mod.cjs +10 -10
  50. package/dist/tsup/client/mod.d.cts +7 -13
  51. package/dist/tsup/client/mod.d.ts +7 -13
  52. package/dist/tsup/client/mod.js +9 -9
  53. package/dist/tsup/common/log.cjs +12 -4
  54. package/dist/tsup/common/log.cjs.map +1 -1
  55. package/dist/tsup/common/log.d.cts +23 -17
  56. package/dist/tsup/common/log.d.ts +23 -17
  57. package/dist/tsup/common/log.js +15 -7
  58. package/dist/tsup/common/websocket.cjs +5 -5
  59. package/dist/tsup/common/websocket.js +4 -4
  60. package/dist/tsup/{common-CpqORuCq.d.cts → common-CXCe7s6i.d.cts} +2 -2
  61. package/dist/tsup/{common-CpqORuCq.d.ts → common-CXCe7s6i.d.ts} +2 -2
  62. package/dist/tsup/{connection-BwUMoe6n.d.ts → connection-BI-6UIBJ.d.ts} +196 -226
  63. package/dist/tsup/{connection-BR_Ve4ku.d.cts → connection-Dyd4NLGW.d.cts} +196 -226
  64. package/dist/tsup/driver-helpers/mod.cjs +6 -9
  65. package/dist/tsup/driver-helpers/mod.cjs.map +1 -1
  66. package/dist/tsup/driver-helpers/mod.d.cts +5 -6
  67. package/dist/tsup/driver-helpers/mod.d.ts +5 -6
  68. package/dist/tsup/driver-helpers/mod.js +6 -9
  69. package/dist/tsup/driver-test-suite/mod.cjs +155 -1363
  70. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -1
  71. package/dist/tsup/driver-test-suite/mod.d.cts +11 -5
  72. package/dist/tsup/driver-test-suite/mod.d.ts +11 -5
  73. package/dist/tsup/driver-test-suite/mod.js +876 -2084
  74. package/dist/tsup/driver-test-suite/mod.js.map +1 -1
  75. package/dist/tsup/inspector/mod.cjs +6 -8
  76. package/dist/tsup/inspector/mod.cjs.map +1 -1
  77. package/dist/tsup/inspector/mod.d.cts +3 -3
  78. package/dist/tsup/inspector/mod.d.ts +3 -3
  79. package/dist/tsup/inspector/mod.js +8 -10
  80. package/dist/tsup/mod.cjs +9 -15
  81. package/dist/tsup/mod.cjs.map +1 -1
  82. package/dist/tsup/mod.d.cts +47 -42
  83. package/dist/tsup/mod.d.ts +47 -42
  84. package/dist/tsup/mod.js +10 -16
  85. package/dist/tsup/{router-endpoints-DAbqVFx2.d.ts → router-endpoints-BTe_Rsdn.d.cts} +2 -3
  86. package/dist/tsup/{router-endpoints-AYkXG8Tl.d.cts → router-endpoints-CBSrKHmo.d.ts} +2 -3
  87. package/dist/tsup/test/mod.cjs +10 -14
  88. package/dist/tsup/test/mod.cjs.map +1 -1
  89. package/dist/tsup/test/mod.d.cts +4 -5
  90. package/dist/tsup/test/mod.d.ts +4 -5
  91. package/dist/tsup/test/mod.js +9 -13
  92. package/dist/tsup/{utils-CT0cv4jd.d.ts → utils-fwx3o3K9.d.cts} +1 -0
  93. package/dist/tsup/{utils-CT0cv4jd.d.cts → utils-fwx3o3K9.d.ts} +1 -0
  94. package/dist/tsup/utils.cjs +3 -3
  95. package/dist/tsup/utils.d.cts +1 -1
  96. package/dist/tsup/utils.d.ts +1 -1
  97. package/dist/tsup/utils.js +2 -2
  98. package/package.json +4 -4
  99. package/src/actor/action.ts +1 -5
  100. package/src/actor/config.ts +27 -295
  101. package/src/actor/connection.ts +9 -12
  102. package/src/actor/context.ts +1 -4
  103. package/src/actor/definition.ts +7 -11
  104. package/src/actor/errors.ts +97 -35
  105. package/src/actor/generic-conn-driver.ts +28 -16
  106. package/src/actor/instance.ts +177 -133
  107. package/src/actor/log.ts +4 -13
  108. package/src/actor/mod.ts +0 -5
  109. package/src/actor/protocol/old.ts +42 -26
  110. package/src/actor/protocol/serde.ts +1 -1
  111. package/src/actor/router-endpoints.ts +41 -38
  112. package/src/actor/router.ts +20 -18
  113. package/src/actor/unstable-react.ts +1 -1
  114. package/src/actor/utils.ts +6 -2
  115. package/src/client/actor-common.ts +1 -1
  116. package/src/client/actor-conn.ts +152 -91
  117. package/src/client/actor-handle.ts +85 -25
  118. package/src/client/actor-query.ts +65 -0
  119. package/src/client/client.ts +29 -98
  120. package/src/client/config.ts +44 -0
  121. package/src/client/errors.ts +1 -0
  122. package/src/client/log.ts +2 -4
  123. package/src/client/mod.ts +16 -12
  124. package/src/client/raw-utils.ts +82 -25
  125. package/src/client/utils.ts +5 -3
  126. package/src/common/fake-event-source.ts +10 -9
  127. package/src/common/inline-websocket-adapter2.ts +39 -30
  128. package/src/common/log.ts +176 -101
  129. package/src/common/logfmt.ts +21 -30
  130. package/src/common/router.ts +12 -19
  131. package/src/common/utils.ts +27 -13
  132. package/src/common/websocket.ts +0 -1
  133. package/src/driver-helpers/mod.ts +1 -1
  134. package/src/driver-test-suite/log.ts +1 -3
  135. package/src/driver-test-suite/mod.ts +86 -60
  136. package/src/driver-test-suite/tests/actor-handle.ts +33 -0
  137. package/src/driver-test-suite/tests/manager-driver.ts +5 -3
  138. package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -226
  139. package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -392
  140. package/src/driver-test-suite/tests/request-access.ts +112 -126
  141. package/src/driver-test-suite/utils.ts +13 -10
  142. package/src/drivers/default.ts +7 -4
  143. package/src/drivers/engine/actor-driver.ts +22 -13
  144. package/src/drivers/engine/config.ts +2 -10
  145. package/src/drivers/engine/kv.ts +1 -1
  146. package/src/drivers/engine/log.ts +1 -3
  147. package/src/drivers/engine/mod.ts +2 -3
  148. package/src/drivers/file-system/actor.ts +1 -1
  149. package/src/drivers/file-system/global-state.ts +33 -20
  150. package/src/drivers/file-system/log.ts +1 -3
  151. package/src/drivers/file-system/manager.ts +31 -8
  152. package/src/inspector/config.ts +9 -4
  153. package/src/inspector/log.ts +1 -1
  154. package/src/inspector/manager.ts +2 -2
  155. package/src/inspector/utils.ts +1 -1
  156. package/src/manager/driver.ts +10 -2
  157. package/src/manager/hono-websocket-adapter.ts +21 -12
  158. package/src/manager/log.ts +2 -4
  159. package/src/manager/mod.ts +1 -1
  160. package/src/manager/router.ts +277 -1657
  161. package/src/manager-api/routes/actors-create.ts +16 -0
  162. package/src/manager-api/routes/actors-delete.ts +4 -0
  163. package/src/manager-api/routes/actors-get-by-id.ts +7 -0
  164. package/src/manager-api/routes/actors-get-or-create-by-id.ts +29 -0
  165. package/src/manager-api/routes/actors-get.ts +7 -0
  166. package/src/manager-api/routes/common.ts +18 -0
  167. package/src/mod.ts +0 -2
  168. package/src/registry/config.ts +1 -1
  169. package/src/registry/log.ts +2 -4
  170. package/src/registry/mod.ts +57 -24
  171. package/src/registry/run-config.ts +31 -33
  172. package/src/registry/serve.ts +4 -5
  173. package/src/remote-manager-driver/actor-http-client.ts +72 -0
  174. package/src/remote-manager-driver/actor-websocket-client.ts +63 -0
  175. package/src/remote-manager-driver/api-endpoints.ts +79 -0
  176. package/src/remote-manager-driver/api-utils.ts +43 -0
  177. package/src/remote-manager-driver/log.ts +5 -0
  178. package/src/remote-manager-driver/mod.ts +274 -0
  179. package/src/{drivers/engine → remote-manager-driver}/ws-proxy.ts +24 -14
  180. package/src/serde.ts +8 -2
  181. package/src/test/log.ts +1 -3
  182. package/src/test/mod.ts +17 -16
  183. package/dist/tsup/chunk-2CRLFV6Z.cjs +0 -202
  184. package/dist/tsup/chunk-2CRLFV6Z.cjs.map +0 -1
  185. package/dist/tsup/chunk-3H7O2A7I.js.map +0 -1
  186. package/dist/tsup/chunk-42I3OZ3Q.js +0 -15
  187. package/dist/tsup/chunk-42I3OZ3Q.js.map +0 -1
  188. package/dist/tsup/chunk-4NSUQZ2H.js.map +0 -1
  189. package/dist/tsup/chunk-6PDXBYI5.js.map +0 -1
  190. package/dist/tsup/chunk-6WKQDDUD.cjs.map +0 -1
  191. package/dist/tsup/chunk-CTBOSFUH.cjs +0 -116
  192. package/dist/tsup/chunk-CTBOSFUH.cjs.map +0 -1
  193. package/dist/tsup/chunk-EGVZZFE2.js +0 -2857
  194. package/dist/tsup/chunk-EGVZZFE2.js.map +0 -1
  195. package/dist/tsup/chunk-FCCPJNMA.cjs.map +0 -1
  196. package/dist/tsup/chunk-FLMTTN27.js.map +0 -1
  197. package/dist/tsup/chunk-GIR3AFFI.cjs.map +0 -1
  198. package/dist/tsup/chunk-INGJP237.js.map +0 -1
  199. package/dist/tsup/chunk-KJCJLKRM.js +0 -116
  200. package/dist/tsup/chunk-KJCJLKRM.js.map +0 -1
  201. package/dist/tsup/chunk-KUPQZYUQ.cjs +0 -15
  202. package/dist/tsup/chunk-KUPQZYUQ.cjs.map +0 -1
  203. package/dist/tsup/chunk-O2MBYIXO.cjs +0 -2857
  204. package/dist/tsup/chunk-O2MBYIXO.cjs.map +0 -1
  205. package/dist/tsup/chunk-OGAPU3UG.cjs.map +0 -1
  206. package/dist/tsup/chunk-OV6AYD4S.js +0 -4406
  207. package/dist/tsup/chunk-OV6AYD4S.js.map +0 -1
  208. package/dist/tsup/chunk-PO4VLDWA.js.map +0 -1
  209. package/dist/tsup/chunk-R2OPSKIV.cjs.map +0 -1
  210. package/dist/tsup/chunk-TZJKSBUQ.cjs.map +0 -1
  211. package/dist/tsup/chunk-UBUC5C3G.cjs +0 -189
  212. package/dist/tsup/chunk-UBUC5C3G.cjs.map +0 -1
  213. package/dist/tsup/chunk-UIM22YJL.cjs +0 -4406
  214. package/dist/tsup/chunk-UIM22YJL.cjs.map +0 -1
  215. package/dist/tsup/chunk-URVFQMYI.cjs +0 -230
  216. package/dist/tsup/chunk-URVFQMYI.cjs.map +0 -1
  217. package/dist/tsup/chunk-UVUPOS46.js +0 -230
  218. package/dist/tsup/chunk-UVUPOS46.js.map +0 -1
  219. package/dist/tsup/chunk-VRRHBNJC.js +0 -189
  220. package/dist/tsup/chunk-VRRHBNJC.js.map +0 -1
  221. package/dist/tsup/chunk-XFSS33EQ.js +0 -202
  222. package/dist/tsup/chunk-XFSS33EQ.js.map +0 -1
  223. package/src/client/http-client-driver.ts +0 -326
  224. package/src/driver-test-suite/test-inline-client-driver.ts +0 -402
  225. package/src/driver-test-suite/tests/actor-auth.ts +0 -591
  226. package/src/drivers/engine/api-endpoints.ts +0 -128
  227. package/src/drivers/engine/api-utils.ts +0 -70
  228. package/src/drivers/engine/manager-driver.ts +0 -391
  229. package/src/inline-client-driver/log.ts +0 -7
  230. package/src/inline-client-driver/mod.ts +0 -385
  231. package/src/manager/auth.ts +0 -121
  232. /package/src/{drivers/engine → actor}/keys.test.ts +0 -0
  233. /package/src/{drivers/engine → actor}/keys.ts +0 -0
@@ -1,17 +1,26 @@
1
1
  import * as cbor from "cbor-x";
2
2
  import invariant from "invariant";
3
3
  import pRetry from "p-retry";
4
- import type { CloseEvent, WebSocket } from "ws";
4
+ import type { CloseEvent } from "ws";
5
5
  import type { AnyActorDefinition } from "@/actor/definition";
6
6
  import { inputDataToBuffer } from "@/actor/protocol/old";
7
7
  import { type Encoding, jsonStringifyCompat } from "@/actor/protocol/serde";
8
+ import { importEventSource } from "@/common/eventsource";
8
9
  import type {
9
10
  UniversalErrorEvent,
10
11
  UniversalEventSource,
11
12
  UniversalMessageEvent,
12
13
  } from "@/common/eventsource-interface";
13
14
  import { assertUnreachable, stringifyError } from "@/common/utils";
15
+ import {
16
+ HEADER_CONN_ID,
17
+ HEADER_CONN_PARAMS,
18
+ HEADER_CONN_TOKEN,
19
+ HEADER_ENCODING,
20
+ type ManagerDriver,
21
+ } from "@/driver-helpers/mod";
14
22
  import type { ActorQuery } from "@/manager/protocol/query";
23
+ import { PATH_CONNECT_WEBSOCKET, type UniversalWebSocket } from "@/mod";
15
24
  import type * as protocol from "@/schemas/client-protocol/mod";
16
25
  import {
17
26
  TO_CLIENT_VERSIONED,
@@ -22,17 +31,17 @@ import {
22
31
  encodingIsBinary,
23
32
  serializeWithEncoding,
24
33
  } from "@/serde";
25
- import { bufferToArrayBuffer, getEnvUniversal } from "@/utils";
34
+ import { bufferToArrayBuffer, getEnvUniversal, httpUserAgent } from "@/utils";
26
35
  import type { ActorDefinitionActions } from "./actor-common";
27
- import {
28
- ACTOR_CONNS_SYMBOL,
29
- type ClientDriver,
30
- type ClientRaw,
31
- TRANSPORT_SYMBOL,
32
- } from "./client";
36
+ import { queryActor } from "./actor-query";
37
+ import { ACTOR_CONNS_SYMBOL, type ClientRaw, TRANSPORT_SYMBOL } from "./client";
33
38
  import * as errors from "./errors";
34
39
  import { logger } from "./log";
35
- import { type WebSocketMessage as ConnMessage, messageLength } from "./utils";
40
+ import {
41
+ type WebSocketMessage as ConnMessage,
42
+ messageLength,
43
+ sendHttpRequest,
44
+ } from "./utils";
36
45
 
37
46
  interface ActionInFlight {
38
47
  name: string;
@@ -65,7 +74,7 @@ export interface SendHttpMessageOpts {
65
74
  }
66
75
 
67
76
  export type ConnTransport =
68
- | { websocket: WebSocket }
77
+ | { websocket: UniversalWebSocket }
69
78
  | { sse: UniversalEventSource };
70
79
 
71
80
  export const CONNECT_SYMBOL = Symbol("connect");
@@ -112,9 +121,9 @@ export class ActorConnRaw {
112
121
  #onOpenPromise?: PromiseWithResolvers<undefined>;
113
122
 
114
123
  #client: ClientRaw;
115
- #driver: ClientDriver;
124
+ #driver: ManagerDriver;
116
125
  #params: unknown;
117
- #encodingKind: Encoding;
126
+ #encoding: Encoding;
118
127
  #actorQuery: ActorQuery;
119
128
 
120
129
  // TODO: ws message queue
@@ -128,15 +137,15 @@ export class ActorConnRaw {
128
137
  */
129
138
  public constructor(
130
139
  client: ClientRaw,
131
- driver: ClientDriver,
140
+ driver: ManagerDriver,
132
141
  params: unknown,
133
- encodingKind: Encoding,
142
+ encoding: Encoding,
134
143
  actorQuery: ActorQuery,
135
144
  ) {
136
145
  this.#client = client;
137
146
  this.#driver = driver;
138
147
  this.#params = params;
139
- this.#encodingKind = encodingKind;
148
+ this.#encoding = encoding;
140
149
  this.#actorQuery = actorQuery;
141
150
 
142
151
  this.#keepNodeAliveInterval = setInterval(() => 60_000);
@@ -160,7 +169,7 @@ export class ActorConnRaw {
160
169
  args: Args;
161
170
  signal?: AbortSignal;
162
171
  }): Promise<Response> {
163
- logger().debug("action", { name: opts.name, args: opts.args });
172
+ logger().debug({ msg: "action", name: opts.name, args: opts.args });
164
173
 
165
174
  // If we have an active connection, use the websockactionId
166
175
  const actionId = this.#actionIdCounter;
@@ -214,7 +223,8 @@ enc
214
223
  maxTimeout: 30_000,
215
224
 
216
225
  onFailedAttempt: (error) => {
217
- logger().warn("failed to reconnect", {
226
+ logger().warn({
227
+ msg: "failed to reconnect",
218
228
  attempt: error.attemptNumber,
219
229
  error: stringifyError(error),
220
230
  });
@@ -226,7 +236,7 @@ enc
226
236
  } catch (err) {
227
237
  if ((err as Error).name === "AbortError") {
228
238
  // Ignore abortions
229
- logger().info("connection retry aborted");
239
+ logger().info({ msg: "connection retry aborted" });
230
240
  return;
231
241
  } else {
232
242
  // Unknown error
@@ -260,17 +270,21 @@ enc
260
270
  }
261
271
  }
262
272
 
263
- async #connectWebSocket({ signal }: { signal?: AbortSignal } = {}) {
264
- const ws = await this.#driver.connectWebSocket(
273
+ async #connectWebSocket() {
274
+ const { actorId } = await queryActor(
265
275
  undefined,
266
276
  this.#actorQuery,
267
- this.#encodingKind,
277
+ this.#driver,
278
+ );
279
+ const ws = await this.#driver.openWebSocket(
280
+ PATH_CONNECT_WEBSOCKET,
281
+ actorId,
282
+ this.#encoding,
268
283
  this.#params,
269
- signal ? { signal } : undefined,
270
284
  );
271
285
  this.#transport = { websocket: ws };
272
286
  ws.addEventListener("open", () => {
273
- logger().debug("websocket open");
287
+ logger().debug({ msg: "websocket open" });
274
288
  });
275
289
  ws.addEventListener("message", async (ev) => {
276
290
  this.#handleOnMessage(ev.data);
@@ -283,36 +297,58 @@ enc
283
297
  });
284
298
  }
285
299
 
286
- async #connectSse({ signal }: { signal?: AbortSignal } = {}) {
287
- const eventSource = await this.#driver.connectSse(
300
+ async #connectSse() {
301
+ const EventSource = await importEventSource();
302
+
303
+ // Get the actor ID
304
+ const { actorId } = await queryActor(
288
305
  undefined,
289
306
  this.#actorQuery,
290
- this.#encodingKind,
291
- this.#params,
292
- signal ? { signal } : undefined,
307
+ this.#driver,
293
308
  );
309
+ logger().debug({ msg: "found actor for sse connection", actorId });
310
+ invariant(actorId, "Missing actor ID");
311
+
312
+ logger().debug({
313
+ msg: "opening sse connection",
314
+ actorId,
315
+ encoding: this.#encoding,
316
+ });
317
+
318
+ const eventSource = new EventSource("http://actor/connect/sse", {
319
+ fetch: (input, init) => {
320
+ return this.#driver.sendRequest(
321
+ actorId,
322
+ new Request(input, {
323
+ ...init,
324
+ headers: {
325
+ ...init?.headers,
326
+ "User-Agent": httpUserAgent(),
327
+ [HEADER_ENCODING]: this.#encoding,
328
+ ...(this.#params !== undefined
329
+ ? { [HEADER_CONN_PARAMS]: JSON.stringify(this.#params) }
330
+ : {}),
331
+ },
332
+ }),
333
+ );
334
+ },
335
+ }) as UniversalEventSource;
336
+
294
337
  this.#transport = { sse: eventSource };
295
- eventSource.onopen = () => {
296
- logger().debug("eventsource open");
297
- // #handleOnOpen is called on "i" event
298
- };
299
- eventSource.onmessage = (ev: UniversalMessageEvent) => {
338
+
339
+ eventSource.addEventListener("message", (ev: UniversalMessageEvent) => {
300
340
  this.#handleOnMessage(ev.data);
301
- };
302
- eventSource.onerror = (_ev: UniversalErrorEvent) => {
303
- if (eventSource.readyState === eventSource.CLOSED) {
304
- // This error indicates a close event
305
- this.#handleOnClose(new Event("error"));
306
- } else {
307
- // Log error since event source is still open
308
- this.#handleOnError();
309
- }
310
- };
341
+ });
342
+
343
+ eventSource.addEventListener("error", (ev: UniversalErrorEvent) => {
344
+ this.#handleOnError();
345
+ });
311
346
  }
312
347
 
313
348
  /** Called by the onopen event from drivers. */
314
349
  #handleOnOpen() {
315
- logger().debug("socket open", {
350
+ logger().debug({
351
+ msg: "socket open",
316
352
  messageQueueLength: this.#messageQueue.length,
317
353
  });
318
354
 
@@ -320,7 +356,7 @@ enc
320
356
  if (this.#onOpenPromise) {
321
357
  this.#onOpenPromise.resolve(undefined);
322
358
  } else {
323
- logger().warn("#onOpenPromise is undefined");
359
+ logger().warn({ msg: "#onOpenPromise is undefined" });
324
360
  }
325
361
 
326
362
  // Resubscribe to all active events
@@ -340,7 +376,8 @@ enc
340
376
 
341
377
  /** Called by the onmessage event from drivers. */
342
378
  async #handleOnMessage(data: any) {
343
- logger().trace("received message", {
379
+ logger().trace({
380
+ msg: "received message",
344
381
  dataType: typeof data,
345
382
  isBlob: data instanceof Blob,
346
383
  isArrayBuffer: data instanceof ArrayBuffer,
@@ -348,12 +385,12 @@ enc
348
385
 
349
386
  const response = await this.#parseMessage(data as ConnMessage);
350
387
  logger().trace(
351
- "parsed message",
352
388
  getEnvUniversal("_RIVETKIT_LOG_MESSAGE")
353
389
  ? {
390
+ msg: "parsed message",
354
391
  message: jsonStringifyCompat(response).substring(0, 100) + "...",
355
392
  }
356
- : {},
393
+ : { msg: "parsed message" },
357
394
  );
358
395
 
359
396
  if (response.body.tag === "Init") {
@@ -361,36 +398,46 @@ enc
361
398
  this.#actorId = response.body.val.actorId;
362
399
  this.#connectionId = response.body.val.connectionId;
363
400
  this.#connectionToken = response.body.val.connectionToken;
364
- logger().trace("received init message", {
401
+ logger().trace({
402
+ msg: "received init message",
365
403
  actorId: this.#actorId,
366
404
  connectionId: this.#connectionId,
367
405
  });
368
406
  this.#handleOnOpen();
369
407
  } else if (response.body.tag === "Error") {
370
408
  // Connection error
371
- const { code, message, metadata, actionId } = response.body.val;
409
+ const { group, code, message, metadata, actionId } = response.body.val;
372
410
 
373
411
  if (actionId) {
374
412
  const inFlight = this.#takeActionInFlight(Number(actionId));
375
413
 
376
- logger().warn("action error", {
414
+ logger().warn({
415
+ msg: "action error",
377
416
  actionId: actionId,
378
417
  actionName: inFlight?.name,
418
+ group,
379
419
  code,
380
420
  message,
381
421
  metadata,
382
422
  });
383
423
 
384
- inFlight.reject(new errors.ActorError(code, message, metadata));
424
+ inFlight.reject(new errors.ActorError(group, code, message, metadata));
385
425
  } else {
386
- logger().warn("connection error", {
426
+ logger().warn({
427
+ msg: "connection error",
428
+ group,
387
429
  code,
388
430
  message,
389
431
  metadata,
390
432
  });
391
433
 
392
434
  // Create a connection error
393
- const actorError = new errors.ActorError(code, message, metadata);
435
+ const actorError = new errors.ActorError(
436
+ group,
437
+ code,
438
+ message,
439
+ metadata,
440
+ );
394
441
 
395
442
  // If we have an onOpenPromise, reject it with the error
396
443
  if (this.#onOpenPromise) {
@@ -409,18 +456,20 @@ enc
409
456
  } else if (response.body.tag === "ActionResponse") {
410
457
  // Action response OK
411
458
  const { id: actionId } = response.body.val;
412
- logger().trace("received action response", {
459
+ logger().trace({
460
+ msg: "received action response",
413
461
  actionId,
414
462
  });
415
463
 
416
464
  const inFlight = this.#takeActionInFlight(Number(actionId));
417
- logger().trace("resolving action promise", {
465
+ logger().trace({
466
+ msg: "resolving action promise",
418
467
  actionId,
419
468
  actionName: inFlight?.name,
420
469
  });
421
470
  inFlight.resolve(response.body.val);
422
471
  } else if (response.body.tag === "Event") {
423
- logger().trace("received event", { name: response.body.val.name });
472
+ logger().trace({ msg: "received event", name: response.body.val.name });
424
473
  this.#dispatchEvent(response.body.val);
425
474
  } else {
426
475
  assertUnreachable(response.body);
@@ -442,13 +491,15 @@ enc
442
491
  // These properties will be undefined
443
492
  const closeEvent = event as CloseEvent;
444
493
  if (closeEvent.wasClean) {
445
- logger().info("socket closed", {
494
+ logger().info({
495
+ msg: "socket closed",
446
496
  code: closeEvent.code,
447
497
  reason: closeEvent.reason,
448
498
  wasClean: closeEvent.wasClean,
449
499
  });
450
500
  } else {
451
- logger().warn("socket closed", {
501
+ logger().warn({
502
+ msg: "socket closed",
452
503
  code: closeEvent.code,
453
504
  reason: closeEvent.reason,
454
505
  wasClean: closeEvent.wasClean,
@@ -514,7 +565,8 @@ enc
514
565
  try {
515
566
  handler(error);
516
567
  } catch (err) {
517
- logger().error("Error in connection error handler", {
568
+ logger().error({
569
+ msg: "error in connection error handler",
518
570
  error: stringifyError(err),
519
571
  });
520
572
  }
@@ -559,7 +611,7 @@ enc
559
611
  * @param {string} eventName - The name of the event to subscribe to.
560
612
  * @param {(...args: Args) => void} callback - The callback function to execute when the event is triggered.
561
613
  * @returns {EventUnsubscribe} - A function to unsubscribe from the event.
562
- * @see {@link https://rivet.gg/docs/events|Events Documentation}
614
+ * @see {@link https://rivet.dev/docs/events|Events Documentation}
563
615
  */
564
616
  on<Args extends Array<unknown> = unknown[]>(
565
617
  eventName: string,
@@ -575,7 +627,7 @@ enc
575
627
  * @param {string} eventName - The name of the event to subscribe to.
576
628
  * @param {(...args: Args) => void} callback - The callback function to execute when the event is triggered.
577
629
  * @returns {EventUnsubscribe} - A function to unsubscribe from the event.
578
- * @see {@link https://rivet.gg/docs/events|Events Documentation}
630
+ * @see {@link https://rivet.dev/docs/events|Events Documentation}
579
631
  */
580
632
  once<Args extends Array<unknown> = unknown[]>(
581
633
  eventName: string,
@@ -612,16 +664,18 @@ enc
612
664
  if (this.#transport.websocket.readyState === 1) {
613
665
  try {
614
666
  const messageSerialized = serializeWithEncoding(
615
- this.#encodingKind,
667
+ this.#encoding,
616
668
  message,
617
669
  TO_SERVER_VERSIONED,
618
670
  );
619
671
  this.#transport.websocket.send(messageSerialized);
620
- logger().trace("sent websocket message", {
672
+ logger().trace({
673
+ msg: "sent websocket message",
621
674
  len: messageLength(messageSerialized),
622
675
  });
623
676
  } catch (error) {
624
- logger().warn("failed to send message, added to queue", {
677
+ logger().warn({
678
+ msg: "failed to send message, added to queue",
625
679
  error,
626
680
  });
627
681
 
@@ -644,7 +698,7 @@ enc
644
698
 
645
699
  if (!opts?.ephemeral && queueMessage) {
646
700
  this.#messageQueue.push(message);
647
- logger().debug("queued connection message");
701
+ logger().debug({ msg: "queued connection message" });
648
702
  }
649
703
  }
650
704
 
@@ -657,29 +711,40 @@ enc
657
711
  throw new errors.InternalError("Missing connection ID or token.");
658
712
 
659
713
  logger().trace(
660
- "sent http message",
661
714
  getEnvUniversal("_RIVETKIT_LOG_MESSAGE")
662
715
  ? {
663
- message: jsonStringifyCompat(message).substring(0, 100) + "...",
716
+ msg: "sent http message",
717
+ message: `${jsonStringifyCompat(message).substring(0, 100)}...`,
664
718
  }
665
- : {},
719
+ : { msg: "sent http message" },
666
720
  );
667
721
 
668
- await this.#driver.sendHttpMessage(
669
- undefined,
670
- this.#actorId,
671
- this.#encodingKind,
672
- this.#connectionId,
673
- this.#connectionToken,
674
- message,
675
- opts?.signal ? { signal: opts.signal } : undefined,
676
- );
722
+ logger().debug({
723
+ msg: "sending http message",
724
+ actorId: this.#actorId,
725
+ connectionId: this.#connectionId,
726
+ });
727
+
728
+ // Send an HTTP request to the connections endpoint
729
+ await sendHttpRequest({
730
+ url: "http://actor/connections/message",
731
+ method: "POST",
732
+ headers: {
733
+ [HEADER_ENCODING]: this.#encoding,
734
+ [HEADER_CONN_ID]: this.#connectionId,
735
+ [HEADER_CONN_TOKEN]: this.#connectionToken,
736
+ },
737
+ body: message,
738
+ encoding: this.#encoding,
739
+ skipParseResponse: true,
740
+ customFetch: this.#driver.sendRequest.bind(this.#driver, this.#actorId),
741
+ requestVersionedDataHandler: TO_SERVER_VERSIONED,
742
+ responseVersionedDataHandler: TO_CLIENT_VERSIONED,
743
+ });
677
744
  } catch (error) {
678
745
  // TODO: This will not automatically trigger a re-broadcast of HTTP events since SSE is separate from the HTTP action
679
746
 
680
- logger().warn("failed to send message, added to queue", {
681
- error,
682
- });
747
+ logger().warn({ msg: "failed to send message, added to queue", error });
683
748
 
684
749
  // Assuming the socket is disconnected and will be reconnected soon
685
750
  //
@@ -694,7 +759,7 @@ enc
694
759
  invariant(this.#transport, "transport must be defined");
695
760
 
696
761
  // Decode base64 since SSE sends raw strings
697
- if (encodingIsBinary(this.#encodingKind) && "sse" in this.#transport) {
762
+ if (encodingIsBinary(this.#encoding) && "sse" in this.#transport) {
698
763
  if (typeof data === "string") {
699
764
  const binaryString = atob(data);
700
765
  data = new Uint8Array(
@@ -709,11 +774,7 @@ enc
709
774
 
710
775
  const buffer = await inputDataToBuffer(data);
711
776
 
712
- return deserializeWithEncoding(
713
- this.#encodingKind,
714
- buffer,
715
- TO_CLIENT_VERSIONED,
716
- );
777
+ return deserializeWithEncoding(this.#encoding, buffer, TO_CLIENT_VERSIONED);
717
778
  }
718
779
 
719
780
  /**
@@ -725,12 +786,12 @@ enc
725
786
  // Internally, this "disposes" the connection
726
787
 
727
788
  if (this.#disposed) {
728
- logger().warn("connection already disconnected");
789
+ logger().warn({ msg: "connection already disconnected" });
729
790
  return;
730
791
  }
731
792
  this.#disposed = true;
732
793
 
733
- logger().debug("disposing actor conn");
794
+ logger().debug({ msg: "disposing actor conn" });
734
795
 
735
796
  // Clear interval so NodeJS process can exit
736
797
  clearInterval(this.#keepNodeAliveInterval);
@@ -751,11 +812,11 @@ enc
751
812
  ws.readyState === 2 /* CLOSING */ ||
752
813
  ws.readyState === 3 /* CLOSED */
753
814
  ) {
754
- logger().debug("ws already closed or closing");
815
+ logger().debug({ msg: "ws already closed or closing" });
755
816
  } else {
756
817
  const { promise, resolve } = Promise.withResolvers();
757
818
  ws.addEventListener("close", () => {
758
- logger().debug("ws closed");
819
+ logger().debug({ msg: "ws closed" });
759
820
  resolve(undefined);
760
821
  });
761
822
  ws.close();
@@ -1,18 +1,30 @@
1
+ import * as cbor from "cbor-x";
1
2
  import invariant from "invariant";
2
3
  import type { AnyActorDefinition } from "@/actor/definition";
3
4
  import type { Encoding } from "@/actor/protocol/serde";
4
5
  import { assertUnreachable } from "@/actor/utils";
6
+ import { deconstructError } from "@/common/utils";
5
7
  import { importWebSocket } from "@/common/websocket";
8
+ import {
9
+ HEADER_CONN_PARAMS,
10
+ HEADER_ENCODING,
11
+ type ManagerDriver,
12
+ } from "@/driver-helpers/mod";
6
13
  import type { ActorQuery } from "@/manager/protocol/query";
14
+ import type * as protocol from "@/schemas/client-protocol/mod";
15
+ import {
16
+ HTTP_ACTION_REQUEST_VERSIONED,
17
+ HTTP_ACTION_RESPONSE_VERSIONED,
18
+ } from "@/schemas/client-protocol/versioned";
19
+ import { bufferToArrayBuffer } from "@/utils";
7
20
  import type { ActorDefinitionActions } from "./actor-common";
8
21
  import { type ActorConn, ActorConnRaw } from "./actor-conn";
9
- import {
10
- type ClientDriver,
11
- type ClientRaw,
12
- CREATE_ACTOR_CONN_PROXY,
13
- } from "./client";
22
+ import { queryActor } from "./actor-query";
23
+ import { type ClientRaw, CREATE_ACTOR_CONN_PROXY } from "./client";
24
+ import { ActorError } from "./errors";
14
25
  import { logger } from "./log";
15
26
  import { rawHttpFetch, rawWebSocket } from "./raw-utils";
27
+ import { sendHttpRequest } from "./utils";
16
28
 
17
29
  /**
18
30
  * Provides underlying functions for stateless {@link ActorHandle} for action calls.
@@ -22,8 +34,8 @@ import { rawHttpFetch, rawWebSocket } from "./raw-utils";
22
34
  */
23
35
  export class ActorHandleRaw {
24
36
  #client: ClientRaw;
25
- #driver: ClientDriver;
26
- #encodingKind: Encoding;
37
+ #driver: ManagerDriver;
38
+ #encoding: Encoding;
27
39
  #actorQuery: ActorQuery;
28
40
  #params: unknown;
29
41
 
@@ -36,14 +48,14 @@ export class ActorHandleRaw {
36
48
  */
37
49
  public constructor(
38
50
  client: any,
39
- driver: ClientDriver,
51
+ driver: ManagerDriver,
40
52
  params: unknown,
41
- encodingKind: Encoding,
53
+ encoding: Encoding,
42
54
  actorQuery: ActorQuery,
43
55
  ) {
44
56
  this.#client = client;
45
57
  this.#driver = driver;
46
- this.#encodingKind = encodingKind;
58
+ this.#encoding = encoding;
47
59
  this.#actorQuery = actorQuery;
48
60
  this.#params = params;
49
61
  }
@@ -63,15 +75,64 @@ export class ActorHandleRaw {
63
75
  args: Args;
64
76
  signal?: AbortSignal;
65
77
  }): Promise<Response> {
66
- return await this.#driver.action<Args, Response>(
67
- undefined,
68
- this.#actorQuery,
69
- this.#encodingKind,
70
- this.#params,
71
- opts.name,
72
- opts.args,
73
- { signal: opts.signal },
74
- );
78
+ // return await this.#driver.action<Args, Response>(
79
+ // undefined,
80
+ // this.#actorQuery,
81
+ // this.#encodingKind,
82
+ // this.#params,
83
+ // opts.name,
84
+ // opts.args,
85
+ // { signal: opts.signal },
86
+ // );
87
+ try {
88
+ // Get the actor ID
89
+ const { actorId } = await queryActor(
90
+ undefined,
91
+ this.#actorQuery,
92
+ this.#driver,
93
+ );
94
+ logger().debug({ msg: "found actor for action", actorId });
95
+ invariant(actorId, "Missing actor ID");
96
+
97
+ // Invoke the action
98
+ logger().debug({
99
+ msg: "handling action",
100
+ name: opts.name,
101
+ encoding: this.#encoding,
102
+ });
103
+ const responseData = await sendHttpRequest<
104
+ protocol.HttpActionRequest,
105
+ protocol.HttpActionResponse
106
+ >({
107
+ url: `http://actor/action/${encodeURIComponent(opts.name)}`,
108
+ method: "POST",
109
+ headers: {
110
+ [HEADER_ENCODING]: this.#encoding,
111
+ ...(this.#params !== undefined
112
+ ? { [HEADER_CONN_PARAMS]: JSON.stringify(this.#params) }
113
+ : {}),
114
+ },
115
+ body: {
116
+ args: bufferToArrayBuffer(cbor.encode(opts.args)),
117
+ } satisfies protocol.HttpActionRequest,
118
+ encoding: this.#encoding,
119
+ customFetch: this.#driver.sendRequest.bind(this.#driver, actorId),
120
+ signal: opts?.signal,
121
+ requestVersionedDataHandler: HTTP_ACTION_REQUEST_VERSIONED,
122
+ responseVersionedDataHandler: HTTP_ACTION_RESPONSE_VERSIONED,
123
+ });
124
+
125
+ return cbor.decode(new Uint8Array(responseData.output));
126
+ } catch (err) {
127
+ // Standardize to ClientActorError instead of the native backend error
128
+ const { group, code, message, metadata } = deconstructError(
129
+ err,
130
+ logger(),
131
+ {},
132
+ true,
133
+ );
134
+ throw new ActorError(group, code, message, metadata);
135
+ }
75
136
  }
76
137
 
77
138
  /**
@@ -81,7 +142,8 @@ export class ActorHandleRaw {
81
142
  * @returns {ActorConn<AD>} A connection to the actor.
82
143
  */
83
144
  connect(): ActorConn<AnyActorDefinition> {
84
- logger().debug("establishing connection from handle", {
145
+ logger().debug({
146
+ msg: "establishing connection from handle",
85
147
  query: this.#actorQuery,
86
148
  });
87
149
 
@@ -89,7 +151,7 @@ export class ActorHandleRaw {
89
151
  this.#client,
90
152
  this.#driver,
91
153
  this.#params,
92
- this.#encodingKind,
154
+ this.#encoding,
93
155
  this.#actorQuery,
94
156
  );
95
157
 
@@ -158,12 +220,10 @@ export class ActorHandleRaw {
158
220
  assertUnreachable(this.#actorQuery);
159
221
  }
160
222
 
161
- const actorId = await this.#driver.resolveActorId(
223
+ const { actorId } = await queryActor(
162
224
  undefined,
163
225
  this.#actorQuery,
164
- this.#encodingKind,
165
- this.#params,
166
- signal ? { signal } : undefined,
226
+ this.#driver,
167
227
  );
168
228
 
169
229
  this.#actorQuery = { getForId: { actorId, name } };