@rivetkit/cloudflare-workers 2.0.2 → 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 +3 -3
- package/dist/mod.cjs +41 -41
- package/dist/mod.cjs.map +1 -1
- package/dist/mod.d.cts +69 -19
- package/dist/mod.d.ts +69 -19
- package/dist/mod.js +42 -42
- package/dist/mod.js.map +1 -1
- package/package.json +3 -2
- package/src/actor-driver.ts +5 -5
- package/src/actor-handler-do.ts +8 -21
- package/src/config.ts +1 -1
- package/src/handler.ts +2 -4
- package/src/log.ts +2 -4
- package/src/manager-driver.ts +35 -23
package/src/actor-handler-do.ts
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
import { DurableObject, env } from "cloudflare:workers";
|
|
2
|
-
import type {
|
|
3
|
-
ActorKey,
|
|
4
|
-
ActorRouter,
|
|
5
|
-
Registry,
|
|
6
|
-
RunConfig,
|
|
7
|
-
} from "@rivetkit/core";
|
|
8
|
-
import {
|
|
9
|
-
createActorRouter,
|
|
10
|
-
createClientWithDriver,
|
|
11
|
-
createInlineClientDriver,
|
|
12
|
-
} from "@rivetkit/core";
|
|
13
|
-
import { serializeEmptyPersistData } from "@rivetkit/core/driver-helpers";
|
|
14
2
|
import type { ExecutionContext } from "hono";
|
|
15
3
|
import invariant from "invariant";
|
|
4
|
+
import type { ActorKey, ActorRouter, Registry, RunConfig } from "rivetkit";
|
|
5
|
+
import { createActorRouter, createClientWithDriver } from "rivetkit";
|
|
6
|
+
import { serializeEmptyPersistData } from "rivetkit/driver-helpers";
|
|
16
7
|
import {
|
|
17
8
|
CloudflareDurableObjectGlobalState,
|
|
18
9
|
createCloudflareActorsActorDriverBuilder,
|
|
@@ -92,7 +83,7 @@ export function createActorDurableObject(
|
|
|
92
83
|
const key = res.get(KEYS.KEY) as ActorKey;
|
|
93
84
|
if (!key) throw new Error("missing actor key");
|
|
94
85
|
|
|
95
|
-
logger().debug("already initialized",
|
|
86
|
+
logger().debug({ msg: "already initialized", name, key });
|
|
96
87
|
|
|
97
88
|
this.#initialized = { name, key };
|
|
98
89
|
this.#initializedPromise.resolve();
|
|
@@ -128,9 +119,7 @@ export function createActorDurableObject(
|
|
|
128
119
|
);
|
|
129
120
|
|
|
130
121
|
// Create inline client
|
|
131
|
-
const inlineClient = createClientWithDriver(
|
|
132
|
-
createInlineClientDriver(managerDriver),
|
|
133
|
-
);
|
|
122
|
+
const inlineClient = createClientWithDriver(managerDriver);
|
|
134
123
|
|
|
135
124
|
// Create actor driver
|
|
136
125
|
const actorDriver = runConfig.driver.actor(
|
|
@@ -169,7 +158,7 @@ export function createActorDurableObject(
|
|
|
169
158
|
key: req.key,
|
|
170
159
|
};
|
|
171
160
|
|
|
172
|
-
logger().debug("initialized actor",
|
|
161
|
+
logger().debug({ msg: "initialized actor", key: req.key });
|
|
173
162
|
|
|
174
163
|
// Preemptively actor so the lifecycle hooks are called
|
|
175
164
|
await this.#loadActor();
|
|
@@ -194,9 +183,7 @@ export function createActorDurableObject(
|
|
|
194
183
|
registry.config,
|
|
195
184
|
runConfig,
|
|
196
185
|
);
|
|
197
|
-
const inlineClient = createClientWithDriver(
|
|
198
|
-
createInlineClientDriver(managerDriver),
|
|
199
|
-
);
|
|
186
|
+
const inlineClient = createClientWithDriver(managerDriver);
|
|
200
187
|
const actorDriver = runConfig.driver.actor(
|
|
201
188
|
registry.config,
|
|
202
189
|
runConfig,
|
|
@@ -206,7 +193,7 @@ export function createActorDurableObject(
|
|
|
206
193
|
|
|
207
194
|
// Load the actor instance and trigger alarm
|
|
208
195
|
const actor = await actorDriver.loadActor(actorId);
|
|
209
|
-
await actor.
|
|
196
|
+
await actor._onAlarm();
|
|
210
197
|
}
|
|
211
198
|
};
|
|
212
199
|
}
|
package/src/config.ts
CHANGED
package/src/handler.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { env } from "cloudflare:workers";
|
|
2
|
-
import type { Registry, RunConfig } from "@rivetkit/core";
|
|
3
|
-
import type { Client } from "@rivetkit/core/client";
|
|
4
2
|
import { Hono } from "hono";
|
|
3
|
+
import type { Registry, RunConfig } from "rivetkit";
|
|
4
|
+
import type { Client } from "rivetkit/client";
|
|
5
5
|
import {
|
|
6
6
|
type ActorHandlerInterface,
|
|
7
7
|
createActorDurableObject,
|
|
@@ -53,8 +53,6 @@ export function createServer<R extends Registry<any>>(
|
|
|
53
53
|
// Create config
|
|
54
54
|
const runConfig = {
|
|
55
55
|
...config,
|
|
56
|
-
// The worker acts as a server and doesn't run actors
|
|
57
|
-
role: "server",
|
|
58
56
|
driver: {
|
|
59
57
|
name: "cloudflare-workers",
|
|
60
58
|
manager: () => new CloudflareActorsManagerDriver(),
|
package/src/log.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { getLogger } from "
|
|
2
|
-
|
|
3
|
-
export const LOGGER_NAME = "driver-cloudflare-workers";
|
|
1
|
+
import { getLogger } from "rivetkit/log";
|
|
4
2
|
|
|
5
3
|
export function logger() {
|
|
6
|
-
return getLogger(
|
|
4
|
+
return getLogger("driver-cloudflare-workers");
|
|
7
5
|
}
|
package/src/manager-driver.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context as HonoContext } from "hono";
|
|
2
|
+
import type { Encoding, UniversalWebSocket } from "rivetkit";
|
|
2
3
|
import {
|
|
3
4
|
type ActorOutput,
|
|
4
5
|
type CreateInput,
|
|
@@ -8,11 +9,10 @@ import {
|
|
|
8
9
|
HEADER_AUTH_DATA,
|
|
9
10
|
HEADER_CONN_PARAMS,
|
|
10
11
|
HEADER_ENCODING,
|
|
11
|
-
|
|
12
|
+
type ManagerDisplayInformation,
|
|
12
13
|
type ManagerDriver,
|
|
13
|
-
} from "
|
|
14
|
-
import { ActorAlreadyExists, InternalError } from "
|
|
15
|
-
import type { Context as HonoContext } from "hono";
|
|
14
|
+
} from "rivetkit/driver-helpers";
|
|
15
|
+
import { ActorAlreadyExists, InternalError } from "rivetkit/errors";
|
|
16
16
|
import { getCloudflareAmbientEnv } from "./handler";
|
|
17
17
|
import { logger } from "./log";
|
|
18
18
|
import type { Bindings } from "./mod";
|
|
@@ -24,7 +24,6 @@ interface ActorData {
|
|
|
24
24
|
key: string[];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
// Key constants similar to Redis implementation
|
|
28
27
|
const KEYS = {
|
|
29
28
|
ACTOR: {
|
|
30
29
|
// Combined key for actor metadata (name and key)
|
|
@@ -51,7 +50,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
51
50
|
async sendRequest(actorId: string, actorRequest: Request): Promise<Response> {
|
|
52
51
|
const env = getCloudflareAmbientEnv();
|
|
53
52
|
|
|
54
|
-
logger().debug(
|
|
53
|
+
logger().debug({
|
|
54
|
+
msg: "sending request to durable object",
|
|
55
55
|
actorId,
|
|
56
56
|
method: actorRequest.method,
|
|
57
57
|
url: actorRequest.url,
|
|
@@ -68,10 +68,14 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
68
68
|
actorId: string,
|
|
69
69
|
encoding: Encoding,
|
|
70
70
|
params: unknown,
|
|
71
|
-
): Promise<
|
|
71
|
+
): Promise<UniversalWebSocket> {
|
|
72
72
|
const env = getCloudflareAmbientEnv();
|
|
73
73
|
|
|
74
|
-
logger().debug(
|
|
74
|
+
logger().debug({
|
|
75
|
+
msg: "opening websocket to durable object",
|
|
76
|
+
actorId,
|
|
77
|
+
path,
|
|
78
|
+
});
|
|
75
79
|
|
|
76
80
|
// Make a fetch request to the Durable Object with WebSocket upgrade
|
|
77
81
|
const id = env.ACTOR_DO.idFromString(actorId);
|
|
@@ -80,7 +84,6 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
80
84
|
const headers: Record<string, string> = {
|
|
81
85
|
Upgrade: "websocket",
|
|
82
86
|
Connection: "Upgrade",
|
|
83
|
-
[HEADER_EXPOSE_INTERNAL_ERROR]: "true",
|
|
84
87
|
[HEADER_ENCODING]: encoding,
|
|
85
88
|
};
|
|
86
89
|
if (params) {
|
|
@@ -92,10 +95,7 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
92
95
|
// Use the path parameter to determine the URL
|
|
93
96
|
const url = `http://actor${path}`;
|
|
94
97
|
|
|
95
|
-
logger().debug("rewriting websocket url",
|
|
96
|
-
from: path,
|
|
97
|
-
to: url,
|
|
98
|
-
});
|
|
98
|
+
logger().debug({ msg: "rewriting websocket url", from: path, to: url });
|
|
99
99
|
|
|
100
100
|
const response = await stub.fetch(url, {
|
|
101
101
|
headers,
|
|
@@ -108,7 +108,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
108
108
|
);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
logger().debug(
|
|
111
|
+
logger().debug({
|
|
112
|
+
msg: "durable object websocket connection open",
|
|
112
113
|
actorId,
|
|
113
114
|
});
|
|
114
115
|
|
|
@@ -123,7 +124,7 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
123
124
|
(webSocket as any).dispatchEvent(event);
|
|
124
125
|
}, 0);
|
|
125
126
|
|
|
126
|
-
return webSocket as unknown as
|
|
127
|
+
return webSocket as unknown as UniversalWebSocket;
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
async proxyRequest(
|
|
@@ -131,7 +132,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
131
132
|
actorRequest: Request,
|
|
132
133
|
actorId: string,
|
|
133
134
|
): Promise<Response> {
|
|
134
|
-
logger().debug(
|
|
135
|
+
logger().debug({
|
|
136
|
+
msg: "forwarding request to durable object",
|
|
135
137
|
actorId,
|
|
136
138
|
method: actorRequest.method,
|
|
137
139
|
url: actorRequest.url,
|
|
@@ -151,7 +153,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
151
153
|
params: unknown,
|
|
152
154
|
authData: unknown,
|
|
153
155
|
): Promise<Response> {
|
|
154
|
-
logger().debug(
|
|
156
|
+
logger().debug({
|
|
157
|
+
msg: "forwarding websocket to durable object",
|
|
155
158
|
actorId,
|
|
156
159
|
path,
|
|
157
160
|
});
|
|
@@ -167,7 +170,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
167
170
|
const newUrl = new URL(`http://actor${path}`);
|
|
168
171
|
const actorRequest = new Request(newUrl, c.req.raw);
|
|
169
172
|
|
|
170
|
-
logger().debug(
|
|
173
|
+
logger().debug({
|
|
174
|
+
msg: "rewriting websocket url",
|
|
171
175
|
from: c.req.url,
|
|
172
176
|
to: actorRequest.url,
|
|
173
177
|
});
|
|
@@ -184,7 +188,6 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
184
188
|
}
|
|
185
189
|
|
|
186
190
|
// Add RivetKit headers
|
|
187
|
-
actorRequest.headers.set(HEADER_EXPOSE_INTERNAL_ERROR, "true");
|
|
188
191
|
actorRequest.headers.set(HEADER_ENCODING, encoding);
|
|
189
192
|
if (params) {
|
|
190
193
|
actorRequest.headers.set(HEADER_CONN_PARAMS, JSON.stringify(params));
|
|
@@ -231,7 +234,7 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
231
234
|
> {
|
|
232
235
|
const env = getCloudflareAmbientEnv();
|
|
233
236
|
|
|
234
|
-
logger().debug("getWithKey: searching for actor",
|
|
237
|
+
logger().debug({ msg: "getWithKey: searching for actor", name, key });
|
|
235
238
|
|
|
236
239
|
// Generate deterministic ID from the name and key
|
|
237
240
|
// This is aligned with how createActor generates IDs
|
|
@@ -244,7 +247,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
244
247
|
});
|
|
245
248
|
|
|
246
249
|
if (!actorData) {
|
|
247
|
-
logger().debug(
|
|
250
|
+
logger().debug({
|
|
251
|
+
msg: "getWithKey: no actor found with matching name and key",
|
|
248
252
|
name,
|
|
249
253
|
key,
|
|
250
254
|
actorId,
|
|
@@ -252,7 +256,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
252
256
|
return undefined;
|
|
253
257
|
}
|
|
254
258
|
|
|
255
|
-
logger().debug(
|
|
259
|
+
logger().debug({
|
|
260
|
+
msg: "getWithKey: found actor with matching name and key",
|
|
256
261
|
actorId,
|
|
257
262
|
name,
|
|
258
263
|
key,
|
|
@@ -338,4 +343,11 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
338
343
|
key: actorData.key,
|
|
339
344
|
};
|
|
340
345
|
}
|
|
346
|
+
|
|
347
|
+
displayInformation(): ManagerDisplayInformation {
|
|
348
|
+
return {
|
|
349
|
+
name: "Cloudflare Workers",
|
|
350
|
+
properties: {},
|
|
351
|
+
};
|
|
352
|
+
}
|
|
341
353
|
}
|