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,765 @@
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
+ undefined
16
+ >;
17
+
18
+ export interface ActorTypes<
19
+ TState,
20
+ TConnParams,
21
+ TConnState,
22
+ TVars,
23
+ TInput,
24
+ TAuthData,
25
+ TDatabase extends AnyDatabaseProvider,
26
+ > {
27
+ state?: TState;
28
+ connParams?: TConnParams;
29
+ connState?: TConnState;
30
+ vars?: TVars;
31
+ input?: TInput;
32
+ authData?: TAuthData;
33
+ database?: TDatabase;
34
+ }
35
+
36
+ // This schema is used to validate the input at runtime. The generic types are defined below in `ActorConfig`.
37
+ //
38
+ // We don't use Zod generics with `z.custom` because:
39
+ // (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
40
+ // (b) it makes the type definitions incredibly difficult to read as opposed to vanilla TypeScript.
41
+ export const ActorConfigSchema = z
42
+ .object({
43
+ onAuth: z.function().optional(),
44
+ onCreate: z.function().optional(),
45
+ onStart: z.function().optional(),
46
+ onStop: z.function().optional(),
47
+ onStateChange: z.function().optional(),
48
+ onBeforeConnect: z.function().optional(),
49
+ onConnect: z.function().optional(),
50
+ onDisconnect: z.function().optional(),
51
+ onBeforeActionResponse: z.function().optional(),
52
+ onFetch: z.function().optional(),
53
+ onWebSocket: z.function().optional(),
54
+ actions: z.record(z.function()).default({}),
55
+ state: z.any().optional(),
56
+ createState: z.function().optional(),
57
+ connState: z.any().optional(),
58
+ createConnState: z.function().optional(),
59
+ vars: z.any().optional(),
60
+ db: z.any().optional(),
61
+ createVars: z.function().optional(),
62
+ options: z
63
+ .object({
64
+ createVarsTimeout: z.number().positive().default(5000),
65
+ createConnStateTimeout: z.number().positive().default(5000),
66
+ onConnectTimeout: z.number().positive().default(5000),
67
+ // This must be less than ACTOR_STOP_THRESHOLD_MS
68
+ onStopTimeout: z.number().positive().default(5000),
69
+ stateSaveInterval: z.number().positive().default(10_000),
70
+ actionTimeout: z.number().positive().default(60_000),
71
+ // Max time to wait for waitUntil background promises during shutdown
72
+ waitUntilTimeout: z.number().positive().default(15_000),
73
+ connectionLivenessTimeout: z.number().positive().default(2500),
74
+ connectionLivenessInterval: z.number().positive().default(5000),
75
+ noSleep: z.boolean().default(false),
76
+ sleepTimeout: z.number().positive().default(30_000),
77
+ })
78
+ .strict()
79
+ .default({}),
80
+ })
81
+ .strict()
82
+ .refine(
83
+ (data) => !(data.state !== undefined && data.createState !== undefined),
84
+ {
85
+ message: "Cannot define both 'state' and 'createState'",
86
+ path: ["state"],
87
+ },
88
+ )
89
+ .refine(
90
+ (data) =>
91
+ !(data.connState !== undefined && data.createConnState !== undefined),
92
+ {
93
+ message: "Cannot define both 'connState' and 'createConnState'",
94
+ path: ["connState"],
95
+ },
96
+ )
97
+ .refine(
98
+ (data) => !(data.vars !== undefined && data.createVars !== undefined),
99
+ {
100
+ message: "Cannot define both 'vars' and 'createVars'",
101
+ path: ["vars"],
102
+ },
103
+ );
104
+
105
+ export interface OnConnectOptions {
106
+ /**
107
+ * The request object associated with the connection.
108
+ *
109
+ * @experimental
110
+ */
111
+ request?: Request;
112
+ }
113
+
114
+ // Creates state config
115
+ //
116
+ // This must have only one or the other or else TState will not be able to be inferred
117
+ //
118
+ // Data returned from this handler will be available on `c.state`.
119
+ type CreateState<
120
+ TState,
121
+ TConnParams,
122
+ TConnState,
123
+ TVars,
124
+ TInput,
125
+ TAuthData,
126
+ TDatabase,
127
+ > =
128
+ | { state: TState }
129
+ | {
130
+ createState: (c: InitContext, input: TInput) => TState | Promise<TState>;
131
+ }
132
+ | Record<never, never>;
133
+
134
+ // Creates connection state config
135
+ //
136
+ // This must have only one or the other or else TState will not be able to be inferred
137
+ //
138
+ // Data returned from this handler will be available on `c.conn.state`.
139
+ type CreateConnState<
140
+ TState,
141
+ TConnParams,
142
+ TConnState,
143
+ TVars,
144
+ TInput,
145
+ TAuthData,
146
+ TDatabase,
147
+ > =
148
+ | { connState: TConnState }
149
+ | {
150
+ createConnState: (
151
+ c: InitContext,
152
+ opts: OnConnectOptions,
153
+ params: TConnParams,
154
+ ) => TConnState | Promise<TConnState>;
155
+ }
156
+ | Record<never, never>;
157
+
158
+ // Creates vars config
159
+ //
160
+ // This must have only one or the other or else TState will not be able to be inferred
161
+ /**
162
+ * @experimental
163
+ */
164
+ type CreateVars<
165
+ TState,
166
+ TConnParams,
167
+ TConnState,
168
+ TVars,
169
+ TInput,
170
+ TAuthData,
171
+ TDatabase,
172
+ > =
173
+ | {
174
+ /**
175
+ * @experimental
176
+ */
177
+ vars: TVars;
178
+ }
179
+ | {
180
+ /**
181
+ * @experimental
182
+ */
183
+ createVars: (c: InitContext, driverCtx: any) => TVars | Promise<TVars>;
184
+ }
185
+ | Record<never, never>;
186
+
187
+ // Creates auth config
188
+ //
189
+ // This must have only one or the other or else TAuthData will not be able to be inferred
190
+ type OnAuth<TConnParams, TAuthData> =
191
+ | {
192
+ /**
193
+ * Called on the HTTP server before clients can interact with the actor.
194
+ *
195
+ * Only called for public endpoints. Calls to actors from within the backend
196
+ * do not trigger this handler.
197
+ *
198
+ * Data returned from this handler will be available on `c.conn.auth`.
199
+ *
200
+ * This function is required for any public HTTP endpoint access. Use this hook
201
+ * to validate client credentials and return authentication data that will be
202
+ * available on connections. This runs on the HTTP server (not the actor)
203
+ * in order to reduce load on the actor & prevent denial of server attacks
204
+ * against individual actors.
205
+ *
206
+ * If you need access to actor state for authentication, use onBeforeConnect
207
+ * with an empty onAuth function instead.
208
+ *
209
+ * You can also provide your own authentication middleware on your router if you
210
+ * choose, then use onAuth to pass the authentication data (e.g. user ID) to the
211
+ * actor itself.
212
+ *
213
+ * @param opts Authentication options including request and intent
214
+ * @returns Authentication data to attach to connections (must be serializable)
215
+ * @throws Throw an error to deny access to the actor
216
+ */
217
+ onAuth: (
218
+ opts: OnAuthOptions,
219
+ params: TConnParams,
220
+ ) => TAuthData | Promise<TAuthData>;
221
+ }
222
+ | Record<never, never>;
223
+
224
+ export interface Actions<
225
+ TState,
226
+ TConnParams,
227
+ TConnState,
228
+ TVars,
229
+ TInput,
230
+ TAuthData,
231
+ TDatabase extends AnyDatabaseProvider,
232
+ > {
233
+ [Action: string]: (
234
+ c: ActionContext<
235
+ TState,
236
+ TConnParams,
237
+ TConnState,
238
+ TVars,
239
+ TInput,
240
+ TAuthData,
241
+ TDatabase
242
+ >,
243
+ ...args: any[]
244
+ ) => any;
245
+ }
246
+
247
+ //export type ActorConfig<TState, TConnParams, TConnState, TVars, TInput, TAuthData> = BaseActorConfig<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
248
+ // ActorConfigLifecycle<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
249
+ // CreateState<TState, TConnParams, TConnState, TVars, TInput, TAuthData> &
250
+ // CreateConnState<TState, TConnParams, TConnState, TVars, TInput, TAuthData>;
251
+
252
+ /**
253
+ * @experimental
254
+ */
255
+ export type AuthIntent = "get" | "create" | "connect" | "action" | "message";
256
+
257
+ export interface OnAuthOptions {
258
+ request: Request;
259
+ /**
260
+ * @experimental
261
+ */
262
+ intents: Set<AuthIntent>;
263
+ }
264
+
265
+ interface BaseActorConfig<
266
+ TState,
267
+ TConnParams,
268
+ TConnState,
269
+ TVars,
270
+ TInput,
271
+ TAuthData,
272
+ TDatabase extends AnyDatabaseProvider,
273
+ TActions extends Actions<
274
+ TState,
275
+ TConnParams,
276
+ TConnState,
277
+ TVars,
278
+ TInput,
279
+ TAuthData,
280
+ TDatabase
281
+ >,
282
+ > {
283
+ /**
284
+ * Called when the actor is first initialized.
285
+ *
286
+ * Use this hook to initialize your actor's state.
287
+ * This is called before any other lifecycle hooks.
288
+ */
289
+ onCreate?: (
290
+ c: ActorContext<
291
+ TState,
292
+ TConnParams,
293
+ TConnState,
294
+ TVars,
295
+ TInput,
296
+ TAuthData,
297
+ TDatabase
298
+ >,
299
+ input: TInput,
300
+ ) => void | Promise<void>;
301
+
302
+ /**
303
+ * Called when the actor is started and ready to receive connections and action.
304
+ *
305
+ * Use this hook to initialize resources needed for the actor's operation
306
+ * (timers, external connections, etc.)
307
+ *
308
+ * @returns Void or a Promise that resolves when startup is complete
309
+ */
310
+ onStart?: (
311
+ c: ActorContext<
312
+ TState,
313
+ TConnParams,
314
+ TConnState,
315
+ TVars,
316
+ TInput,
317
+ TAuthData,
318
+ TDatabase
319
+ >,
320
+ ) => void | Promise<void>;
321
+
322
+ /**
323
+ * Called when the actor is stopping or sleeping.
324
+ *
325
+ * Use this hook to clean up resources, save state, or perform
326
+ * any shutdown operations before the actor sleeps or stops.
327
+ *
328
+ * Not supported on all platforms.
329
+ *
330
+ * @returns Void or a Promise that resolves when shutdown is complete
331
+ */
332
+ onStop?: (
333
+ c: ActorContext<
334
+ TState,
335
+ TConnParams,
336
+ TConnState,
337
+ TVars,
338
+ TInput,
339
+ TAuthData,
340
+ TDatabase
341
+ >,
342
+ ) => void | Promise<void>;
343
+
344
+ /**
345
+ * Called when the actor's state changes.
346
+ *
347
+ * Use this hook to react to state changes, such as updating
348
+ * external systems or triggering events.
349
+ *
350
+ * State changes made within this hook will NOT trigger
351
+ * another onStateChange call, preventing infinite recursion.
352
+ *
353
+ * @param newState The updated state
354
+ */
355
+ onStateChange?: (
356
+ c: ActorContext<
357
+ TState,
358
+ TConnParams,
359
+ TConnState,
360
+ TVars,
361
+ TInput,
362
+ TAuthData,
363
+ TDatabase
364
+ >,
365
+ newState: TState,
366
+ ) => void;
367
+
368
+ /**
369
+ * Called before a client connects to the actor.
370
+ *
371
+ * Unlike onAuth, this handler is still called for both internal and
372
+ * public clients.
373
+ *
374
+ * Use this hook to determine if a connection should be accepted
375
+ * and to initialize connection-specific state. Unlike onAuth, this runs
376
+ * on the actor and has access to actor state, but uses slightly
377
+ * more resources on the actor rather than authenticating with onAuth.
378
+ *
379
+ * For authentication without actor state access, prefer onAuth.
380
+ *
381
+ * For authentication with actor state, use onBeforeConnect with an empty
382
+ * onAuth handler.
383
+ *
384
+ * @param opts Connection parameters including client-provided data
385
+ * @returns The initial connection state or a Promise that resolves to it
386
+ * @throws Throw an error to reject the connection
387
+ */
388
+ onBeforeConnect?: (
389
+ c: ActorContext<
390
+ TState,
391
+ TConnParams,
392
+ TConnState,
393
+ TVars,
394
+ TInput,
395
+ TAuthData,
396
+ TDatabase
397
+ >,
398
+ opts: OnConnectOptions,
399
+ params: TConnParams,
400
+ ) => void | Promise<void>;
401
+
402
+ /**
403
+ * Called when a client successfully connects to the actor.
404
+ *
405
+ * Use this hook to perform actions when a connection is established,
406
+ * such as sending initial data or updating the actor's state.
407
+ *
408
+ * @param conn The connection object
409
+ * @returns Void or a Promise that resolves when connection handling is complete
410
+ */
411
+ onConnect?: (
412
+ c: ActorContext<
413
+ TState,
414
+ TConnParams,
415
+ TConnState,
416
+ TVars,
417
+ TInput,
418
+ TAuthData,
419
+ TDatabase
420
+ >,
421
+ conn: Conn<
422
+ TState,
423
+ TConnParams,
424
+ TConnState,
425
+ TVars,
426
+ TInput,
427
+ TAuthData,
428
+ TDatabase
429
+ >,
430
+ ) => void | Promise<void>;
431
+
432
+ /**
433
+ * Called when a client disconnects from the actor.
434
+ *
435
+ * Use this hook to clean up resources associated with the connection
436
+ * or update the actor's state.
437
+ *
438
+ * @param conn The connection that is being closed
439
+ * @returns Void or a Promise that resolves when disconnect handling is complete
440
+ */
441
+ onDisconnect?: (
442
+ c: ActorContext<
443
+ TState,
444
+ TConnParams,
445
+ TConnState,
446
+ TVars,
447
+ TInput,
448
+ TAuthData,
449
+ TDatabase
450
+ >,
451
+ conn: Conn<
452
+ TState,
453
+ TConnParams,
454
+ TConnState,
455
+ TVars,
456
+ TInput,
457
+ TAuthData,
458
+ TDatabase
459
+ >,
460
+ ) => void | Promise<void>;
461
+
462
+ /**
463
+ * Called before sending an action response to the client.
464
+ *
465
+ * Use this hook to modify or transform the output of an action before it's sent
466
+ * to the client. This is useful for formatting responses, adding metadata,
467
+ * or applying transformations to the output.
468
+ *
469
+ * @param name The name of the action that was called
470
+ * @param args The arguments that were passed to the action
471
+ * @param output The output that will be sent to the client
472
+ * @returns The modified output to send to the client
473
+ */
474
+ onBeforeActionResponse?: <Out>(
475
+ c: ActorContext<
476
+ TState,
477
+ TConnParams,
478
+ TConnState,
479
+ TVars,
480
+ TInput,
481
+ TAuthData,
482
+ TDatabase
483
+ >,
484
+ name: string,
485
+ args: unknown[],
486
+ output: Out,
487
+ ) => Out | Promise<Out>;
488
+
489
+ /**
490
+ * Called when a raw HTTP request is made to the actor.
491
+ *
492
+ * This handler receives raw HTTP requests made to `/actors/{actorName}/http/*` endpoints.
493
+ * Use this hook to handle custom HTTP patterns, REST APIs, or other HTTP-based protocols.
494
+ *
495
+ * @param request The raw HTTP request object
496
+ * @returns A Response object to send back, or void to continue with default routing
497
+ */
498
+ onFetch?: (
499
+ c: ActorContext<
500
+ TState,
501
+ TConnParams,
502
+ TConnState,
503
+ TVars,
504
+ TInput,
505
+ TAuthData,
506
+ TDatabase
507
+ >,
508
+ request: Request,
509
+ opts: { auth: TAuthData },
510
+ ) => Response | Promise<Response>;
511
+
512
+ /**
513
+ * Called when a raw WebSocket connection is established to the actor.
514
+ *
515
+ * This handler receives WebSocket connections made to `/actors/{actorName}/websocket/*` endpoints.
516
+ * Use this hook to handle custom WebSocket protocols, binary streams, or other WebSocket-based communication.
517
+ *
518
+ * @param websocket The raw WebSocket connection
519
+ * @param request The original HTTP upgrade request
520
+ */
521
+ onWebSocket?: (
522
+ c: ActorContext<
523
+ TState,
524
+ TConnParams,
525
+ TConnState,
526
+ TVars,
527
+ TInput,
528
+ TAuthData,
529
+ TDatabase
530
+ >,
531
+ websocket: UniversalWebSocket,
532
+ opts: { request: Request; auth: TAuthData },
533
+ ) => void | Promise<void>;
534
+
535
+ actions: TActions;
536
+ }
537
+
538
+ type ActorDatabaseConfig<TDatabase extends AnyDatabaseProvider> =
539
+ | {
540
+ /**
541
+ * @experimental
542
+ */
543
+ db: TDatabase;
544
+ }
545
+ | Record<never, never>;
546
+
547
+ // 1. Infer schema
548
+ // 2. Omit keys that we'll manually define (because of generics)
549
+ // 3. Define our own types that have generic constraints
550
+ export type ActorConfig<
551
+ TState,
552
+ TConnParams,
553
+ TConnState,
554
+ TVars,
555
+ TInput,
556
+ TAuthData,
557
+ TDatabase extends AnyDatabaseProvider,
558
+ > = Omit<
559
+ z.infer<typeof ActorConfigSchema>,
560
+ | "actions"
561
+ | "onAuth"
562
+ | "onCreate"
563
+ | "onStart"
564
+ | "onStateChange"
565
+ | "onBeforeConnect"
566
+ | "onConnect"
567
+ | "onDisconnect"
568
+ | "onBeforeActionResponse"
569
+ | "onFetch"
570
+ | "onWebSocket"
571
+ | "state"
572
+ | "createState"
573
+ | "connState"
574
+ | "createConnState"
575
+ | "vars"
576
+ | "createVars"
577
+ | "db"
578
+ > &
579
+ BaseActorConfig<
580
+ TState,
581
+ TConnParams,
582
+ TConnState,
583
+ TVars,
584
+ TInput,
585
+ TAuthData,
586
+ TDatabase,
587
+ Actions<
588
+ TState,
589
+ TConnParams,
590
+ TConnState,
591
+ TVars,
592
+ TInput,
593
+ TAuthData,
594
+ TDatabase
595
+ >
596
+ > &
597
+ OnAuth<TConnParams, TAuthData> &
598
+ CreateState<
599
+ TState,
600
+ TConnParams,
601
+ TConnState,
602
+ TVars,
603
+ TInput,
604
+ TAuthData,
605
+ TDatabase
606
+ > &
607
+ CreateConnState<
608
+ TState,
609
+ TConnParams,
610
+ TConnState,
611
+ TVars,
612
+ TInput,
613
+ TAuthData,
614
+ TDatabase
615
+ > &
616
+ CreateVars<
617
+ TState,
618
+ TConnParams,
619
+ TConnState,
620
+ TVars,
621
+ TInput,
622
+ TAuthData,
623
+ TDatabase
624
+ > &
625
+ ActorDatabaseConfig<TDatabase>;
626
+
627
+ // See description on `ActorConfig`
628
+ export type ActorConfigInput<
629
+ TState = undefined,
630
+ TConnParams = undefined,
631
+ TConnState = undefined,
632
+ TVars = undefined,
633
+ TInput = undefined,
634
+ TAuthData = undefined,
635
+ TDatabase extends AnyDatabaseProvider = undefined,
636
+ TActions extends Actions<
637
+ TState,
638
+ TConnParams,
639
+ TConnState,
640
+ TVars,
641
+ TInput,
642
+ TAuthData,
643
+ TDatabase
644
+ > = Record<never, never>,
645
+ > = {
646
+ types?: ActorTypes<
647
+ TState,
648
+ TConnParams,
649
+ TConnState,
650
+ TVars,
651
+ TInput,
652
+ TAuthData,
653
+ TDatabase
654
+ >;
655
+ } & Omit<
656
+ z.input<typeof ActorConfigSchema>,
657
+ | "actions"
658
+ | "onAuth"
659
+ | "onCreate"
660
+ | "onStart"
661
+ | "onStop"
662
+ | "onStateChange"
663
+ | "onBeforeConnect"
664
+ | "onConnect"
665
+ | "onDisconnect"
666
+ | "onBeforeActionResponse"
667
+ | "onFetch"
668
+ | "onWebSocket"
669
+ | "state"
670
+ | "createState"
671
+ | "connState"
672
+ | "createConnState"
673
+ | "vars"
674
+ | "createVars"
675
+ | "db"
676
+ > &
677
+ BaseActorConfig<
678
+ TState,
679
+ TConnParams,
680
+ TConnState,
681
+ TVars,
682
+ TInput,
683
+ TAuthData,
684
+ TDatabase,
685
+ TActions
686
+ > &
687
+ OnAuth<TConnParams, TAuthData> &
688
+ CreateState<
689
+ TState,
690
+ TConnParams,
691
+ TConnState,
692
+ TVars,
693
+ TInput,
694
+ TAuthData,
695
+ TDatabase
696
+ > &
697
+ CreateConnState<
698
+ TState,
699
+ TConnParams,
700
+ TConnState,
701
+ TVars,
702
+ TInput,
703
+ TAuthData,
704
+ TDatabase
705
+ > &
706
+ CreateVars<
707
+ TState,
708
+ TConnParams,
709
+ TConnState,
710
+ TVars,
711
+ TInput,
712
+ TAuthData,
713
+ TDatabase
714
+ > &
715
+ ActorDatabaseConfig<TDatabase>;
716
+
717
+ // For testing type definitions:
718
+ export function test<
719
+ TState,
720
+ TConnParams,
721
+ TConnState,
722
+ TVars,
723
+ TInput,
724
+ TAuthData,
725
+ TDatabase extends AnyDatabaseProvider,
726
+ TActions extends Actions<
727
+ TState,
728
+ TConnParams,
729
+ TConnState,
730
+ TVars,
731
+ TInput,
732
+ TAuthData,
733
+ TDatabase
734
+ >,
735
+ >(
736
+ input: ActorConfigInput<
737
+ TState,
738
+ TConnParams,
739
+ TConnState,
740
+ TVars,
741
+ TInput,
742
+ TAuthData,
743
+ TDatabase,
744
+ TActions
745
+ >,
746
+ ): ActorConfig<
747
+ TState,
748
+ TConnParams,
749
+ TConnState,
750
+ TVars,
751
+ TInput,
752
+ TAuthData,
753
+ TDatabase
754
+ > {
755
+ const config = ActorConfigSchema.parse(input) as ActorConfig<
756
+ TState,
757
+ TConnParams,
758
+ TConnState,
759
+ TVars,
760
+ TInput,
761
+ TAuthData,
762
+ TDatabase
763
+ >;
764
+ return config;
765
+ }