@rivetkit/cloudflare-workers 2.2.1 → 2.3.0-rc.13

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 CHANGED
@@ -1,11 +1,49 @@
1
- # RivetKit Cloudflare Workers Adapter
1
+ # @rivetkit/cloudflare-workers
2
2
 
3
- _Library to build and scale stateful workloads_
3
+ Cloudflare Workers integration for [RivetKit](https://rivet.dev) actors.
4
4
 
5
- [Learn More →](https://github.com/rivet-dev/rivetkit)
5
+ Host Rivet Actors on Cloudflare Workers with a single import. The wasm runtime
6
+ and the fetch-based WebSocket shim are wired automatically.
6
7
 
7
- [Discord](https://rivet.dev/discord) — [Documentation](https://rivetkit.org) — [Issues](https://github.com/rivet-dev/rivetkit/issues)
8
+ ```ts
9
+ import { actor } from "rivetkit";
10
+ import { createHandler } from "@rivetkit/cloudflare-workers";
8
11
 
9
- ## License
12
+ const counter = actor({
13
+ state: { count: 0 },
14
+ actions: {
15
+ increment: (c, amount = 1) => (c.state.count += amount),
16
+ getCount: (c) => c.state.count,
17
+ },
18
+ });
10
19
 
11
- Apache 2.0
20
+ export default createHandler({ use: { counter } });
21
+ ```
22
+
23
+ Set `RIVET_ENDPOINT` in `wrangler.toml` `[vars]` (namespace and token may be
24
+ embedded in the URL as `https://namespace:token@host`).
25
+
26
+ ## Mounting your own routes
27
+
28
+ Pass `fetch` to handle everything outside the Rivet manager API path
29
+ (`/api/rivet`). Use `setup` to get a typed registry so a `createClient` call is
30
+ fully typed:
31
+
32
+ ```ts
33
+ import { createHandler, setup } from "@rivetkit/cloudflare-workers";
34
+ import { createClient } from "rivetkit/client";
35
+ import { Hono } from "hono";
36
+
37
+ const registry = setup({ use: { counter } });
38
+
39
+ const app = new Hono<{ Bindings: { RIVET_ENDPOINT: string } }>();
40
+ app.post("/increment/:name", async (c) => {
41
+ const client = createClient<typeof registry>({ endpoint: c.env.RIVET_ENDPOINT });
42
+ const count = await client.counter.getOrCreate(c.req.param("name")).increment(1);
43
+ return c.json({ count });
44
+ });
45
+
46
+ export default createHandler(registry, { fetch: app.fetch });
47
+ ```
48
+
49
+ Learn more at https://rivet.dev/docs.
package/dist/mod.d.mts ADDED
@@ -0,0 +1,44 @@
1
+ import { RegistryActors, RegistryConfigInput, Registry } from 'rivetkit';
2
+
3
+ /** Config passed to `setup` / `createHandler`. The wasm runtime is wired automatically. */
4
+ type CloudflareSetupConfig<A extends RegistryActors> = Omit<RegistryConfigInput<A>, "runtime" | "wasm">;
5
+ /**
6
+ * Wraps rivetkit's `setup` with the Cloudflare Workers WebAssembly runtime wired
7
+ * in. Returns a typed `Registry`, so you can derive a typed client with
8
+ * `createClient<typeof registry>(...)` and pass the same registry to
9
+ * `createHandler`.
10
+ */
11
+ declare function setup<A extends RegistryActors>(config: CloudflareSetupConfig<A>): Registry<A>;
12
+ interface CreateHandlerOptions {
13
+ /**
14
+ * Path the Rivet manager API is mounted at. Defaults to `/api/rivet`.
15
+ *
16
+ * `rivet dev` and the engine poll `<managerPath>/metadata`, so changing this
17
+ * also requires configuring the engine-side serverless runner URL to match.
18
+ */
19
+ managerPath?: string;
20
+ /**
21
+ * Handler for requests that fall outside the Rivet manager API path. Accepts
22
+ * a plain `(request, env, ctx)` handler or a framework `fetch` such as
23
+ * Hono's `app.fetch`.
24
+ */
25
+ fetch?: (request: Request, ...args: any[]) => Response | Promise<Response>;
26
+ }
27
+ interface CloudflareHandler {
28
+ fetch(request: Request, env: unknown, ctx: unknown): Promise<Response>;
29
+ }
30
+ /**
31
+ * Creates a Cloudflare Workers handler that hosts Rivet Actors on the wasm
32
+ * runtime. Accepts either a registry from this package's `setup` or a setup
33
+ * config (which is wired through `setup` for you).
34
+ *
35
+ * The Rivet manager API is mounted at `managerPath` (default `/api/rivet`).
36
+ * Requests outside that path are routed to `options.fetch` if provided, letting
37
+ * you mount your own routes alongside Rivet. The engine endpoint is read from
38
+ * `RIVET_ENDPOINT` (with `RIVET_NAMESPACE`, `RIVET_TOKEN`, `RIVET_POOL` optional)
39
+ * unless set in the config.
40
+ */
41
+ declare function createHandler<A extends RegistryActors>(registry: Registry<A>, options?: CreateHandlerOptions): CloudflareHandler;
42
+ declare function createHandler<A extends RegistryActors>(config: CloudflareSetupConfig<A>, options?: CreateHandlerOptions): CloudflareHandler;
43
+
44
+ export { type CloudflareHandler, type CloudflareSetupConfig, type CreateHandlerOptions, createHandler, setup };
package/dist/mod.d.ts CHANGED
@@ -1,88 +1,44 @@
1
- import { Client, Registry, ActorKey } from 'rivetkit';
2
- export { Client } from 'rivetkit';
3
- import { DurableObject } from 'cloudflare:workers';
4
- import { GetUpgradeWebSocket } from 'rivetkit/utils';
5
- import { z } from 'zod/v4';
1
+ import { RegistryActors, RegistryConfigInput, Registry } from 'rivetkit';
6
2
 
7
- interface DriverContext {
8
- state: DurableObjectState;
9
- }
10
-
11
- declare const ConfigSchema: z.ZodDefault<z.ZodObject<{
12
- managerPath: z.ZodDefault<z.ZodOptional<z.ZodString>>;
13
- runnerKey: z.ZodOptional<z.ZodString>;
14
- noWelcome: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
15
- fetch: z.ZodOptional<z.ZodCustom<ExportedHandlerFetchHandler<{
16
- RIVET: Client<any>;
17
- }, unknown>, ExportedHandlerFetchHandler<{
18
- RIVET: Client<any>;
19
- }, unknown>>>;
20
- }, z.core.$strip>>;
21
- type InputConfig = z.input<typeof ConfigSchema>;
22
- type Config = z.infer<typeof ConfigSchema>;
23
-
24
- /** Cloudflare Workers env */
25
- interface Bindings {
26
- ACTOR_KV: KVNamespace;
27
- ACTOR_DO: DurableObjectNamespace<ActorHandlerInterface>;
28
- }
29
- interface InlineOutput<A extends Registry<any>> {
30
- /** Client to communicate with the actors. */
31
- client: Client<A>;
32
- /** Fetch handler to manually route requests to the Rivet manager API. */
33
- fetch: (request: Request, ...args: any) => Response | Promise<Response>;
34
- config: Config;
35
- ActorHandler: DurableObjectConstructor;
36
- }
37
- interface HandlerOutput {
38
- handler: ExportedHandler<Bindings>;
39
- ActorHandler: DurableObjectConstructor;
40
- }
3
+ /** Config passed to `setup` / `createHandler`. The wasm runtime is wired automatically. */
4
+ type CloudflareSetupConfig<A extends RegistryActors> = Omit<RegistryConfigInput<A>, "runtime" | "wasm">;
41
5
  /**
42
- * Creates an inline client for accessing Rivet Actors privately without a public manager API.
43
- *
44
- * If you want to expose a public manager API, either:
45
- *
46
- * - Use `createHandler` to expose the Rivet API on `/api/rivet`
47
- * - Forward Rivet API requests to `InlineOutput::fetch`
6
+ * Wraps rivetkit's `setup` with the Cloudflare Workers WebAssembly runtime wired
7
+ * in. Returns a typed `Registry`, so you can derive a typed client with
8
+ * `createClient<typeof registry>(...)` and pass the same registry to
9
+ * `createHandler`.
48
10
  */
49
- declare function createInlineClient<R extends Registry<any>>(registry: R, inputConfig?: InputConfig): InlineOutput<R>;
11
+ declare function setup<A extends RegistryActors>(config: CloudflareSetupConfig<A>): Registry<A>;
12
+ interface CreateHandlerOptions {
13
+ /**
14
+ * Path the Rivet manager API is mounted at. Defaults to `/api/rivet`.
15
+ *
16
+ * `rivet dev` and the engine poll `<managerPath>/metadata`, so changing this
17
+ * also requires configuring the engine-side serverless runner URL to match.
18
+ */
19
+ managerPath?: string;
20
+ /**
21
+ * Handler for requests that fall outside the Rivet manager API path. Accepts
22
+ * a plain `(request, env, ctx)` handler or a framework `fetch` such as
23
+ * Hono's `app.fetch`.
24
+ */
25
+ fetch?: (request: Request, ...args: any[]) => Response | Promise<Response>;
26
+ }
27
+ interface CloudflareHandler {
28
+ fetch(request: Request, env: unknown, ctx: unknown): Promise<Response>;
29
+ }
50
30
  /**
51
- * Creates a handler to be exported from a Cloudflare Worker.
52
- *
53
- * This will automatically expose the Rivet manager API on `/api/rivet`.
31
+ * Creates a Cloudflare Workers handler that hosts Rivet Actors on the wasm
32
+ * runtime. Accepts either a registry from this package's `setup` or a setup
33
+ * config (which is wired through `setup` for you).
54
34
  *
55
- * This includes a `fetch` handler and `ActorHandler` Durable Object.
35
+ * The Rivet manager API is mounted at `managerPath` (default `/api/rivet`).
36
+ * Requests outside that path are routed to `options.fetch` if provided, letting
37
+ * you mount your own routes alongside Rivet. The engine endpoint is read from
38
+ * `RIVET_ENDPOINT` (with `RIVET_NAMESPACE`, `RIVET_TOKEN`, `RIVET_POOL` optional)
39
+ * unless set in the config.
56
40
  */
57
- declare function createHandler(registry: Registry<any>, inputConfig?: InputConfig): HandlerOutput;
58
-
59
- interface ActorHandlerInterface extends DurableObject {
60
- create(req: ActorInitRequest): Promise<ActorInitResponse>;
61
- getMetadata(): Promise<{
62
- actorId: string;
63
- name: string;
64
- key: ActorKey;
65
- destroying: boolean;
66
- } | undefined>;
67
- managerKvGet(key: Uint8Array): Promise<Uint8Array | null>;
68
- }
69
- interface ActorInitRequest {
70
- name: string;
71
- key: ActorKey;
72
- input?: unknown;
73
- allowExisting: boolean;
74
- }
75
- type ActorInitResponse = {
76
- success: {
77
- actorId: string;
78
- created: boolean;
79
- };
80
- } | {
81
- error: {
82
- actorAlreadyExists: true;
83
- };
84
- };
85
- type DurableObjectConstructor = new (...args: ConstructorParameters<typeof DurableObject<Bindings>>) => DurableObject<Bindings>;
86
- declare function createActorDurableObject(registry: Registry<any>, getUpgradeWebSocket: GetUpgradeWebSocket): DurableObjectConstructor;
41
+ declare function createHandler<A extends RegistryActors>(registry: Registry<A>, options?: CreateHandlerOptions): CloudflareHandler;
42
+ declare function createHandler<A extends RegistryActors>(config: CloudflareSetupConfig<A>, options?: CreateHandlerOptions): CloudflareHandler;
87
43
 
88
- export { type Bindings, type InputConfig as Config, type DriverContext, type HandlerOutput, type InlineOutput, createActorDurableObject, createHandler, createInlineClient };
44
+ export { type CloudflareHandler, type CloudflareSetupConfig, type CreateHandlerOptions, createHandler, setup };