rivetkit 2.0.24-rc.1 → 2.0.25-rc.1
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/v1.ts +6 -0
- package/dist/schemas/actor-persist/v2.ts +9 -3
- package/dist/schemas/actor-persist/v3.ts +280 -0
- package/dist/schemas/client-protocol/v1.ts +6 -0
- package/dist/schemas/client-protocol/v2.ts +438 -0
- package/dist/schemas/file-system-driver/v1.ts +6 -0
- package/dist/schemas/file-system-driver/v2.ts +142 -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-ZTH3KYFH.cjs → chunk-3FG5OJ3G.cjs} +3 -3
- package/dist/tsup/{chunk-ZTH3KYFH.cjs.map → chunk-3FG5OJ3G.cjs.map} +1 -1
- package/dist/tsup/{chunk-BLK27ES3.js → chunk-6JN6W6G3.js} +44 -56
- package/dist/tsup/chunk-6JN6W6G3.js.map +1 -0
- package/dist/tsup/chunk-7IBNNGQ2.js +514 -0
- package/dist/tsup/chunk-7IBNNGQ2.js.map +1 -0
- package/dist/tsup/{chunk-36JJ4IQB.cjs → chunk-AZATXPR4.cjs} +4 -8
- package/dist/tsup/chunk-AZATXPR4.cjs.map +1 -0
- package/dist/tsup/chunk-B7MENRD5.cjs +5694 -0
- package/dist/tsup/chunk-B7MENRD5.cjs.map +1 -0
- package/dist/tsup/{chunk-BOMZS2TJ.js → chunk-BBVFDEYD.js} +9 -9
- package/dist/tsup/chunk-BBVFDEYD.js.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-2JYPS5YM.cjs → chunk-E63WZNMR.cjs} +6 -6
- package/dist/tsup/chunk-E63WZNMR.cjs.map +1 -0
- package/dist/tsup/{chunk-YBG6R7LX.js → chunk-EDGN4OC7.js} +3 -7
- package/dist/tsup/chunk-EDGN4OC7.js.map +1 -0
- package/dist/tsup/{chunk-BYMKMOBS.js → chunk-FLOQ3UWM.js} +1844 -1681
- package/dist/tsup/chunk-FLOQ3UWM.js.map +1 -0
- package/dist/tsup/{chunk-7L65NNWP.cjs → chunk-H7GV5DIW.cjs} +187 -185
- package/dist/tsup/chunk-H7GV5DIW.cjs.map +1 -0
- package/dist/tsup/{chunk-227FEWMB.js → chunk-HZYZ7JSF.js} +3322 -2251
- package/dist/tsup/chunk-HZYZ7JSF.js.map +1 -0
- package/dist/tsup/{chunk-FX7TWFQR.js → chunk-IDJK7ILQ.js} +2 -6
- package/dist/tsup/chunk-IDJK7ILQ.js.map +1 -0
- package/dist/tsup/{chunk-VHGY7PU5.cjs → chunk-ILFXA4AL.cjs} +1900 -1737
- package/dist/tsup/chunk-ILFXA4AL.cjs.map +1 -0
- package/dist/tsup/chunk-MV6M3FDL.cjs +514 -0
- package/dist/tsup/chunk-MV6M3FDL.cjs.map +1 -0
- package/dist/tsup/{chunk-PLUN2NQT.js → chunk-NWBKMCWC.js} +189 -187
- package/dist/tsup/chunk-NWBKMCWC.js.map +1 -0
- package/dist/tsup/{chunk-CD33GT6Z.js → chunk-QIHBDXTO.js} +2 -2
- package/dist/tsup/{chunk-G64QUEDJ.js → chunk-W6RDS6NW.js} +23 -28
- package/dist/tsup/chunk-W6RDS6NW.js.map +1 -0
- package/dist/tsup/{chunk-INNFK746.cjs → chunk-WQU4M4ZC.cjs} +10 -14
- package/dist/tsup/chunk-WQU4M4ZC.cjs.map +1 -0
- package/dist/tsup/{chunk-SHVX2QUR.cjs → chunk-XKZA47XS.cjs} +17 -17
- package/dist/tsup/chunk-XKZA47XS.cjs.map +1 -0
- package/dist/tsup/{chunk-HHFKKVLR.cjs → chunk-YHWIOWVA.cjs} +45 -57
- package/dist/tsup/chunk-YHWIOWVA.cjs.map +1 -0
- package/dist/tsup/{chunk-YBHYXIP6.js → chunk-YVL6IRUM.js} +3 -3
- package/dist/tsup/chunk-YVL6IRUM.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-QIHBDXTO.js.map} +0 -0
package/src/actor/conn.ts
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import * as cbor from "cbor-x";
|
|
2
|
-
import invariant from "invariant";
|
|
3
|
-
import { PersistedHibernatableWebSocket } from "@/schemas/actor-persist/mod";
|
|
4
|
-
import type * as protocol from "@/schemas/client-protocol/mod";
|
|
5
|
-
import { TO_CLIENT_VERSIONED } from "@/schemas/client-protocol/versioned";
|
|
6
|
-
import { arrayBuffersEqual, bufferToArrayBuffer } from "@/utils";
|
|
7
|
-
import {
|
|
8
|
-
CONN_DRIVERS,
|
|
9
|
-
ConnDriverKind,
|
|
10
|
-
type ConnDriverState,
|
|
11
|
-
ConnReadyState,
|
|
12
|
-
getConnDriverKindFromState,
|
|
13
|
-
} from "./conn-drivers";
|
|
14
|
-
import type { ConnSocket } from "./conn-socket";
|
|
15
|
-
import type { AnyDatabaseProvider } from "./database";
|
|
16
|
-
import * as errors from "./errors";
|
|
17
|
-
import { type ActorInstance, PERSIST_SYMBOL } from "./instance";
|
|
18
|
-
import type { PersistedConn } from "./persisted";
|
|
19
|
-
import { CachedSerializer } from "./protocol/serde";
|
|
20
|
-
import { generateSecureToken } from "./utils";
|
|
21
|
-
|
|
22
|
-
export function generateConnId(): string {
|
|
23
|
-
return crypto.randomUUID();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function generateConnToken(): string {
|
|
27
|
-
return generateSecureToken(32);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function generateConnRequestId(): string {
|
|
31
|
-
return crypto.randomUUID();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type ConnId = string;
|
|
35
|
-
|
|
36
|
-
export type AnyConn = Conn<any, any, any, any, any, any>;
|
|
37
|
-
|
|
38
|
-
export type ConnectionStatus = "connected" | "reconnecting";
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Represents a client connection to a actor.
|
|
42
|
-
*
|
|
43
|
-
* Manages connection-specific data and controls the connection lifecycle.
|
|
44
|
-
*
|
|
45
|
-
* @see {@link https://rivet.dev/docs/connections|Connection Documentation}
|
|
46
|
-
*/
|
|
47
|
-
export class Conn<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
|
|
48
|
-
subscriptions: Set<string> = new Set<string>();
|
|
49
|
-
|
|
50
|
-
// TODO: Remove this cyclical reference
|
|
51
|
-
#actor: ActorInstance<S, CP, CS, V, I, DB>;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* The proxied state that notifies of changes automatically.
|
|
55
|
-
*
|
|
56
|
-
* Any data that should be stored indefinitely should be held within this object.
|
|
57
|
-
*/
|
|
58
|
-
__persist: PersistedConn<CP, CS>;
|
|
59
|
-
|
|
60
|
-
get __driverState(): ConnDriverState | undefined {
|
|
61
|
-
return this.__socket?.driverState;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Socket connected to this connection.
|
|
66
|
-
*
|
|
67
|
-
* If undefined, then nothing is connected to this.
|
|
68
|
-
*/
|
|
69
|
-
__socket?: ConnSocket;
|
|
70
|
-
|
|
71
|
-
get __status(): ConnectionStatus {
|
|
72
|
-
// TODO: isHibernatible might be true while the actual hibernatable websocket has disconnected
|
|
73
|
-
if (this.__socket || this.isHibernatable) {
|
|
74
|
-
return "connected";
|
|
75
|
-
} else {
|
|
76
|
-
return "reconnecting";
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public get params(): CP {
|
|
81
|
-
return this.__persist.params;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public get __stateEnabled() {
|
|
85
|
-
return this.#actor.connStateEnabled;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Gets the current state of the connection.
|
|
90
|
-
*
|
|
91
|
-
* Throws an error if the state is not enabled.
|
|
92
|
-
*/
|
|
93
|
-
public get state(): CS {
|
|
94
|
-
this.#validateStateEnabled();
|
|
95
|
-
if (!this.__persist.state) throw new Error("state should exists");
|
|
96
|
-
return this.__persist.state;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Sets the state of the connection.
|
|
101
|
-
*
|
|
102
|
-
* Throws an error if the state is not enabled.
|
|
103
|
-
*/
|
|
104
|
-
public set state(value: CS) {
|
|
105
|
-
this.#validateStateEnabled();
|
|
106
|
-
this.__persist.state = value;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Unique identifier for the connection.
|
|
111
|
-
*/
|
|
112
|
-
public get id(): ConnId {
|
|
113
|
-
return this.__persist.connId;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Token used to authenticate this request.
|
|
118
|
-
*/
|
|
119
|
-
public get _token(): string {
|
|
120
|
-
return this.__persist.token;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Status of the connection.
|
|
125
|
-
*/
|
|
126
|
-
public get status(): ConnectionStatus {
|
|
127
|
-
return this.__status;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @experimental
|
|
132
|
-
*
|
|
133
|
-
* If the underlying connection can hibernate.
|
|
134
|
-
*/
|
|
135
|
-
public get isHibernatable(): boolean {
|
|
136
|
-
if (!this.__persist.hibernatableRequestId) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
return (
|
|
140
|
-
this.#actor[PERSIST_SYMBOL].hibernatableWebSocket.findIndex((x) =>
|
|
141
|
-
arrayBuffersEqual(
|
|
142
|
-
x.requestId,
|
|
143
|
-
this.__persist.hibernatableRequestId!,
|
|
144
|
-
),
|
|
145
|
-
) > -1
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Timestamp of the last time the connection was seen, i.e. the last time the connection was active and checked for liveness.
|
|
151
|
-
*/
|
|
152
|
-
public get lastSeen(): number {
|
|
153
|
-
return this.__persist.lastSeen;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Initializes a new instance of the Connection class.
|
|
158
|
-
*
|
|
159
|
-
* This should only be constructed by {@link Actor}.
|
|
160
|
-
*
|
|
161
|
-
* @protected
|
|
162
|
-
*/
|
|
163
|
-
public constructor(
|
|
164
|
-
actor: ActorInstance<S, CP, CS, V, I, DB>,
|
|
165
|
-
persist: PersistedConn<CP, CS>,
|
|
166
|
-
) {
|
|
167
|
-
this.#actor = actor;
|
|
168
|
-
this.__persist = persist;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
#validateStateEnabled() {
|
|
172
|
-
if (!this.__stateEnabled) {
|
|
173
|
-
throw new errors.ConnStateNotEnabled();
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Sends a WebSocket message to the client.
|
|
179
|
-
*
|
|
180
|
-
* @param message - The message to send.
|
|
181
|
-
*
|
|
182
|
-
* @protected
|
|
183
|
-
*/
|
|
184
|
-
public _sendMessage(message: CachedSerializer<protocol.ToClient>) {
|
|
185
|
-
if (this.__driverState) {
|
|
186
|
-
const driverKind = getConnDriverKindFromState(this.__driverState);
|
|
187
|
-
const driver = CONN_DRIVERS[driverKind];
|
|
188
|
-
if (driver.sendMessage) {
|
|
189
|
-
driver.sendMessage(
|
|
190
|
-
this.#actor,
|
|
191
|
-
this,
|
|
192
|
-
(this.__driverState as any)[driverKind],
|
|
193
|
-
message,
|
|
194
|
-
);
|
|
195
|
-
} else {
|
|
196
|
-
this.#actor.rLog.debug({
|
|
197
|
-
msg: "conn driver does not support sending messages",
|
|
198
|
-
conn: this.id,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
} else {
|
|
202
|
-
this.#actor.rLog.warn({
|
|
203
|
-
msg: "missing connection driver state for send message",
|
|
204
|
-
conn: this.id,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Sends an event with arguments to the client.
|
|
211
|
-
*
|
|
212
|
-
* @param eventName - The name of the event.
|
|
213
|
-
* @param args - The arguments for the event.
|
|
214
|
-
* @see {@link https://rivet.dev/docs/events|Events Documentation}
|
|
215
|
-
*/
|
|
216
|
-
public send(eventName: string, ...args: unknown[]) {
|
|
217
|
-
this.#actor.inspector.emitter.emit("eventFired", {
|
|
218
|
-
type: "event",
|
|
219
|
-
eventName,
|
|
220
|
-
args,
|
|
221
|
-
connId: this.id,
|
|
222
|
-
});
|
|
223
|
-
this._sendMessage(
|
|
224
|
-
new CachedSerializer<protocol.ToClient>(
|
|
225
|
-
{
|
|
226
|
-
body: {
|
|
227
|
-
tag: "Event",
|
|
228
|
-
val: {
|
|
229
|
-
name: eventName,
|
|
230
|
-
args: bufferToArrayBuffer(cbor.encode(args)),
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
TO_CLIENT_VERSIONED,
|
|
235
|
-
),
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Disconnects the client with an optional reason.
|
|
241
|
-
*
|
|
242
|
-
* @param reason - The reason for disconnection.
|
|
243
|
-
*/
|
|
244
|
-
public async disconnect(reason?: string) {
|
|
245
|
-
if (this.__socket && this.__driverState) {
|
|
246
|
-
const driverKind = getConnDriverKindFromState(this.__driverState);
|
|
247
|
-
const driver = CONN_DRIVERS[driverKind];
|
|
248
|
-
if (driver.disconnect) {
|
|
249
|
-
driver.disconnect(
|
|
250
|
-
this.#actor,
|
|
251
|
-
this,
|
|
252
|
-
(this.__driverState as any)[driverKind],
|
|
253
|
-
reason,
|
|
254
|
-
);
|
|
255
|
-
} else {
|
|
256
|
-
this.#actor.rLog.debug({
|
|
257
|
-
msg: "no disconnect handler for conn driver",
|
|
258
|
-
conn: this.id,
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
this.#actor.__connDisconnected(this, true, this.__socket.requestId);
|
|
263
|
-
} else {
|
|
264
|
-
this.#actor.rLog.warn({
|
|
265
|
-
msg: "missing connection driver state for disconnect",
|
|
266
|
-
conn: this.id,
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
this.__socket = undefined;
|
|
271
|
-
}
|
|
272
|
-
}
|