rivetkit 2.0.2 → 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,260 @@
1
+ import * as cbor from "cbor-x";
2
+ import type * as protocol from "@/schemas/client-protocol/mod";
3
+ import { TO_CLIENT_VERSIONED } from "@/schemas/client-protocol/versioned";
4
+ import { bufferToArrayBuffer } from "@/utils";
5
+ import type { AnyDatabaseProvider } from "./database";
6
+ import { type ConnDriver, ConnectionReadyState } from "./driver";
7
+ import * as errors from "./errors";
8
+ import type { ActorInstance } from "./instance";
9
+ import { logger } from "./log";
10
+ import type { PersistedConn } from "./persisted";
11
+ import { CachedSerializer } from "./protocol/serde";
12
+ import { generateSecureToken } from "./utils";
13
+
14
+ export function generateConnId(): string {
15
+ return crypto.randomUUID();
16
+ }
17
+
18
+ export function generateConnToken(): string {
19
+ return generateSecureToken(32);
20
+ }
21
+
22
+ export type ConnId = string;
23
+
24
+ export type AnyConn = Conn<any, any, any, any, any, any, any>;
25
+
26
+ export const CONNECTION_DRIVER_WEBSOCKET = "webSocket";
27
+ export const CONNECTION_DRIVER_SSE = "sse";
28
+ export const CONNECTION_DRIVER_HTTP = "http";
29
+
30
+ export type ConnectionDriver =
31
+ | typeof CONNECTION_DRIVER_WEBSOCKET
32
+ | typeof CONNECTION_DRIVER_SSE
33
+ | typeof CONNECTION_DRIVER_HTTP;
34
+
35
+ export type ConnectionStatus = "connected" | "reconnecting";
36
+
37
+ export const CONNECTION_CHECK_LIVENESS_SYMBOL = Symbol("checkLiveness");
38
+
39
+ /**
40
+ * Represents a client connection to a actor.
41
+ *
42
+ * Manages connection-specific data and controls the connection lifecycle.
43
+ *
44
+ * @see {@link https://rivet.gg/docs/connections|Connection Documentation}
45
+ */
46
+ export class Conn<S, CP, CS, V, I, AD, DB extends AnyDatabaseProvider> {
47
+ subscriptions: Set<string> = new Set<string>();
48
+
49
+ #stateEnabled: boolean;
50
+
51
+ // TODO: Remove this cyclical reference
52
+ #actor: ActorInstance<S, CP, CS, V, I, AD, DB>;
53
+
54
+ #status: ConnectionStatus = "connected";
55
+
56
+ /**
57
+ * The proxied state that notifies of changes automatically.
58
+ *
59
+ * Any data that should be stored indefinitely should be held within this object.
60
+ */
61
+ __persist: PersistedConn<CP, CS>;
62
+
63
+ /**
64
+ * Driver used to manage realtime connection communication.
65
+ *
66
+ * @protected
67
+ */
68
+ #driver: ConnDriver;
69
+
70
+ public get params(): CP {
71
+ return this.__persist.params;
72
+ }
73
+
74
+ public get auth(): AD {
75
+ return this.__persist.authData as AD;
76
+ }
77
+
78
+ public get driver(): ConnectionDriver {
79
+ return this.__persist.connDriver as ConnectionDriver;
80
+ }
81
+
82
+ public get _stateEnabled() {
83
+ return this.#stateEnabled;
84
+ }
85
+
86
+ /**
87
+ * Gets the current state of the connection.
88
+ *
89
+ * Throws an error if the state is not enabled.
90
+ */
91
+ public get state(): CS {
92
+ this.#validateStateEnabled();
93
+ if (!this.__persist.state) throw new Error("state should exists");
94
+ return this.__persist.state;
95
+ }
96
+
97
+ /**
98
+ * Sets the state of the connection.
99
+ *
100
+ * Throws an error if the state is not enabled.
101
+ */
102
+ public set state(value: CS) {
103
+ this.#validateStateEnabled();
104
+ this.__persist.state = value;
105
+ }
106
+
107
+ /**
108
+ * Unique identifier for the connection.
109
+ */
110
+ public get id(): ConnId {
111
+ return this.__persist.connId;
112
+ }
113
+
114
+ /**
115
+ * Token used to authenticate this request.
116
+ */
117
+ public get _token(): string {
118
+ return this.__persist.token;
119
+ }
120
+
121
+ /**
122
+ * Status of the connection.
123
+ */
124
+ public get status(): ConnectionStatus {
125
+ return this.#status;
126
+ }
127
+
128
+ /**
129
+ * Timestamp of the last time the connection was seen, i.e. the last time the connection was active and checked for liveness.
130
+ */
131
+ public get lastSeen(): number {
132
+ return this.__persist.lastSeen;
133
+ }
134
+
135
+ /**
136
+ * Initializes a new instance of the Connection class.
137
+ *
138
+ * This should only be constructed by {@link Actor}.
139
+ *
140
+ * @protected
141
+ */
142
+ public constructor(
143
+ actor: ActorInstance<S, CP, CS, V, I, AD, DB>,
144
+ persist: PersistedConn<CP, CS>,
145
+ driver: ConnDriver,
146
+ stateEnabled: boolean,
147
+ ) {
148
+ this.#actor = actor;
149
+ this.__persist = persist;
150
+ this.#driver = driver;
151
+ this.#stateEnabled = stateEnabled;
152
+ }
153
+
154
+ #validateStateEnabled() {
155
+ if (!this.#stateEnabled) {
156
+ throw new errors.ConnStateNotEnabled();
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Sends a WebSocket message to the client.
162
+ *
163
+ * @param message - The message to send.
164
+ *
165
+ * @protected
166
+ */
167
+ public _sendMessage(message: CachedSerializer<protocol.ToClient>) {
168
+ this.#driver.sendMessage?.(
169
+ this.#actor,
170
+ this,
171
+ this.__persist.connDriverState,
172
+ message,
173
+ );
174
+ }
175
+
176
+ /**
177
+ * Sends an event with arguments to the client.
178
+ *
179
+ * @param eventName - The name of the event.
180
+ * @param args - The arguments for the event.
181
+ * @see {@link https://rivet.gg/docs/events|Events Documentation}
182
+ */
183
+ public send(eventName: string, ...args: unknown[]) {
184
+ this.#actor.inspector.emitter.emit("eventFired", {
185
+ type: "event",
186
+ eventName,
187
+ args,
188
+ connId: this.id,
189
+ });
190
+ this._sendMessage(
191
+ new CachedSerializer<protocol.ToClient>(
192
+ {
193
+ body: {
194
+ tag: "Event",
195
+ val: {
196
+ name: eventName,
197
+ args: bufferToArrayBuffer(cbor.encode(args)),
198
+ },
199
+ },
200
+ },
201
+ TO_CLIENT_VERSIONED,
202
+ ),
203
+ );
204
+ }
205
+
206
+ /**
207
+ * Disconnects the client with an optional reason.
208
+ *
209
+ * @param reason - The reason for disconnection.
210
+ */
211
+ public async disconnect(reason?: string) {
212
+ this.#status = "reconnecting";
213
+ await this.#driver.disconnect(
214
+ this.#actor,
215
+ this,
216
+ this.__persist.connDriverState,
217
+ reason,
218
+ );
219
+ }
220
+
221
+ /**
222
+ * This method checks the connection's liveness by querying the driver for its ready state.
223
+ * If the connection is not closed, it updates the last liveness timestamp and returns `true`.
224
+ * Otherwise, it returns `false`.
225
+ * @internal
226
+ */
227
+ [CONNECTION_CHECK_LIVENESS_SYMBOL]() {
228
+ const readyState = this.#driver.getConnectionReadyState(this.#actor, this);
229
+
230
+ const isConnectionClosed =
231
+ readyState === ConnectionReadyState.CLOSED ||
232
+ readyState === ConnectionReadyState.CLOSING ||
233
+ readyState === undefined;
234
+
235
+ const newLastSeen = Date.now();
236
+ const newStatus = isConnectionClosed ? "reconnecting" : "connected";
237
+
238
+ logger().debug("liveness probe for connection", {
239
+ connId: this.id,
240
+ actorId: this.#actor.id,
241
+ readyState,
242
+
243
+ status: this.#status,
244
+ newStatus,
245
+
246
+ lastSeen: this.__persist.lastSeen,
247
+ currentTs: newLastSeen,
248
+ });
249
+
250
+ if (!isConnectionClosed) {
251
+ this.__persist.lastSeen = newLastSeen;
252
+ }
253
+
254
+ this.#status = newStatus;
255
+ return {
256
+ status: this.#status,
257
+ lastSeen: this.__persist.lastSeen,
258
+ };
259
+ }
260
+ }
@@ -0,0 +1,171 @@
1
+ import type { ActorKey } from "@/actor/mod";
2
+ import type { Client } from "@/client/client";
3
+ import type { Logger } from "@/common/log";
4
+ import type { Registry } from "@/registry/mod";
5
+ import type { Conn, ConnId } from "./connection";
6
+ import type { AnyDatabaseProvider, InferDatabaseClient } from "./database";
7
+ import type { ActorInstance, SaveStateOptions } from "./instance";
8
+ import type { Schedule } from "./schedule";
9
+
10
+ /**
11
+ * ActorContext class that provides access to actor methods and state
12
+ */
13
+ export class ActorContext<
14
+ TState,
15
+ TConnParams,
16
+ TConnState,
17
+ TVars,
18
+ TInput,
19
+ TAuthData,
20
+ TDatabase extends AnyDatabaseProvider,
21
+ > {
22
+ #actor: ActorInstance<
23
+ TState,
24
+ TConnParams,
25
+ TConnState,
26
+ TVars,
27
+ TInput,
28
+ TAuthData,
29
+ TDatabase
30
+ >;
31
+
32
+ constructor(
33
+ actor: ActorInstance<
34
+ TState,
35
+ TConnParams,
36
+ TConnState,
37
+ TVars,
38
+ TInput,
39
+ TAuthData,
40
+ TDatabase
41
+ >,
42
+ ) {
43
+ this.#actor = actor;
44
+ }
45
+
46
+ /**
47
+ * Get the actor state
48
+ */
49
+ get state(): TState {
50
+ return this.#actor.state;
51
+ }
52
+
53
+ /**
54
+ * Get the actor variables
55
+ */
56
+ get vars(): TVars {
57
+ return this.#actor.vars;
58
+ }
59
+
60
+ /**
61
+ * Broadcasts an event to all connected clients.
62
+ * @param name - The name of the event.
63
+ * @param args - The arguments to send with the event.
64
+ */
65
+ broadcast<Args extends Array<unknown>>(name: string, ...args: Args): void {
66
+ this.#actor._broadcast(name, ...args);
67
+ return;
68
+ }
69
+
70
+ /**
71
+ * Gets the logger instance.
72
+ */
73
+ get log(): Logger {
74
+ return this.#actor.log;
75
+ }
76
+
77
+ /**
78
+ * Gets actor ID.
79
+ */
80
+ get actorId(): string {
81
+ return this.#actor.id;
82
+ }
83
+
84
+ /**
85
+ * Gets the actor name.
86
+ */
87
+ get name(): string {
88
+ return this.#actor.name;
89
+ }
90
+
91
+ /**
92
+ * Gets the actor key.
93
+ */
94
+ get key(): ActorKey {
95
+ return this.#actor.key;
96
+ }
97
+
98
+ /**
99
+ * Gets the region.
100
+ */
101
+ get region(): string {
102
+ return this.#actor.region;
103
+ }
104
+
105
+ /**
106
+ * Gets the scheduler.
107
+ */
108
+ get schedule(): Schedule {
109
+ return this.#actor.schedule;
110
+ }
111
+
112
+ /**
113
+ * Gets the map of connections.
114
+ */
115
+ get conns(): Map<
116
+ ConnId,
117
+ Conn<TState, TConnParams, TConnState, TVars, TInput, TAuthData, TDatabase>
118
+ > {
119
+ return this.#actor.conns;
120
+ }
121
+
122
+ /**
123
+ * Returns the client for the given registry.
124
+ */
125
+ client<R extends Registry<any>>(): Client<R> {
126
+ return this.#actor.inlineClient as Client<R>;
127
+ }
128
+
129
+ /**
130
+ * Gets the database.
131
+ * @experimental
132
+ * @throws {DatabaseNotEnabled} If the database is not enabled.
133
+ */
134
+ get db(): InferDatabaseClient<TDatabase> {
135
+ return this.#actor.db;
136
+ }
137
+
138
+ /**
139
+ * Forces the state to get saved.
140
+ *
141
+ * @param opts - Options for saving the state.
142
+ */
143
+ async saveState(opts: SaveStateOptions): Promise<void> {
144
+ return this.#actor.saveState(opts);
145
+ }
146
+
147
+ /**
148
+ * Prevents the actor from sleeping until promise is complete.
149
+ */
150
+ waitUntil(promise: Promise<void>): void {
151
+ this.#actor._waitUntil(promise);
152
+ }
153
+
154
+ /**
155
+ * AbortSignal that fires when the actor is stopping.
156
+ */
157
+ get abortSignal(): AbortSignal {
158
+ return this.#actor.abortSignal;
159
+ }
160
+
161
+ /**
162
+ * Forces the actor to sleep.
163
+ *
164
+ * Not supported on all drivers.
165
+ *
166
+ * @experimental
167
+ */
168
+ sleep() {
169
+ this.#actor._sleep();
170
+ }
171
+ }
@@ -0,0 +1,23 @@
1
+ export type InferDatabaseClient<DBProvider extends AnyDatabaseProvider> =
2
+ DBProvider extends DatabaseProvider<any>
3
+ ? Awaited<ReturnType<DBProvider["createClient"]>>
4
+ : never;
5
+
6
+ export type AnyDatabaseProvider = DatabaseProvider<any> | undefined;
7
+
8
+ export type DatabaseProvider<DB extends { execute: (query: string) => any }> = {
9
+ /**
10
+ * Creates a new database client for the actor.
11
+ * The result is passed to the actor context as `c.db`.
12
+ * @experimental
13
+ */
14
+ createClient: (ctx: {
15
+ getDatabase: () => Promise<string | unknown>;
16
+ }) => Promise<DB>;
17
+ /**
18
+ * Runs before the actor has started.
19
+ * Use this to run migrations or other setup tasks.
20
+ * @experimental
21
+ */
22
+ onMigrate: (client: DB) => void | Promise<void>;
23
+ };
@@ -0,0 +1,86 @@
1
+ import type { RegistryConfig } from "@/registry/config";
2
+ import type { ActionContext } from "./action";
3
+ import type { Actions, ActorConfig } from "./config";
4
+ import type { ActorContext } from "./context";
5
+ import type { AnyDatabaseProvider } from "./database";
6
+ import { ActorInstance } from "./instance";
7
+
8
+ export type AnyActorDefinition = ActorDefinition<
9
+ any,
10
+ any,
11
+ any,
12
+ any,
13
+ any,
14
+ any,
15
+ any,
16
+ any
17
+ >;
18
+
19
+ /**
20
+ * Extracts the context type from an ActorDefinition
21
+ */
22
+ export type ActorContextOf<AD extends AnyActorDefinition> =
23
+ AD extends ActorDefinition<
24
+ infer S,
25
+ infer CP,
26
+ infer CS,
27
+ infer V,
28
+ infer I,
29
+ infer AD,
30
+ infer DB,
31
+ any
32
+ >
33
+ ? ActorContext<S, CP, CS, V, I, AD, DB>
34
+ : never;
35
+
36
+ /**
37
+ * Extracts the context type from an ActorDefinition
38
+ */
39
+ export type ActionContextOf<AD extends AnyActorDefinition> =
40
+ AD extends ActorDefinition<
41
+ infer S,
42
+ infer CP,
43
+ infer CS,
44
+ infer V,
45
+ infer I,
46
+ infer AD,
47
+ infer DB,
48
+ any
49
+ >
50
+ ? ActionContext<S, CP, CS, V, I, AD, DB>
51
+ : never;
52
+
53
+ export class ActorDefinition<
54
+ S,
55
+ CP,
56
+ CS,
57
+ V,
58
+ I,
59
+ AD,
60
+ DB extends AnyDatabaseProvider,
61
+ R extends Actions<S, CP, CS, V, I, AD, DB>,
62
+ > {
63
+ #config: ActorConfig<S, CP, CS, V, I, AD, DB>;
64
+
65
+ constructor(config: ActorConfig<S, CP, CS, V, I, AD, DB>) {
66
+ this.#config = config;
67
+ }
68
+
69
+ get config(): ActorConfig<S, CP, CS, V, I, AD, DB> {
70
+ return this.#config;
71
+ }
72
+
73
+ instantiate(): ActorInstance<S, CP, CS, V, I, AD, DB> {
74
+ return new ActorInstance(this.#config);
75
+ }
76
+ }
77
+
78
+ export function lookupInRegistry(
79
+ registryConfig: RegistryConfig,
80
+ name: string,
81
+ ): AnyActorDefinition {
82
+ // Build actor
83
+ const definition = registryConfig.use[name];
84
+ if (!definition) throw new Error(`no actor in registry for name ${name}`);
85
+ return definition;
86
+ }
@@ -0,0 +1,84 @@
1
+ import type { CachedSerializer } from "@/actor/protocol/serde";
2
+ import type { AnyClient } from "@/client/client";
3
+ import type { ManagerDriver } from "@/manager/driver";
4
+ import type { RegistryConfig } from "@/registry/config";
5
+ import type { RunConfig } from "@/registry/run-config";
6
+ import type * as protocol from "@/schemas/client-protocol/mod";
7
+ import type { AnyConn, ConnectionDriver } from "./connection";
8
+ import type { GenericConnGlobalState } from "./generic-conn-driver";
9
+ import type { AnyActorInstance } from "./instance";
10
+
11
+ export type ConnectionDriversMap = Record<ConnectionDriver, ConnDriver>;
12
+
13
+ export type ActorDriverBuilder = (
14
+ registryConfig: RegistryConfig,
15
+ runConfig: RunConfig,
16
+ managerDriver: ManagerDriver,
17
+ inlineClient: AnyClient,
18
+ ) => ActorDriver;
19
+
20
+ export interface ActorDriver {
21
+ //load(): Promise<LoadOutput>;
22
+
23
+ loadActor(actorId: string): Promise<AnyActorInstance>;
24
+
25
+ getGenericConnGlobalState(actorId: string): GenericConnGlobalState;
26
+
27
+ getContext(actorId: string): unknown;
28
+
29
+ readPersistedData(actorId: string): Promise<Uint8Array | undefined>;
30
+
31
+ /** ActorInstance ensure that only one instance of writePersistedData is called in parallel at a time. */
32
+ writePersistedData(actorId: string, data: Uint8Array): Promise<void>;
33
+
34
+ // Schedule
35
+ /** ActorInstance ensure that only one instance of setAlarm is called in parallel at a time. */
36
+ setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void>;
37
+
38
+ // Database
39
+ /**
40
+ * @experimental
41
+ * This is an experimental API that may change in the future.
42
+ */
43
+ getDatabase(actorId: string): Promise<unknown | undefined>;
44
+
45
+ sleep?(actorId: string): Promise<void>;
46
+
47
+ shutdown?(immediate: boolean): Promise<void>;
48
+ }
49
+
50
+ export enum ConnectionReadyState {
51
+ UNKNOWN = -1,
52
+ CONNECTING = 0,
53
+ OPEN = 1,
54
+ CLOSING = 2,
55
+ CLOSED = 3,
56
+ }
57
+
58
+ export interface ConnDriver<ConnDriverState = unknown> {
59
+ sendMessage?(
60
+ actor: AnyActorInstance,
61
+ conn: AnyConn,
62
+ state: ConnDriverState,
63
+ message: CachedSerializer<protocol.ToClient>,
64
+ ): void;
65
+
66
+ /**
67
+ * This returns a promise since we commonly disconnect at the end of a program, and not waiting will cause the socket to not close cleanly.
68
+ */
69
+ disconnect(
70
+ actor: AnyActorInstance,
71
+ conn: AnyConn,
72
+ state: ConnDriverState,
73
+ reason?: string,
74
+ ): Promise<void>;
75
+
76
+ /**
77
+ * Returns the ready state of the connection.
78
+ * This is used to determine if the connection is ready to send messages, or if the connection is stale.
79
+ */
80
+ getConnectionReadyState(
81
+ actor: AnyActorInstance,
82
+ conn: AnyConn,
83
+ ): ConnectionReadyState | undefined;
84
+ }