rivetkit 2.0.3 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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-6PDXBYI5.js → chunk-3F2YSRJL.js} +8 -23
- package/dist/tsup/chunk-3F2YSRJL.js.map +1 -0
- package/dist/tsup/chunk-4CXBCT26.cjs +250 -0
- package/dist/tsup/chunk-4CXBCT26.cjs.map +1 -0
- package/dist/tsup/chunk-4R73YDN3.cjs +20 -0
- package/dist/tsup/chunk-4R73YDN3.cjs.map +1 -0
- package/dist/tsup/{chunk-OGAPU3UG.cjs → chunk-6LJT3QRL.cjs} +39 -25
- package/dist/tsup/chunk-6LJT3QRL.cjs.map +1 -0
- package/dist/tsup/{chunk-6WKQDDUD.cjs → chunk-GICQ3YCU.cjs} +143 -141
- package/dist/tsup/chunk-GICQ3YCU.cjs.map +1 -0
- package/dist/tsup/{chunk-FLMTTN27.js → chunk-H26RP6GD.js} +15 -8
- package/dist/tsup/chunk-H26RP6GD.js.map +1 -0
- package/dist/tsup/chunk-HI3HWJRC.js +20 -0
- package/dist/tsup/chunk-HI3HWJRC.js.map +1 -0
- package/dist/tsup/{chunk-4NSUQZ2H.js → chunk-HLLF4B4Q.js} +116 -114
- package/dist/tsup/chunk-HLLF4B4Q.js.map +1 -0
- package/dist/tsup/{chunk-FCCPJNMA.cjs → chunk-IH6CKNDW.cjs} +12 -27
- package/dist/tsup/chunk-IH6CKNDW.cjs.map +1 -0
- package/dist/tsup/chunk-LV2S3OU3.js +250 -0
- package/dist/tsup/chunk-LV2S3OU3.js.map +1 -0
- package/dist/tsup/{chunk-R2OPSKIV.cjs → chunk-LWNKVZG5.cjs} +20 -13
- package/dist/tsup/chunk-LWNKVZG5.cjs.map +1 -0
- package/dist/tsup/{chunk-INGJP237.js → chunk-NFU2BBT5.js} +102 -43
- package/dist/tsup/chunk-NFU2BBT5.js.map +1 -0
- package/dist/tsup/{chunk-3H7O2A7I.js → chunk-PQY7KKTL.js} +33 -19
- package/dist/tsup/chunk-PQY7KKTL.js.map +1 -0
- package/dist/tsup/{chunk-PO4VLDWA.js → chunk-QK72M5JB.js} +3 -5
- package/dist/tsup/chunk-QK72M5JB.js.map +1 -0
- package/dist/tsup/{chunk-TZJKSBUQ.cjs → chunk-QNNXFOQV.cjs} +3 -5
- package/dist/tsup/chunk-QNNXFOQV.cjs.map +1 -0
- package/dist/tsup/{chunk-GIR3AFFI.cjs → chunk-SBHHJ6QS.cjs} +102 -43
- package/dist/tsup/chunk-SBHHJ6QS.cjs.map +1 -0
- package/dist/tsup/chunk-TQ62L3X7.js +325 -0
- package/dist/tsup/chunk-TQ62L3X7.js.map +1 -0
- package/dist/tsup/chunk-VO7ZRVVD.cjs +6293 -0
- package/dist/tsup/chunk-VO7ZRVVD.cjs.map +1 -0
- package/dist/tsup/chunk-WHBPJNGW.cjs +325 -0
- package/dist/tsup/chunk-WHBPJNGW.cjs.map +1 -0
- package/dist/tsup/chunk-XJQHKJ4P.js +6293 -0
- package/dist/tsup/chunk-XJQHKJ4P.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-BI-6UIBJ.d.ts} +196 -226
- package/dist/tsup/{connection-BR_Ve4ku.d.cts → connection-Dyd4NLGW.d.cts} +196 -226
- 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 +155 -1363
- package/dist/tsup/driver-test-suite/mod.cjs.map +1 -1
- package/dist/tsup/driver-test-suite/mod.d.cts +11 -5
- package/dist/tsup/driver-test-suite/mod.d.ts +11 -5
- package/dist/tsup/driver-test-suite/mod.js +876 -2084
- 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-BTe_Rsdn.d.cts} +2 -3
- package/dist/tsup/{router-endpoints-AYkXG8Tl.d.cts → router-endpoints-CBSrKHmo.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 +3 -3
- package/dist/tsup/utils.d.cts +1 -1
- package/dist/tsup/utils.d.ts +1 -1
- package/dist/tsup/utils.js +2 -2
- package/package.json +4 -4
- 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 +97 -35
- 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 +41 -38
- package/src/actor/router.ts +20 -18
- 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 +86 -60
- package/src/driver-test-suite/tests/actor-handle.ts +33 -0
- 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 +13 -10
- 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 +33 -20
- package/src/drivers/file-system/log.ts +1 -3
- package/src/drivers/file-system/manager.ts +31 -8
- 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 +277 -1657
- 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 +57 -24
- package/src/registry/run-config.ts +31 -33
- package/src/registry/serve.ts +4 -5
- package/src/remote-manager-driver/actor-http-client.ts +72 -0
- package/src/remote-manager-driver/actor-websocket-client.ts +63 -0
- package/src/remote-manager-driver/api-endpoints.ts +79 -0
- package/src/remote-manager-driver/api-utils.ts +43 -0
- package/src/remote-manager-driver/log.ts +5 -0
- package/src/remote-manager-driver/mod.ts +274 -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/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/test-inline-client-driver.ts +0 -402
- 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
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { apiCall } from "./api-utils";
|
|
2
|
-
import type { Config } from "./config";
|
|
3
|
-
import { serializeActorKey } from "./keys";
|
|
4
|
-
|
|
5
|
-
// MARK: Common types
|
|
6
|
-
export type RivetId = string;
|
|
7
|
-
|
|
8
|
-
export interface Actor {
|
|
9
|
-
actor_id: RivetId;
|
|
10
|
-
name: string;
|
|
11
|
-
key: string;
|
|
12
|
-
namespace_id: RivetId;
|
|
13
|
-
runner_name_selector: string;
|
|
14
|
-
create_ts: number;
|
|
15
|
-
connectable_ts?: number | null;
|
|
16
|
-
destroy_ts?: number | null;
|
|
17
|
-
sleep_ts?: number | null;
|
|
18
|
-
start_ts?: number | null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface ActorsGetResponse {
|
|
22
|
-
actor: Actor;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ActorsGetByIdResponse {
|
|
26
|
-
actor_id?: RivetId | null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface ActorsGetOrCreateResponse {
|
|
30
|
-
actor: Actor;
|
|
31
|
-
created: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface ActorsGetOrCreateByIdResponse {
|
|
35
|
-
actor_id: RivetId;
|
|
36
|
-
created: boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface ActorsCreateRequest {
|
|
40
|
-
name: string;
|
|
41
|
-
runner_name_selector: string;
|
|
42
|
-
crash_policy: string;
|
|
43
|
-
key?: string | null;
|
|
44
|
-
input?: string | null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface ActorsCreateResponse {
|
|
48
|
-
actor: Actor;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// MARK: Get actor
|
|
52
|
-
export async function getActor(
|
|
53
|
-
config: Config,
|
|
54
|
-
actorId: RivetId,
|
|
55
|
-
): Promise<ActorsGetResponse> {
|
|
56
|
-
return apiCall<never, ActorsGetResponse>(
|
|
57
|
-
config.endpoint,
|
|
58
|
-
config.namespace,
|
|
59
|
-
"GET",
|
|
60
|
-
`/actors/${encodeURIComponent(actorId)}`,
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// MARK: Get actor by id
|
|
65
|
-
export async function getActorById(
|
|
66
|
-
config: Config,
|
|
67
|
-
name: string,
|
|
68
|
-
key: string[],
|
|
69
|
-
): Promise<ActorsGetByIdResponse> {
|
|
70
|
-
const serializedKey = serializeActorKey(key);
|
|
71
|
-
return apiCall<never, ActorsGetByIdResponse>(
|
|
72
|
-
config.endpoint,
|
|
73
|
-
config.namespace,
|
|
74
|
-
"GET",
|
|
75
|
-
`/actors/by-id?name=${encodeURIComponent(name)}&key=${encodeURIComponent(serializedKey)}`,
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// MARK: Get or create actor by id
|
|
80
|
-
export interface ActorsGetOrCreateByIdRequest {
|
|
81
|
-
name: string;
|
|
82
|
-
key: string;
|
|
83
|
-
runner_name_selector: string;
|
|
84
|
-
crash_policy: string;
|
|
85
|
-
input?: string | null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export async function getOrCreateActorById(
|
|
89
|
-
config: Config,
|
|
90
|
-
request: ActorsGetOrCreateByIdRequest,
|
|
91
|
-
): Promise<ActorsGetOrCreateByIdResponse> {
|
|
92
|
-
return apiCall<ActorsGetOrCreateByIdRequest, ActorsGetOrCreateByIdResponse>(
|
|
93
|
-
config.endpoint,
|
|
94
|
-
config.namespace,
|
|
95
|
-
"PUT",
|
|
96
|
-
`/actors/by-id`,
|
|
97
|
-
request,
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// MARK: Create actor
|
|
102
|
-
export async function createActor(
|
|
103
|
-
config: Config,
|
|
104
|
-
request: ActorsCreateRequest,
|
|
105
|
-
): Promise<ActorsCreateResponse> {
|
|
106
|
-
return apiCall<ActorsCreateRequest, ActorsCreateResponse>(
|
|
107
|
-
config.endpoint,
|
|
108
|
-
config.namespace,
|
|
109
|
-
"POST",
|
|
110
|
-
`/actors`,
|
|
111
|
-
request,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// MARK: Destroy actor
|
|
116
|
-
export type ActorsDeleteResponse = {};
|
|
117
|
-
|
|
118
|
-
export async function destroyActor(
|
|
119
|
-
config: Config,
|
|
120
|
-
actorId: RivetId,
|
|
121
|
-
): Promise<ActorsDeleteResponse> {
|
|
122
|
-
return apiCall<never, ActorsDeleteResponse>(
|
|
123
|
-
config.endpoint,
|
|
124
|
-
config.namespace,
|
|
125
|
-
"DELETE",
|
|
126
|
-
`/actors/${encodeURIComponent(actorId)}`,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { logger } from "./log";
|
|
2
|
-
|
|
3
|
-
// Error class for Engine API errors
|
|
4
|
-
export class EngineApiError extends Error {
|
|
5
|
-
constructor(
|
|
6
|
-
public readonly group: string,
|
|
7
|
-
public readonly code: string,
|
|
8
|
-
message?: string,
|
|
9
|
-
) {
|
|
10
|
-
super(message || `Engine API error: ${group}/${code}`);
|
|
11
|
-
this.name = "EngineApiError";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Helper function for making API calls
|
|
16
|
-
export async function apiCall<TInput = unknown, TOutput = unknown>(
|
|
17
|
-
endpoint: string,
|
|
18
|
-
namespace: string,
|
|
19
|
-
method: "GET" | "POST" | "PUT" | "DELETE",
|
|
20
|
-
path: string,
|
|
21
|
-
body?: TInput,
|
|
22
|
-
): Promise<TOutput> {
|
|
23
|
-
const url = `${endpoint}${path}${path.includes("?") ? "&" : "?"}namespace=${encodeURIComponent(namespace)}`;
|
|
24
|
-
|
|
25
|
-
const options: RequestInit = {
|
|
26
|
-
method,
|
|
27
|
-
headers: {
|
|
28
|
-
"Content-Type": "application/json",
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
if (body !== undefined && method !== "GET") {
|
|
33
|
-
options.body = JSON.stringify(body);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
logger().debug("making api call", { method, url });
|
|
37
|
-
|
|
38
|
-
const response = await fetch(url, options);
|
|
39
|
-
|
|
40
|
-
if (!response.ok) {
|
|
41
|
-
const errorText = await response.text();
|
|
42
|
-
logger().error("api call failed", {
|
|
43
|
-
status: response.status,
|
|
44
|
-
statusText: response.statusText,
|
|
45
|
-
error: errorText,
|
|
46
|
-
method,
|
|
47
|
-
path,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// Try to parse error response
|
|
51
|
-
try {
|
|
52
|
-
const errorData = JSON.parse(errorText);
|
|
53
|
-
if (errorData.kind === "error" && errorData.group && errorData.code) {
|
|
54
|
-
throw new EngineApiError(
|
|
55
|
-
errorData.group,
|
|
56
|
-
errorData.code,
|
|
57
|
-
errorData.message,
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
} catch (parseError) {
|
|
61
|
-
// If parsing fails or it's not our expected error format, continue
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
throw new Error(
|
|
65
|
-
`API call failed: ${response.status} ${response.statusText}`,
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return response.json() as Promise<TOutput>;
|
|
70
|
-
}
|
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
import * as cbor from "cbor-x";
|
|
2
|
-
import type { Context as HonoContext } from "hono";
|
|
3
|
-
import invariant from "invariant";
|
|
4
|
-
import { ActorAlreadyExists } from "@/actor/errors";
|
|
5
|
-
import {
|
|
6
|
-
HEADER_AUTH_DATA,
|
|
7
|
-
HEADER_CONN_PARAMS,
|
|
8
|
-
HEADER_ENCODING,
|
|
9
|
-
HEADER_EXPOSE_INTERNAL_ERROR,
|
|
10
|
-
} from "@/actor/router-endpoints";
|
|
11
|
-
import { generateRandomString } from "@/actor/utils";
|
|
12
|
-
import { importWebSocket } from "@/common/websocket";
|
|
13
|
-
import type {
|
|
14
|
-
ActorOutput,
|
|
15
|
-
CreateInput,
|
|
16
|
-
GetForIdInput,
|
|
17
|
-
GetOrCreateWithKeyInput,
|
|
18
|
-
GetWithKeyInput,
|
|
19
|
-
ManagerDriver,
|
|
20
|
-
} from "@/driver-helpers/mod";
|
|
21
|
-
import { type Encoding, noopNext, type RunConfig } from "@/mod";
|
|
22
|
-
import {
|
|
23
|
-
createActor,
|
|
24
|
-
destroyActor,
|
|
25
|
-
getActor,
|
|
26
|
-
getActorById,
|
|
27
|
-
getOrCreateActorById,
|
|
28
|
-
} from "./api-endpoints";
|
|
29
|
-
import { EngineApiError } from "./api-utils";
|
|
30
|
-
import type { Config } from "./config";
|
|
31
|
-
import { deserializeActorKey, serializeActorKey } from "./keys";
|
|
32
|
-
import { logger } from "./log";
|
|
33
|
-
import { createWebSocketProxy } from "./ws-proxy";
|
|
34
|
-
|
|
35
|
-
export class EngineManagerDriver implements ManagerDriver {
|
|
36
|
-
#config: Config;
|
|
37
|
-
#runConfig: RunConfig;
|
|
38
|
-
#importWebSocketPromise: Promise<typeof WebSocket>;
|
|
39
|
-
|
|
40
|
-
constructor(config: Config, runConfig: RunConfig) {
|
|
41
|
-
this.#config = config;
|
|
42
|
-
this.#runConfig = runConfig;
|
|
43
|
-
if (!this.#runConfig.inspector.token()) {
|
|
44
|
-
const token = generateRandomString();
|
|
45
|
-
this.#runConfig.inspector.token = () => token;
|
|
46
|
-
}
|
|
47
|
-
this.#importWebSocketPromise = importWebSocket();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async sendRequest(actorId: string, actorRequest: Request): Promise<Response> {
|
|
51
|
-
logger().debug("sending request to actor via guard", {
|
|
52
|
-
actorId,
|
|
53
|
-
method: actorRequest.method,
|
|
54
|
-
url: actorRequest.url,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return this.#forwardHttpRequest(actorRequest, actorId);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async openWebSocket(
|
|
61
|
-
path: string,
|
|
62
|
-
actorId: string,
|
|
63
|
-
encoding: Encoding,
|
|
64
|
-
params: unknown,
|
|
65
|
-
): Promise<WebSocket> {
|
|
66
|
-
const WebSocket = await this.#importWebSocketPromise;
|
|
67
|
-
|
|
68
|
-
// WebSocket connections go through guard
|
|
69
|
-
const guardUrl = `${this.#config.endpoint}${path}`;
|
|
70
|
-
|
|
71
|
-
logger().debug("opening websocket to actor via guard", {
|
|
72
|
-
actorId,
|
|
73
|
-
path,
|
|
74
|
-
guardUrl,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Create WebSocket connection
|
|
78
|
-
const ws = new WebSocket(guardUrl, {
|
|
79
|
-
headers: buildGuardHeadersForWebSocket(actorId, encoding, params),
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
logger().debug("websocket connection opened", { actorId });
|
|
83
|
-
|
|
84
|
-
return ws;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async proxyRequest(
|
|
88
|
-
_c: HonoContext,
|
|
89
|
-
actorRequest: Request,
|
|
90
|
-
actorId: string,
|
|
91
|
-
): Promise<Response> {
|
|
92
|
-
logger().debug("forwarding request to actor via guard", {
|
|
93
|
-
actorId,
|
|
94
|
-
method: actorRequest.method,
|
|
95
|
-
url: actorRequest.url,
|
|
96
|
-
hasBody: !!actorRequest.body,
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
return this.#forwardHttpRequest(actorRequest, actorId);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async proxyWebSocket(
|
|
103
|
-
c: HonoContext,
|
|
104
|
-
path: string,
|
|
105
|
-
actorId: string,
|
|
106
|
-
encoding: Encoding,
|
|
107
|
-
params: unknown,
|
|
108
|
-
authData: unknown,
|
|
109
|
-
): Promise<Response> {
|
|
110
|
-
const upgradeWebSocket = this.#runConfig.getUpgradeWebSocket?.();
|
|
111
|
-
invariant(upgradeWebSocket, "missing getUpgradeWebSocket");
|
|
112
|
-
|
|
113
|
-
const guardUrl = `${this.#config.endpoint}${path}`;
|
|
114
|
-
const wsGuardUrl = guardUrl.replace("http://", "ws://");
|
|
115
|
-
|
|
116
|
-
logger().debug("forwarding websocket to actor via guard", {
|
|
117
|
-
actorId,
|
|
118
|
-
path,
|
|
119
|
-
guardUrl,
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// Build headers
|
|
123
|
-
const headers = buildGuardHeadersForWebSocket(
|
|
124
|
-
actorId,
|
|
125
|
-
encoding,
|
|
126
|
-
params,
|
|
127
|
-
authData,
|
|
128
|
-
);
|
|
129
|
-
const args = await createWebSocketProxy(c, wsGuardUrl, headers);
|
|
130
|
-
|
|
131
|
-
return await upgradeWebSocket(() => args)(c, noopNext());
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
extraStartupLog() {
|
|
135
|
-
return {
|
|
136
|
-
engine: this.#config.endpoint,
|
|
137
|
-
namespace: this.#config.namespace,
|
|
138
|
-
runner: this.#config.runnerName,
|
|
139
|
-
address: Object.values(this.#config.addresses)
|
|
140
|
-
.map((v) => `${v.host}:${v.port}`)
|
|
141
|
-
.join(", "),
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async getForId({
|
|
146
|
-
c,
|
|
147
|
-
name,
|
|
148
|
-
actorId,
|
|
149
|
-
}: GetForIdInput): Promise<ActorOutput | undefined> {
|
|
150
|
-
// Fetch from API if not in cache
|
|
151
|
-
try {
|
|
152
|
-
const response = await getActor(this.#config, actorId);
|
|
153
|
-
|
|
154
|
-
// Validate name matches
|
|
155
|
-
if (response.actor.name !== name) {
|
|
156
|
-
logger().debug("actor name mismatch from api", {
|
|
157
|
-
actorId,
|
|
158
|
-
apiName: response.actor.name,
|
|
159
|
-
requestedName: name,
|
|
160
|
-
});
|
|
161
|
-
return undefined;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const keyRaw = response.actor.key;
|
|
165
|
-
invariant(keyRaw, `actor ${actorId} should have key`);
|
|
166
|
-
const key = deserializeActorKey(keyRaw);
|
|
167
|
-
|
|
168
|
-
return {
|
|
169
|
-
actorId,
|
|
170
|
-
name,
|
|
171
|
-
key,
|
|
172
|
-
};
|
|
173
|
-
} catch (error) {
|
|
174
|
-
if (
|
|
175
|
-
error instanceof EngineApiError &&
|
|
176
|
-
(error as EngineApiError).group === "actor" &&
|
|
177
|
-
(error as EngineApiError).code === "not_found"
|
|
178
|
-
) {
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
throw error;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async getWithKey({
|
|
186
|
-
c,
|
|
187
|
-
name,
|
|
188
|
-
key,
|
|
189
|
-
}: GetWithKeyInput): Promise<ActorOutput | undefined> {
|
|
190
|
-
logger().debug("getWithKey: searching for actor", { name, key });
|
|
191
|
-
|
|
192
|
-
// If not in local cache, fetch by key from API
|
|
193
|
-
try {
|
|
194
|
-
const response = await getActorById(this.#config, name, key);
|
|
195
|
-
|
|
196
|
-
if (!response.actor_id) {
|
|
197
|
-
return undefined;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const actorId = response.actor_id;
|
|
201
|
-
|
|
202
|
-
logger().debug("getWithKey: found actor via api", {
|
|
203
|
-
actorId,
|
|
204
|
-
name,
|
|
205
|
-
key,
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return {
|
|
209
|
-
actorId,
|
|
210
|
-
name,
|
|
211
|
-
key,
|
|
212
|
-
};
|
|
213
|
-
} catch (error) {
|
|
214
|
-
if (
|
|
215
|
-
error instanceof EngineApiError &&
|
|
216
|
-
(error as EngineApiError).group === "actor" &&
|
|
217
|
-
(error as EngineApiError).code === "not_found"
|
|
218
|
-
) {
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
throw error;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async getOrCreateWithKey(
|
|
226
|
-
input: GetOrCreateWithKeyInput,
|
|
227
|
-
): Promise<ActorOutput> {
|
|
228
|
-
const { c, name, key, input: actorInput, region } = input;
|
|
229
|
-
|
|
230
|
-
logger().info(
|
|
231
|
-
"getOrCreateWithKey: getting or creating actor via engine api",
|
|
232
|
-
{
|
|
233
|
-
name,
|
|
234
|
-
key,
|
|
235
|
-
},
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
const response = await getOrCreateActorById(this.#config, {
|
|
239
|
-
name,
|
|
240
|
-
key: serializeActorKey(key),
|
|
241
|
-
runner_name_selector: this.#config.runnerName,
|
|
242
|
-
input: input ? cbor.encode(actorInput).toString("base64") : undefined,
|
|
243
|
-
crash_policy: "sleep",
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
const actorId = response.actor_id;
|
|
247
|
-
|
|
248
|
-
logger().info("getOrCreateWithKey: actor ready", {
|
|
249
|
-
actorId,
|
|
250
|
-
name,
|
|
251
|
-
key,
|
|
252
|
-
created: response.created,
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
return {
|
|
256
|
-
actorId,
|
|
257
|
-
name,
|
|
258
|
-
key,
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
async createActor({
|
|
263
|
-
c,
|
|
264
|
-
name,
|
|
265
|
-
key,
|
|
266
|
-
input,
|
|
267
|
-
}: CreateInput): Promise<ActorOutput> {
|
|
268
|
-
// Check if actor with the same name and key already exists
|
|
269
|
-
const existingActor = await this.getWithKey({ c, name, key });
|
|
270
|
-
if (existingActor) {
|
|
271
|
-
throw new ActorAlreadyExists(name, key);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
logger().info("creating actor via engine api", { name, key });
|
|
275
|
-
|
|
276
|
-
// Create actor via engine API
|
|
277
|
-
const result = await createActor(this.#config, {
|
|
278
|
-
name,
|
|
279
|
-
runner_name_selector: this.#config.runnerName,
|
|
280
|
-
key: serializeActorKey(key),
|
|
281
|
-
input: input ? cbor.encode(input).toString("base64") : null,
|
|
282
|
-
crash_policy: "sleep",
|
|
283
|
-
});
|
|
284
|
-
const actorId = result.actor.actor_id;
|
|
285
|
-
|
|
286
|
-
logger().info("actor created", { actorId, name, key });
|
|
287
|
-
|
|
288
|
-
return {
|
|
289
|
-
actorId,
|
|
290
|
-
name,
|
|
291
|
-
key,
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
async destroyActor(actorId: string): Promise<void> {
|
|
296
|
-
logger().info("destroying actor via engine api", { actorId });
|
|
297
|
-
|
|
298
|
-
await destroyActor(this.#config, actorId);
|
|
299
|
-
|
|
300
|
-
logger().info("actor destroyed", { actorId });
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async #forwardHttpRequest(
|
|
304
|
-
actorRequest: Request,
|
|
305
|
-
actorId: string,
|
|
306
|
-
): Promise<Response> {
|
|
307
|
-
// Route through guard port
|
|
308
|
-
const url = new URL(actorRequest.url);
|
|
309
|
-
const guardUrl = `${this.#config.endpoint}${url.pathname}${url.search}`;
|
|
310
|
-
|
|
311
|
-
// Handle body properly based on method and presence
|
|
312
|
-
let bodyToSend: ArrayBuffer | null = null;
|
|
313
|
-
const guardHeaders = buildGuardHeadersForHttp(actorRequest, actorId);
|
|
314
|
-
|
|
315
|
-
if (
|
|
316
|
-
actorRequest.body &&
|
|
317
|
-
actorRequest.method !== "GET" &&
|
|
318
|
-
actorRequest.method !== "HEAD"
|
|
319
|
-
) {
|
|
320
|
-
if (actorRequest.bodyUsed) {
|
|
321
|
-
throw new Error("Request body has already been consumed");
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// TODO: This buffers the entire request in memory every time. We
|
|
325
|
-
// need to properly implement streaming bodies.
|
|
326
|
-
// Clone and read the body to ensure it can be sent
|
|
327
|
-
const clonedRequest = actorRequest.clone();
|
|
328
|
-
bodyToSend = await clonedRequest.arrayBuffer();
|
|
329
|
-
|
|
330
|
-
// If this is a streaming request, we need to convert the headers
|
|
331
|
-
// for the basic array buffer
|
|
332
|
-
guardHeaders.delete("transfer-encoding");
|
|
333
|
-
guardHeaders.set(
|
|
334
|
-
"content-length",
|
|
335
|
-
String((bodyToSend as ArrayBuffer).byteLength),
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
const guardRequest = new Request(guardUrl, {
|
|
340
|
-
method: actorRequest.method,
|
|
341
|
-
headers: guardHeaders,
|
|
342
|
-
body: bodyToSend,
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
return mutableResponse(await fetch(guardRequest));
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function mutableResponse(fetchRes: Response): Response {
|
|
350
|
-
// We cannot return the raw response from `fetch` since the response type is not mutable.
|
|
351
|
-
//
|
|
352
|
-
// In order for middleware to be able to mutate the response, we need to build a new Response object that is mutable.
|
|
353
|
-
return new Response(fetchRes.body, fetchRes);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function buildGuardHeadersForHttp(
|
|
357
|
-
actorRequest: Request,
|
|
358
|
-
actorId: string,
|
|
359
|
-
): Headers {
|
|
360
|
-
const headers = new Headers();
|
|
361
|
-
// Copy all headers from the original request
|
|
362
|
-
for (const [key, value] of actorRequest.headers.entries()) {
|
|
363
|
-
headers.set(key, value);
|
|
364
|
-
}
|
|
365
|
-
// Add guard-specific headers
|
|
366
|
-
headers.set("x-rivet-target", "actor");
|
|
367
|
-
headers.set("x-rivet-actor", actorId);
|
|
368
|
-
headers.set("x-rivet-port", "main");
|
|
369
|
-
return headers;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
function buildGuardHeadersForWebSocket(
|
|
373
|
-
actorId: string,
|
|
374
|
-
encoding: Encoding,
|
|
375
|
-
params?: unknown,
|
|
376
|
-
authData?: unknown,
|
|
377
|
-
): Record<string, string> {
|
|
378
|
-
const headers: Record<string, string> = {};
|
|
379
|
-
headers["x-rivet-target"] = "actor";
|
|
380
|
-
headers["x-rivet-actor"] = actorId;
|
|
381
|
-
headers["x-rivet-port"] = "main";
|
|
382
|
-
headers[HEADER_EXPOSE_INTERNAL_ERROR] = "true";
|
|
383
|
-
headers[HEADER_ENCODING] = encoding;
|
|
384
|
-
if (params) {
|
|
385
|
-
headers[HEADER_CONN_PARAMS] = JSON.stringify(params);
|
|
386
|
-
}
|
|
387
|
-
if (authData) {
|
|
388
|
-
headers[HEADER_AUTH_DATA] = JSON.stringify(authData);
|
|
389
|
-
}
|
|
390
|
-
return headers;
|
|
391
|
-
}
|