rivetkit 2.0.2 → 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 (246) hide show
  1. package/README.md +3 -5
  2. package/dist/schemas/actor-persist/v1.ts +225 -0
  3. package/dist/schemas/client-protocol/v1.ts +435 -0
  4. package/dist/schemas/file-system-driver/v1.ts +102 -0
  5. package/dist/tsup/actor/errors.cjs +77 -0
  6. package/dist/tsup/actor/errors.cjs.map +1 -0
  7. package/dist/tsup/actor/errors.d.cts +156 -0
  8. package/dist/tsup/actor/errors.d.ts +156 -0
  9. package/dist/tsup/actor/errors.js +77 -0
  10. package/dist/tsup/actor/errors.js.map +1 -0
  11. package/dist/tsup/chunk-3F2YSRJL.js +117 -0
  12. package/dist/tsup/chunk-3F2YSRJL.js.map +1 -0
  13. package/dist/tsup/chunk-4CXBCT26.cjs +250 -0
  14. package/dist/tsup/chunk-4CXBCT26.cjs.map +1 -0
  15. package/dist/tsup/chunk-4R73YDN3.cjs +20 -0
  16. package/dist/tsup/chunk-4R73YDN3.cjs.map +1 -0
  17. package/dist/tsup/chunk-6LJT3QRL.cjs +539 -0
  18. package/dist/tsup/chunk-6LJT3QRL.cjs.map +1 -0
  19. package/dist/tsup/chunk-GICQ3YCU.cjs +1792 -0
  20. package/dist/tsup/chunk-GICQ3YCU.cjs.map +1 -0
  21. package/dist/tsup/chunk-H26RP6GD.js +251 -0
  22. package/dist/tsup/chunk-H26RP6GD.js.map +1 -0
  23. package/dist/tsup/chunk-HI3HWJRC.js +20 -0
  24. package/dist/tsup/chunk-HI3HWJRC.js.map +1 -0
  25. package/dist/tsup/chunk-HLLF4B4Q.js +1792 -0
  26. package/dist/tsup/chunk-HLLF4B4Q.js.map +1 -0
  27. package/dist/tsup/chunk-IH6CKNDW.cjs +117 -0
  28. package/dist/tsup/chunk-IH6CKNDW.cjs.map +1 -0
  29. package/dist/tsup/chunk-LV2S3OU3.js +250 -0
  30. package/dist/tsup/chunk-LV2S3OU3.js.map +1 -0
  31. package/dist/tsup/chunk-LWNKVZG5.cjs +251 -0
  32. package/dist/tsup/chunk-LWNKVZG5.cjs.map +1 -0
  33. package/dist/tsup/chunk-NFU2BBT5.js +374 -0
  34. package/dist/tsup/chunk-NFU2BBT5.js.map +1 -0
  35. package/dist/tsup/chunk-PQY7KKTL.js +539 -0
  36. package/dist/tsup/chunk-PQY7KKTL.js.map +1 -0
  37. package/dist/tsup/chunk-QK72M5JB.js +45 -0
  38. package/dist/tsup/chunk-QK72M5JB.js.map +1 -0
  39. package/dist/tsup/chunk-QNNXFOQV.cjs +45 -0
  40. package/dist/tsup/chunk-QNNXFOQV.cjs.map +1 -0
  41. package/dist/tsup/chunk-SBHHJ6QS.cjs +374 -0
  42. package/dist/tsup/chunk-SBHHJ6QS.cjs.map +1 -0
  43. package/dist/tsup/chunk-TQ62L3X7.js +325 -0
  44. package/dist/tsup/chunk-TQ62L3X7.js.map +1 -0
  45. package/dist/tsup/chunk-VO7ZRVVD.cjs +6293 -0
  46. package/dist/tsup/chunk-VO7ZRVVD.cjs.map +1 -0
  47. package/dist/tsup/chunk-WHBPJNGW.cjs +325 -0
  48. package/dist/tsup/chunk-WHBPJNGW.cjs.map +1 -0
  49. package/dist/tsup/chunk-XJQHKJ4P.js +6293 -0
  50. package/dist/tsup/chunk-XJQHKJ4P.js.map +1 -0
  51. package/dist/tsup/client/mod.cjs +32 -0
  52. package/dist/tsup/client/mod.cjs.map +1 -0
  53. package/dist/tsup/client/mod.d.cts +20 -0
  54. package/dist/tsup/client/mod.d.ts +20 -0
  55. package/dist/tsup/client/mod.js +32 -0
  56. package/dist/tsup/client/mod.js.map +1 -0
  57. package/dist/tsup/common/log.cjs +21 -0
  58. package/dist/tsup/common/log.cjs.map +1 -0
  59. package/dist/tsup/common/log.d.cts +26 -0
  60. package/dist/tsup/common/log.d.ts +26 -0
  61. package/dist/tsup/common/log.js +21 -0
  62. package/dist/tsup/common/log.js.map +1 -0
  63. package/dist/tsup/common/websocket.cjs +10 -0
  64. package/dist/tsup/common/websocket.cjs.map +1 -0
  65. package/dist/tsup/common/websocket.d.cts +3 -0
  66. package/dist/tsup/common/websocket.d.ts +3 -0
  67. package/dist/tsup/common/websocket.js +10 -0
  68. package/dist/tsup/common/websocket.js.map +1 -0
  69. package/dist/tsup/common-CXCe7s6i.d.cts +218 -0
  70. package/dist/tsup/common-CXCe7s6i.d.ts +218 -0
  71. package/dist/tsup/connection-BI-6UIBJ.d.ts +2087 -0
  72. package/dist/tsup/connection-Dyd4NLGW.d.cts +2087 -0
  73. package/dist/tsup/driver-helpers/mod.cjs +30 -0
  74. package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
  75. package/dist/tsup/driver-helpers/mod.d.cts +17 -0
  76. package/dist/tsup/driver-helpers/mod.d.ts +17 -0
  77. package/dist/tsup/driver-helpers/mod.js +30 -0
  78. package/dist/tsup/driver-helpers/mod.js.map +1 -0
  79. package/dist/tsup/driver-test-suite/mod.cjs +3411 -0
  80. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
  81. package/dist/tsup/driver-test-suite/mod.d.cts +63 -0
  82. package/dist/tsup/driver-test-suite/mod.d.ts +63 -0
  83. package/dist/tsup/driver-test-suite/mod.js +3411 -0
  84. package/dist/tsup/driver-test-suite/mod.js.map +1 -0
  85. package/dist/tsup/inspector/mod.cjs +51 -0
  86. package/dist/tsup/inspector/mod.cjs.map +1 -0
  87. package/dist/tsup/inspector/mod.d.cts +408 -0
  88. package/dist/tsup/inspector/mod.d.ts +408 -0
  89. package/dist/tsup/inspector/mod.js +51 -0
  90. package/dist/tsup/inspector/mod.js.map +1 -0
  91. package/dist/tsup/mod.cjs +67 -0
  92. package/dist/tsup/mod.cjs.map +1 -0
  93. package/dist/tsup/mod.d.cts +105 -0
  94. package/dist/tsup/mod.d.ts +105 -0
  95. package/dist/tsup/mod.js +67 -0
  96. package/dist/tsup/mod.js.map +1 -0
  97. package/dist/tsup/router-endpoints-BTe_Rsdn.d.cts +65 -0
  98. package/dist/tsup/router-endpoints-CBSrKHmo.d.ts +65 -0
  99. package/dist/tsup/test/mod.cjs +17 -0
  100. package/dist/tsup/test/mod.cjs.map +1 -0
  101. package/dist/tsup/test/mod.d.cts +26 -0
  102. package/dist/tsup/test/mod.d.ts +26 -0
  103. package/dist/tsup/test/mod.js +17 -0
  104. package/dist/tsup/test/mod.js.map +1 -0
  105. package/dist/tsup/utils-fwx3o3K9.d.cts +18 -0
  106. package/dist/tsup/utils-fwx3o3K9.d.ts +18 -0
  107. package/dist/tsup/utils.cjs +26 -0
  108. package/dist/tsup/utils.cjs.map +1 -0
  109. package/dist/tsup/utils.d.cts +36 -0
  110. package/dist/tsup/utils.d.ts +36 -0
  111. package/dist/tsup/utils.js +26 -0
  112. package/dist/tsup/utils.js.map +1 -0
  113. package/package.json +208 -5
  114. package/src/actor/action.ts +178 -0
  115. package/src/actor/config.ts +497 -0
  116. package/src/actor/connection.ts +257 -0
  117. package/src/actor/context.ts +168 -0
  118. package/src/actor/database.ts +23 -0
  119. package/src/actor/definition.ts +82 -0
  120. package/src/actor/driver.ts +84 -0
  121. package/src/actor/errors.ts +422 -0
  122. package/src/actor/generic-conn-driver.ts +246 -0
  123. package/src/actor/instance.ts +1844 -0
  124. package/src/actor/keys.test.ts +266 -0
  125. package/src/actor/keys.ts +89 -0
  126. package/src/actor/log.ts +6 -0
  127. package/src/actor/mod.ts +108 -0
  128. package/src/actor/persisted.ts +42 -0
  129. package/src/actor/protocol/old.ts +297 -0
  130. package/src/actor/protocol/serde.ts +131 -0
  131. package/src/actor/router-endpoints.ts +688 -0
  132. package/src/actor/router.ts +265 -0
  133. package/src/actor/schedule.ts +17 -0
  134. package/src/actor/unstable-react.ts +110 -0
  135. package/src/actor/utils.ts +102 -0
  136. package/src/client/actor-common.ts +30 -0
  137. package/src/client/actor-conn.ts +865 -0
  138. package/src/client/actor-handle.ts +268 -0
  139. package/src/client/actor-query.ts +65 -0
  140. package/src/client/client.ts +554 -0
  141. package/src/client/config.ts +44 -0
  142. package/src/client/errors.ts +42 -0
  143. package/src/client/log.ts +5 -0
  144. package/src/client/mod.ts +60 -0
  145. package/src/client/raw-utils.ts +149 -0
  146. package/src/client/test.ts +44 -0
  147. package/src/client/utils.ts +152 -0
  148. package/src/common/eventsource-interface.ts +47 -0
  149. package/src/common/eventsource.ts +80 -0
  150. package/src/common/fake-event-source.ts +267 -0
  151. package/src/common/inline-websocket-adapter2.ts +454 -0
  152. package/src/common/log-levels.ts +27 -0
  153. package/src/common/log.ts +214 -0
  154. package/src/common/logfmt.ts +219 -0
  155. package/src/common/network.ts +2 -0
  156. package/src/common/router.ts +80 -0
  157. package/src/common/utils.ts +336 -0
  158. package/src/common/versioned-data.ts +95 -0
  159. package/src/common/websocket-interface.ts +49 -0
  160. package/src/common/websocket.ts +42 -0
  161. package/src/driver-helpers/mod.ts +22 -0
  162. package/src/driver-helpers/utils.ts +17 -0
  163. package/src/driver-test-suite/log.ts +5 -0
  164. package/src/driver-test-suite/mod.ts +239 -0
  165. package/src/driver-test-suite/tests/action-features.ts +136 -0
  166. package/src/driver-test-suite/tests/actor-conn-state.ts +249 -0
  167. package/src/driver-test-suite/tests/actor-conn.ts +349 -0
  168. package/src/driver-test-suite/tests/actor-driver.ts +25 -0
  169. package/src/driver-test-suite/tests/actor-error-handling.ts +158 -0
  170. package/src/driver-test-suite/tests/actor-handle.ts +292 -0
  171. package/src/driver-test-suite/tests/actor-inline-client.ts +152 -0
  172. package/src/driver-test-suite/tests/actor-inspector.ts +570 -0
  173. package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
  174. package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
  175. package/src/driver-test-suite/tests/actor-schedule.ts +108 -0
  176. package/src/driver-test-suite/tests/actor-sleep.ts +413 -0
  177. package/src/driver-test-suite/tests/actor-state.ts +54 -0
  178. package/src/driver-test-suite/tests/actor-vars.ts +93 -0
  179. package/src/driver-test-suite/tests/manager-driver.ts +367 -0
  180. package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -0
  181. package/src/driver-test-suite/tests/raw-http-request-properties.ts +414 -0
  182. package/src/driver-test-suite/tests/raw-http.ts +347 -0
  183. package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -0
  184. package/src/driver-test-suite/tests/raw-websocket.ts +484 -0
  185. package/src/driver-test-suite/tests/request-access.ts +230 -0
  186. package/src/driver-test-suite/utils.ts +71 -0
  187. package/src/drivers/default.ts +34 -0
  188. package/src/drivers/engine/actor-driver.ts +369 -0
  189. package/src/drivers/engine/config.ts +31 -0
  190. package/src/drivers/engine/kv.ts +3 -0
  191. package/src/drivers/engine/log.ts +5 -0
  192. package/src/drivers/engine/mod.ts +35 -0
  193. package/src/drivers/file-system/actor.ts +91 -0
  194. package/src/drivers/file-system/global-state.ts +686 -0
  195. package/src/drivers/file-system/log.ts +5 -0
  196. package/src/drivers/file-system/manager.ts +329 -0
  197. package/src/drivers/file-system/mod.ts +48 -0
  198. package/src/drivers/file-system/utils.ts +109 -0
  199. package/src/globals.d.ts +6 -0
  200. package/src/inspector/actor.ts +298 -0
  201. package/src/inspector/config.ts +88 -0
  202. package/src/inspector/log.ts +5 -0
  203. package/src/inspector/manager.ts +86 -0
  204. package/src/inspector/mod.ts +2 -0
  205. package/src/inspector/protocol/actor.ts +10 -0
  206. package/src/inspector/protocol/common.ts +196 -0
  207. package/src/inspector/protocol/manager.ts +10 -0
  208. package/src/inspector/protocol/mod.ts +2 -0
  209. package/src/inspector/utils.ts +76 -0
  210. package/src/manager/driver.ts +88 -0
  211. package/src/manager/hono-websocket-adapter.ts +342 -0
  212. package/src/manager/log.ts +5 -0
  213. package/src/manager/mod.ts +2 -0
  214. package/src/manager/protocol/mod.ts +24 -0
  215. package/src/manager/protocol/query.ts +89 -0
  216. package/src/manager/router.ts +412 -0
  217. package/src/manager-api/routes/actors-create.ts +16 -0
  218. package/src/manager-api/routes/actors-delete.ts +4 -0
  219. package/src/manager-api/routes/actors-get-by-id.ts +7 -0
  220. package/src/manager-api/routes/actors-get-or-create-by-id.ts +29 -0
  221. package/src/manager-api/routes/actors-get.ts +7 -0
  222. package/src/manager-api/routes/common.ts +18 -0
  223. package/src/mod.ts +18 -0
  224. package/src/registry/config.ts +32 -0
  225. package/src/registry/log.ts +5 -0
  226. package/src/registry/mod.ts +157 -0
  227. package/src/registry/run-config.ts +52 -0
  228. package/src/registry/serve.ts +52 -0
  229. package/src/remote-manager-driver/actor-http-client.ts +72 -0
  230. package/src/remote-manager-driver/actor-websocket-client.ts +63 -0
  231. package/src/remote-manager-driver/api-endpoints.ts +79 -0
  232. package/src/remote-manager-driver/api-utils.ts +43 -0
  233. package/src/remote-manager-driver/log.ts +5 -0
  234. package/src/remote-manager-driver/mod.ts +274 -0
  235. package/src/remote-manager-driver/ws-proxy.ts +180 -0
  236. package/src/schemas/actor-persist/mod.ts +1 -0
  237. package/src/schemas/actor-persist/versioned.ts +25 -0
  238. package/src/schemas/client-protocol/mod.ts +1 -0
  239. package/src/schemas/client-protocol/versioned.ts +63 -0
  240. package/src/schemas/file-system-driver/mod.ts +1 -0
  241. package/src/schemas/file-system-driver/versioned.ts +28 -0
  242. package/src/serde.ts +90 -0
  243. package/src/test/config.ts +16 -0
  244. package/src/test/log.ts +5 -0
  245. package/src/test/mod.ts +154 -0
  246. package/src/utils.ts +172 -0
@@ -0,0 +1,497 @@
1
+ import { z } from "zod";
2
+ import type { UniversalWebSocket } from "@/common/websocket-interface";
3
+ import type { ActionContext } from "./action";
4
+ import type { Conn } from "./connection";
5
+ import type { ActorContext } from "./context";
6
+ import type { AnyDatabaseProvider } from "./database";
7
+
8
+ export type InitContext = ActorContext<
9
+ undefined,
10
+ undefined,
11
+ undefined,
12
+ undefined,
13
+ undefined,
14
+ undefined
15
+ >;
16
+
17
+ export interface ActorTypes<
18
+ TState,
19
+ TConnParams,
20
+ TConnState,
21
+ TVars,
22
+ TInput,
23
+ TDatabase extends AnyDatabaseProvider,
24
+ > {
25
+ state?: TState;
26
+ connParams?: TConnParams;
27
+ connState?: TConnState;
28
+ vars?: TVars;
29
+ input?: TInput;
30
+ database?: TDatabase;
31
+ }
32
+
33
+ // This schema is used to validate the input at runtime. The generic types are defined below in `ActorConfig`.
34
+ //
35
+ // We don't use Zod generics with `z.custom` because:
36
+ // (a) there seems to be a weird bug in either Zod, tsup, or TSC that causese external packages to have different types from `z.infer` than from within the same package and
37
+ // (b) it makes the type definitions incredibly difficult to read as opposed to vanilla TypeScript.
38
+ export const ActorConfigSchema = z
39
+ .object({
40
+ onCreate: z.function().optional(),
41
+ onStart: z.function().optional(),
42
+ onStop: z.function().optional(),
43
+ onStateChange: z.function().optional(),
44
+ onBeforeConnect: z.function().optional(),
45
+ onConnect: z.function().optional(),
46
+ onDisconnect: z.function().optional(),
47
+ onBeforeActionResponse: z.function().optional(),
48
+ onFetch: z.function().optional(),
49
+ onWebSocket: z.function().optional(),
50
+ actions: z.record(z.function()).default({}),
51
+ state: z.any().optional(),
52
+ createState: z.function().optional(),
53
+ connState: z.any().optional(),
54
+ createConnState: z.function().optional(),
55
+ vars: z.any().optional(),
56
+ db: z.any().optional(),
57
+ createVars: z.function().optional(),
58
+ options: z
59
+ .object({
60
+ createVarsTimeout: z.number().positive().default(5000),
61
+ createConnStateTimeout: z.number().positive().default(5000),
62
+ onConnectTimeout: z.number().positive().default(5000),
63
+ // This must be less than ACTOR_STOP_THRESHOLD_MS
64
+ onStopTimeout: z.number().positive().default(5000),
65
+ stateSaveInterval: z.number().positive().default(10_000),
66
+ actionTimeout: z.number().positive().default(60_000),
67
+ // Max time to wait for waitUntil background promises during shutdown
68
+ waitUntilTimeout: z.number().positive().default(15_000),
69
+ connectionLivenessTimeout: z.number().positive().default(2500),
70
+ connectionLivenessInterval: z.number().positive().default(5000),
71
+ noSleep: z.boolean().default(false),
72
+ sleepTimeout: z.number().positive().default(30_000),
73
+ })
74
+ .strict()
75
+ .default({}),
76
+ })
77
+ .strict()
78
+ .refine(
79
+ (data) => !(data.state !== undefined && data.createState !== undefined),
80
+ {
81
+ message: "Cannot define both 'state' and 'createState'",
82
+ path: ["state"],
83
+ },
84
+ )
85
+ .refine(
86
+ (data) =>
87
+ !(data.connState !== undefined && data.createConnState !== undefined),
88
+ {
89
+ message: "Cannot define both 'connState' and 'createConnState'",
90
+ path: ["connState"],
91
+ },
92
+ )
93
+ .refine(
94
+ (data) => !(data.vars !== undefined && data.createVars !== undefined),
95
+ {
96
+ message: "Cannot define both 'vars' and 'createVars'",
97
+ path: ["vars"],
98
+ },
99
+ );
100
+
101
+ export interface OnConnectOptions {
102
+ /**
103
+ * The request object associated with the connection.
104
+ *
105
+ * @experimental
106
+ */
107
+ request?: Request;
108
+ }
109
+
110
+ // Creates state config
111
+ //
112
+ // This must have only one or the other or else TState will not be able to be inferred
113
+ //
114
+ // Data returned from this handler will be available on `c.state`.
115
+ type CreateState<TState, TConnParams, TConnState, TVars, TInput, TDatabase> =
116
+ | { state: TState }
117
+ | {
118
+ createState: (c: InitContext, input: TInput) => TState | Promise<TState>;
119
+ }
120
+ | Record<never, never>;
121
+
122
+ // Creates connection state config
123
+ //
124
+ // This must have only one or the other or else TState will not be able to be inferred
125
+ //
126
+ // Data returned from this handler will be available on `c.conn.state`.
127
+ type CreateConnState<
128
+ TState,
129
+ TConnParams,
130
+ TConnState,
131
+ TVars,
132
+ TInput,
133
+ TDatabase,
134
+ > =
135
+ | { connState: TConnState }
136
+ | {
137
+ createConnState: (
138
+ c: InitContext,
139
+ opts: OnConnectOptions,
140
+ params: TConnParams,
141
+ ) => TConnState | Promise<TConnState>;
142
+ }
143
+ | Record<never, never>;
144
+
145
+ // Creates vars config
146
+ //
147
+ // This must have only one or the other or else TState will not be able to be inferred
148
+ /**
149
+ * @experimental
150
+ */
151
+ type CreateVars<TState, TConnParams, TConnState, TVars, TInput, TDatabase> =
152
+ | {
153
+ /**
154
+ * @experimental
155
+ */
156
+ vars: TVars;
157
+ }
158
+ | {
159
+ /**
160
+ * @experimental
161
+ */
162
+ createVars: (c: InitContext, driverCtx: any) => TVars | Promise<TVars>;
163
+ }
164
+ | Record<never, never>;
165
+
166
+ export interface Actions<
167
+ TState,
168
+ TConnParams,
169
+ TConnState,
170
+ TVars,
171
+ TInput,
172
+ TDatabase extends AnyDatabaseProvider,
173
+ > {
174
+ [Action: string]: (
175
+ c: ActionContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
176
+ ...args: any[]
177
+ ) => any;
178
+ }
179
+
180
+ //export type ActorConfig<TState, TConnParams, TConnState, TVars, TInput, TAuthData> = BaseActorConfig<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
181
+ // ActorConfigLifecycle<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
182
+ // CreateState<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
183
+ // CreateConnState<TState, TConnParams, TConnState, TVars, TInput, TAuthData>;
184
+
185
+ /**
186
+ * @experimental
187
+ */
188
+ export type AuthIntent = "get" | "create" | "connect" | "action" | "message";
189
+
190
+ interface BaseActorConfig<
191
+ TState,
192
+ TConnParams,
193
+ TConnState,
194
+ TVars,
195
+ TInput,
196
+ TDatabase extends AnyDatabaseProvider,
197
+ TActions extends Actions<
198
+ TState,
199
+ TConnParams,
200
+ TConnState,
201
+ TVars,
202
+ TInput,
203
+ TDatabase
204
+ >,
205
+ > {
206
+ /**
207
+ * Called when the actor is first initialized.
208
+ *
209
+ * Use this hook to initialize your actor's state.
210
+ * This is called before any other lifecycle hooks.
211
+ */
212
+ onCreate?: (
213
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
214
+ input: TInput,
215
+ ) => void | Promise<void>;
216
+
217
+ /**
218
+ * Called when the actor is started and ready to receive connections and action.
219
+ *
220
+ * Use this hook to initialize resources needed for the actor's operation
221
+ * (timers, external connections, etc.)
222
+ *
223
+ * @returns Void or a Promise that resolves when startup is complete
224
+ */
225
+ onStart?: (
226
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
227
+ ) => void | Promise<void>;
228
+
229
+ /**
230
+ * Called when the actor is stopping or sleeping.
231
+ *
232
+ * Use this hook to clean up resources, save state, or perform
233
+ * any shutdown operations before the actor sleeps or stops.
234
+ *
235
+ * Not supported on all platforms.
236
+ *
237
+ * @returns Void or a Promise that resolves when shutdown is complete
238
+ */
239
+ onStop?: (
240
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
241
+ ) => void | Promise<void>;
242
+
243
+ /**
244
+ * Called when the actor's state changes.
245
+ *
246
+ * Use this hook to react to state changes, such as updating
247
+ * external systems or triggering events.
248
+ *
249
+ * State changes made within this hook will NOT trigger
250
+ * another onStateChange call, preventing infinite recursion.
251
+ *
252
+ * @param newState The updated state
253
+ */
254
+ onStateChange?: (
255
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
256
+ newState: TState,
257
+ ) => void;
258
+
259
+ /**
260
+ * Called before a client connects to the actor.
261
+ *
262
+ * Use this hook to determine if a connection should be accepted
263
+ * and to initialize connection-specific state.
264
+ *
265
+ * @param opts Connection parameters including client-provided data
266
+ * @returns The initial connection state or a Promise that resolves to it
267
+ * @throws Throw an error to reject the connection
268
+ */
269
+ onBeforeConnect?: (
270
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
271
+ opts: OnConnectOptions,
272
+ params: TConnParams,
273
+ ) => void | Promise<void>;
274
+
275
+ /**
276
+ * Called when a client successfully connects to the actor.
277
+ *
278
+ * Use this hook to perform actions when a connection is established,
279
+ * such as sending initial data or updating the actor's state.
280
+ *
281
+ * @param conn The connection object
282
+ * @returns Void or a Promise that resolves when connection handling is complete
283
+ */
284
+ onConnect?: (
285
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
286
+ conn: Conn<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
287
+ ) => void | Promise<void>;
288
+
289
+ /**
290
+ * Called when a client disconnects from the actor.
291
+ *
292
+ * Use this hook to clean up resources associated with the connection
293
+ * or update the actor's state.
294
+ *
295
+ * @param conn The connection that is being closed
296
+ * @returns Void or a Promise that resolves when disconnect handling is complete
297
+ */
298
+ onDisconnect?: (
299
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
300
+ conn: Conn<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
301
+ ) => void | Promise<void>;
302
+
303
+ /**
304
+ * Called before sending an action response to the client.
305
+ *
306
+ * Use this hook to modify or transform the output of an action before it's sent
307
+ * to the client. This is useful for formatting responses, adding metadata,
308
+ * or applying transformations to the output.
309
+ *
310
+ * @param name The name of the action that was called
311
+ * @param args The arguments that were passed to the action
312
+ * @param output The output that will be sent to the client
313
+ * @returns The modified output to send to the client
314
+ */
315
+ onBeforeActionResponse?: <Out>(
316
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
317
+ name: string,
318
+ args: unknown[],
319
+ output: Out,
320
+ ) => Out | Promise<Out>;
321
+
322
+ /**
323
+ * Called when a raw HTTP request is made to the actor.
324
+ *
325
+ * This handler receives raw HTTP requests made to `/actors/{actorName}/http/*` endpoints.
326
+ * Use this hook to handle custom HTTP patterns, REST APIs, or other HTTP-based protocols.
327
+ *
328
+ * @param request The raw HTTP request object
329
+ * @returns A Response object to send back, or void to continue with default routing
330
+ */
331
+ onFetch?: (
332
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
333
+ request: Request,
334
+ opts: {},
335
+ ) => Response | Promise<Response>;
336
+
337
+ /**
338
+ * Called when a raw WebSocket connection is established to the actor.
339
+ *
340
+ * This handler receives WebSocket connections made to `/actors/{actorName}/websocket/*` endpoints.
341
+ * Use this hook to handle custom WebSocket protocols, binary streams, or other WebSocket-based communication.
342
+ *
343
+ * @param websocket The raw WebSocket connection
344
+ * @param request The original HTTP upgrade request
345
+ */
346
+ onWebSocket?: (
347
+ c: ActorContext<TState, TConnParams, TConnState, TVars, TInput, TDatabase>,
348
+ websocket: UniversalWebSocket,
349
+ opts: { request: Request },
350
+ ) => void | Promise<void>;
351
+
352
+ actions: TActions;
353
+ }
354
+
355
+ type ActorDatabaseConfig<TDatabase extends AnyDatabaseProvider> =
356
+ | {
357
+ /**
358
+ * @experimental
359
+ */
360
+ db: TDatabase;
361
+ }
362
+ | Record<never, never>;
363
+
364
+ // 1. Infer schema
365
+ // 2. Omit keys that we'll manually define (because of generics)
366
+ // 3. Define our own types that have generic constraints
367
+ export type ActorConfig<
368
+ TState,
369
+ TConnParams,
370
+ TConnState,
371
+ TVars,
372
+ TInput,
373
+ TDatabase extends AnyDatabaseProvider,
374
+ > = Omit<
375
+ z.infer<typeof ActorConfigSchema>,
376
+ | "actions"
377
+ | "onCreate"
378
+ | "onStart"
379
+ | "onStateChange"
380
+ | "onBeforeConnect"
381
+ | "onConnect"
382
+ | "onDisconnect"
383
+ | "onBeforeActionResponse"
384
+ | "onFetch"
385
+ | "onWebSocket"
386
+ | "state"
387
+ | "createState"
388
+ | "connState"
389
+ | "createConnState"
390
+ | "vars"
391
+ | "createVars"
392
+ | "db"
393
+ > &
394
+ BaseActorConfig<
395
+ TState,
396
+ TConnParams,
397
+ TConnState,
398
+ TVars,
399
+ TInput,
400
+ TDatabase,
401
+ Actions<TState, TConnParams, TConnState, TVars, TInput, TDatabase>
402
+ > &
403
+ CreateState<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
404
+ CreateConnState<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
405
+ CreateVars<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
406
+ ActorDatabaseConfig<TDatabase>;
407
+
408
+ // See description on `ActorConfig`
409
+ export type ActorConfigInput<
410
+ TState = undefined,
411
+ TConnParams = undefined,
412
+ TConnState = undefined,
413
+ TVars = undefined,
414
+ TInput = undefined,
415
+ TDatabase extends AnyDatabaseProvider = undefined,
416
+ TActions extends Actions<
417
+ TState,
418
+ TConnParams,
419
+ TConnState,
420
+ TVars,
421
+ TInput,
422
+ TDatabase
423
+ > = Record<never, never>,
424
+ > = {
425
+ types?: ActorTypes<TState, TConnParams, TConnState, TVars, TInput, TDatabase>;
426
+ } & Omit<
427
+ z.input<typeof ActorConfigSchema>,
428
+ | "actions"
429
+ | "onCreate"
430
+ | "onStart"
431
+ | "onStop"
432
+ | "onStateChange"
433
+ | "onBeforeConnect"
434
+ | "onConnect"
435
+ | "onDisconnect"
436
+ | "onBeforeActionResponse"
437
+ | "onFetch"
438
+ | "onWebSocket"
439
+ | "state"
440
+ | "createState"
441
+ | "connState"
442
+ | "createConnState"
443
+ | "vars"
444
+ | "createVars"
445
+ | "db"
446
+ > &
447
+ BaseActorConfig<
448
+ TState,
449
+ TConnParams,
450
+ TConnState,
451
+ TVars,
452
+ TInput,
453
+ TDatabase,
454
+ TActions
455
+ > &
456
+ CreateState<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
457
+ CreateConnState<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
458
+ CreateVars<TState, TConnParams, TConnState, TVars, TInput, TDatabase> &
459
+ ActorDatabaseConfig<TDatabase>;
460
+
461
+ // For testing type definitions:
462
+ export function test<
463
+ TState,
464
+ TConnParams,
465
+ TConnState,
466
+ TVars,
467
+ TInput,
468
+ TDatabase extends AnyDatabaseProvider,
469
+ TActions extends Actions<
470
+ TState,
471
+ TConnParams,
472
+ TConnState,
473
+ TVars,
474
+ TInput,
475
+ TDatabase
476
+ >,
477
+ >(
478
+ input: ActorConfigInput<
479
+ TState,
480
+ TConnParams,
481
+ TConnState,
482
+ TVars,
483
+ TInput,
484
+ TDatabase,
485
+ TActions
486
+ >,
487
+ ): ActorConfig<TState, TConnParams, TConnState, TVars, TInput, TDatabase> {
488
+ const config = ActorConfigSchema.parse(input) as ActorConfig<
489
+ TState,
490
+ TConnParams,
491
+ TConnState,
492
+ TVars,
493
+ TInput,
494
+ TDatabase
495
+ >;
496
+ return config;
497
+ }