rivetkit 2.0.24-rc.1 → 2.0.24
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.
- package/dist/schemas/actor-persist/v2.ts +3 -3
- package/dist/schemas/actor-persist/v3.ts +274 -0
- package/dist/schemas/client-protocol/v2.ts +432 -0
- package/dist/schemas/file-system-driver/v2.ts +136 -0
- package/dist/tsup/actor/errors.cjs +2 -4
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/actor/errors.d.cts +7 -10
- package/dist/tsup/actor/errors.d.ts +7 -10
- package/dist/tsup/actor/errors.js +9 -11
- package/dist/tsup/{actor-router-consts-B3Lu87yJ.d.cts → actor-router-consts-DzI2szci.d.cts} +5 -9
- package/dist/tsup/{actor-router-consts-B3Lu87yJ.d.ts → actor-router-consts-DzI2szci.d.ts} +5 -9
- package/dist/tsup/{chunk-HHFKKVLR.cjs → chunk-3543NCSN.cjs} +45 -57
- package/dist/tsup/chunk-3543NCSN.cjs.map +1 -0
- package/dist/tsup/chunk-4SHILYS5.cjs +5694 -0
- package/dist/tsup/chunk-4SHILYS5.cjs.map +1 -0
- package/dist/tsup/{chunk-ZTH3KYFH.cjs → chunk-5BZO5XPS.cjs} +3 -3
- package/dist/tsup/{chunk-ZTH3KYFH.cjs.map → chunk-5BZO5XPS.cjs.map} +1 -1
- package/dist/tsup/{chunk-PLUN2NQT.js → chunk-BAIGSF64.js} +189 -187
- package/dist/tsup/chunk-BAIGSF64.js.map +1 -0
- package/dist/tsup/{chunk-SHVX2QUR.cjs → chunk-CHLZBSI2.cjs} +17 -17
- package/dist/tsup/chunk-CHLZBSI2.cjs.map +1 -0
- package/dist/tsup/chunk-D3SLADUD.cjs +512 -0
- package/dist/tsup/chunk-D3SLADUD.cjs.map +1 -0
- package/dist/tsup/{chunk-KSRXX3Z4.cjs → chunk-D6762AOA.cjs} +20 -25
- package/dist/tsup/chunk-D6762AOA.cjs.map +1 -0
- package/dist/tsup/{chunk-7L65NNWP.cjs → chunk-DLK5YCTN.cjs} +187 -185
- package/dist/tsup/chunk-DLK5YCTN.cjs.map +1 -0
- package/dist/tsup/{chunk-YBG6R7LX.js → chunk-DUJQWGYD.js} +3 -7
- package/dist/tsup/chunk-DUJQWGYD.js.map +1 -0
- package/dist/tsup/{chunk-CD33GT6Z.js → chunk-EIPANQMF.js} +2 -2
- package/dist/tsup/{chunk-2JYPS5YM.cjs → chunk-ESMTDP7G.cjs} +6 -6
- package/dist/tsup/chunk-ESMTDP7G.cjs.map +1 -0
- package/dist/tsup/{chunk-VHGY7PU5.cjs → chunk-FVAKREFB.cjs} +1900 -1737
- package/dist/tsup/chunk-FVAKREFB.cjs.map +1 -0
- package/dist/tsup/{chunk-BLK27ES3.js → chunk-I3XT7WOF.js} +44 -56
- package/dist/tsup/chunk-I3XT7WOF.js.map +1 -0
- package/dist/tsup/{chunk-YBHYXIP6.js → chunk-IMDS5T42.js} +3 -3
- package/dist/tsup/chunk-IMDS5T42.js.map +1 -0
- package/dist/tsup/{chunk-INNFK746.cjs → chunk-J3HZJF2P.cjs} +10 -14
- package/dist/tsup/chunk-J3HZJF2P.cjs.map +1 -0
- package/dist/tsup/{chunk-BYMKMOBS.js → chunk-MBBJUHSP.js} +1844 -1681
- package/dist/tsup/chunk-MBBJUHSP.js.map +1 -0
- package/dist/tsup/{chunk-BOMZS2TJ.js → chunk-MO5CB6MD.js} +9 -9
- package/dist/tsup/chunk-MO5CB6MD.js.map +1 -0
- package/dist/tsup/chunk-OFOTPKAH.js +512 -0
- package/dist/tsup/chunk-OFOTPKAH.js.map +1 -0
- package/dist/tsup/{chunk-G64QUEDJ.js → chunk-W6RDS6NW.js} +23 -28
- package/dist/tsup/chunk-W6RDS6NW.js.map +1 -0
- package/dist/tsup/{chunk-36JJ4IQB.cjs → chunk-YC5DUHPM.cjs} +4 -8
- package/dist/tsup/chunk-YC5DUHPM.cjs.map +1 -0
- package/dist/tsup/{chunk-FX7TWFQR.js → chunk-YC7YPM2T.js} +2 -6
- package/dist/tsup/chunk-YC7YPM2T.js.map +1 -0
- package/dist/tsup/{chunk-227FEWMB.js → chunk-ZSPU5R4C.js} +3322 -2251
- package/dist/tsup/chunk-ZSPU5R4C.js.map +1 -0
- package/dist/tsup/client/mod.cjs +9 -9
- package/dist/tsup/client/mod.d.cts +5 -7
- package/dist/tsup/client/mod.d.ts +5 -7
- package/dist/tsup/client/mod.js +8 -8
- package/dist/tsup/common/log.cjs +3 -3
- package/dist/tsup/common/log.js +2 -2
- package/dist/tsup/common/websocket.cjs +4 -4
- package/dist/tsup/common/websocket.js +3 -3
- package/dist/tsup/{conn-B3Vhbgnd.d.ts → config-BRDYDraU.d.cts} +1119 -1047
- package/dist/tsup/{conn-DJWL3nGx.d.cts → config-Bo-blHpJ.d.ts} +1119 -1047
- package/dist/tsup/driver-helpers/mod.cjs +5 -13
- package/dist/tsup/driver-helpers/mod.cjs.map +1 -1
- package/dist/tsup/driver-helpers/mod.d.cts +11 -9
- package/dist/tsup/driver-helpers/mod.d.ts +11 -9
- package/dist/tsup/driver-helpers/mod.js +14 -22
- package/dist/tsup/driver-test-suite/mod.cjs +474 -303
- package/dist/tsup/driver-test-suite/mod.cjs.map +1 -1
- package/dist/tsup/driver-test-suite/mod.d.cts +6 -9
- package/dist/tsup/driver-test-suite/mod.d.ts +6 -9
- package/dist/tsup/driver-test-suite/mod.js +1085 -914
- package/dist/tsup/driver-test-suite/mod.js.map +1 -1
- package/dist/tsup/inspector/mod.cjs +6 -6
- package/dist/tsup/inspector/mod.d.cts +5 -7
- package/dist/tsup/inspector/mod.d.ts +5 -7
- package/dist/tsup/inspector/mod.js +5 -5
- package/dist/tsup/mod.cjs +10 -16
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +23 -25
- package/dist/tsup/mod.d.ts +23 -25
- package/dist/tsup/mod.js +17 -23
- package/dist/tsup/test/mod.cjs +11 -11
- package/dist/tsup/test/mod.d.cts +4 -6
- package/dist/tsup/test/mod.d.ts +4 -6
- package/dist/tsup/test/mod.js +10 -10
- package/dist/tsup/utils.cjs +3 -5
- package/dist/tsup/utils.cjs.map +1 -1
- package/dist/tsup/utils.d.cts +1 -2
- package/dist/tsup/utils.d.ts +1 -2
- package/dist/tsup/utils.js +2 -4
- package/package.json +13 -6
- package/src/actor/config.ts +56 -44
- package/src/actor/conn/driver.ts +61 -0
- package/src/actor/conn/drivers/http.ts +17 -0
- package/src/actor/conn/drivers/raw-request.ts +24 -0
- package/src/actor/conn/drivers/raw-websocket.ts +65 -0
- package/src/actor/conn/drivers/websocket.ts +129 -0
- package/src/actor/conn/mod.ts +232 -0
- package/src/actor/conn/persisted.ts +81 -0
- package/src/actor/conn/state-manager.ts +196 -0
- package/src/actor/contexts/action.ts +23 -0
- package/src/actor/{context.ts → contexts/actor.ts} +19 -8
- package/src/actor/contexts/conn-init.ts +31 -0
- package/src/actor/contexts/conn.ts +48 -0
- package/src/actor/contexts/create-conn-state.ts +13 -0
- package/src/actor/contexts/on-before-connect.ts +13 -0
- package/src/actor/contexts/on-connect.ts +22 -0
- package/src/actor/contexts/request.ts +48 -0
- package/src/actor/contexts/websocket.ts +48 -0
- package/src/actor/definition.ts +3 -3
- package/src/actor/driver.ts +36 -5
- package/src/actor/errors.ts +19 -24
- package/src/actor/instance/connection-manager.ts +465 -0
- package/src/actor/instance/event-manager.ts +292 -0
- package/src/actor/instance/kv.ts +15 -0
- package/src/actor/instance/mod.ts +1107 -0
- package/src/actor/instance/persisted.ts +67 -0
- package/src/actor/instance/schedule-manager.ts +349 -0
- package/src/actor/instance/state-manager.ts +502 -0
- package/src/actor/mod.ts +13 -16
- package/src/actor/protocol/old.ts +131 -43
- package/src/actor/protocol/serde.ts +19 -4
- package/src/actor/router-endpoints.ts +61 -586
- package/src/actor/router-websocket-endpoints.ts +408 -0
- package/src/actor/router.ts +63 -197
- package/src/actor/schedule.ts +1 -1
- package/src/client/actor-conn.ts +183 -249
- package/src/client/actor-handle.ts +29 -6
- package/src/client/client.ts +0 -4
- package/src/client/config.ts +1 -4
- package/src/client/mod.ts +0 -1
- package/src/client/raw-utils.ts +3 -3
- package/src/client/utils.ts +85 -39
- package/src/common/actor-router-consts.ts +5 -12
- package/src/common/{inline-websocket-adapter2.ts → inline-websocket-adapter.ts} +26 -48
- package/src/common/log.ts +1 -1
- package/src/common/router.ts +28 -17
- package/src/common/utils.ts +2 -0
- package/src/driver-helpers/mod.ts +7 -10
- package/src/driver-helpers/utils.ts +18 -9
- package/src/driver-test-suite/mod.ts +26 -50
- package/src/driver-test-suite/test-inline-client-driver.ts +27 -51
- package/src/driver-test-suite/tests/actor-conn-hibernation.ts +150 -0
- package/src/driver-test-suite/tests/actor-conn-state.ts +1 -4
- package/src/driver-test-suite/tests/actor-conn.ts +5 -9
- package/src/driver-test-suite/tests/actor-destroy.ts +294 -0
- package/src/driver-test-suite/tests/actor-driver.ts +0 -7
- package/src/driver-test-suite/tests/actor-handle.ts +12 -12
- package/src/driver-test-suite/tests/actor-metadata.ts +1 -1
- package/src/driver-test-suite/tests/manager-driver.ts +1 -1
- package/src/driver-test-suite/tests/raw-http-direct-registry.ts +8 -8
- package/src/driver-test-suite/tests/raw-http-request-properties.ts +6 -5
- package/src/driver-test-suite/tests/raw-http.ts +5 -5
- package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +7 -7
- package/src/driver-test-suite/tests/request-access.ts +4 -4
- package/src/driver-test-suite/utils.ts +6 -10
- package/src/drivers/engine/actor-driver.ts +614 -424
- package/src/drivers/engine/mod.ts +0 -1
- package/src/drivers/file-system/actor.ts +24 -12
- package/src/drivers/file-system/global-state.ts +427 -37
- package/src/drivers/file-system/manager.ts +71 -83
- package/src/drivers/file-system/mod.ts +3 -0
- package/src/drivers/file-system/utils.ts +18 -8
- package/src/engine-process/mod.ts +38 -38
- package/src/inspector/utils.ts +7 -5
- package/src/manager/driver.ts +11 -4
- package/src/manager/gateway.ts +4 -29
- package/src/manager/protocol/mod.ts +0 -2
- package/src/manager/protocol/query.ts +0 -4
- package/src/manager/router.ts +67 -64
- package/src/manager-api/actors.ts +13 -0
- package/src/mod.ts +1 -3
- package/src/registry/mod.ts +20 -20
- package/src/registry/serve.ts +9 -14
- package/src/remote-manager-driver/actor-websocket-client.ts +1 -16
- package/src/remote-manager-driver/api-endpoints.ts +13 -1
- package/src/remote-manager-driver/api-utils.ts +8 -0
- package/src/remote-manager-driver/metadata.ts +58 -0
- package/src/remote-manager-driver/mod.ts +47 -62
- package/src/remote-manager-driver/ws-proxy.ts +1 -1
- package/src/schemas/actor-persist/mod.ts +1 -1
- package/src/schemas/actor-persist/versioned.ts +56 -31
- package/src/schemas/client-protocol/mod.ts +1 -1
- package/src/schemas/client-protocol/versioned.ts +41 -21
- package/src/schemas/client-protocol-zod/mod.ts +103 -0
- package/src/schemas/file-system-driver/mod.ts +1 -1
- package/src/schemas/file-system-driver/versioned.ts +42 -19
- package/src/serde.ts +33 -11
- package/src/test/mod.ts +7 -3
- package/src/utils/node.ts +173 -0
- package/src/utils.ts +0 -4
- package/dist/tsup/chunk-227FEWMB.js.map +0 -1
- package/dist/tsup/chunk-2JYPS5YM.cjs.map +0 -1
- package/dist/tsup/chunk-36JJ4IQB.cjs.map +0 -1
- package/dist/tsup/chunk-7L65NNWP.cjs.map +0 -1
- package/dist/tsup/chunk-BLK27ES3.js.map +0 -1
- package/dist/tsup/chunk-BOMZS2TJ.js.map +0 -1
- package/dist/tsup/chunk-BYMKMOBS.js.map +0 -1
- package/dist/tsup/chunk-FX7TWFQR.js.map +0 -1
- package/dist/tsup/chunk-G64QUEDJ.js.map +0 -1
- package/dist/tsup/chunk-HHFKKVLR.cjs.map +0 -1
- package/dist/tsup/chunk-INNFK746.cjs.map +0 -1
- package/dist/tsup/chunk-KSRXX3Z4.cjs.map +0 -1
- package/dist/tsup/chunk-O44LFKSB.cjs +0 -4623
- package/dist/tsup/chunk-O44LFKSB.cjs.map +0 -1
- package/dist/tsup/chunk-PLUN2NQT.js.map +0 -1
- package/dist/tsup/chunk-S4UJG7ZE.js +0 -1119
- package/dist/tsup/chunk-S4UJG7ZE.js.map +0 -1
- package/dist/tsup/chunk-SHVX2QUR.cjs.map +0 -1
- package/dist/tsup/chunk-VFB23BYZ.cjs +0 -1119
- package/dist/tsup/chunk-VFB23BYZ.cjs.map +0 -1
- package/dist/tsup/chunk-VHGY7PU5.cjs.map +0 -1
- package/dist/tsup/chunk-YBG6R7LX.js.map +0 -1
- package/dist/tsup/chunk-YBHYXIP6.js.map +0 -1
- package/src/actor/action.ts +0 -178
- package/src/actor/conn-drivers.ts +0 -216
- package/src/actor/conn-socket.ts +0 -8
- package/src/actor/conn.ts +0 -272
- package/src/actor/instance.ts +0 -2336
- package/src/actor/persisted.ts +0 -49
- package/src/actor/unstable-react.ts +0 -110
- package/src/driver-test-suite/tests/actor-reconnect.ts +0 -170
- package/src/drivers/engine/kv.ts +0 -3
- package/src/manager/hono-websocket-adapter.ts +0 -393
- /package/dist/tsup/{chunk-CD33GT6Z.js.map → chunk-EIPANQMF.js.map} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rivetkit",
|
|
3
|
-
"version": "2.0.24
|
|
3
|
+
"version": "2.0.24",
|
|
4
4
|
"description": "Lightweight libraries for building stateful actors on edge platforms",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -165,24 +165,26 @@
|
|
|
165
165
|
"pino": "^9.5.0",
|
|
166
166
|
"uuid": "^12.0.0",
|
|
167
167
|
"zod": "^3.25.76",
|
|
168
|
-
"@rivetkit/engine-runner": "2.0.24
|
|
168
|
+
"@rivetkit/engine-runner": "2.0.24"
|
|
169
169
|
},
|
|
170
170
|
"devDependencies": {
|
|
171
171
|
"@bare-ts/tools": "^0.13.0",
|
|
172
|
+
"@biomejs/biome": "^2.2.3",
|
|
172
173
|
"@hono/node-server": "^1.18.2",
|
|
173
174
|
"@hono/node-ws": "^1.1.1",
|
|
174
175
|
"@types/invariant": "^2",
|
|
175
176
|
"@types/node": "^22.13.1",
|
|
176
177
|
"@types/ws": "^8",
|
|
177
178
|
"@vitest/ui": "3.1.1",
|
|
178
|
-
"bundle-require": "^5.1.0",
|
|
179
179
|
"commander": "^12.1.0",
|
|
180
180
|
"eventsource": "^4.0.0",
|
|
181
181
|
"tsup": "^8.4.0",
|
|
182
182
|
"tsx": "^4.19.4",
|
|
183
183
|
"typescript": "^5.7.3",
|
|
184
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
184
185
|
"vitest": "^3.1.1",
|
|
185
|
-
"ws": "^8.18.1"
|
|
186
|
+
"ws": "^8.18.1",
|
|
187
|
+
"zod-to-json-schema": "^3.24.6"
|
|
186
188
|
},
|
|
187
189
|
"peerDependencies": {
|
|
188
190
|
"@hono/node-server": "^1.14.0",
|
|
@@ -207,10 +209,15 @@
|
|
|
207
209
|
"stableVersion": "0.8.0",
|
|
208
210
|
"scripts": {
|
|
209
211
|
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/common/websocket.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/test/mod.ts src/inspector/mod.ts",
|
|
210
|
-
"build:schema": "./scripts/compile-bare.ts compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && ./scripts/compile-bare.ts compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v1.bare -o dist/schemas/actor-persist/v1.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v2.bare -o dist/schemas/actor-persist/v2.ts",
|
|
212
|
+
"build:schema": "./scripts/compile-bare.ts compile schemas/client-protocol/v1.bare -o dist/schemas/client-protocol/v1.ts && ./scripts/compile-bare.ts compile schemas/client-protocol/v2.bare -o dist/schemas/client-protocol/v2.ts && ./scripts/compile-bare.ts compile schemas/file-system-driver/v1.bare -o dist/schemas/file-system-driver/v1.ts && ./scripts/compile-bare.ts compile schemas/file-system-driver/v2.bare -o dist/schemas/file-system-driver/v2.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v1.bare -o dist/schemas/actor-persist/v1.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v2.bare -o dist/schemas/actor-persist/v2.ts && ./scripts/compile-bare.ts compile schemas/actor-persist/v3.bare -o dist/schemas/actor-persist/v3.ts",
|
|
211
213
|
"check-types": "tsc --noEmit",
|
|
214
|
+
"lint": "biome check .",
|
|
215
|
+
"lint:fix": "biome check --write .",
|
|
216
|
+
"format": "biome format .",
|
|
217
|
+
"format:write": "biome format --write .",
|
|
212
218
|
"test": "vitest run",
|
|
213
219
|
"test:watch": "vitest",
|
|
214
|
-
"dump-openapi": "tsx scripts/dump-openapi.ts"
|
|
220
|
+
"dump-openapi": "tsx scripts/dump-openapi.ts",
|
|
221
|
+
"dump-asyncapi": "tsx scripts/dump-asyncapi.ts"
|
|
215
222
|
}
|
|
216
223
|
}
|
package/src/actor/config.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { UniversalWebSocket } from "@/common/websocket-interface";
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import type { ActorContext } from "./
|
|
3
|
+
import type { Conn } from "./conn/mod";
|
|
4
|
+
import type { ActionContext } from "./contexts/action";
|
|
5
|
+
import type { ActorContext } from "./contexts/actor";
|
|
6
|
+
import type { CreateConnStateContext } from "./contexts/create-conn-state";
|
|
7
|
+
import type { OnBeforeConnectContext } from "./contexts/on-before-connect";
|
|
8
|
+
import type { OnConnectContext } from "./contexts/on-connect";
|
|
9
|
+
import type { RequestContext } from "./contexts/request";
|
|
10
|
+
import type { WebSocketContext } from "./contexts/websocket";
|
|
6
11
|
import type { AnyDatabaseProvider } from "./database";
|
|
7
12
|
|
|
8
13
|
export type InitContext = ActorContext<
|
|
@@ -38,14 +43,15 @@ export interface ActorTypes<
|
|
|
38
43
|
export const ActorConfigSchema = z
|
|
39
44
|
.object({
|
|
40
45
|
onCreate: z.function().optional(),
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
onDestroy: z.function().optional(),
|
|
47
|
+
onWake: z.function().optional(),
|
|
48
|
+
onSleep: z.function().optional(),
|
|
43
49
|
onStateChange: z.function().optional(),
|
|
44
50
|
onBeforeConnect: z.function().optional(),
|
|
45
51
|
onConnect: z.function().optional(),
|
|
46
52
|
onDisconnect: z.function().optional(),
|
|
47
53
|
onBeforeActionResponse: z.function().optional(),
|
|
48
|
-
|
|
54
|
+
onRequest: z.function().optional(),
|
|
49
55
|
onWebSocket: z.function().optional(),
|
|
50
56
|
actions: z.record(z.function()).default({}),
|
|
51
57
|
state: z.any().optional(),
|
|
@@ -61,7 +67,8 @@ export const ActorConfigSchema = z
|
|
|
61
67
|
createConnStateTimeout: z.number().positive().default(5000),
|
|
62
68
|
onConnectTimeout: z.number().positive().default(5000),
|
|
63
69
|
// This must be less than ACTOR_STOP_THRESHOLD_MS
|
|
64
|
-
|
|
70
|
+
onSleepTimeout: z.number().positive().default(5000),
|
|
71
|
+
onDestroyTimeout: z.number().positive().default(5000),
|
|
65
72
|
stateSaveInterval: z.number().positive().default(10_000),
|
|
66
73
|
actionTimeout: z.number().positive().default(60_000),
|
|
67
74
|
// Max time to wait for waitUntil background promises during shutdown
|
|
@@ -70,8 +77,14 @@ export const ActorConfigSchema = z
|
|
|
70
77
|
connectionLivenessInterval: z.number().positive().default(5000),
|
|
71
78
|
noSleep: z.boolean().default(false),
|
|
72
79
|
sleepTimeout: z.number().positive().default(30_000),
|
|
73
|
-
/**
|
|
74
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Can hibernate WebSockets for onWebSocket.
|
|
82
|
+
*
|
|
83
|
+
* WebSockets using actions/events are hibernatable by default.
|
|
84
|
+
*
|
|
85
|
+
* @experimental
|
|
86
|
+
**/
|
|
87
|
+
canHibernateWebSocket: z
|
|
75
88
|
.union([
|
|
76
89
|
z.boolean(),
|
|
77
90
|
z
|
|
@@ -111,15 +124,6 @@ export const ActorConfigSchema = z
|
|
|
111
124
|
},
|
|
112
125
|
);
|
|
113
126
|
|
|
114
|
-
export interface OnConnectOptions {
|
|
115
|
-
/**
|
|
116
|
-
* The request object associated with the connection.
|
|
117
|
-
*
|
|
118
|
-
* @experimental
|
|
119
|
-
*/
|
|
120
|
-
request?: Request;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
127
|
// Creates state config
|
|
124
128
|
//
|
|
125
129
|
// This must have only one or the other or else TState will not be able to be inferred
|
|
@@ -146,13 +150,12 @@ type CreateConnState<
|
|
|
146
150
|
TConnState,
|
|
147
151
|
TVars,
|
|
148
152
|
TInput,
|
|
149
|
-
TDatabase,
|
|
153
|
+
TDatabase extends AnyDatabaseProvider,
|
|
150
154
|
> =
|
|
151
155
|
| { connState: TConnState }
|
|
152
156
|
| {
|
|
153
157
|
createConnState: (
|
|
154
|
-
c:
|
|
155
|
-
opts: OnConnectOptions,
|
|
158
|
+
c: CreateConnStateContext<TState, TVars, TInput, TDatabase>,
|
|
156
159
|
params: TConnParams,
|
|
157
160
|
) => TConnState | Promise<TConnState>;
|
|
158
161
|
}
|
|
@@ -247,6 +250,20 @@ interface BaseActorConfig<
|
|
|
247
250
|
input: TInput,
|
|
248
251
|
) => void | Promise<void>;
|
|
249
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Called when the actor is destroyed.
|
|
255
|
+
*/
|
|
256
|
+
onDestroy?: (
|
|
257
|
+
c: ActorContext<
|
|
258
|
+
TState,
|
|
259
|
+
TConnParams,
|
|
260
|
+
TConnState,
|
|
261
|
+
TVars,
|
|
262
|
+
TInput,
|
|
263
|
+
TDatabase
|
|
264
|
+
>,
|
|
265
|
+
) => void | Promise<void>;
|
|
266
|
+
|
|
250
267
|
/**
|
|
251
268
|
* Called when the actor is started and ready to receive connections and action.
|
|
252
269
|
*
|
|
@@ -255,7 +272,7 @@ interface BaseActorConfig<
|
|
|
255
272
|
*
|
|
256
273
|
* @returns Void or a Promise that resolves when startup is complete
|
|
257
274
|
*/
|
|
258
|
-
|
|
275
|
+
onWake?: (
|
|
259
276
|
c: ActorContext<
|
|
260
277
|
TState,
|
|
261
278
|
TConnParams,
|
|
@@ -276,7 +293,7 @@ interface BaseActorConfig<
|
|
|
276
293
|
*
|
|
277
294
|
* @returns Void or a Promise that resolves when shutdown is complete
|
|
278
295
|
*/
|
|
279
|
-
|
|
296
|
+
onSleep?: (
|
|
280
297
|
c: ActorContext<
|
|
281
298
|
TState,
|
|
282
299
|
TConnParams,
|
|
@@ -321,15 +338,7 @@ interface BaseActorConfig<
|
|
|
321
338
|
* @throws Throw an error to reject the connection
|
|
322
339
|
*/
|
|
323
340
|
onBeforeConnect?: (
|
|
324
|
-
c:
|
|
325
|
-
TState,
|
|
326
|
-
TConnParams,
|
|
327
|
-
TConnState,
|
|
328
|
-
TVars,
|
|
329
|
-
TInput,
|
|
330
|
-
TDatabase
|
|
331
|
-
>,
|
|
332
|
-
opts: OnConnectOptions,
|
|
341
|
+
c: OnBeforeConnectContext<TState, TVars, TInput, TDatabase>,
|
|
333
342
|
params: TConnParams,
|
|
334
343
|
) => void | Promise<void>;
|
|
335
344
|
|
|
@@ -343,7 +352,7 @@ interface BaseActorConfig<
|
|
|
343
352
|
* @returns Void or a Promise that resolves when connection handling is complete
|
|
344
353
|
*/
|
|
345
354
|
onConnect?: (
|
|
346
|
-
c:
|
|
355
|
+
c: OnConnectContext<
|
|
347
356
|
TState,
|
|
348
357
|
TConnParams,
|
|
349
358
|
TConnState,
|
|
@@ -407,11 +416,13 @@ interface BaseActorConfig<
|
|
|
407
416
|
* This handler receives raw HTTP requests made to `/actors/{actorName}/http/*` endpoints.
|
|
408
417
|
* Use this hook to handle custom HTTP patterns, REST APIs, or other HTTP-based protocols.
|
|
409
418
|
*
|
|
419
|
+
* @param c The request context with access to the connection
|
|
410
420
|
* @param request The raw HTTP request object
|
|
421
|
+
* @param opts Additional options
|
|
411
422
|
* @returns A Response object to send back, or void to continue with default routing
|
|
412
423
|
*/
|
|
413
|
-
|
|
414
|
-
c:
|
|
424
|
+
onRequest?: (
|
|
425
|
+
c: RequestContext<
|
|
415
426
|
TState,
|
|
416
427
|
TConnParams,
|
|
417
428
|
TConnState,
|
|
@@ -420,7 +431,6 @@ interface BaseActorConfig<
|
|
|
420
431
|
TDatabase
|
|
421
432
|
>,
|
|
422
433
|
request: Request,
|
|
423
|
-
opts: {},
|
|
424
434
|
) => Response | Promise<Response>;
|
|
425
435
|
|
|
426
436
|
/**
|
|
@@ -429,11 +439,12 @@ interface BaseActorConfig<
|
|
|
429
439
|
* This handler receives WebSocket connections made to `/actors/{actorName}/websocket/*` endpoints.
|
|
430
440
|
* Use this hook to handle custom WebSocket protocols, binary streams, or other WebSocket-based communication.
|
|
431
441
|
*
|
|
442
|
+
* @param c The WebSocket context with access to the connection
|
|
432
443
|
* @param websocket The raw WebSocket connection
|
|
433
|
-
* @param
|
|
444
|
+
* @param opts Additional options including the original HTTP upgrade request
|
|
434
445
|
*/
|
|
435
446
|
onWebSocket?: (
|
|
436
|
-
c:
|
|
447
|
+
c: WebSocketContext<
|
|
437
448
|
TState,
|
|
438
449
|
TConnParams,
|
|
439
450
|
TConnState,
|
|
@@ -442,7 +453,6 @@ interface BaseActorConfig<
|
|
|
442
453
|
TDatabase
|
|
443
454
|
>,
|
|
444
455
|
websocket: UniversalWebSocket,
|
|
445
|
-
opts: { request: Request },
|
|
446
456
|
) => void | Promise<void>;
|
|
447
457
|
|
|
448
458
|
actions: TActions;
|
|
@@ -471,13 +481,14 @@ export type ActorConfig<
|
|
|
471
481
|
z.infer<typeof ActorConfigSchema>,
|
|
472
482
|
| "actions"
|
|
473
483
|
| "onCreate"
|
|
474
|
-
| "
|
|
484
|
+
| "onDestroy"
|
|
485
|
+
| "onWake"
|
|
475
486
|
| "onStateChange"
|
|
476
487
|
| "onBeforeConnect"
|
|
477
488
|
| "onConnect"
|
|
478
489
|
| "onDisconnect"
|
|
479
490
|
| "onBeforeActionResponse"
|
|
480
|
-
| "
|
|
491
|
+
| "onRequest"
|
|
481
492
|
| "onWebSocket"
|
|
482
493
|
| "state"
|
|
483
494
|
| "createState"
|
|
@@ -530,14 +541,15 @@ export type ActorConfigInput<
|
|
|
530
541
|
z.input<typeof ActorConfigSchema>,
|
|
531
542
|
| "actions"
|
|
532
543
|
| "onCreate"
|
|
533
|
-
| "
|
|
534
|
-
| "
|
|
544
|
+
| "onDestroy"
|
|
545
|
+
| "onWake"
|
|
546
|
+
| "onSleep"
|
|
535
547
|
| "onStateChange"
|
|
536
548
|
| "onBeforeConnect"
|
|
537
549
|
| "onConnect"
|
|
538
550
|
| "onDisconnect"
|
|
539
551
|
| "onBeforeActionResponse"
|
|
540
|
-
| "
|
|
552
|
+
| "onRequest"
|
|
541
553
|
| "onWebSocket"
|
|
542
554
|
| "state"
|
|
543
555
|
| "createState"
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { AnyConn } from "@/actor/conn/mod";
|
|
2
|
+
import type { AnyActorInstance } from "@/actor/instance/mod";
|
|
3
|
+
import type { CachedSerializer } from "@/actor/protocol/serde";
|
|
4
|
+
|
|
5
|
+
export enum DriverReadyState {
|
|
6
|
+
UNKNOWN = -1,
|
|
7
|
+
CONNECTING = 0,
|
|
8
|
+
OPEN = 1,
|
|
9
|
+
CLOSING = 2,
|
|
10
|
+
CLOSED = 3,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ConnDriver {
|
|
14
|
+
/** The type of driver. Used for debug purposes only. */
|
|
15
|
+
type: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* If defined, this connection driver talks the RivetKit client driver (see
|
|
19
|
+
* schemas/client-protocol/).
|
|
20
|
+
*
|
|
21
|
+
* If enabled, events like `Init`, subscription events, etc. will be sent
|
|
22
|
+
* to this connection.
|
|
23
|
+
*/
|
|
24
|
+
rivetKitProtocol?: {
|
|
25
|
+
/** Sends a RivetKit client message. */
|
|
26
|
+
sendMessage(
|
|
27
|
+
actor: AnyActorInstance,
|
|
28
|
+
conn: AnyConn,
|
|
29
|
+
message: CachedSerializer<any, any, any>,
|
|
30
|
+
): void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* If the connection can be hibernated. If true, this will allow the actor to go to sleep while the connection is still active.
|
|
35
|
+
**/
|
|
36
|
+
hibernatable?: {
|
|
37
|
+
gatewayId: ArrayBuffer;
|
|
38
|
+
requestId: ArrayBuffer;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 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.
|
|
43
|
+
*/
|
|
44
|
+
disconnect(
|
|
45
|
+
actor: AnyActorInstance,
|
|
46
|
+
conn: AnyConn,
|
|
47
|
+
reason?: string,
|
|
48
|
+
): Promise<void>;
|
|
49
|
+
|
|
50
|
+
/** Terminates the connection without graceful handling. */
|
|
51
|
+
terminate?(actor: AnyActorInstance, conn: AnyConn): void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns the ready state of the connection.
|
|
55
|
+
* This is used to determine if the connection is ready to send messages, or if the connection is stale.
|
|
56
|
+
*/
|
|
57
|
+
getConnectionReadyState(
|
|
58
|
+
actor: AnyActorInstance,
|
|
59
|
+
conn: AnyConn,
|
|
60
|
+
): DriverReadyState | undefined;
|
|
61
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ConnDriver, DriverReadyState } from "../driver";
|
|
2
|
+
|
|
3
|
+
export type ConnHttpState = Record<never, never>;
|
|
4
|
+
|
|
5
|
+
export function createHttpDriver(): ConnDriver {
|
|
6
|
+
return {
|
|
7
|
+
type: "http",
|
|
8
|
+
getConnectionReadyState(_actor, _conn) {
|
|
9
|
+
// TODO: This might not be the correct logic
|
|
10
|
+
return DriverReadyState.OPEN;
|
|
11
|
+
},
|
|
12
|
+
disconnect: async () => {
|
|
13
|
+
// Noop
|
|
14
|
+
// TODO: Configure with abort signals to abort the request
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ConnDriver } from "../driver";
|
|
2
|
+
import { DriverReadyState } from "../driver";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a raw HTTP connection driver.
|
|
6
|
+
*
|
|
7
|
+
* This driver is used for raw HTTP connections that don't use the RivetKit protocol.
|
|
8
|
+
* Unlike the standard HTTP driver, this provides connection lifecycle management
|
|
9
|
+
* for tracking the HTTP request through the actor's onRequest handler.
|
|
10
|
+
*/
|
|
11
|
+
export function createRawRequestDriver(): ConnDriver {
|
|
12
|
+
return {
|
|
13
|
+
type: "raw-request",
|
|
14
|
+
|
|
15
|
+
disconnect: async () => {
|
|
16
|
+
// Noop
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
getConnectionReadyState: (): DriverReadyState | undefined => {
|
|
20
|
+
// HTTP connections are always considered open until the request completes
|
|
21
|
+
return DriverReadyState.OPEN;
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { AnyConn } from "@/actor/conn/mod";
|
|
2
|
+
import type { AnyActorInstance } from "@/actor/instance/mod";
|
|
3
|
+
import type { UniversalWebSocket } from "@/common/websocket-interface";
|
|
4
|
+
import { loggerWithoutContext } from "../../log";
|
|
5
|
+
import { type ConnDriver, DriverReadyState } from "../driver";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a raw WebSocket connection driver.
|
|
9
|
+
*
|
|
10
|
+
* This driver is used for raw WebSocket connections that don't use the RivetKit protocol.
|
|
11
|
+
* Unlike the standard WebSocket driver, this doesn't have sendMessage since raw WebSockets
|
|
12
|
+
* don't handle messages from the RivetKit protocol - they handle messages directly in the
|
|
13
|
+
* actor's onWebSocket handler.
|
|
14
|
+
*/
|
|
15
|
+
export function createRawWebSocketDriver(
|
|
16
|
+
hibernatable: ConnDriver['hibernatable'],
|
|
17
|
+
closePromise: Promise<void>,
|
|
18
|
+
): { driver: ConnDriver; setWebSocket(ws: UniversalWebSocket): void } {
|
|
19
|
+
let websocket: UniversalWebSocket | undefined;
|
|
20
|
+
|
|
21
|
+
const driver: ConnDriver = {
|
|
22
|
+
type: "raw-websocket",
|
|
23
|
+
hibernatable,
|
|
24
|
+
|
|
25
|
+
// No sendMessage implementation since this is a raw WebSocket that doesn't
|
|
26
|
+
// handle messages from the RivetKit protocol
|
|
27
|
+
|
|
28
|
+
disconnect: async (
|
|
29
|
+
_actor: AnyActorInstance,
|
|
30
|
+
_conn: AnyConn,
|
|
31
|
+
reason?: string,
|
|
32
|
+
) => {
|
|
33
|
+
if (!websocket) {
|
|
34
|
+
loggerWithoutContext().warn(
|
|
35
|
+
"disconnecting raw ws without websocket",
|
|
36
|
+
);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Close socket
|
|
41
|
+
websocket.close(1000, reason);
|
|
42
|
+
|
|
43
|
+
// Wait for socket to close gracefully
|
|
44
|
+
await closePromise;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
terminate: () => {
|
|
48
|
+
(websocket as any)?.terminate?.();
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
getConnectionReadyState: (
|
|
52
|
+
_actor: AnyActorInstance,
|
|
53
|
+
_conn: AnyConn,
|
|
54
|
+
): DriverReadyState | undefined => {
|
|
55
|
+
return websocket?.readyState ?? DriverReadyState.CONNECTING;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
driver,
|
|
61
|
+
setWebSocket(ws) {
|
|
62
|
+
websocket = ws;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { WSContext } from "hono/ws";
|
|
2
|
+
import type { AnyConn } from "@/actor/conn/mod";
|
|
3
|
+
import type { AnyActorInstance } from "@/actor/instance/mod";
|
|
4
|
+
import type { CachedSerializer, Encoding } from "@/actor/protocol/serde";
|
|
5
|
+
import { loggerWithoutContext } from "../../log";
|
|
6
|
+
import { type ConnDriver, DriverReadyState } from "../driver";
|
|
7
|
+
|
|
8
|
+
export type ConnDriverWebSocketState = Record<never, never>;
|
|
9
|
+
|
|
10
|
+
export function createWebSocketDriver(
|
|
11
|
+
hibernatable: ConnDriver['hibernatable'],
|
|
12
|
+
encoding: Encoding,
|
|
13
|
+
closePromise: Promise<void>,
|
|
14
|
+
): { driver: ConnDriver; setWebSocket(ws: WSContext): void } {
|
|
15
|
+
loggerWithoutContext().debug({
|
|
16
|
+
msg: "createWebSocketDriver creating driver",
|
|
17
|
+
hibernatable,
|
|
18
|
+
});
|
|
19
|
+
// Wait for WS to open
|
|
20
|
+
let websocket: WSContext | undefined;
|
|
21
|
+
|
|
22
|
+
const driver: ConnDriver = {
|
|
23
|
+
type: "websocket",
|
|
24
|
+
hibernatable,
|
|
25
|
+
rivetKitProtocol: {
|
|
26
|
+
sendMessage: (
|
|
27
|
+
actor: AnyActorInstance,
|
|
28
|
+
conn: AnyConn,
|
|
29
|
+
message: CachedSerializer<any, any, any>,
|
|
30
|
+
) => {
|
|
31
|
+
if (!websocket) {
|
|
32
|
+
actor.rLog.warn({
|
|
33
|
+
msg: "websocket not open",
|
|
34
|
+
connId: conn.id,
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (websocket.readyState !== DriverReadyState.OPEN) {
|
|
39
|
+
actor.rLog.warn({
|
|
40
|
+
msg: "attempting to send message to closed websocket, this is likely a bug in RivetKit",
|
|
41
|
+
connId: conn.id,
|
|
42
|
+
wsReadyState: websocket.readyState,
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const serialized = message.serialize(encoding);
|
|
48
|
+
|
|
49
|
+
actor.rLog.debug({
|
|
50
|
+
msg: "sending websocket message",
|
|
51
|
+
encoding: encoding,
|
|
52
|
+
dataType: typeof serialized,
|
|
53
|
+
isUint8Array: serialized instanceof Uint8Array,
|
|
54
|
+
isArrayBuffer: serialized instanceof ArrayBuffer,
|
|
55
|
+
dataLength:
|
|
56
|
+
(serialized as any).byteLength ||
|
|
57
|
+
(serialized as any).length,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Convert Uint8Array to ArrayBuffer for proper transmission
|
|
61
|
+
if (serialized instanceof Uint8Array) {
|
|
62
|
+
const buffer = serialized.buffer.slice(
|
|
63
|
+
serialized.byteOffset,
|
|
64
|
+
serialized.byteOffset + serialized.byteLength,
|
|
65
|
+
);
|
|
66
|
+
// Handle SharedArrayBuffer case
|
|
67
|
+
if (buffer instanceof SharedArrayBuffer) {
|
|
68
|
+
const arrayBuffer = new ArrayBuffer(buffer.byteLength);
|
|
69
|
+
new Uint8Array(arrayBuffer).set(new Uint8Array(buffer));
|
|
70
|
+
actor.rLog.debug({
|
|
71
|
+
msg: "converted SharedArrayBuffer to ArrayBuffer",
|
|
72
|
+
byteLength: arrayBuffer.byteLength,
|
|
73
|
+
});
|
|
74
|
+
websocket.send(arrayBuffer);
|
|
75
|
+
} else {
|
|
76
|
+
actor.rLog.debug({
|
|
77
|
+
msg: "sending ArrayBuffer",
|
|
78
|
+
byteLength: buffer.byteLength,
|
|
79
|
+
});
|
|
80
|
+
websocket.send(buffer);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
actor.rLog.debug({
|
|
84
|
+
msg: "sending string data",
|
|
85
|
+
length: (serialized as string).length,
|
|
86
|
+
});
|
|
87
|
+
websocket.send(serialized);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
disconnect: async (
|
|
93
|
+
_actor: AnyActorInstance,
|
|
94
|
+
_conn: AnyConn,
|
|
95
|
+
reason?: string,
|
|
96
|
+
) => {
|
|
97
|
+
if (!websocket) {
|
|
98
|
+
loggerWithoutContext().warn(
|
|
99
|
+
"disconnecting ws without websocket",
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Close socket
|
|
105
|
+
websocket.close(1000, reason);
|
|
106
|
+
|
|
107
|
+
// Create promise to wait for socket to close gracefully
|
|
108
|
+
await closePromise;
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
terminate: () => {
|
|
112
|
+
(websocket as any).terminate();
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
getConnectionReadyState: (
|
|
116
|
+
_actor: AnyActorInstance,
|
|
117
|
+
_conn: AnyConn,
|
|
118
|
+
): DriverReadyState | undefined => {
|
|
119
|
+
return websocket?.readyState ?? DriverReadyState.CONNECTING;
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
driver,
|
|
125
|
+
setWebSocket(ws) {
|
|
126
|
+
websocket = ws;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|