rivetkit 2.0.1 → 2.0.3

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 (253) hide show
  1. package/dist/schemas/actor-persist/v1.ts +228 -0
  2. package/dist/schemas/client-protocol/v1.ts +429 -0
  3. package/dist/schemas/file-system-driver/v1.ts +102 -0
  4. package/dist/tsup/actor/errors.cjs +69 -0
  5. package/dist/tsup/actor/errors.cjs.map +1 -0
  6. package/dist/tsup/actor/errors.d.cts +143 -0
  7. package/dist/tsup/actor/errors.d.ts +143 -0
  8. package/dist/tsup/actor/errors.js +69 -0
  9. package/dist/tsup/actor/errors.js.map +1 -0
  10. package/dist/tsup/chunk-2CRLFV6Z.cjs +202 -0
  11. package/dist/tsup/chunk-2CRLFV6Z.cjs.map +1 -0
  12. package/dist/tsup/chunk-3H7O2A7I.js +525 -0
  13. package/dist/tsup/chunk-3H7O2A7I.js.map +1 -0
  14. package/dist/tsup/chunk-42I3OZ3Q.js +15 -0
  15. package/dist/tsup/chunk-42I3OZ3Q.js.map +1 -0
  16. package/dist/tsup/chunk-4NSUQZ2H.js +1790 -0
  17. package/dist/tsup/chunk-4NSUQZ2H.js.map +1 -0
  18. package/dist/tsup/chunk-6PDXBYI5.js +132 -0
  19. package/dist/tsup/chunk-6PDXBYI5.js.map +1 -0
  20. package/dist/tsup/chunk-6WKQDDUD.cjs +1790 -0
  21. package/dist/tsup/chunk-6WKQDDUD.cjs.map +1 -0
  22. package/dist/tsup/chunk-CTBOSFUH.cjs +116 -0
  23. package/dist/tsup/chunk-CTBOSFUH.cjs.map +1 -0
  24. package/dist/tsup/chunk-EGVZZFE2.js +2857 -0
  25. package/dist/tsup/chunk-EGVZZFE2.js.map +1 -0
  26. package/dist/tsup/chunk-FCCPJNMA.cjs +132 -0
  27. package/dist/tsup/chunk-FCCPJNMA.cjs.map +1 -0
  28. package/dist/tsup/chunk-FLMTTN27.js +244 -0
  29. package/dist/tsup/chunk-FLMTTN27.js.map +1 -0
  30. package/dist/tsup/chunk-GIR3AFFI.cjs +315 -0
  31. package/dist/tsup/chunk-GIR3AFFI.cjs.map +1 -0
  32. package/dist/tsup/chunk-INGJP237.js +315 -0
  33. package/dist/tsup/chunk-INGJP237.js.map +1 -0
  34. package/dist/tsup/chunk-KJCJLKRM.js +116 -0
  35. package/dist/tsup/chunk-KJCJLKRM.js.map +1 -0
  36. package/dist/tsup/chunk-KUPQZYUQ.cjs +15 -0
  37. package/dist/tsup/chunk-KUPQZYUQ.cjs.map +1 -0
  38. package/dist/tsup/chunk-O2MBYIXO.cjs +2857 -0
  39. package/dist/tsup/chunk-O2MBYIXO.cjs.map +1 -0
  40. package/dist/tsup/chunk-OGAPU3UG.cjs +525 -0
  41. package/dist/tsup/chunk-OGAPU3UG.cjs.map +1 -0
  42. package/dist/tsup/chunk-OV6AYD4S.js +4406 -0
  43. package/dist/tsup/chunk-OV6AYD4S.js.map +1 -0
  44. package/dist/tsup/chunk-PO4VLDWA.js +47 -0
  45. package/dist/tsup/chunk-PO4VLDWA.js.map +1 -0
  46. package/dist/tsup/chunk-R2OPSKIV.cjs +244 -0
  47. package/dist/tsup/chunk-R2OPSKIV.cjs.map +1 -0
  48. package/dist/tsup/chunk-TZJKSBUQ.cjs +47 -0
  49. package/dist/tsup/chunk-TZJKSBUQ.cjs.map +1 -0
  50. package/dist/tsup/chunk-UBUC5C3G.cjs +189 -0
  51. package/dist/tsup/chunk-UBUC5C3G.cjs.map +1 -0
  52. package/dist/tsup/chunk-UIM22YJL.cjs +4406 -0
  53. package/dist/tsup/chunk-UIM22YJL.cjs.map +1 -0
  54. package/dist/tsup/chunk-URVFQMYI.cjs +230 -0
  55. package/dist/tsup/chunk-URVFQMYI.cjs.map +1 -0
  56. package/dist/tsup/chunk-UVUPOS46.js +230 -0
  57. package/dist/tsup/chunk-UVUPOS46.js.map +1 -0
  58. package/dist/tsup/chunk-VRRHBNJC.js +189 -0
  59. package/dist/tsup/chunk-VRRHBNJC.js.map +1 -0
  60. package/dist/tsup/chunk-XFSS33EQ.js +202 -0
  61. package/dist/tsup/chunk-XFSS33EQ.js.map +1 -0
  62. package/dist/tsup/client/mod.cjs +32 -0
  63. package/dist/tsup/client/mod.cjs.map +1 -0
  64. package/dist/tsup/client/mod.d.cts +26 -0
  65. package/dist/tsup/client/mod.d.ts +26 -0
  66. package/dist/tsup/client/mod.js +32 -0
  67. package/dist/tsup/client/mod.js.map +1 -0
  68. package/dist/tsup/common/log.cjs +13 -0
  69. package/dist/tsup/common/log.cjs.map +1 -0
  70. package/dist/tsup/common/log.d.cts +20 -0
  71. package/dist/tsup/common/log.d.ts +20 -0
  72. package/dist/tsup/common/log.js +13 -0
  73. package/dist/tsup/common/log.js.map +1 -0
  74. package/dist/tsup/common/websocket.cjs +10 -0
  75. package/dist/tsup/common/websocket.cjs.map +1 -0
  76. package/dist/tsup/common/websocket.d.cts +3 -0
  77. package/dist/tsup/common/websocket.d.ts +3 -0
  78. package/dist/tsup/common/websocket.js +10 -0
  79. package/dist/tsup/common/websocket.js.map +1 -0
  80. package/dist/tsup/common-CpqORuCq.d.cts +218 -0
  81. package/dist/tsup/common-CpqORuCq.d.ts +218 -0
  82. package/dist/tsup/connection-BR_Ve4ku.d.cts +2117 -0
  83. package/dist/tsup/connection-BwUMoe6n.d.ts +2117 -0
  84. package/dist/tsup/driver-helpers/mod.cjs +33 -0
  85. package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
  86. package/dist/tsup/driver-helpers/mod.d.cts +18 -0
  87. package/dist/tsup/driver-helpers/mod.d.ts +18 -0
  88. package/dist/tsup/driver-helpers/mod.js +33 -0
  89. package/dist/tsup/driver-helpers/mod.js.map +1 -0
  90. package/dist/tsup/driver-test-suite/mod.cjs +4619 -0
  91. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
  92. package/dist/tsup/driver-test-suite/mod.d.cts +57 -0
  93. package/dist/tsup/driver-test-suite/mod.d.ts +57 -0
  94. package/dist/tsup/driver-test-suite/mod.js +4619 -0
  95. package/dist/tsup/driver-test-suite/mod.js.map +1 -0
  96. package/dist/tsup/inspector/mod.cjs +53 -0
  97. package/dist/tsup/inspector/mod.cjs.map +1 -0
  98. package/dist/tsup/inspector/mod.d.cts +408 -0
  99. package/dist/tsup/inspector/mod.d.ts +408 -0
  100. package/dist/tsup/inspector/mod.js +53 -0
  101. package/dist/tsup/inspector/mod.js.map +1 -0
  102. package/dist/tsup/mod.cjs +73 -0
  103. package/dist/tsup/mod.cjs.map +1 -0
  104. package/dist/tsup/mod.d.cts +100 -0
  105. package/dist/tsup/mod.d.ts +100 -0
  106. package/dist/tsup/mod.js +73 -0
  107. package/dist/tsup/mod.js.map +1 -0
  108. package/dist/tsup/router-endpoints-AYkXG8Tl.d.cts +66 -0
  109. package/dist/tsup/router-endpoints-DAbqVFx2.d.ts +66 -0
  110. package/dist/tsup/test/mod.cjs +21 -0
  111. package/dist/tsup/test/mod.cjs.map +1 -0
  112. package/dist/tsup/test/mod.d.cts +27 -0
  113. package/dist/tsup/test/mod.d.ts +27 -0
  114. package/dist/tsup/test/mod.js +21 -0
  115. package/dist/tsup/test/mod.js.map +1 -0
  116. package/dist/tsup/utils-CT0cv4jd.d.cts +17 -0
  117. package/dist/tsup/utils-CT0cv4jd.d.ts +17 -0
  118. package/dist/tsup/utils.cjs +26 -0
  119. package/dist/tsup/utils.cjs.map +1 -0
  120. package/dist/tsup/utils.d.cts +36 -0
  121. package/dist/tsup/utils.d.ts +36 -0
  122. package/dist/tsup/utils.js +26 -0
  123. package/dist/tsup/utils.js.map +1 -0
  124. package/package.json +208 -5
  125. package/src/actor/action.ts +182 -0
  126. package/src/actor/config.ts +765 -0
  127. package/src/actor/connection.ts +260 -0
  128. package/src/actor/context.ts +171 -0
  129. package/src/actor/database.ts +23 -0
  130. package/src/actor/definition.ts +86 -0
  131. package/src/actor/driver.ts +84 -0
  132. package/src/actor/errors.ts +360 -0
  133. package/src/actor/generic-conn-driver.ts +234 -0
  134. package/src/actor/instance.ts +1800 -0
  135. package/src/actor/log.ts +15 -0
  136. package/src/actor/mod.ts +113 -0
  137. package/src/actor/persisted.ts +42 -0
  138. package/src/actor/protocol/old.ts +281 -0
  139. package/src/actor/protocol/serde.ts +131 -0
  140. package/src/actor/router-endpoints.ts +685 -0
  141. package/src/actor/router.ts +263 -0
  142. package/src/actor/schedule.ts +17 -0
  143. package/src/actor/unstable-react.ts +110 -0
  144. package/src/actor/utils.ts +98 -0
  145. package/src/client/actor-common.ts +30 -0
  146. package/src/client/actor-conn.ts +804 -0
  147. package/src/client/actor-handle.ts +208 -0
  148. package/src/client/client.ts +623 -0
  149. package/src/client/errors.ts +41 -0
  150. package/src/client/http-client-driver.ts +326 -0
  151. package/src/client/log.ts +7 -0
  152. package/src/client/mod.ts +56 -0
  153. package/src/client/raw-utils.ts +92 -0
  154. package/src/client/test.ts +44 -0
  155. package/src/client/utils.ts +150 -0
  156. package/src/common/eventsource-interface.ts +47 -0
  157. package/src/common/eventsource.ts +80 -0
  158. package/src/common/fake-event-source.ts +266 -0
  159. package/src/common/inline-websocket-adapter2.ts +445 -0
  160. package/src/common/log-levels.ts +27 -0
  161. package/src/common/log.ts +139 -0
  162. package/src/common/logfmt.ts +228 -0
  163. package/src/common/network.ts +2 -0
  164. package/src/common/router.ts +87 -0
  165. package/src/common/utils.ts +322 -0
  166. package/src/common/versioned-data.ts +95 -0
  167. package/src/common/websocket-interface.ts +49 -0
  168. package/src/common/websocket.ts +43 -0
  169. package/src/driver-helpers/mod.ts +22 -0
  170. package/src/driver-helpers/utils.ts +17 -0
  171. package/src/driver-test-suite/log.ts +7 -0
  172. package/src/driver-test-suite/mod.ts +213 -0
  173. package/src/driver-test-suite/test-inline-client-driver.ts +402 -0
  174. package/src/driver-test-suite/tests/action-features.ts +136 -0
  175. package/src/driver-test-suite/tests/actor-auth.ts +591 -0
  176. package/src/driver-test-suite/tests/actor-conn-state.ts +249 -0
  177. package/src/driver-test-suite/tests/actor-conn.ts +349 -0
  178. package/src/driver-test-suite/tests/actor-driver.ts +25 -0
  179. package/src/driver-test-suite/tests/actor-error-handling.ts +158 -0
  180. package/src/driver-test-suite/tests/actor-handle.ts +259 -0
  181. package/src/driver-test-suite/tests/actor-inline-client.ts +152 -0
  182. package/src/driver-test-suite/tests/actor-inspector.ts +570 -0
  183. package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
  184. package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
  185. package/src/driver-test-suite/tests/actor-schedule.ts +108 -0
  186. package/src/driver-test-suite/tests/actor-sleep.ts +413 -0
  187. package/src/driver-test-suite/tests/actor-state.ts +54 -0
  188. package/src/driver-test-suite/tests/actor-vars.ts +93 -0
  189. package/src/driver-test-suite/tests/manager-driver.ts +365 -0
  190. package/src/driver-test-suite/tests/raw-http-direct-registry.ts +226 -0
  191. package/src/driver-test-suite/tests/raw-http-request-properties.ts +414 -0
  192. package/src/driver-test-suite/tests/raw-http.ts +347 -0
  193. package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +392 -0
  194. package/src/driver-test-suite/tests/raw-websocket.ts +484 -0
  195. package/src/driver-test-suite/tests/request-access.ts +244 -0
  196. package/src/driver-test-suite/utils.ts +68 -0
  197. package/src/drivers/default.ts +31 -0
  198. package/src/drivers/engine/actor-driver.ts +360 -0
  199. package/src/drivers/engine/api-endpoints.ts +128 -0
  200. package/src/drivers/engine/api-utils.ts +70 -0
  201. package/src/drivers/engine/config.ts +39 -0
  202. package/src/drivers/engine/keys.test.ts +266 -0
  203. package/src/drivers/engine/keys.ts +89 -0
  204. package/src/drivers/engine/kv.ts +3 -0
  205. package/src/drivers/engine/log.ts +7 -0
  206. package/src/drivers/engine/manager-driver.ts +391 -0
  207. package/src/drivers/engine/mod.ts +36 -0
  208. package/src/drivers/engine/ws-proxy.ts +170 -0
  209. package/src/drivers/file-system/actor.ts +91 -0
  210. package/src/drivers/file-system/global-state.ts +673 -0
  211. package/src/drivers/file-system/log.ts +7 -0
  212. package/src/drivers/file-system/manager.ts +306 -0
  213. package/src/drivers/file-system/mod.ts +48 -0
  214. package/src/drivers/file-system/utils.ts +109 -0
  215. package/src/globals.d.ts +6 -0
  216. package/src/inline-client-driver/log.ts +7 -0
  217. package/src/inline-client-driver/mod.ts +385 -0
  218. package/src/inspector/actor.ts +298 -0
  219. package/src/inspector/config.ts +83 -0
  220. package/src/inspector/log.ts +5 -0
  221. package/src/inspector/manager.ts +86 -0
  222. package/src/inspector/mod.ts +2 -0
  223. package/src/inspector/protocol/actor.ts +10 -0
  224. package/src/inspector/protocol/common.ts +196 -0
  225. package/src/inspector/protocol/manager.ts +10 -0
  226. package/src/inspector/protocol/mod.ts +2 -0
  227. package/src/inspector/utils.ts +76 -0
  228. package/src/manager/auth.ts +121 -0
  229. package/src/manager/driver.ts +80 -0
  230. package/src/manager/hono-websocket-adapter.ts +333 -0
  231. package/src/manager/log.ts +7 -0
  232. package/src/manager/mod.ts +2 -0
  233. package/src/manager/protocol/mod.ts +24 -0
  234. package/src/manager/protocol/query.ts +89 -0
  235. package/src/manager/router.ts +1792 -0
  236. package/src/mod.ts +20 -0
  237. package/src/registry/config.ts +32 -0
  238. package/src/registry/log.ts +7 -0
  239. package/src/registry/mod.ts +124 -0
  240. package/src/registry/run-config.ts +54 -0
  241. package/src/registry/serve.ts +53 -0
  242. package/src/schemas/actor-persist/mod.ts +1 -0
  243. package/src/schemas/actor-persist/versioned.ts +25 -0
  244. package/src/schemas/client-protocol/mod.ts +1 -0
  245. package/src/schemas/client-protocol/versioned.ts +63 -0
  246. package/src/schemas/file-system-driver/mod.ts +1 -0
  247. package/src/schemas/file-system-driver/versioned.ts +28 -0
  248. package/src/serde.ts +84 -0
  249. package/src/test/config.ts +16 -0
  250. package/src/test/log.ts +7 -0
  251. package/src/test/mod.ts +153 -0
  252. package/src/utils.ts +172 -0
  253. package/README.md +0 -13
@@ -0,0 +1,623 @@
1
+ import type { Context as HonoContext } from "hono";
2
+ import type { WebSocket } from "ws";
3
+ import type { AnyActorDefinition } from "@/actor/definition";
4
+ import type { Transport } from "@/actor/protocol/old";
5
+ import type { Encoding } from "@/actor/protocol/serde";
6
+ import type { UniversalEventSource } from "@/common/eventsource-interface";
7
+ import type { ActorQuery } from "@/manager/protocol/query";
8
+ import type { Registry } from "@/mod";
9
+ import type { ToServer } from "@/schemas/client-protocol/mod";
10
+ import type { ActorActionFunction } from "./actor-common";
11
+ import {
12
+ type ActorConn,
13
+ type ActorConnRaw,
14
+ CONNECT_SYMBOL,
15
+ } from "./actor-conn";
16
+ import { type ActorHandle, ActorHandleRaw } from "./actor-handle";
17
+ import { logger } from "./log";
18
+
19
+ /** Extract the actor registry from the registry definition. */
20
+ export type ExtractActorsFromRegistry<A extends Registry<any>> =
21
+ A extends Registry<infer Actors> ? Actors : never;
22
+
23
+ /** Extract the registry definition from the client. */
24
+ export type ExtractRegistryFromClient<C extends Client<Registry<{}>>> =
25
+ C extends Client<infer A> ? A : never;
26
+
27
+ /**
28
+ * Represents a actor accessor that provides methods to interact with a specific actor.
29
+ */
30
+ export interface ActorAccessor<AD extends AnyActorDefinition> {
31
+ /**
32
+ * Gets a stateless handle to a actor by its key, but does not create the actor if it doesn't exist.
33
+ * The actor name is automatically injected from the property accessor.
34
+ *
35
+ * @template AD The actor class that this handle is for.
36
+ * @param {string | string[]} [key=[]] - The key to identify the actor. Can be a single string or an array of strings.
37
+ * @param {GetWithIdOptions} [opts] - Options for getting the actor.
38
+ * @returns {ActorHandle<AD>} - A handle to the actor.
39
+ */
40
+ get(key?: string | string[], opts?: GetWithIdOptions): ActorHandle<AD>;
41
+
42
+ /**
43
+ * Gets a stateless handle to a actor by its key, creating it if necessary.
44
+ * The actor name is automatically injected from the property accessor.
45
+ *
46
+ * @template AD The actor class that this handle is for.
47
+ * @param {string | string[]} [key=[]] - The key to identify the actor. Can be a single string or an array of strings.
48
+ * @param {GetOptions} [opts] - Options for getting the actor.
49
+ * @returns {ActorHandle<AD>} - A handle to the actor.
50
+ */
51
+ getOrCreate(
52
+ key?: string | string[],
53
+ opts?: GetOrCreateOptions,
54
+ ): ActorHandle<AD>;
55
+
56
+ /**
57
+ * Gets a stateless handle to a actor by its ID.
58
+ *
59
+ * @template AD The actor class that this handle is for.
60
+ * @param {string} actorId - The ID of the actor.
61
+ * @param {GetWithIdOptions} [opts] - Options for getting the actor.
62
+ * @returns {ActorHandle<AD>} - A handle to the actor.
63
+ */
64
+ getForId(actorId: string, opts?: GetWithIdOptions): ActorHandle<AD>;
65
+
66
+ /**
67
+ * Creates a new actor with the name automatically injected from the property accessor,
68
+ * and returns a stateless handle to it with the actor ID resolved.
69
+ *
70
+ * @template AD The actor class that this handle is for.
71
+ * @param {string | string[]} key - The key to identify the actor. Can be a single string or an array of strings.
72
+ * @param {CreateOptions} [opts] - Options for creating the actor (excluding name and key).
73
+ * @returns {Promise<ActorHandle<AD>>} - A promise that resolves to a handle to the actor.
74
+ */
75
+ create(
76
+ key?: string | string[],
77
+ opts?: CreateOptions,
78
+ ): Promise<ActorHandle<AD>>;
79
+ }
80
+
81
+ /**
82
+ * Options for configuring the client.
83
+ * @typedef {Object} ClientOptions
84
+ */
85
+ export interface ClientOptions {
86
+ encoding?: Encoding;
87
+ transport?: Transport;
88
+ }
89
+
90
+ /**
91
+ * Options for querying actors.
92
+ * @typedef {Object} QueryOptions
93
+ * @property {unknown} [parameters] - Parameters to pass to the connection.
94
+ */
95
+ export interface QueryOptions {
96
+ /** Parameters to pass to the connection. */
97
+ params?: unknown;
98
+ /** Signal to abort the request. */
99
+ signal?: AbortSignal;
100
+ }
101
+
102
+ /**
103
+ * Options for getting a actor by ID.
104
+ * @typedef {QueryOptions} GetWithIdOptions
105
+ */
106
+ export interface GetWithIdOptions extends QueryOptions {}
107
+
108
+ /**
109
+ * Options for getting a actor.
110
+ * @typedef {QueryOptions} GetOptions
111
+ */
112
+ export interface GetOptions extends QueryOptions {}
113
+
114
+ /**
115
+ * Options for getting or creating a actor.
116
+ * @typedef {QueryOptions} GetOrCreateOptions
117
+ * @property {string} [createInRegion] - Region to create the actor in if it doesn't exist.
118
+ */
119
+ export interface GetOrCreateOptions extends QueryOptions {
120
+ /** Region to create the actor in if it doesn't exist. */
121
+ createInRegion?: string;
122
+ /** Input data to pass to the actor. */
123
+ createWithInput?: unknown;
124
+ }
125
+
126
+ /**
127
+ * Options for creating a actor.
128
+ * @typedef {QueryOptions} CreateOptions
129
+ * @property {string} [region] - The region to create the actor in.
130
+ */
131
+ export interface CreateOptions extends QueryOptions {
132
+ /** The region to create the actor in. */
133
+ region?: string;
134
+ /** Input data to pass to the actor. */
135
+ input?: unknown;
136
+ }
137
+
138
+ /**
139
+ * Represents a region to connect to.
140
+ * @typedef {Object} Region
141
+ * @property {string} id - The region ID.
142
+ * @property {string} name - The region name.
143
+ * @see {@link https://rivet.gg/docs/edge|Edge Networking}
144
+ * @see {@link https://rivet.gg/docs/regions|Available Regions}
145
+ */
146
+ export interface Region {
147
+ /**
148
+ * The region slug.
149
+ */
150
+ id: string;
151
+
152
+ /**
153
+ * The human-friendly region name.
154
+ */
155
+ name: string;
156
+ }
157
+
158
+ export const ACTOR_CONNS_SYMBOL = Symbol("actorConns");
159
+ export const CREATE_ACTOR_CONN_PROXY = Symbol("createActorConnProxy");
160
+ export const TRANSPORT_SYMBOL = Symbol("transport");
161
+
162
+ export interface ClientDriver {
163
+ action<Args extends Array<unknown> = unknown[], Response = unknown>(
164
+ c: HonoContext | undefined,
165
+ actorQuery: ActorQuery,
166
+ encoding: Encoding,
167
+ params: unknown,
168
+ name: string,
169
+ args: Args,
170
+ opts: { signal?: AbortSignal } | undefined,
171
+ ): Promise<Response>;
172
+ resolveActorId(
173
+ c: HonoContext | undefined,
174
+ actorQuery: ActorQuery,
175
+ encodingKind: Encoding,
176
+ params: unknown,
177
+ opts: { signal?: AbortSignal } | undefined,
178
+ ): Promise<string>;
179
+ connectWebSocket(
180
+ c: HonoContext | undefined,
181
+ actorQuery: ActorQuery,
182
+ encodingKind: Encoding,
183
+ params: unknown,
184
+ opts: { signal?: AbortSignal } | undefined,
185
+ ): Promise<WebSocket>;
186
+ connectSse(
187
+ c: HonoContext | undefined,
188
+ actorQuery: ActorQuery,
189
+ encodingKind: Encoding,
190
+ params: unknown,
191
+ opts: { signal?: AbortSignal } | undefined,
192
+ ): Promise<UniversalEventSource>;
193
+ sendHttpMessage(
194
+ c: HonoContext | undefined,
195
+ actorId: string,
196
+ encoding: Encoding,
197
+ connectionId: string,
198
+ connectionToken: string,
199
+ message: ToServer,
200
+ opts: { signal?: AbortSignal } | undefined,
201
+ ): Promise<void>;
202
+ rawHttpRequest(
203
+ c: HonoContext | undefined,
204
+ actorQuery: ActorQuery,
205
+ encoding: Encoding,
206
+ params: unknown,
207
+ path: string,
208
+ init: RequestInit,
209
+ opts: { signal?: AbortSignal } | undefined,
210
+ ): Promise<Response>;
211
+ rawWebSocket(
212
+ c: HonoContext | undefined,
213
+ actorQuery: ActorQuery,
214
+ encoding: Encoding,
215
+ params: unknown,
216
+ path: string,
217
+ protocols: string | string[] | undefined,
218
+ opts: { signal?: AbortSignal } | undefined,
219
+ ): Promise<WebSocket>;
220
+ }
221
+
222
+ /**
223
+ * Client for managing & connecting to actors.
224
+ *
225
+ * @template A The actors map type that defines the available actors.
226
+ * @see {@link https://rivet.gg/docs/manage|Create & Manage Actors}
227
+ */
228
+ export class ClientRaw {
229
+ #disposed = false;
230
+
231
+ [ACTOR_CONNS_SYMBOL] = new Set<ActorConnRaw>();
232
+
233
+ #driver: ClientDriver;
234
+ #encodingKind: Encoding;
235
+ [TRANSPORT_SYMBOL]: Transport;
236
+
237
+ /**
238
+ * Creates an instance of Client.
239
+ *
240
+ * @param {string} managerEndpoint - The manager endpoint. See {@link https://rivet.gg/docs/setup|Initial Setup} for instructions on getting the manager endpoint.
241
+ * @param {ClientOptions} [opts] - Options for configuring the client.
242
+ * @see {@link https://rivet.gg/docs/setup|Initial Setup}
243
+ */
244
+ public constructor(driver: ClientDriver, opts?: ClientOptions) {
245
+ this.#driver = driver;
246
+
247
+ this.#encodingKind = opts?.encoding ?? "bare";
248
+ this[TRANSPORT_SYMBOL] = opts?.transport ?? "websocket";
249
+ }
250
+
251
+ /**
252
+ * Gets a stateless handle to a actor by its ID.
253
+ *
254
+ * @template AD The actor class that this handle is for.
255
+ * @param {string} name - The name of the actor.
256
+ * @param {string} actorId - The ID of the actor.
257
+ * @param {GetWithIdOptions} [opts] - Options for getting the actor.
258
+ * @returns {ActorHandle<AD>} - A handle to the actor.
259
+ */
260
+ getForId<AD extends AnyActorDefinition>(
261
+ name: string,
262
+ actorId: string,
263
+ opts?: GetWithIdOptions,
264
+ ): ActorHandle<AD> {
265
+ logger().debug("get handle to actor with id", {
266
+ name,
267
+ actorId,
268
+ params: opts?.params,
269
+ });
270
+
271
+ const actorQuery: ActorQuery = {
272
+ getForId: {
273
+ name,
274
+ actorId,
275
+ },
276
+ };
277
+
278
+ const handle = this.#createHandle(opts?.params, actorQuery);
279
+ return createActorProxy(handle) as ActorHandle<AD>;
280
+ }
281
+
282
+ /**
283
+ * Gets a stateless handle to a actor by its key, but does not create the actor if it doesn't exist.
284
+ *
285
+ * @template AD The actor class that this handle is for.
286
+ * @param {string} name - The name of the actor.
287
+ * @param {string | string[]} [key=[]] - The key to identify the actor. Can be a single string or an array of strings.
288
+ * @param {GetWithIdOptions} [opts] - Options for getting the actor.
289
+ * @returns {ActorHandle<AD>} - A handle to the actor.
290
+ */
291
+ get<AD extends AnyActorDefinition>(
292
+ name: string,
293
+ key?: string | string[],
294
+ opts?: GetWithIdOptions,
295
+ ): ActorHandle<AD> {
296
+ // Convert string to array of strings
297
+ const keyArray: string[] = typeof key === "string" ? [key] : key || [];
298
+
299
+ logger().debug("get handle to actor", {
300
+ name,
301
+ key: keyArray,
302
+ parameters: opts?.params,
303
+ });
304
+
305
+ const actorQuery: ActorQuery = {
306
+ getForKey: {
307
+ name,
308
+ key: keyArray,
309
+ },
310
+ };
311
+
312
+ const handle = this.#createHandle(opts?.params, actorQuery);
313
+ return createActorProxy(handle) as ActorHandle<AD>;
314
+ }
315
+
316
+ /**
317
+ * Gets a stateless handle to a actor by its key, creating it if necessary.
318
+ *
319
+ * @template AD The actor class that this handle is for.
320
+ * @param {string} name - The name of the actor.
321
+ * @param {string | string[]} [key=[]] - The key to identify the actor. Can be a single string or an array of strings.
322
+ * @param {GetOptions} [opts] - Options for getting the actor.
323
+ * @returns {ActorHandle<AD>} - A handle to the actor.
324
+ */
325
+ getOrCreate<AD extends AnyActorDefinition>(
326
+ name: string,
327
+ key?: string | string[],
328
+ opts?: GetOrCreateOptions,
329
+ ): ActorHandle<AD> {
330
+ // Convert string to array of strings
331
+ const keyArray: string[] = typeof key === "string" ? [key] : key || [];
332
+
333
+ logger().debug("get or create handle to actor", {
334
+ name,
335
+ key: keyArray,
336
+ parameters: opts?.params,
337
+ createInRegion: opts?.createInRegion,
338
+ });
339
+
340
+ const actorQuery: ActorQuery = {
341
+ getOrCreateForKey: {
342
+ name,
343
+ key: keyArray,
344
+ input: opts?.createWithInput,
345
+ region: opts?.createInRegion,
346
+ },
347
+ };
348
+
349
+ const handle = this.#createHandle(opts?.params, actorQuery);
350
+ return createActorProxy(handle) as ActorHandle<AD>;
351
+ }
352
+
353
+ /**
354
+ * Creates a new actor with the provided key and returns a stateless handle to it.
355
+ * Resolves the actor ID and returns a handle with getForId query.
356
+ *
357
+ * @template AD The actor class that this handle is for.
358
+ * @param {string} name - The name of the actor.
359
+ * @param {string | string[]} key - The key to identify the actor. Can be a single string or an array of strings.
360
+ * @param {CreateOptions} [opts] - Options for creating the actor (excluding name and key).
361
+ * @returns {Promise<ActorHandle<AD>>} - A promise that resolves to a handle to the actor.
362
+ */
363
+ async create<AD extends AnyActorDefinition>(
364
+ name: string,
365
+ key?: string | string[],
366
+ opts?: CreateOptions,
367
+ ): Promise<ActorHandle<AD>> {
368
+ // Convert string to array of strings
369
+ const keyArray: string[] = typeof key === "string" ? [key] : key || [];
370
+
371
+ const createQuery = {
372
+ create: {
373
+ ...opts,
374
+ // Do these last to override `opts`
375
+ name,
376
+ key: keyArray,
377
+ },
378
+ } satisfies ActorQuery;
379
+
380
+ logger().debug("create actor handle", {
381
+ name,
382
+ key: keyArray,
383
+ parameters: opts?.params,
384
+ create: createQuery.create,
385
+ });
386
+
387
+ // Create the actor
388
+ const actorId = await this.#driver.resolveActorId(
389
+ undefined,
390
+ createQuery,
391
+ this.#encodingKind,
392
+ opts?.params,
393
+ opts?.signal ? { signal: opts.signal } : undefined,
394
+ );
395
+ logger().debug("created actor with ID", {
396
+ name,
397
+ key: keyArray,
398
+ actorId,
399
+ });
400
+
401
+ // Create handle with actor ID
402
+ const getForIdQuery = {
403
+ getForId: {
404
+ name,
405
+ actorId,
406
+ },
407
+ } satisfies ActorQuery;
408
+ const handle = this.#createHandle(opts?.params, getForIdQuery);
409
+
410
+ const proxy = createActorProxy(handle) as ActorHandle<AD>;
411
+
412
+ return proxy;
413
+ }
414
+
415
+ #createHandle(params: unknown, actorQuery: ActorQuery): ActorHandleRaw {
416
+ return new ActorHandleRaw(
417
+ this,
418
+ this.#driver,
419
+ params,
420
+ this.#encodingKind,
421
+ actorQuery,
422
+ );
423
+ }
424
+
425
+ [CREATE_ACTOR_CONN_PROXY]<AD extends AnyActorDefinition>(
426
+ conn: ActorConnRaw,
427
+ ): ActorConn<AD> {
428
+ // Save to connection list
429
+ this[ACTOR_CONNS_SYMBOL].add(conn);
430
+
431
+ // Start connection
432
+ conn[CONNECT_SYMBOL]();
433
+
434
+ return createActorProxy(conn) as ActorConn<AD>;
435
+ }
436
+
437
+ /**
438
+ * Disconnects from all actors.
439
+ *
440
+ * @returns {Promise<void>} A promise that resolves when all connections are closed.
441
+ */
442
+ async dispose(): Promise<void> {
443
+ if (this.#disposed) {
444
+ logger().warn("client already disconnected");
445
+ return;
446
+ }
447
+ this.#disposed = true;
448
+
449
+ logger().debug("disposing client");
450
+
451
+ const disposePromises = [];
452
+
453
+ // Dispose all connections
454
+ for (const conn of this[ACTOR_CONNS_SYMBOL].values()) {
455
+ disposePromises.push(conn.dispose());
456
+ }
457
+
458
+ await Promise.all(disposePromises);
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Client type with actor accessors.
464
+ * This adds property accessors for actor names to the ClientRaw base class.
465
+ *
466
+ * @template A The actor registry type.
467
+ */
468
+ export type Client<A extends Registry<any>> = ClientRaw & {
469
+ [K in keyof ExtractActorsFromRegistry<A>]: ActorAccessor<
470
+ ExtractActorsFromRegistry<A>[K]
471
+ >;
472
+ };
473
+
474
+ export type AnyClient = Client<Registry<any>>;
475
+
476
+ export function createClientWithDriver<A extends Registry<any>>(
477
+ driver: ClientDriver,
478
+ opts?: ClientOptions,
479
+ ): Client<A> {
480
+ const client = new ClientRaw(driver, opts);
481
+
482
+ // Create proxy for accessing actors by name
483
+ return new Proxy(client, {
484
+ get: (target: ClientRaw, prop: string | symbol, receiver: unknown) => {
485
+ // Get the real property if it exists
486
+ if (typeof prop === "symbol" || prop in target) {
487
+ const value = Reflect.get(target, prop, receiver);
488
+ // Preserve method binding
489
+ if (typeof value === "function") {
490
+ return value.bind(target);
491
+ }
492
+ return value;
493
+ }
494
+
495
+ // Handle actor accessor for string properties (actor names)
496
+ if (typeof prop === "string") {
497
+ // Return actor accessor object with methods
498
+ return {
499
+ // Handle methods (stateless action)
500
+ get: (
501
+ key?: string | string[],
502
+ opts?: GetWithIdOptions,
503
+ ): ActorHandle<ExtractActorsFromRegistry<A>[typeof prop]> => {
504
+ return target.get<ExtractActorsFromRegistry<A>[typeof prop]>(
505
+ prop,
506
+ key,
507
+ opts,
508
+ );
509
+ },
510
+ getOrCreate: (
511
+ key?: string | string[],
512
+ opts?: GetOptions,
513
+ ): ActorHandle<ExtractActorsFromRegistry<A>[typeof prop]> => {
514
+ return target.getOrCreate<
515
+ ExtractActorsFromRegistry<A>[typeof prop]
516
+ >(prop, key, opts);
517
+ },
518
+ getForId: (
519
+ actorId: string,
520
+ opts?: GetWithIdOptions,
521
+ ): ActorHandle<ExtractActorsFromRegistry<A>[typeof prop]> => {
522
+ return target.getForId<ExtractActorsFromRegistry<A>[typeof prop]>(
523
+ prop,
524
+ actorId,
525
+ opts,
526
+ );
527
+ },
528
+ create: async (
529
+ key: string | string[],
530
+ opts: CreateOptions = {},
531
+ ): Promise<
532
+ ActorHandle<ExtractActorsFromRegistry<A>[typeof prop]>
533
+ > => {
534
+ return await target.create<
535
+ ExtractActorsFromRegistry<A>[typeof prop]
536
+ >(prop, key, opts);
537
+ },
538
+ } as ActorAccessor<ExtractActorsFromRegistry<A>[typeof prop]>;
539
+ }
540
+
541
+ return undefined;
542
+ },
543
+ }) as Client<A>;
544
+ }
545
+
546
+ /**
547
+ * Creates a proxy for a actor that enables calling actions without explicitly using `.action`.
548
+ **/
549
+ function createActorProxy<AD extends AnyActorDefinition>(
550
+ handle: ActorHandleRaw | ActorConnRaw,
551
+ ): ActorHandle<AD> | ActorConn<AD> {
552
+ // Stores returned action functions for faster calls
553
+ const methodCache = new Map<string, ActorActionFunction>();
554
+ return new Proxy(handle, {
555
+ get(target: ActorHandleRaw, prop: string | symbol, receiver: unknown) {
556
+ // Handle built-in Symbol properties
557
+ if (typeof prop === "symbol") {
558
+ return Reflect.get(target, prop, receiver);
559
+ }
560
+
561
+ // Handle built-in Promise methods and existing properties
562
+ if (prop === "constructor" || prop in target) {
563
+ const value = Reflect.get(target, prop, receiver);
564
+ // Preserve method binding
565
+ if (typeof value === "function") {
566
+ return value.bind(target);
567
+ }
568
+ return value;
569
+ }
570
+
571
+ // Create action function that preserves 'this' context
572
+ if (typeof prop === "string") {
573
+ // If JS is attempting to calling this as a promise, ignore it
574
+ if (prop === "then") return undefined;
575
+
576
+ let method = methodCache.get(prop);
577
+ if (!method) {
578
+ method = (...args: unknown[]) => target.action({ name: prop, args });
579
+ methodCache.set(prop, method);
580
+ }
581
+ return method;
582
+ }
583
+ },
584
+
585
+ // Support for 'in' operator
586
+ has(target: ActorHandleRaw, prop: string | symbol) {
587
+ // All string properties are potentially action functions
588
+ if (typeof prop === "string") {
589
+ return true;
590
+ }
591
+ // For symbols, defer to the target's own has behavior
592
+ return Reflect.has(target, prop);
593
+ },
594
+
595
+ // Support instanceof checks
596
+ getPrototypeOf(target: ActorHandleRaw) {
597
+ return Reflect.getPrototypeOf(target);
598
+ },
599
+
600
+ // Prevent property enumeration of non-existent action methods
601
+ ownKeys(target: ActorHandleRaw) {
602
+ return Reflect.ownKeys(target);
603
+ },
604
+
605
+ // Support proper property descriptors
606
+ getOwnPropertyDescriptor(target: ActorHandleRaw, prop: string | symbol) {
607
+ const targetDescriptor = Reflect.getOwnPropertyDescriptor(target, prop);
608
+ if (targetDescriptor) {
609
+ return targetDescriptor;
610
+ }
611
+ if (typeof prop === "string") {
612
+ // Make action methods appear non-enumerable
613
+ return {
614
+ configurable: true,
615
+ enumerable: false,
616
+ writable: false,
617
+ value: (...args: unknown[]) => target.action({ name: prop, args }),
618
+ };
619
+ }
620
+ return undefined;
621
+ },
622
+ }) as ActorHandle<AD> | ActorConn<AD>;
623
+ }
@@ -0,0 +1,41 @@
1
+ import { MAX_CONN_PARAMS_SIZE } from "@/common//network";
2
+
3
+ export class ActorClientError extends Error {}
4
+
5
+ export class InternalError extends ActorClientError {}
6
+
7
+ export class ManagerError extends ActorClientError {
8
+ constructor(error: string, opts?: ErrorOptions) {
9
+ super(`Manager error: ${error}`, opts);
10
+ }
11
+ }
12
+
13
+ export class MalformedResponseMessage extends ActorClientError {
14
+ constructor(cause?: unknown) {
15
+ super(`Malformed response message: ${cause}`, { cause });
16
+ }
17
+ }
18
+
19
+ export class ActorError extends ActorClientError {
20
+ __type = "ActorError";
21
+
22
+ constructor(
23
+ public readonly code: string,
24
+ message: string,
25
+ public readonly metadata?: unknown,
26
+ ) {
27
+ super(message);
28
+ }
29
+ }
30
+
31
+ export class HttpRequestError extends ActorClientError {
32
+ constructor(message: string, opts?: { cause?: unknown }) {
33
+ super(`HTTP request error: ${message}`, { cause: opts?.cause });
34
+ }
35
+ }
36
+
37
+ export class ActorConnDisposed extends ActorClientError {
38
+ constructor() {
39
+ super("Attempting to interact with a disposed actor connection.");
40
+ }
41
+ }