@rivetkit/cloudflare-workers 2.0.33 → 2.0.34-rc.2
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/mod.cjs +81 -66
- package/dist/mod.cjs.map +1 -1
- package/dist/mod.d.cts +8 -62
- package/dist/mod.d.ts +8 -62
- package/dist/mod.js +82 -67
- package/dist/mod.js.map +1 -1
- package/package.json +2 -2
- package/src/actor-driver.ts +3 -9
- package/src/actor-handler-do.ts +24 -25
- package/src/config.ts +15 -12
- package/src/handler.ts +34 -20
- package/src/manager-driver.ts +28 -11
package/src/handler.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { env } from "cloudflare:workers";
|
|
2
|
-
import type { Client, Registry
|
|
2
|
+
import type { Client, Registry } from "rivetkit";
|
|
3
|
+
import { createClientWithDriver } from "rivetkit";
|
|
4
|
+
import { buildManagerRouter } from "rivetkit/driver-helpers";
|
|
3
5
|
import {
|
|
4
6
|
type ActorHandlerInterface,
|
|
5
7
|
createActorDurableObject,
|
|
@@ -46,7 +48,7 @@ export interface HandlerOutput {
|
|
|
46
48
|
*
|
|
47
49
|
* If you want to expose a public manager API, either:
|
|
48
50
|
*
|
|
49
|
-
* - Use `createHandler` to expose the Rivet API on `/rivet`
|
|
51
|
+
* - Use `createHandler` to expose the Rivet API on `/api/rivet`
|
|
50
52
|
* - Forward Rivet API requests to `InlineOutput::fetch`
|
|
51
53
|
*/
|
|
52
54
|
export function createInlineClient<R extends Registry<any>>(
|
|
@@ -61,31 +63,43 @@ export function createInlineClient<R extends Registry<any>>(
|
|
|
61
63
|
// Parse config
|
|
62
64
|
const config = ConfigSchema.parse(inputConfig);
|
|
63
65
|
|
|
64
|
-
// Create config
|
|
65
|
-
const runConfig = {
|
|
66
|
-
...config,
|
|
67
|
-
driver: {
|
|
68
|
-
name: "cloudflare-workers",
|
|
69
|
-
manager: () => new CloudflareActorsManagerDriver(),
|
|
70
|
-
// HACK: We can't build the actor driver until we're inside the Durable Object
|
|
71
|
-
actor: undefined as any,
|
|
72
|
-
},
|
|
73
|
-
getUpgradeWebSocket: () => upgradeWebSocket,
|
|
74
|
-
} satisfies RunConfig;
|
|
75
|
-
|
|
76
66
|
// Create Durable Object
|
|
77
|
-
const ActorHandler = createActorDurableObject(
|
|
67
|
+
const ActorHandler = createActorDurableObject(
|
|
68
|
+
registry,
|
|
69
|
+
() => upgradeWebSocket,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Configure registry for cloudflare-workers
|
|
73
|
+
registry.config.noWelcome = true;
|
|
74
|
+
// Disable inspector since it's not supported on Cloudflare Workers
|
|
75
|
+
registry.config.inspector = {
|
|
76
|
+
enabled: false,
|
|
77
|
+
token: () => "",
|
|
78
|
+
};
|
|
79
|
+
// Set manager base path to "/" since the cloudflare handler strips the /api/rivet prefix
|
|
80
|
+
registry.config.managerBasePath = "/";
|
|
81
|
+
const parsedConfig = registry.parseConfig();
|
|
82
|
+
|
|
83
|
+
// Create manager driver
|
|
84
|
+
const managerDriver = new CloudflareActorsManagerDriver();
|
|
85
|
+
|
|
86
|
+
// Build the manager router (has actor management endpoints like /actors)
|
|
87
|
+
const { router } = buildManagerRouter(
|
|
88
|
+
parsedConfig,
|
|
89
|
+
managerDriver,
|
|
90
|
+
() => upgradeWebSocket,
|
|
91
|
+
);
|
|
78
92
|
|
|
79
|
-
// Create
|
|
80
|
-
const
|
|
93
|
+
// Create client using the manager driver
|
|
94
|
+
const client = createClientWithDriver<R>(managerDriver);
|
|
81
95
|
|
|
82
|
-
return { client, fetch, config, ActorHandler };
|
|
96
|
+
return { client, fetch: router.fetch.bind(router), config, ActorHandler };
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
/**
|
|
86
100
|
* Creates a handler to be exported from a Cloudflare Worker.
|
|
87
101
|
*
|
|
88
|
-
* This will automatically expose the Rivet manager API on `/rivet`.
|
|
102
|
+
* This will automatically expose the Rivet manager API on `/api/rivet`.
|
|
89
103
|
*
|
|
90
104
|
* This includes a `fetch` handler and `ActorHandler` Durable Object.
|
|
91
105
|
*/
|
|
@@ -120,7 +134,7 @@ export function createHandler<R extends Registry<any>>(
|
|
|
120
134
|
return config.fetch(request, env, ctx);
|
|
121
135
|
} else {
|
|
122
136
|
return new Response(
|
|
123
|
-
"This is a RivetKit server.\n\nLearn more at https://
|
|
137
|
+
"This is a RivetKit server.\n\nLearn more at https://rivet.dev\n",
|
|
124
138
|
{ status: 200 },
|
|
125
139
|
);
|
|
126
140
|
}
|
package/src/manager-driver.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import type { Context as HonoContext } from "hono";
|
|
2
|
-
import type { Encoding, UniversalWebSocket } from "rivetkit";
|
|
1
|
+
import type { Hono, Context as HonoContext } from "hono";
|
|
2
|
+
import type { Encoding, RegistryConfig, UniversalWebSocket } from "rivetkit";
|
|
3
3
|
import {
|
|
4
4
|
type ActorOutput,
|
|
5
5
|
type CreateInput,
|
|
6
6
|
type GetForIdInput,
|
|
7
7
|
type GetOrCreateWithKeyInput,
|
|
8
8
|
type GetWithKeyInput,
|
|
9
|
-
generateRandomString,
|
|
10
9
|
type ListActorsInput,
|
|
11
10
|
type ManagerDisplayInformation,
|
|
12
11
|
type ManagerDriver,
|
|
@@ -141,6 +140,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
141
140
|
actorRequest: Request,
|
|
142
141
|
actorId: string,
|
|
143
142
|
): Promise<Response> {
|
|
143
|
+
const env = getCloudflareAmbientEnv();
|
|
144
|
+
|
|
144
145
|
// Parse actor ID to get DO ID
|
|
145
146
|
const [doId] = parseActorId(actorId);
|
|
146
147
|
|
|
@@ -152,8 +153,8 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
152
153
|
url: actorRequest.url,
|
|
153
154
|
});
|
|
154
155
|
|
|
155
|
-
const id =
|
|
156
|
-
const stub =
|
|
156
|
+
const id = env.ACTOR_DO.idFromString(doId);
|
|
157
|
+
const stub = env.ACTOR_DO.get(id);
|
|
157
158
|
|
|
158
159
|
return await stub.fetch(actorRequest);
|
|
159
160
|
}
|
|
@@ -218,15 +219,17 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
218
219
|
);
|
|
219
220
|
|
|
220
221
|
// Parse actor ID to get DO ID
|
|
222
|
+
const env = getCloudflareAmbientEnv();
|
|
221
223
|
const [doId] = parseActorId(actorId);
|
|
222
|
-
const id =
|
|
223
|
-
const stub =
|
|
224
|
+
const id = env.ACTOR_DO.idFromString(doId);
|
|
225
|
+
const stub = env.ACTOR_DO.get(id);
|
|
224
226
|
|
|
225
227
|
return await stub.fetch(actorRequest);
|
|
226
228
|
}
|
|
227
229
|
|
|
228
230
|
async getForId({
|
|
229
231
|
c,
|
|
232
|
+
name,
|
|
230
233
|
actorId,
|
|
231
234
|
}: GetForIdInput<{ Bindings: Bindings }>): Promise<
|
|
232
235
|
ActorOutput | undefined
|
|
@@ -412,12 +415,26 @@ export class CloudflareActorsManagerDriver implements ManagerDriver {
|
|
|
412
415
|
|
|
413
416
|
displayInformation(): ManagerDisplayInformation {
|
|
414
417
|
return {
|
|
415
|
-
|
|
416
|
-
|
|
418
|
+
properties: {
|
|
419
|
+
Driver: "Cloudflare Workers",
|
|
420
|
+
},
|
|
417
421
|
};
|
|
418
422
|
}
|
|
419
423
|
|
|
420
|
-
|
|
421
|
-
|
|
424
|
+
setGetUpgradeWebSocket(): void {
|
|
425
|
+
// No-op for Cloudflare Workers - WebSocket upgrades are handled by the DO
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async kvGet(actorId: string, key: Uint8Array): Promise<string | null> {
|
|
429
|
+
const env = getCloudflareAmbientEnv();
|
|
430
|
+
|
|
431
|
+
// Parse actor ID to get DO ID
|
|
432
|
+
const [doId] = parseActorId(actorId);
|
|
433
|
+
|
|
434
|
+
const id = env.ACTOR_DO.idFromString(doId);
|
|
435
|
+
const stub = env.ACTOR_DO.get(id);
|
|
436
|
+
|
|
437
|
+
const value = await stub.managerKvGet(key);
|
|
438
|
+
return value !== null ? new TextDecoder().decode(value) : null;
|
|
422
439
|
}
|
|
423
440
|
}
|