@rivetkit/cloudflare-workers 2.0.2 → 2.0.4-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/README.md +3 -3
- package/dist/mod.cjs +793 -366
- package/dist/mod.cjs.map +1 -1
- package/dist/mod.d.cts +70 -161
- package/dist/mod.d.ts +70 -161
- package/dist/mod.js +804 -377
- package/dist/mod.js.map +1 -1
- package/package.json +9 -5
- package/src/actor-driver.ts +227 -76
- package/src/actor-handler-do.ts +338 -113
- package/src/actor-id.ts +38 -0
- package/src/actor-kv.ts +71 -0
- package/src/config.ts +22 -10
- package/src/global-kv.ts +6 -0
- package/src/handler.ts +103 -52
- package/src/log.ts +2 -4
- package/src/manager-driver.ts +237 -134
- package/src/mod.ts +9 -1
- package/src/websocket.ts +17 -6
package/src/handler.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { env } from "cloudflare:workers";
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
2
|
+
import type { Client, Registry } from "rivetkit";
|
|
3
|
+
import { createClientWithDriver } from "rivetkit";
|
|
4
|
+
import { buildManagerRouter } from "rivetkit/driver-helpers";
|
|
5
5
|
import {
|
|
6
6
|
type ActorHandlerInterface,
|
|
7
7
|
createActorDurableObject,
|
|
8
8
|
type DurableObjectConstructor,
|
|
9
9
|
} from "./actor-handler-do";
|
|
10
|
-
import { ConfigSchema, type InputConfig } from "./config";
|
|
10
|
+
import { type Config, ConfigSchema, type InputConfig } from "./config";
|
|
11
11
|
import { CloudflareActorsManagerDriver } from "./manager-driver";
|
|
12
12
|
import { upgradeWebSocket } from "./websocket";
|
|
13
13
|
|
|
@@ -26,69 +26,120 @@ export function getCloudflareAmbientEnv(): Bindings {
|
|
|
26
26
|
return env as unknown as Bindings;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
interface
|
|
30
|
-
|
|
29
|
+
export interface InlineOutput<A extends Registry<any>> {
|
|
30
|
+
/** Client to communicate with the actors. */
|
|
31
|
+
client: Client<A>;
|
|
32
|
+
|
|
33
|
+
/** Fetch handler to manually route requests to the Rivet manager API. */
|
|
34
|
+
fetch: (request: Request, ...args: any) => Response | Promise<Response>;
|
|
35
|
+
|
|
36
|
+
config: Config;
|
|
37
|
+
|
|
31
38
|
ActorHandler: DurableObjectConstructor;
|
|
32
39
|
}
|
|
33
40
|
|
|
34
|
-
interface
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
export interface HandlerOutput {
|
|
42
|
+
handler: ExportedHandler<Bindings>;
|
|
43
|
+
ActorHandler: DurableObjectConstructor;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Creates an inline client for accessing Rivet Actors privately without a public manager API.
|
|
48
|
+
*
|
|
49
|
+
* If you want to expose a public manager API, either:
|
|
50
|
+
*
|
|
51
|
+
* - Use `createHandler` to expose the Rivet API on `/api/rivet`
|
|
52
|
+
* - Forward Rivet API requests to `InlineOutput::fetch`
|
|
53
|
+
*/
|
|
54
|
+
export function createInlineClient<R extends Registry<any>>(
|
|
40
55
|
registry: R,
|
|
41
56
|
inputConfig?: InputConfig,
|
|
42
|
-
):
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
): InlineOutput<R> {
|
|
58
|
+
// HACK: Cloudflare does not support using `crypto.randomUUID()` before start, so we pass a default value
|
|
59
|
+
//
|
|
60
|
+
// Runner key is not used on Cloudflare
|
|
61
|
+
inputConfig = { ...inputConfig, runnerKey: "" };
|
|
46
62
|
|
|
47
|
-
|
|
48
|
-
registry: R,
|
|
49
|
-
inputConfig?: InputConfig,
|
|
50
|
-
): SetupOutput<R> {
|
|
63
|
+
// Parse config
|
|
51
64
|
const config = ConfigSchema.parse(inputConfig);
|
|
52
65
|
|
|
53
|
-
// Create config
|
|
54
|
-
const runConfig = {
|
|
55
|
-
...config,
|
|
56
|
-
// The worker acts as a server and doesn't run actors
|
|
57
|
-
role: "server",
|
|
58
|
-
driver: {
|
|
59
|
-
name: "cloudflare-workers",
|
|
60
|
-
manager: () => new CloudflareActorsManagerDriver(),
|
|
61
|
-
// HACK: We can't build the actor driver until we're inside the Durable Object
|
|
62
|
-
actor: undefined as any,
|
|
63
|
-
},
|
|
64
|
-
getUpgradeWebSocket: () => upgradeWebSocket,
|
|
65
|
-
} satisfies RunConfig;
|
|
66
|
-
|
|
67
66
|
// Create Durable Object
|
|
68
|
-
const ActorHandler = createActorDurableObject(
|
|
67
|
+
const ActorHandler = createActorDurableObject(
|
|
68
|
+
registry,
|
|
69
|
+
() => upgradeWebSocket,
|
|
70
|
+
);
|
|
69
71
|
|
|
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();
|
|
72
82
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
createHandler: (hono) => {
|
|
76
|
-
// Build base router
|
|
77
|
-
const app = hono ?? new Hono();
|
|
83
|
+
// Create manager driver
|
|
84
|
+
const managerDriver = new CloudflareActorsManagerDriver();
|
|
78
85
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
// Build the manager router (has actor management endpoints like /actors)
|
|
87
|
+
const { router } = buildManagerRouter(
|
|
88
|
+
parsedConfig,
|
|
89
|
+
managerDriver,
|
|
90
|
+
() => upgradeWebSocket,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Create client using the manager driver
|
|
94
|
+
const client = createClientWithDriver<R>(managerDriver);
|
|
83
95
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
fetch: (request, env, ctx) => {
|
|
87
|
-
return app.fetch(request, env, ctx);
|
|
88
|
-
},
|
|
89
|
-
} satisfies ExportedHandler<Bindings>;
|
|
96
|
+
return { client, fetch: router.fetch.bind(router), config, ActorHandler };
|
|
97
|
+
}
|
|
90
98
|
|
|
91
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Creates a handler to be exported from a Cloudflare Worker.
|
|
101
|
+
*
|
|
102
|
+
* This will automatically expose the Rivet manager API on `/api/rivet`.
|
|
103
|
+
*
|
|
104
|
+
* This includes a `fetch` handler and `ActorHandler` Durable Object.
|
|
105
|
+
*/
|
|
106
|
+
export function createHandler<R extends Registry<any>>(
|
|
107
|
+
registry: R,
|
|
108
|
+
inputConfig?: InputConfig,
|
|
109
|
+
): HandlerOutput {
|
|
110
|
+
const { client, fetch, config, ActorHandler } = createInlineClient(
|
|
111
|
+
registry,
|
|
112
|
+
inputConfig,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// Create Cloudflare handler
|
|
116
|
+
const handler = {
|
|
117
|
+
fetch: async (request, cfEnv, ctx) => {
|
|
118
|
+
const url = new URL(request.url);
|
|
119
|
+
|
|
120
|
+
// Inject Rivet env
|
|
121
|
+
const env = Object.assign({ RIVET: client }, cfEnv);
|
|
122
|
+
|
|
123
|
+
// Mount Rivet manager API
|
|
124
|
+
if (url.pathname.startsWith(config.managerPath)) {
|
|
125
|
+
const strippedPath = url.pathname.substring(
|
|
126
|
+
config.managerPath.length,
|
|
127
|
+
);
|
|
128
|
+
url.pathname = strippedPath;
|
|
129
|
+
const modifiedRequest = new Request(url.toString(), request);
|
|
130
|
+
return fetch(modifiedRequest, env, ctx);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (config.fetch) {
|
|
134
|
+
return config.fetch(request, env, ctx);
|
|
135
|
+
} else {
|
|
136
|
+
return new Response(
|
|
137
|
+
"This is a RivetKit server.\n\nLearn more at https://rivet.dev\n",
|
|
138
|
+
{ status: 200 },
|
|
139
|
+
);
|
|
140
|
+
}
|
|
92
141
|
},
|
|
93
|
-
}
|
|
142
|
+
} satisfies ExportedHandler<Bindings>;
|
|
143
|
+
|
|
144
|
+
return { handler, ActorHandler };
|
|
94
145
|
}
|
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
|
}
|