rivetkit 2.0.3 → 2.0.5
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/README.md +11 -0
- package/dist/schemas/actor-persist/v1.ts +21 -24
- package/dist/schemas/client-protocol/v1.ts +6 -0
- package/dist/tsup/actor/errors.cjs +10 -2
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/actor/errors.d.cts +17 -4
- package/dist/tsup/actor/errors.d.ts +17 -4
- package/dist/tsup/actor/errors.js +11 -3
- package/dist/tsup/{chunk-4NSUQZ2H.js → chunk-2MD57QF4.js} +119 -115
- package/dist/tsup/chunk-2MD57QF4.js.map +1 -0
- package/dist/tsup/{chunk-GIR3AFFI.cjs → chunk-5QGQK44L.cjs} +103 -44
- package/dist/tsup/chunk-5QGQK44L.cjs.map +1 -0
- package/dist/tsup/chunk-5YTI25C3.cjs +250 -0
- package/dist/tsup/chunk-5YTI25C3.cjs.map +1 -0
- package/dist/tsup/chunk-B2QGJGZQ.js +338 -0
- package/dist/tsup/chunk-B2QGJGZQ.js.map +1 -0
- package/dist/tsup/{chunk-3H7O2A7I.js → chunk-CFFKMUYH.js} +61 -22
- package/dist/tsup/chunk-CFFKMUYH.js.map +1 -0
- package/dist/tsup/{chunk-FLMTTN27.js → chunk-CKA54YQN.js} +15 -8
- package/dist/tsup/chunk-CKA54YQN.js.map +1 -0
- package/dist/tsup/chunk-D7NWUCRK.cjs +20 -0
- package/dist/tsup/chunk-D7NWUCRK.cjs.map +1 -0
- package/dist/tsup/{chunk-FCCPJNMA.cjs → chunk-FGFT4FVX.cjs} +12 -27
- package/dist/tsup/chunk-FGFT4FVX.cjs.map +1 -0
- package/dist/tsup/chunk-I5VTWPHW.js +20 -0
- package/dist/tsup/chunk-I5VTWPHW.js.map +1 -0
- package/dist/tsup/{chunk-6WKQDDUD.cjs → chunk-IRMBWX36.cjs} +146 -142
- package/dist/tsup/chunk-IRMBWX36.cjs.map +1 -0
- package/dist/tsup/chunk-L7QRXNWP.js +6562 -0
- package/dist/tsup/chunk-L7QRXNWP.js.map +1 -0
- package/dist/tsup/{chunk-R2OPSKIV.cjs → chunk-LZIBTLEY.cjs} +20 -13
- package/dist/tsup/chunk-LZIBTLEY.cjs.map +1 -0
- package/dist/tsup/chunk-MRZS2J4X.cjs +6562 -0
- package/dist/tsup/chunk-MRZS2J4X.cjs.map +1 -0
- package/dist/tsup/{chunk-PO4VLDWA.js → chunk-PG3K2LI7.js} +3 -5
- package/dist/tsup/chunk-PG3K2LI7.js.map +1 -0
- package/dist/tsup/{chunk-TZJKSBUQ.cjs → chunk-PHSQJ6QI.cjs} +3 -5
- package/dist/tsup/chunk-PHSQJ6QI.cjs.map +1 -0
- package/dist/tsup/chunk-RM2SVURR.cjs +338 -0
- package/dist/tsup/chunk-RM2SVURR.cjs.map +1 -0
- package/dist/tsup/{chunk-OGAPU3UG.cjs → chunk-WADSS5X4.cjs} +66 -27
- package/dist/tsup/chunk-WADSS5X4.cjs.map +1 -0
- package/dist/tsup/chunk-WNGOBAA7.js +250 -0
- package/dist/tsup/chunk-WNGOBAA7.js.map +1 -0
- package/dist/tsup/{chunk-INGJP237.js → chunk-YPZFLUO6.js} +103 -44
- package/dist/tsup/chunk-YPZFLUO6.js.map +1 -0
- package/dist/tsup/{chunk-6PDXBYI5.js → chunk-YW6Y6VNE.js} +8 -23
- package/dist/tsup/chunk-YW6Y6VNE.js.map +1 -0
- package/dist/tsup/client/mod.cjs +10 -10
- package/dist/tsup/client/mod.d.cts +7 -13
- package/dist/tsup/client/mod.d.ts +7 -13
- package/dist/tsup/client/mod.js +9 -9
- package/dist/tsup/common/log.cjs +12 -4
- package/dist/tsup/common/log.cjs.map +1 -1
- package/dist/tsup/common/log.d.cts +23 -17
- package/dist/tsup/common/log.d.ts +23 -17
- package/dist/tsup/common/log.js +15 -7
- package/dist/tsup/common/websocket.cjs +5 -5
- package/dist/tsup/common/websocket.js +4 -4
- package/dist/tsup/{common-CpqORuCq.d.cts → common-CXCe7s6i.d.cts} +2 -2
- package/dist/tsup/{common-CpqORuCq.d.ts → common-CXCe7s6i.d.ts} +2 -2
- package/dist/tsup/{connection-BwUMoe6n.d.ts → connection-BvE-Oq7t.d.ts} +215 -234
- package/dist/tsup/{connection-BR_Ve4ku.d.cts → connection-DTzmWwU5.d.cts} +215 -234
- package/dist/tsup/driver-helpers/mod.cjs +6 -9
- package/dist/tsup/driver-helpers/mod.cjs.map +1 -1
- package/dist/tsup/driver-helpers/mod.d.cts +5 -6
- package/dist/tsup/driver-helpers/mod.d.ts +5 -6
- package/dist/tsup/driver-helpers/mod.js +6 -9
- package/dist/tsup/driver-test-suite/mod.cjs +615 -1357
- package/dist/tsup/driver-test-suite/mod.cjs.map +1 -1
- package/dist/tsup/driver-test-suite/mod.d.cts +12 -6
- package/dist/tsup/driver-test-suite/mod.d.ts +12 -6
- package/dist/tsup/driver-test-suite/mod.js +1334 -2076
- package/dist/tsup/driver-test-suite/mod.js.map +1 -1
- package/dist/tsup/inspector/mod.cjs +6 -8
- package/dist/tsup/inspector/mod.cjs.map +1 -1
- package/dist/tsup/inspector/mod.d.cts +3 -3
- package/dist/tsup/inspector/mod.d.ts +3 -3
- package/dist/tsup/inspector/mod.js +8 -10
- package/dist/tsup/mod.cjs +9 -15
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +47 -42
- package/dist/tsup/mod.d.ts +47 -42
- package/dist/tsup/mod.js +10 -16
- package/dist/tsup/{router-endpoints-DAbqVFx2.d.ts → router-endpoints-CctffZNL.d.cts} +2 -3
- package/dist/tsup/{router-endpoints-AYkXG8Tl.d.cts → router-endpoints-DFm1BglJ.d.ts} +2 -3
- package/dist/tsup/test/mod.cjs +10 -14
- package/dist/tsup/test/mod.cjs.map +1 -1
- package/dist/tsup/test/mod.d.cts +4 -5
- package/dist/tsup/test/mod.d.ts +4 -5
- package/dist/tsup/test/mod.js +9 -13
- package/dist/tsup/{utils-CT0cv4jd.d.ts → utils-fwx3o3K9.d.cts} +1 -0
- package/dist/tsup/{utils-CT0cv4jd.d.cts → utils-fwx3o3K9.d.ts} +1 -0
- package/dist/tsup/utils.cjs +5 -3
- package/dist/tsup/utils.cjs.map +1 -1
- package/dist/tsup/utils.d.cts +19 -2
- package/dist/tsup/utils.d.ts +19 -2
- package/dist/tsup/utils.js +4 -2
- package/package.json +6 -6
- package/src/actor/action.ts +1 -5
- package/src/actor/config.ts +27 -295
- package/src/actor/connection.ts +9 -12
- package/src/actor/context.ts +1 -4
- package/src/actor/definition.ts +7 -11
- package/src/actor/errors.ts +98 -36
- package/src/actor/generic-conn-driver.ts +28 -16
- package/src/actor/instance.ts +177 -133
- package/src/actor/log.ts +4 -13
- package/src/actor/mod.ts +0 -5
- package/src/actor/protocol/old.ts +42 -26
- package/src/actor/protocol/serde.ts +1 -1
- package/src/actor/router-endpoints.ts +47 -39
- package/src/actor/router.ts +22 -19
- package/src/actor/unstable-react.ts +1 -1
- package/src/actor/utils.ts +6 -2
- package/src/client/actor-common.ts +1 -1
- package/src/client/actor-conn.ts +152 -91
- package/src/client/actor-handle.ts +85 -25
- package/src/client/actor-query.ts +65 -0
- package/src/client/client.ts +29 -98
- package/src/client/config.ts +44 -0
- package/src/client/errors.ts +1 -0
- package/src/client/log.ts +2 -4
- package/src/client/mod.ts +16 -12
- package/src/client/raw-utils.ts +82 -25
- package/src/client/utils.ts +5 -3
- package/src/common/fake-event-source.ts +10 -9
- package/src/common/inline-websocket-adapter2.ts +39 -30
- package/src/common/log.ts +176 -101
- package/src/common/logfmt.ts +21 -30
- package/src/common/router.ts +12 -19
- package/src/common/utils.ts +27 -13
- package/src/common/websocket.ts +0 -1
- package/src/driver-helpers/mod.ts +1 -1
- package/src/driver-test-suite/log.ts +1 -3
- package/src/driver-test-suite/mod.ts +87 -61
- package/src/driver-test-suite/test-inline-client-driver.ts +441 -255
- package/src/driver-test-suite/tests/actor-error-handling.ts +4 -12
- package/src/driver-test-suite/tests/actor-handle.ts +33 -0
- package/src/driver-test-suite/tests/actor-inspector.ts +2 -1
- package/src/driver-test-suite/tests/manager-driver.ts +5 -3
- package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -226
- package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -392
- package/src/driver-test-suite/tests/request-access.ts +112 -126
- package/src/driver-test-suite/utils.ts +10 -6
- package/src/drivers/default.ts +7 -4
- package/src/drivers/engine/actor-driver.ts +22 -13
- package/src/drivers/engine/config.ts +2 -10
- package/src/drivers/engine/kv.ts +1 -1
- package/src/drivers/engine/log.ts +1 -3
- package/src/drivers/engine/mod.ts +2 -3
- package/src/drivers/file-system/actor.ts +1 -1
- package/src/drivers/file-system/global-state.ts +36 -21
- package/src/drivers/file-system/log.ts +1 -3
- package/src/drivers/file-system/manager.ts +33 -15
- package/src/inspector/config.ts +9 -4
- package/src/inspector/log.ts +1 -1
- package/src/inspector/manager.ts +2 -2
- package/src/inspector/utils.ts +1 -1
- package/src/manager/driver.ts +10 -2
- package/src/manager/hono-websocket-adapter.ts +21 -12
- package/src/manager/log.ts +2 -4
- package/src/manager/mod.ts +1 -1
- package/src/manager/router.ts +378 -1390
- package/src/manager-api/routes/actors-create.ts +16 -0
- package/src/manager-api/routes/actors-delete.ts +4 -0
- package/src/manager-api/routes/actors-get-by-id.ts +7 -0
- package/src/manager-api/routes/actors-get-or-create-by-id.ts +29 -0
- package/src/manager-api/routes/actors-get.ts +7 -0
- package/src/manager-api/routes/common.ts +18 -0
- package/src/mod.ts +0 -2
- package/src/registry/config.ts +1 -1
- package/src/registry/log.ts +2 -4
- package/src/registry/mod.ts +63 -34
- package/src/registry/run-config.ts +39 -26
- package/src/registry/serve.ts +4 -5
- package/src/remote-manager-driver/actor-http-client.ts +74 -0
- package/src/remote-manager-driver/actor-websocket-client.ts +64 -0
- package/src/remote-manager-driver/api-endpoints.ts +79 -0
- package/src/remote-manager-driver/api-utils.ts +46 -0
- package/src/remote-manager-driver/log.ts +5 -0
- package/src/remote-manager-driver/mod.ts +275 -0
- package/src/{drivers/engine → remote-manager-driver}/ws-proxy.ts +24 -14
- package/src/serde.ts +8 -2
- package/src/test/log.ts +1 -3
- package/src/test/mod.ts +17 -16
- package/src/utils.ts +53 -0
- package/dist/tsup/chunk-2CRLFV6Z.cjs +0 -202
- package/dist/tsup/chunk-2CRLFV6Z.cjs.map +0 -1
- package/dist/tsup/chunk-3H7O2A7I.js.map +0 -1
- package/dist/tsup/chunk-42I3OZ3Q.js +0 -15
- package/dist/tsup/chunk-42I3OZ3Q.js.map +0 -1
- package/dist/tsup/chunk-4NSUQZ2H.js.map +0 -1
- package/dist/tsup/chunk-6PDXBYI5.js.map +0 -1
- package/dist/tsup/chunk-6WKQDDUD.cjs.map +0 -1
- package/dist/tsup/chunk-CTBOSFUH.cjs +0 -116
- package/dist/tsup/chunk-CTBOSFUH.cjs.map +0 -1
- package/dist/tsup/chunk-EGVZZFE2.js +0 -2857
- package/dist/tsup/chunk-EGVZZFE2.js.map +0 -1
- package/dist/tsup/chunk-FCCPJNMA.cjs.map +0 -1
- package/dist/tsup/chunk-FLMTTN27.js.map +0 -1
- package/dist/tsup/chunk-GIR3AFFI.cjs.map +0 -1
- package/dist/tsup/chunk-INGJP237.js.map +0 -1
- package/dist/tsup/chunk-KJCJLKRM.js +0 -116
- package/dist/tsup/chunk-KJCJLKRM.js.map +0 -1
- package/dist/tsup/chunk-KUPQZYUQ.cjs +0 -15
- package/dist/tsup/chunk-KUPQZYUQ.cjs.map +0 -1
- package/dist/tsup/chunk-O2MBYIXO.cjs +0 -2857
- package/dist/tsup/chunk-O2MBYIXO.cjs.map +0 -1
- package/dist/tsup/chunk-OGAPU3UG.cjs.map +0 -1
- package/dist/tsup/chunk-OV6AYD4S.js +0 -4406
- package/dist/tsup/chunk-OV6AYD4S.js.map +0 -1
- package/dist/tsup/chunk-PO4VLDWA.js.map +0 -1
- package/dist/tsup/chunk-R2OPSKIV.cjs.map +0 -1
- package/dist/tsup/chunk-TZJKSBUQ.cjs.map +0 -1
- package/dist/tsup/chunk-UBUC5C3G.cjs +0 -189
- package/dist/tsup/chunk-UBUC5C3G.cjs.map +0 -1
- package/dist/tsup/chunk-UIM22YJL.cjs +0 -4406
- package/dist/tsup/chunk-UIM22YJL.cjs.map +0 -1
- package/dist/tsup/chunk-URVFQMYI.cjs +0 -230
- package/dist/tsup/chunk-URVFQMYI.cjs.map +0 -1
- package/dist/tsup/chunk-UVUPOS46.js +0 -230
- package/dist/tsup/chunk-UVUPOS46.js.map +0 -1
- package/dist/tsup/chunk-VRRHBNJC.js +0 -189
- package/dist/tsup/chunk-VRRHBNJC.js.map +0 -1
- package/dist/tsup/chunk-XFSS33EQ.js +0 -202
- package/dist/tsup/chunk-XFSS33EQ.js.map +0 -1
- package/src/client/http-client-driver.ts +0 -326
- package/src/driver-test-suite/tests/actor-auth.ts +0 -591
- package/src/drivers/engine/api-endpoints.ts +0 -128
- package/src/drivers/engine/api-utils.ts +0 -70
- package/src/drivers/engine/manager-driver.ts +0 -391
- package/src/inline-client-driver/log.ts +0 -7
- package/src/inline-client-driver/mod.ts +0 -385
- package/src/manager/auth.ts +0 -121
- /package/src/{drivers/engine → actor}/keys.test.ts +0 -0
- /package/src/{drivers/engine → actor}/keys.ts +0 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Context as HonoContext } from "hono";
|
|
2
|
+
import * as errors from "@/actor/errors";
|
|
3
|
+
import type { ManagerDriver } from "@/driver-helpers/mod";
|
|
4
|
+
import type { ActorQuery } from "@/manager/protocol/query";
|
|
5
|
+
import { logger } from "./log";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Query the manager driver to get or create a actor based on the provided query
|
|
9
|
+
*/
|
|
10
|
+
export async function queryActor(
|
|
11
|
+
c: HonoContext | undefined,
|
|
12
|
+
query: ActorQuery,
|
|
13
|
+
managerDriver: ManagerDriver,
|
|
14
|
+
): Promise<{ actorId: string }> {
|
|
15
|
+
logger().debug({ msg: "querying actor", query: JSON.stringify(query) });
|
|
16
|
+
let actorOutput: { actorId: string };
|
|
17
|
+
if ("getForId" in query) {
|
|
18
|
+
const output = await managerDriver.getForId({
|
|
19
|
+
c,
|
|
20
|
+
name: query.getForId.name,
|
|
21
|
+
actorId: query.getForId.actorId,
|
|
22
|
+
});
|
|
23
|
+
if (!output) throw new errors.ActorNotFound(query.getForId.actorId);
|
|
24
|
+
actorOutput = output;
|
|
25
|
+
} else if ("getForKey" in query) {
|
|
26
|
+
const existingActor = await managerDriver.getWithKey({
|
|
27
|
+
c,
|
|
28
|
+
name: query.getForKey.name,
|
|
29
|
+
key: query.getForKey.key,
|
|
30
|
+
});
|
|
31
|
+
if (!existingActor) {
|
|
32
|
+
throw new errors.ActorNotFound(
|
|
33
|
+
`${query.getForKey.name}:${JSON.stringify(query.getForKey.key)}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
actorOutput = existingActor;
|
|
37
|
+
} else if ("getOrCreateForKey" in query) {
|
|
38
|
+
const getOrCreateOutput = await managerDriver.getOrCreateWithKey({
|
|
39
|
+
c,
|
|
40
|
+
name: query.getOrCreateForKey.name,
|
|
41
|
+
key: query.getOrCreateForKey.key,
|
|
42
|
+
input: query.getOrCreateForKey.input,
|
|
43
|
+
region: query.getOrCreateForKey.region,
|
|
44
|
+
});
|
|
45
|
+
actorOutput = {
|
|
46
|
+
actorId: getOrCreateOutput.actorId,
|
|
47
|
+
};
|
|
48
|
+
} else if ("create" in query) {
|
|
49
|
+
const createOutput = await managerDriver.createActor({
|
|
50
|
+
c,
|
|
51
|
+
name: query.create.name,
|
|
52
|
+
key: query.create.key,
|
|
53
|
+
input: query.create.input,
|
|
54
|
+
region: query.create.region,
|
|
55
|
+
});
|
|
56
|
+
actorOutput = {
|
|
57
|
+
actorId: createOutput.actorId,
|
|
58
|
+
};
|
|
59
|
+
} else {
|
|
60
|
+
throw new errors.InvalidRequest("Invalid query format");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
logger().debug({ msg: "actor query result", actorId: actorOutput.actorId });
|
|
64
|
+
return { actorId: actorOutput.actorId };
|
|
65
|
+
}
|
package/src/client/client.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import type { Context as HonoContext } from "hono";
|
|
2
|
-
import type { WebSocket } from "ws";
|
|
3
1
|
import type { AnyActorDefinition } from "@/actor/definition";
|
|
4
2
|
import type { Transport } from "@/actor/protocol/old";
|
|
5
3
|
import type { Encoding } from "@/actor/protocol/serde";
|
|
6
|
-
import type {
|
|
4
|
+
import type { ManagerDriver } from "@/driver-helpers/mod";
|
|
7
5
|
import type { ActorQuery } from "@/manager/protocol/query";
|
|
8
6
|
import type { Registry } from "@/mod";
|
|
9
|
-
import type { ToServer } from "@/schemas/client-protocol/mod";
|
|
10
7
|
import type { ActorActionFunction } from "./actor-common";
|
|
11
8
|
import {
|
|
12
9
|
type ActorConn,
|
|
@@ -14,8 +11,12 @@ import {
|
|
|
14
11
|
CONNECT_SYMBOL,
|
|
15
12
|
} from "./actor-conn";
|
|
16
13
|
import { type ActorHandle, ActorHandleRaw } from "./actor-handle";
|
|
14
|
+
import { queryActor } from "./actor-query";
|
|
15
|
+
import type { ClientConfig } from "./config";
|
|
17
16
|
import { logger } from "./log";
|
|
18
17
|
|
|
18
|
+
export type { ClientConfig, ClientConfigInput } from "./config";
|
|
19
|
+
|
|
19
20
|
/** Extract the actor registry from the registry definition. */
|
|
20
21
|
export type ExtractActorsFromRegistry<A extends Registry<any>> =
|
|
21
22
|
A extends Registry<infer Actors> ? Actors : never;
|
|
@@ -78,15 +79,6 @@ export interface ActorAccessor<AD extends AnyActorDefinition> {
|
|
|
78
79
|
): Promise<ActorHandle<AD>>;
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
/**
|
|
82
|
-
* Options for configuring the client.
|
|
83
|
-
* @typedef {Object} ClientOptions
|
|
84
|
-
*/
|
|
85
|
-
export interface ClientOptions {
|
|
86
|
-
encoding?: Encoding;
|
|
87
|
-
transport?: Transport;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
82
|
/**
|
|
91
83
|
* Options for querying actors.
|
|
92
84
|
* @typedef {Object} QueryOptions
|
|
@@ -140,8 +132,8 @@ export interface CreateOptions extends QueryOptions {
|
|
|
140
132
|
* @typedef {Object} Region
|
|
141
133
|
* @property {string} id - The region ID.
|
|
142
134
|
* @property {string} name - The region name.
|
|
143
|
-
* @see {@link https://rivet.
|
|
144
|
-
* @see {@link https://rivet.
|
|
135
|
+
* @see {@link https://rivet.dev/docs/edge|Edge Networking}
|
|
136
|
+
* @see {@link https://rivet.dev/docs/regions|Available Regions}
|
|
145
137
|
*/
|
|
146
138
|
export interface Region {
|
|
147
139
|
/**
|
|
@@ -159,89 +151,29 @@ export const ACTOR_CONNS_SYMBOL = Symbol("actorConns");
|
|
|
159
151
|
export const CREATE_ACTOR_CONN_PROXY = Symbol("createActorConnProxy");
|
|
160
152
|
export const TRANSPORT_SYMBOL = Symbol("transport");
|
|
161
153
|
|
|
162
|
-
export interface ClientDriver {
|
|
163
|
-
action<Args extends Array<unknown> = unknown[], Response = unknown>(
|
|
164
|
-
c: HonoContext | undefined,
|
|
165
|
-
actorQuery: ActorQuery,
|
|
166
|
-
encoding: Encoding,
|
|
167
|
-
params: unknown,
|
|
168
|
-
name: string,
|
|
169
|
-
args: Args,
|
|
170
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
171
|
-
): Promise<Response>;
|
|
172
|
-
resolveActorId(
|
|
173
|
-
c: HonoContext | undefined,
|
|
174
|
-
actorQuery: ActorQuery,
|
|
175
|
-
encodingKind: Encoding,
|
|
176
|
-
params: unknown,
|
|
177
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
178
|
-
): Promise<string>;
|
|
179
|
-
connectWebSocket(
|
|
180
|
-
c: HonoContext | undefined,
|
|
181
|
-
actorQuery: ActorQuery,
|
|
182
|
-
encodingKind: Encoding,
|
|
183
|
-
params: unknown,
|
|
184
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
185
|
-
): Promise<WebSocket>;
|
|
186
|
-
connectSse(
|
|
187
|
-
c: HonoContext | undefined,
|
|
188
|
-
actorQuery: ActorQuery,
|
|
189
|
-
encodingKind: Encoding,
|
|
190
|
-
params: unknown,
|
|
191
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
192
|
-
): Promise<UniversalEventSource>;
|
|
193
|
-
sendHttpMessage(
|
|
194
|
-
c: HonoContext | undefined,
|
|
195
|
-
actorId: string,
|
|
196
|
-
encoding: Encoding,
|
|
197
|
-
connectionId: string,
|
|
198
|
-
connectionToken: string,
|
|
199
|
-
message: ToServer,
|
|
200
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
201
|
-
): Promise<void>;
|
|
202
|
-
rawHttpRequest(
|
|
203
|
-
c: HonoContext | undefined,
|
|
204
|
-
actorQuery: ActorQuery,
|
|
205
|
-
encoding: Encoding,
|
|
206
|
-
params: unknown,
|
|
207
|
-
path: string,
|
|
208
|
-
init: RequestInit,
|
|
209
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
210
|
-
): Promise<Response>;
|
|
211
|
-
rawWebSocket(
|
|
212
|
-
c: HonoContext | undefined,
|
|
213
|
-
actorQuery: ActorQuery,
|
|
214
|
-
encoding: Encoding,
|
|
215
|
-
params: unknown,
|
|
216
|
-
path: string,
|
|
217
|
-
protocols: string | string[] | undefined,
|
|
218
|
-
opts: { signal?: AbortSignal } | undefined,
|
|
219
|
-
): Promise<WebSocket>;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
154
|
/**
|
|
223
155
|
* Client for managing & connecting to actors.
|
|
224
156
|
*
|
|
225
157
|
* @template A The actors map type that defines the available actors.
|
|
226
|
-
* @see {@link https://rivet.
|
|
158
|
+
* @see {@link https://rivet.dev/docs/manage|Create & Manage Actors}
|
|
227
159
|
*/
|
|
228
160
|
export class ClientRaw {
|
|
229
161
|
#disposed = false;
|
|
230
162
|
|
|
231
163
|
[ACTOR_CONNS_SYMBOL] = new Set<ActorConnRaw>();
|
|
232
164
|
|
|
233
|
-
#driver:
|
|
165
|
+
#driver: ManagerDriver;
|
|
234
166
|
#encodingKind: Encoding;
|
|
235
167
|
[TRANSPORT_SYMBOL]: Transport;
|
|
236
168
|
|
|
237
169
|
/**
|
|
238
170
|
* Creates an instance of Client.
|
|
239
171
|
*
|
|
240
|
-
* @param {string} managerEndpoint - The manager endpoint. See {@link https://rivet.
|
|
241
|
-
* @param {
|
|
242
|
-
* @see {@link https://rivet.
|
|
172
|
+
* @param {string} managerEndpoint - The manager endpoint. See {@link https://rivet.dev/docs/setup|Initial Setup} for instructions on getting the manager endpoint.
|
|
173
|
+
* @param {ClientConfig} [opts] - Options for configuring the client.
|
|
174
|
+
* @see {@link https://rivet.dev/docs/setup|Initial Setup}
|
|
243
175
|
*/
|
|
244
|
-
public constructor(driver:
|
|
176
|
+
public constructor(driver: ManagerDriver, opts?: ClientConfig) {
|
|
245
177
|
this.#driver = driver;
|
|
246
178
|
|
|
247
179
|
this.#encodingKind = opts?.encoding ?? "bare";
|
|
@@ -262,7 +194,8 @@ export class ClientRaw {
|
|
|
262
194
|
actorId: string,
|
|
263
195
|
opts?: GetWithIdOptions,
|
|
264
196
|
): ActorHandle<AD> {
|
|
265
|
-
logger().debug(
|
|
197
|
+
logger().debug({
|
|
198
|
+
msg: "get handle to actor with id",
|
|
266
199
|
name,
|
|
267
200
|
actorId,
|
|
268
201
|
params: opts?.params,
|
|
@@ -296,7 +229,8 @@ export class ClientRaw {
|
|
|
296
229
|
// Convert string to array of strings
|
|
297
230
|
const keyArray: string[] = typeof key === "string" ? [key] : key || [];
|
|
298
231
|
|
|
299
|
-
logger().debug(
|
|
232
|
+
logger().debug({
|
|
233
|
+
msg: "get handle to actor",
|
|
300
234
|
name,
|
|
301
235
|
key: keyArray,
|
|
302
236
|
parameters: opts?.params,
|
|
@@ -330,7 +264,8 @@ export class ClientRaw {
|
|
|
330
264
|
// Convert string to array of strings
|
|
331
265
|
const keyArray: string[] = typeof key === "string" ? [key] : key || [];
|
|
332
266
|
|
|
333
|
-
logger().debug(
|
|
267
|
+
logger().debug({
|
|
268
|
+
msg: "get or create handle to actor",
|
|
334
269
|
name,
|
|
335
270
|
key: keyArray,
|
|
336
271
|
parameters: opts?.params,
|
|
@@ -377,7 +312,8 @@ export class ClientRaw {
|
|
|
377
312
|
},
|
|
378
313
|
} satisfies ActorQuery;
|
|
379
314
|
|
|
380
|
-
logger().debug(
|
|
315
|
+
logger().debug({
|
|
316
|
+
msg: "create actor handle",
|
|
381
317
|
name,
|
|
382
318
|
key: keyArray,
|
|
383
319
|
parameters: opts?.params,
|
|
@@ -385,14 +321,9 @@ export class ClientRaw {
|
|
|
385
321
|
});
|
|
386
322
|
|
|
387
323
|
// Create the actor
|
|
388
|
-
const actorId = await this.#driver
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
this.#encodingKind,
|
|
392
|
-
opts?.params,
|
|
393
|
-
opts?.signal ? { signal: opts.signal } : undefined,
|
|
394
|
-
);
|
|
395
|
-
logger().debug("created actor with ID", {
|
|
324
|
+
const { actorId } = await queryActor(undefined, createQuery, this.#driver);
|
|
325
|
+
logger().debug({
|
|
326
|
+
msg: "created actor with ID",
|
|
396
327
|
name,
|
|
397
328
|
key: keyArray,
|
|
398
329
|
actorId,
|
|
@@ -441,12 +372,12 @@ export class ClientRaw {
|
|
|
441
372
|
*/
|
|
442
373
|
async dispose(): Promise<void> {
|
|
443
374
|
if (this.#disposed) {
|
|
444
|
-
logger().warn("client already disconnected");
|
|
375
|
+
logger().warn({ msg: "client already disconnected" });
|
|
445
376
|
return;
|
|
446
377
|
}
|
|
447
378
|
this.#disposed = true;
|
|
448
379
|
|
|
449
|
-
logger().debug("disposing client");
|
|
380
|
+
logger().debug({ msg: "disposing client" });
|
|
450
381
|
|
|
451
382
|
const disposePromises = [];
|
|
452
383
|
|
|
@@ -474,10 +405,10 @@ export type Client<A extends Registry<any>> = ClientRaw & {
|
|
|
474
405
|
export type AnyClient = Client<Registry<any>>;
|
|
475
406
|
|
|
476
407
|
export function createClientWithDriver<A extends Registry<any>>(
|
|
477
|
-
driver:
|
|
478
|
-
|
|
408
|
+
driver: ManagerDriver,
|
|
409
|
+
config?: ClientConfig,
|
|
479
410
|
): Client<A> {
|
|
480
|
-
const client = new ClientRaw(driver,
|
|
411
|
+
const client = new ClientRaw(driver, config);
|
|
481
412
|
|
|
482
413
|
// Create proxy for accessing actors by name
|
|
483
414
|
return new Proxy(client, {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import { TransportSchema } from "@/actor/protocol/old";
|
|
3
|
+
import { EncodingSchema } from "@/actor/protocol/serde";
|
|
4
|
+
import { getEnvUniversal, type UpgradeWebSocket } from "@/utils";
|
|
5
|
+
|
|
6
|
+
export type GetUpgradeWebSocket = () => UpgradeWebSocket;
|
|
7
|
+
|
|
8
|
+
export const ClientConfigSchema = z.object({
|
|
9
|
+
/** Configure serving the API */
|
|
10
|
+
api: z
|
|
11
|
+
.object({
|
|
12
|
+
host: z.string().default("127.0.0.1"),
|
|
13
|
+
port: z.number().default(6420),
|
|
14
|
+
})
|
|
15
|
+
.default({}),
|
|
16
|
+
|
|
17
|
+
/** Endpoint to connect to the Rivet engine. Can be configured via RIVET_ENGINE env var. */
|
|
18
|
+
endpoint: z
|
|
19
|
+
.string()
|
|
20
|
+
.nullable()
|
|
21
|
+
.default(() => getEnvUniversal("RIVET_ENGINE") ?? null),
|
|
22
|
+
|
|
23
|
+
namespace: z
|
|
24
|
+
.string()
|
|
25
|
+
.default(() => getEnvUniversal("RIVET_NAMESPACE") ?? "default"),
|
|
26
|
+
|
|
27
|
+
runnerName: z
|
|
28
|
+
.string()
|
|
29
|
+
.default(() => getEnvUniversal("RIVET_RUNNER") ?? "rivetkit"),
|
|
30
|
+
|
|
31
|
+
encoding: EncodingSchema.default("bare"),
|
|
32
|
+
|
|
33
|
+
transport: TransportSchema.default("websocket"),
|
|
34
|
+
|
|
35
|
+
// This is a function to allow for lazy configuration of upgradeWebSocket on the
|
|
36
|
+
// fly. This is required since the dependencies that upgradeWebSocket
|
|
37
|
+
// (specifically Node.js) can sometimes only be specified after the router is
|
|
38
|
+
// created or must be imported async using `await import(...)`
|
|
39
|
+
getUpgradeWebSocket: z.custom<GetUpgradeWebSocket>().optional(),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
export type ClientConfig = z.infer<typeof ClientConfigSchema>;
|
|
43
|
+
|
|
44
|
+
export type ClientConfigInput = z.input<typeof ClientConfigSchema>;
|
package/src/client/errors.ts
CHANGED
package/src/client/log.ts
CHANGED
package/src/client/mod.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Registry } from "@/registry/mod";
|
|
2
|
+
import { RemoteManagerDriver } from "@/remote-manager-driver/mod";
|
|
2
3
|
import {
|
|
3
4
|
type Client,
|
|
4
|
-
type
|
|
5
|
+
type ClientConfigInput,
|
|
5
6
|
createClientWithDriver,
|
|
6
7
|
} from "./client";
|
|
7
|
-
import {
|
|
8
|
+
import { ClientConfigSchema } from "./config";
|
|
8
9
|
|
|
9
10
|
export {
|
|
10
11
|
ActorDefinition,
|
|
@@ -28,7 +29,6 @@ export { ActorHandleRaw } from "./actor-handle";
|
|
|
28
29
|
export type {
|
|
29
30
|
ActorAccessor,
|
|
30
31
|
Client,
|
|
31
|
-
ClientOptions,
|
|
32
32
|
ClientRaw,
|
|
33
33
|
CreateOptions,
|
|
34
34
|
ExtractActorsFromRegistry,
|
|
@@ -41,16 +41,20 @@ export type {
|
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Creates a client with the actor accessor proxy.
|
|
44
|
-
*
|
|
45
|
-
* @template A The actor application type.
|
|
46
|
-
* @param {string} managerEndpoint - The manager endpoint.
|
|
47
|
-
* @param {ClientOptions} [opts] - Options for configuring the client.
|
|
48
|
-
* @returns {Client<A>} - A proxied client that supports the `client.myActor.connect()` syntax.
|
|
49
44
|
*/
|
|
50
45
|
export function createClient<A extends Registry<any>>(
|
|
51
|
-
|
|
52
|
-
opts?: ClientOptions,
|
|
46
|
+
endpointOrConfig?: string | ClientConfigInput,
|
|
53
47
|
): Client<A> {
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
// Parse config
|
|
49
|
+
const configInput =
|
|
50
|
+
endpointOrConfig === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: typeof endpointOrConfig === "string"
|
|
53
|
+
? { endpoint: endpointOrConfig }
|
|
54
|
+
: endpointOrConfig;
|
|
55
|
+
const config = ClientConfigSchema.parse(configInput);
|
|
56
|
+
|
|
57
|
+
// Create client
|
|
58
|
+
const driver = new RemoteManagerDriver(config);
|
|
59
|
+
return createClientWithDriver<A>(driver, config);
|
|
56
60
|
}
|
package/src/client/raw-utils.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import invariant from "invariant";
|
|
2
|
+
import { PATH_RAW_WEBSOCKET_PREFIX } from "@/actor/router";
|
|
3
|
+
import { deconstructError } from "@/common/utils";
|
|
4
|
+
import { HEADER_CONN_PARAMS, type ManagerDriver } from "@/driver-helpers/mod";
|
|
1
5
|
import type { ActorQuery } from "@/manager/protocol/query";
|
|
2
|
-
import
|
|
6
|
+
import { queryActor } from "./actor-query";
|
|
7
|
+
import { ActorError } from "./errors";
|
|
8
|
+
import { logger } from "./log";
|
|
3
9
|
|
|
4
10
|
/**
|
|
5
11
|
* Shared implementation for raw HTTP fetch requests
|
|
6
12
|
*/
|
|
7
13
|
export async function rawHttpFetch(
|
|
8
|
-
driver:
|
|
14
|
+
driver: ManagerDriver,
|
|
9
15
|
actorQuery: ActorQuery,
|
|
10
16
|
params: unknown,
|
|
11
17
|
input: string | URL | Request,
|
|
@@ -55,38 +61,89 @@ export async function rawHttpFetch(
|
|
|
55
61
|
throw new TypeError("Invalid input type for fetch");
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
undefined,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
path
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
try {
|
|
65
|
+
// Get the actor ID
|
|
66
|
+
const { actorId } = await queryActor(undefined, actorQuery, driver);
|
|
67
|
+
logger().debug({ msg: "found actor for raw http", actorId });
|
|
68
|
+
invariant(actorId, "Missing actor ID");
|
|
69
|
+
|
|
70
|
+
// Build the URL with normalized path
|
|
71
|
+
const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
|
|
72
|
+
const url = new URL(`http://actor/raw/http/${normalizedPath}`);
|
|
73
|
+
|
|
74
|
+
// Forward conn params if provided
|
|
75
|
+
const proxyRequestHeaders = new Headers(mergedInit.headers);
|
|
76
|
+
if (params) {
|
|
77
|
+
proxyRequestHeaders.set(HEADER_CONN_PARAMS, JSON.stringify(params));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Forward the request to the actor
|
|
81
|
+
const proxyRequest = new Request(url, {
|
|
82
|
+
...mergedInit,
|
|
83
|
+
headers: proxyRequestHeaders,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return driver.sendRequest(actorId, proxyRequest);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
// Standardize to ClientActorError instead of the native backend error
|
|
89
|
+
const { group, code, message, metadata } = deconstructError(
|
|
90
|
+
err,
|
|
91
|
+
logger(),
|
|
92
|
+
{},
|
|
93
|
+
true,
|
|
94
|
+
);
|
|
95
|
+
throw new ActorError(group, code, message, metadata);
|
|
96
|
+
}
|
|
69
97
|
}
|
|
70
98
|
|
|
71
99
|
/**
|
|
72
100
|
* Shared implementation for raw WebSocket connections
|
|
73
101
|
*/
|
|
74
102
|
export async function rawWebSocket(
|
|
75
|
-
driver:
|
|
103
|
+
driver: ManagerDriver,
|
|
76
104
|
actorQuery: ActorQuery,
|
|
77
105
|
params: unknown,
|
|
78
106
|
path?: string,
|
|
107
|
+
// TODO: Supportp rotocols
|
|
79
108
|
protocols?: string | string[],
|
|
80
109
|
): Promise<any> {
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
110
|
+
// TODO: Do we need encoding in rawWebSocket?
|
|
111
|
+
const encoding = "bare";
|
|
112
|
+
|
|
113
|
+
// Get the actor ID
|
|
114
|
+
const { actorId } = await queryActor(undefined, actorQuery, driver);
|
|
115
|
+
logger().debug({ msg: "found actor for action", actorId });
|
|
116
|
+
invariant(actorId, "Missing actor ID");
|
|
117
|
+
|
|
118
|
+
// Parse path and query parameters
|
|
119
|
+
let pathPortion = "";
|
|
120
|
+
let queryPortion = "";
|
|
121
|
+
if (path) {
|
|
122
|
+
const queryIndex = path.indexOf("?");
|
|
123
|
+
if (queryIndex !== -1) {
|
|
124
|
+
pathPortion = path.substring(0, queryIndex);
|
|
125
|
+
queryPortion = path.substring(queryIndex); // includes the '?'
|
|
126
|
+
} else {
|
|
127
|
+
pathPortion = path;
|
|
128
|
+
}
|
|
129
|
+
// Remove leading slash if present
|
|
130
|
+
if (pathPortion.startsWith("/")) {
|
|
131
|
+
pathPortion = pathPortion.slice(1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const fullPath = `${PATH_RAW_WEBSOCKET_PREFIX}${pathPortion}${queryPortion}`;
|
|
136
|
+
|
|
137
|
+
logger().debug({
|
|
138
|
+
msg: "opening websocket",
|
|
139
|
+
actorId,
|
|
140
|
+
encoding,
|
|
141
|
+
path: fullPath,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Open WebSocket
|
|
145
|
+
const ws = await driver.openWebSocket(fullPath, actorId, encoding, params);
|
|
146
|
+
|
|
147
|
+
// Node & browser WebSocket types are incompatible
|
|
148
|
+
return ws as any;
|
|
92
149
|
}
|
package/src/client/utils.ts
CHANGED
|
@@ -41,15 +41,16 @@ export interface HttpRequestOpts<RequestBody, ResponseBody> {
|
|
|
41
41
|
skipParseResponse?: boolean;
|
|
42
42
|
signal?: AbortSignal;
|
|
43
43
|
customFetch?: (req: Request) => Promise<Response>;
|
|
44
|
-
requestVersionedDataHandler: VersionedDataHandler<RequestBody
|
|
45
|
-
responseVersionedDataHandler: VersionedDataHandler<ResponseBody
|
|
44
|
+
requestVersionedDataHandler: VersionedDataHandler<RequestBody> | undefined;
|
|
45
|
+
responseVersionedDataHandler: VersionedDataHandler<ResponseBody> | undefined;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export async function sendHttpRequest<
|
|
49
49
|
RequestBody = unknown,
|
|
50
50
|
ResponseBody = unknown,
|
|
51
51
|
>(opts: HttpRequestOpts<RequestBody, ResponseBody>): Promise<ResponseBody> {
|
|
52
|
-
logger().debug(
|
|
52
|
+
logger().debug({
|
|
53
|
+
msg: "sending http request",
|
|
53
54
|
url: opts.url,
|
|
54
55
|
encoding: opts.encoding,
|
|
55
56
|
});
|
|
@@ -121,6 +122,7 @@ export async function sendHttpRequest<
|
|
|
121
122
|
|
|
122
123
|
// Throw structured error
|
|
123
124
|
throw new ActorError(
|
|
125
|
+
responseData.group,
|
|
124
126
|
responseData.code,
|
|
125
127
|
responseData.message,
|
|
126
128
|
responseData.metadata
|
|
@@ -7,10 +7,8 @@ import type {
|
|
|
7
7
|
UniversalMessageEvent,
|
|
8
8
|
} from "@/common/eventsource-interface";
|
|
9
9
|
|
|
10
|
-
export const LOGGER_NAME = "fake-event-source";
|
|
11
|
-
|
|
12
10
|
export function logger() {
|
|
13
|
-
return getLogger(
|
|
11
|
+
return getLogger("fake-event-source");
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
/**
|
|
@@ -146,12 +144,12 @@ export class FakeEventSource implements UniversalEventSource {
|
|
|
146
144
|
return;
|
|
147
145
|
}
|
|
148
146
|
|
|
149
|
-
logger().debug("closing FakeEventSource");
|
|
147
|
+
logger().debug({ msg: "closing FakeEventSource" });
|
|
150
148
|
this.readyState = 2; // CLOSED
|
|
151
149
|
|
|
152
150
|
// Call the close callback
|
|
153
151
|
this.#onCloseCallback().catch((err) => {
|
|
154
|
-
logger().error("error in onClose callback",
|
|
152
|
+
logger().error({ msg: "error in onClose callback", error: err });
|
|
155
153
|
});
|
|
156
154
|
|
|
157
155
|
// Dispatch close event
|
|
@@ -223,7 +221,10 @@ export class FakeEventSource implements UniversalEventSource {
|
|
|
223
221
|
try {
|
|
224
222
|
listener.call(this, event);
|
|
225
223
|
} catch (err) {
|
|
226
|
-
logger().error(
|
|
224
|
+
logger().error({
|
|
225
|
+
msg: `error in ${type} event listener`,
|
|
226
|
+
error: err,
|
|
227
|
+
});
|
|
227
228
|
}
|
|
228
229
|
}
|
|
229
230
|
}
|
|
@@ -235,7 +236,7 @@ export class FakeEventSource implements UniversalEventSource {
|
|
|
235
236
|
try {
|
|
236
237
|
this.onopen.call(this as any, event);
|
|
237
238
|
} catch (err) {
|
|
238
|
-
logger().error("error in onopen handler",
|
|
239
|
+
logger().error({ msg: "error in onopen handler", error: err });
|
|
239
240
|
}
|
|
240
241
|
}
|
|
241
242
|
break;
|
|
@@ -244,7 +245,7 @@ export class FakeEventSource implements UniversalEventSource {
|
|
|
244
245
|
try {
|
|
245
246
|
this.onmessage.call(this as any, event);
|
|
246
247
|
} catch (err) {
|
|
247
|
-
logger().error("error in onmessage handler",
|
|
248
|
+
logger().error({ msg: "error in onmessage handler", error: err });
|
|
248
249
|
}
|
|
249
250
|
}
|
|
250
251
|
break;
|
|
@@ -253,7 +254,7 @@ export class FakeEventSource implements UniversalEventSource {
|
|
|
253
254
|
try {
|
|
254
255
|
this.onerror.call(this as any, event);
|
|
255
256
|
} catch (err) {
|
|
256
|
-
logger().error("error in onerror handler",
|
|
257
|
+
logger().error({ msg: "error in onerror handler", error: err });
|
|
257
258
|
}
|
|
258
259
|
}
|
|
259
260
|
break;
|