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,385 +0,0 @@
1
- import * as cbor from "cbor-x";
2
- import type { Context as HonoContext } from "hono";
3
- import invariant from "invariant";
4
- import onChange from "on-change";
5
- import type { WebSocket } from "ws";
6
- import * as errors from "@/actor/errors";
7
- import type { Encoding } from "@/actor/protocol/serde";
8
- import {
9
- PATH_CONNECT_WEBSOCKET,
10
- PATH_RAW_WEBSOCKET_PREFIX,
11
- } from "@/actor/router";
12
- import {
13
- HEADER_CONN_ID,
14
- HEADER_CONN_PARAMS,
15
- HEADER_CONN_TOKEN,
16
- HEADER_ENCODING,
17
- HEADER_EXPOSE_INTERNAL_ERROR,
18
- } from "@/actor/router-endpoints";
19
- import { assertUnreachable } from "@/actor/utils";
20
- import type { ClientDriver } from "@/client/client";
21
- import { ActorError as ClientActorError } from "@/client/errors";
22
- import { sendHttpRequest } from "@/client/utils";
23
- import { importEventSource } from "@/common/eventsource";
24
- import type { UniversalEventSource } from "@/common/eventsource-interface";
25
- import { deconstructError } from "@/common/utils";
26
- import type { ManagerDriver } from "@/manager/driver";
27
- import type { ActorQuery } from "@/manager/protocol/query";
28
- import type { RunConfig } from "@/mod";
29
- import type * as protocol from "@/schemas/client-protocol/mod";
30
- import {
31
- HTTP_ACTION_REQUEST_VERSIONED,
32
- HTTP_ACTION_RESPONSE_VERSIONED,
33
- TO_CLIENT_VERSIONED,
34
- TO_SERVER_VERSIONED,
35
- } from "@/schemas/client-protocol/versioned";
36
- import { bufferToArrayBuffer, httpUserAgent } from "@/utils";
37
- import { logger } from "./log";
38
-
39
- /**
40
- * Client driver that calls the manager driver inline.
41
- *
42
- * This is only applicable to standalone & coordinated topologies.
43
- *
44
- * This driver can access private resources.
45
- *
46
- * This driver serves a double purpose as:
47
- * - Providing the client for the internal requests
48
- * - Provide the driver for the manager HTTP router (see manager/router.ts)
49
- */
50
- export function createInlineClientDriver(
51
- managerDriver: ManagerDriver,
52
- ): ClientDriver {
53
- const driver: ClientDriver = {
54
- action: async <Args extends Array<unknown> = unknown[], Response = unknown>(
55
- c: HonoContext | undefined,
56
- actorQuery: ActorQuery,
57
- encoding: Encoding,
58
- params: unknown,
59
- actionName: string,
60
- args: Args,
61
- opts: { signal?: AbortSignal },
62
- ): Promise<Response> => {
63
- try {
64
- // Get the actor ID
65
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
66
- logger().debug("found actor for action", { actorId });
67
- invariant(actorId, "Missing actor ID");
68
-
69
- // Invoke the action
70
- logger().debug("handling action", { actionName, encoding });
71
- const responseData = await sendHttpRequest<
72
- protocol.HttpActionRequest,
73
- protocol.HttpActionResponse
74
- >({
75
- url: `http://actor/action/${encodeURIComponent(actionName)}`,
76
- method: "POST",
77
- headers: {
78
- [HEADER_ENCODING]: encoding,
79
- ...(params !== undefined
80
- ? { [HEADER_CONN_PARAMS]: JSON.stringify(params) }
81
- : {}),
82
- [HEADER_EXPOSE_INTERNAL_ERROR]: "true",
83
- },
84
- body: {
85
- args: bufferToArrayBuffer(cbor.encode(args)),
86
- } satisfies protocol.HttpActionRequest,
87
- encoding: encoding,
88
- customFetch: managerDriver.sendRequest.bind(managerDriver, actorId),
89
- signal: opts?.signal,
90
- requestVersionedDataHandler: HTTP_ACTION_REQUEST_VERSIONED,
91
- responseVersionedDataHandler: HTTP_ACTION_RESPONSE_VERSIONED,
92
- });
93
-
94
- return cbor.decode(new Uint8Array(responseData.output));
95
- } catch (err) {
96
- // Standardize to ClientActorError instead of the native backend error
97
- const { code, message, metadata } = deconstructError(
98
- err,
99
- logger(),
100
- {},
101
- true,
102
- );
103
- const x = new ClientActorError(code, message, metadata);
104
- throw new ClientActorError(code, message, metadata);
105
- }
106
- },
107
-
108
- resolveActorId: async (
109
- c: HonoContext | undefined,
110
- actorQuery: ActorQuery,
111
- _encodingKind: Encoding,
112
- ): Promise<string> => {
113
- // Get the actor ID
114
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
115
- logger().debug("resolved actor", { actorId });
116
- invariant(actorId, "missing actor ID");
117
-
118
- return actorId;
119
- },
120
-
121
- connectWebSocket: async (
122
- c: HonoContext | undefined,
123
- actorQuery: ActorQuery,
124
- encodingKind: Encoding,
125
- params?: unknown,
126
- ): Promise<WebSocket> => {
127
- // Get the actor ID
128
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
129
- logger().debug("found actor for action", { actorId });
130
- invariant(actorId, "Missing actor ID");
131
-
132
- // Invoke the action
133
- logger().debug("opening websocket", { actorId, encoding: encodingKind });
134
-
135
- // Open WebSocket
136
- const ws = await managerDriver.openWebSocket(
137
- PATH_CONNECT_WEBSOCKET,
138
- actorId,
139
- encodingKind,
140
- params,
141
- );
142
-
143
- // Node & browser WebSocket types are incompatible
144
- return ws as any;
145
- },
146
-
147
- connectSse: async (
148
- c: HonoContext | undefined,
149
- actorQuery: ActorQuery,
150
- encodingKind: Encoding,
151
- params: unknown,
152
- ): Promise<UniversalEventSource> => {
153
- // Get the actor ID
154
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
155
- logger().debug("found actor for sse connection", { actorId });
156
- invariant(actorId, "Missing actor ID");
157
-
158
- logger().debug("opening sse connection", {
159
- actorId,
160
- encoding: encodingKind,
161
- });
162
-
163
- const EventSourceClass = await importEventSource();
164
-
165
- const eventSource = new EventSourceClass("http://actor/connect/sse", {
166
- fetch: (input, init) => {
167
- return fetch(input, {
168
- ...init,
169
- headers: {
170
- ...init?.headers,
171
- "User-Agent": httpUserAgent(),
172
- [HEADER_ENCODING]: encodingKind,
173
- ...(params !== undefined
174
- ? { [HEADER_CONN_PARAMS]: JSON.stringify(params) }
175
- : {}),
176
- [HEADER_EXPOSE_INTERNAL_ERROR]: "true",
177
- },
178
- });
179
- },
180
- }) as UniversalEventSource;
181
-
182
- return eventSource;
183
- },
184
-
185
- sendHttpMessage: async (
186
- c: HonoContext | undefined,
187
- actorId: string,
188
- encoding: Encoding,
189
- connectionId: string,
190
- connectionToken: string,
191
- message: protocol.ToServer,
192
- ): Promise<void> => {
193
- logger().debug("sending http message", { actorId, connectionId });
194
-
195
- // Send an HTTP request to the connections endpoint
196
- await sendHttpRequest({
197
- url: "http://actor/connections/message",
198
- method: "POST",
199
- headers: {
200
- [HEADER_ENCODING]: encoding,
201
- [HEADER_CONN_ID]: connectionId,
202
- [HEADER_CONN_TOKEN]: connectionToken,
203
- [HEADER_EXPOSE_INTERNAL_ERROR]: "true",
204
- },
205
- body: message,
206
- encoding,
207
- skipParseResponse: true,
208
- customFetch: managerDriver.sendRequest.bind(managerDriver, actorId),
209
- requestVersionedDataHandler: TO_SERVER_VERSIONED,
210
- responseVersionedDataHandler: TO_CLIENT_VERSIONED,
211
- });
212
- },
213
-
214
- rawHttpRequest: async (
215
- c: HonoContext | undefined,
216
- actorQuery: ActorQuery,
217
- encoding: Encoding,
218
- params: unknown,
219
- path: string,
220
- init: RequestInit,
221
- ): Promise<Response> => {
222
- try {
223
- // Get the actor ID
224
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
225
- logger().debug("found actor for raw http", { actorId });
226
- invariant(actorId, "Missing actor ID");
227
-
228
- // Build the URL with normalized path
229
- const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
230
- const url = new URL(`http://actor/raw/http/${normalizedPath}`);
231
-
232
- // Forward conn params if provided
233
- const proxyRequestHeaders = new Headers(init.headers);
234
- if (params) {
235
- proxyRequestHeaders.set(HEADER_CONN_PARAMS, JSON.stringify(params));
236
- }
237
-
238
- // Forward the request to the actor
239
- const proxyRequest = new Request(url, {
240
- ...init,
241
- headers: proxyRequestHeaders,
242
- });
243
-
244
- return await managerDriver.sendRequest(actorId, proxyRequest);
245
- } catch (err) {
246
- // Standardize to ClientActorError instead of the native backend error
247
- const { code, message, metadata } = deconstructError(
248
- err,
249
- logger(),
250
- {},
251
- true,
252
- );
253
- throw new ClientActorError(code, message, metadata);
254
- }
255
- },
256
-
257
- rawWebSocket: async (
258
- c: HonoContext | undefined,
259
- actorQuery: ActorQuery,
260
- encoding: Encoding,
261
- params: unknown,
262
- path: string,
263
- protocols: string | string[] | undefined,
264
- ): Promise<WebSocket> => {
265
- // Get the actor ID
266
- const { actorId } = await queryActor(c, actorQuery, managerDriver);
267
- logger().debug("found actor for action", { actorId });
268
- invariant(actorId, "Missing actor ID");
269
-
270
- // Normalize path to match raw HTTP behavior
271
- const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
272
- logger().debug("opening websocket", {
273
- actorId,
274
- encoding,
275
- path: normalizedPath,
276
- });
277
-
278
- // Open WebSocket
279
- const ws = await managerDriver.openWebSocket(
280
- `${PATH_RAW_WEBSOCKET_PREFIX}${normalizedPath}`,
281
- actorId,
282
- encoding,
283
- params,
284
- );
285
-
286
- // Node & browser WebSocket types are incompatible
287
- return ws as any;
288
- },
289
- };
290
-
291
- return driver;
292
- }
293
-
294
- /**
295
- * Query the manager driver to get or create a actor based on the provided query
296
- */
297
- export async function queryActor(
298
- c: HonoContext | undefined,
299
- query: ActorQuery,
300
- driver: ManagerDriver,
301
- ): Promise<{ actorId: string }> {
302
- logger().debug("querying actor", { query });
303
- let actorOutput: { actorId: string };
304
- if ("getForId" in query) {
305
- const output = await driver.getForId({
306
- c,
307
- name: query.getForId.name,
308
- actorId: query.getForId.actorId,
309
- });
310
- if (!output) throw new errors.ActorNotFound(query.getForId.actorId);
311
- actorOutput = output;
312
- } else if ("getForKey" in query) {
313
- const existingActor = await driver.getWithKey({
314
- c,
315
- name: query.getForKey.name,
316
- key: query.getForKey.key,
317
- });
318
- if (!existingActor) {
319
- throw new errors.ActorNotFound(
320
- `${query.getForKey.name}:${JSON.stringify(query.getForKey.key)}`,
321
- );
322
- }
323
- actorOutput = existingActor;
324
- } else if ("getOrCreateForKey" in query) {
325
- const getOrCreateOutput = await driver.getOrCreateWithKey({
326
- c,
327
- name: query.getOrCreateForKey.name,
328
- key: query.getOrCreateForKey.key,
329
- input: query.getOrCreateForKey.input,
330
- region: query.getOrCreateForKey.region,
331
- });
332
- actorOutput = {
333
- actorId: getOrCreateOutput.actorId,
334
- };
335
- } else if ("create" in query) {
336
- const createOutput = await driver.createActor({
337
- c,
338
- name: query.create.name,
339
- key: query.create.key,
340
- input: query.create.input,
341
- region: query.create.region,
342
- });
343
- actorOutput = {
344
- actorId: createOutput.actorId,
345
- };
346
- } else {
347
- throw new errors.InvalidRequest("Invalid query format");
348
- }
349
-
350
- logger().debug("actor query result", {
351
- actorId: actorOutput.actorId,
352
- });
353
- return { actorId: actorOutput.actorId };
354
- }
355
-
356
- /**
357
- * Removes the on-change library's proxy recursively from a value so we can clone it with `structuredClone`.
358
- */
359
- function unproxyRecursive<T>(objProxied: T): T {
360
- const obj = onChange.target<any>(objProxied);
361
-
362
- // Short circuit if this object was proxied
363
- //
364
- // If the reference is different, then this value was proxied and no
365
- // nested values are proxied
366
- if (obj !== objProxied) return obj;
367
-
368
- // Handle null/undefined
369
- if (!obj || typeof obj !== "object") {
370
- return obj;
371
- }
372
-
373
- // Handle arrays
374
- if (Array.isArray(obj)) {
375
- return obj.map((x) => unproxyRecursive<any>(x)) as T;
376
- }
377
-
378
- // Handle objects
379
- const result: any = {};
380
- for (const key in obj) {
381
- result[key] = unproxyRecursive<any>(obj[key]);
382
- }
383
-
384
- return result;
385
- }
@@ -1,121 +0,0 @@
1
- import type { Context as HonoContext } from "hono";
2
- import type { AuthIntent } from "@/actor/config";
3
- import type { AnyActorDefinition } from "@/actor/definition";
4
- import * as errors from "@/actor/errors";
5
- import type { RegistryConfig } from "@/registry/config";
6
- import { stringifyError } from "@/utils";
7
- import type { ManagerDriver } from "./driver";
8
- import { logger } from "./log";
9
- import type { ActorQuery } from "./protocol/query";
10
-
11
- /**
12
- * Get authentication intents from a actor query
13
- */
14
- export function getIntentsFromQuery(query: ActorQuery): Set<AuthIntent> {
15
- const intents = new Set<AuthIntent>();
16
-
17
- if ("getForId" in query) {
18
- intents.add("get");
19
- } else if ("getForKey" in query) {
20
- intents.add("get");
21
- } else if ("getOrCreateForKey" in query) {
22
- intents.add("get");
23
- intents.add("create");
24
- } else if ("create" in query) {
25
- intents.add("create");
26
- }
27
-
28
- return intents;
29
- }
30
-
31
- /**
32
- * Get actor name from a actor query
33
- */
34
- export async function getActorNameFromQuery(
35
- c: HonoContext,
36
- driver: ManagerDriver,
37
- query: ActorQuery,
38
- ): Promise<string> {
39
- if ("getForId" in query) {
40
- // TODO: This will have a duplicate call to getForId between this and queryActor
41
- const output = await driver.getForId({
42
- c,
43
- name: query.getForId.name,
44
- actorId: query.getForId.actorId,
45
- });
46
- if (!output) throw new errors.ActorNotFound(query.getForId.actorId);
47
- return output.name;
48
- } else if ("getForKey" in query) {
49
- return query.getForKey.name;
50
- } else if ("getOrCreateForKey" in query) {
51
- return query.getOrCreateForKey.name;
52
- } else if ("create" in query) {
53
- return query.create.name;
54
- } else {
55
- throw new errors.InvalidRequest("Invalid query format");
56
- }
57
- }
58
-
59
- /**
60
- * Authenticate a request using the actor's onAuth function
61
- */
62
- export async function authenticateRequest(
63
- c: HonoContext,
64
- actorDefinition: AnyActorDefinition,
65
- intents: Set<AuthIntent>,
66
- params: unknown,
67
- ): Promise<unknown> {
68
- if (!("onAuth" in actorDefinition.config)) {
69
- throw new errors.Forbidden(
70
- "Actor requires authentication but no onAuth handler is defined (https://rivet.gg/docs/actors/authentication/). Provide an empty handler to disable auth: `onAuth: () => {}`",
71
- );
72
- }
73
-
74
- try {
75
- const dataOrPromise = actorDefinition.config.onAuth(
76
- {
77
- request: c.req.raw,
78
- intents,
79
- },
80
- params,
81
- );
82
- if (dataOrPromise instanceof Promise) {
83
- return await dataOrPromise;
84
- } else {
85
- return dataOrPromise;
86
- }
87
- } catch (error) {
88
- logger().info("authentication error", { error: stringifyError(error) });
89
- throw error;
90
- }
91
- }
92
-
93
- /**
94
- * Simplified authentication for endpoints that combines all auth steps
95
- */
96
- export async function authenticateEndpoint(
97
- c: HonoContext,
98
- driver: ManagerDriver,
99
- registryConfig: RegistryConfig,
100
- query: ActorQuery,
101
- additionalIntents: AuthIntent[],
102
- params: unknown,
103
- ): Promise<unknown> {
104
- // Get base intents from query
105
- const intents = getIntentsFromQuery(query);
106
-
107
- // Add endpoint-specific intents
108
- for (const intent of additionalIntents) {
109
- intents.add(intent);
110
- }
111
-
112
- // Get actor definition
113
- const actorName = await getActorNameFromQuery(c, driver, query);
114
- const actorDefinition = registryConfig.use[actorName];
115
- if (!actorDefinition) {
116
- throw new errors.ActorNotFound(actorName);
117
- }
118
-
119
- // Authenticate
120
- return await authenticateRequest(c, actorDefinition, intents, params);
121
- }
File without changes
File without changes