@rivetkit/cloudflare-workers 0.0.0-fix-engine-autospawn-binary-path.031e3a4
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 +49 -0
- package/dist/mod.d.mts +44 -0
- package/dist/mod.d.ts +44 -0
- package/dist/mod.js +152 -0
- package/dist/mod.js.map +1 -0
- package/dist/mod.mjs +152 -0
- package/dist/mod.mjs.map +1 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @rivetkit/cloudflare-workers
|
|
2
|
+
|
|
3
|
+
Cloudflare Workers integration for [RivetKit](https://rivet.dev) actors.
|
|
4
|
+
|
|
5
|
+
Host Rivet Actors on Cloudflare Workers with a single import. The wasm runtime
|
|
6
|
+
and the fetch-based WebSocket shim are wired automatically.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { actor } from "rivetkit";
|
|
10
|
+
import { createHandler } from "@rivetkit/cloudflare-workers";
|
|
11
|
+
|
|
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
|
+
});
|
|
19
|
+
|
|
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
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.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class;// src/mod.ts
|
|
2
|
+
var _rivetkitwasm = require('@rivetkit/rivetkit-wasm'); var wasmBindings = _interopRequireWildcard(_rivetkitwasm);
|
|
3
|
+
var _rivetkit_wasm_bgwasm = require('@rivetkit/rivetkit-wasm/rivetkit_wasm_bg.wasm'); var _rivetkit_wasm_bgwasm2 = _interopRequireDefault(_rivetkit_wasm_bgwasm);
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _rivetkit = require('rivetkit');
|
|
8
|
+
|
|
9
|
+
// src/websocket.ts
|
|
10
|
+
var FetchWebSocket = (_class = class _FetchWebSocket {
|
|
11
|
+
static __initStatic() {this.CONNECTING = 0}
|
|
12
|
+
static __initStatic2() {this.OPEN = 1}
|
|
13
|
+
static __initStatic3() {this.CLOSING = 2}
|
|
14
|
+
static __initStatic4() {this.CLOSED = 3}
|
|
15
|
+
__init() {this.binaryType = "arraybuffer"}
|
|
16
|
+
__init2() {this.onopen = null}
|
|
17
|
+
__init3() {this.onmessage = null}
|
|
18
|
+
__init4() {this.onclose = null}
|
|
19
|
+
__init5() {this.onerror = null}
|
|
20
|
+
__init6() {this.readyState = _FetchWebSocket.CONNECTING}
|
|
21
|
+
#socket;
|
|
22
|
+
#pending = [];
|
|
23
|
+
constructor(url, protocols) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);
|
|
24
|
+
void this.#connect(url, protocols);
|
|
25
|
+
}
|
|
26
|
+
async #connect(url, protocols) {
|
|
27
|
+
var _a, _b, _c;
|
|
28
|
+
try {
|
|
29
|
+
const protocolList = Array.isArray(protocols) ? protocols : protocols ? [protocols] : [];
|
|
30
|
+
const headers = new Headers({ Upgrade: "websocket" });
|
|
31
|
+
if (protocolList.length > 0) {
|
|
32
|
+
headers.set("Sec-WebSocket-Protocol", protocolList.join(", "));
|
|
33
|
+
}
|
|
34
|
+
const response = await fetch(
|
|
35
|
+
url.replace(/^ws:/, "http:").replace(/^wss:/, "https:"),
|
|
36
|
+
{ headers }
|
|
37
|
+
);
|
|
38
|
+
const socket = response.webSocket;
|
|
39
|
+
if (!socket) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`websocket upgrade failed with status ${response.status}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
socket.accept();
|
|
45
|
+
socket.binaryType = this.binaryType;
|
|
46
|
+
this.#socket = socket;
|
|
47
|
+
this.readyState = _FetchWebSocket.OPEN;
|
|
48
|
+
socket.addEventListener("message", (event) => {
|
|
49
|
+
var _a2;
|
|
50
|
+
(_a2 = this.onmessage) == null ? void 0 : _a2.call(this, event);
|
|
51
|
+
});
|
|
52
|
+
socket.addEventListener("close", (event) => {
|
|
53
|
+
var _a2;
|
|
54
|
+
this.readyState = _FetchWebSocket.CLOSED;
|
|
55
|
+
(_a2 = this.onclose) == null ? void 0 : _a2.call(this, event);
|
|
56
|
+
});
|
|
57
|
+
socket.addEventListener("error", (event) => {
|
|
58
|
+
var _a2;
|
|
59
|
+
(_a2 = this.onerror) == null ? void 0 : _a2.call(this, event);
|
|
60
|
+
});
|
|
61
|
+
(_a = this.onopen) == null ? void 0 : _a.call(this, new Event("open"));
|
|
62
|
+
for (const data of this.#pending.splice(0)) {
|
|
63
|
+
socket.send(data);
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error("rivetkit cloudflare websocket shim failed", error);
|
|
67
|
+
this.readyState = _FetchWebSocket.CLOSED;
|
|
68
|
+
(_b = this.onerror) == null ? void 0 : _b.call(this, error instanceof Event ? error : new Event("error"));
|
|
69
|
+
(_c = this.onclose) == null ? void 0 : _c.call(this, new CloseEvent("close", { code: 1006 }));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
send(data) {
|
|
73
|
+
var _a;
|
|
74
|
+
if (this.readyState === _FetchWebSocket.CONNECTING) {
|
|
75
|
+
this.#pending.push(data);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
(_a = this.#socket) == null ? void 0 : _a.send(data);
|
|
79
|
+
}
|
|
80
|
+
close(code, reason) {
|
|
81
|
+
var _a;
|
|
82
|
+
this.readyState = _FetchWebSocket.CLOSING;
|
|
83
|
+
(_a = this.#socket) == null ? void 0 : _a.close(code, reason);
|
|
84
|
+
}
|
|
85
|
+
}, _class.__initStatic(), _class.__initStatic2(), _class.__initStatic3(), _class.__initStatic4(), _class);
|
|
86
|
+
var globalScope = globalThis;
|
|
87
|
+
if (!globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__) {
|
|
88
|
+
globalScope.WebSocket = FetchWebSocket;
|
|
89
|
+
globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__ = true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/mod.ts
|
|
93
|
+
var DEFAULT_MANAGER_PATH = "/api/rivet";
|
|
94
|
+
function setup(config) {
|
|
95
|
+
return _rivetkit.setup.call(void 0, {
|
|
96
|
+
runtime: "wasm",
|
|
97
|
+
wasm: { bindings: wasmBindings, initInput: _rivetkit_wasm_bgwasm2.default },
|
|
98
|
+
noWelcome: true,
|
|
99
|
+
...config
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function applyEnv(registry, env, managerPath) {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
const config = registry.config;
|
|
105
|
+
if (!config.endpoint && env.RIVET_ENDPOINT) {
|
|
106
|
+
config.endpoint = env.RIVET_ENDPOINT;
|
|
107
|
+
}
|
|
108
|
+
if (!config.namespace && env.RIVET_NAMESPACE) {
|
|
109
|
+
config.namespace = env.RIVET_NAMESPACE;
|
|
110
|
+
}
|
|
111
|
+
if (!config.token && env.RIVET_TOKEN) {
|
|
112
|
+
config.token = env.RIVET_TOKEN;
|
|
113
|
+
}
|
|
114
|
+
if (env.RIVET_POOL && !((_a = config.envoy) == null ? void 0 : _a.poolName)) {
|
|
115
|
+
config.envoy = { ...config.envoy, poolName: env.RIVET_POOL };
|
|
116
|
+
}
|
|
117
|
+
if (!((_b = config.serverless) == null ? void 0 : _b.basePath)) {
|
|
118
|
+
config.serverless = { ...config.serverless, basePath: managerPath };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function createHandler(registryOrConfig, options = {}) {
|
|
122
|
+
const managerPath = _nullishCoalesce(options.managerPath, () => ( DEFAULT_MANAGER_PATH));
|
|
123
|
+
const registry = registryOrConfig instanceof _rivetkit.Registry ? registryOrConfig : setup(registryOrConfig);
|
|
124
|
+
let envApplied = false;
|
|
125
|
+
return {
|
|
126
|
+
async fetch(request, env, ctx) {
|
|
127
|
+
if (!envApplied) {
|
|
128
|
+
applyEnv(
|
|
129
|
+
registry,
|
|
130
|
+
_nullishCoalesce(env, () => ( {})),
|
|
131
|
+
managerPath
|
|
132
|
+
);
|
|
133
|
+
envApplied = true;
|
|
134
|
+
}
|
|
135
|
+
const url = new URL(request.url);
|
|
136
|
+
if (url.pathname === managerPath || url.pathname.startsWith(`${managerPath}/`)) {
|
|
137
|
+
return registry.handler(request);
|
|
138
|
+
}
|
|
139
|
+
if (options.fetch) {
|
|
140
|
+
return options.fetch(request, env, ctx);
|
|
141
|
+
}
|
|
142
|
+
return new Response(
|
|
143
|
+
"This is a RivetKit server.\n\nLearn more at https://rivet.dev\n"
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
exports.createHandler = createHandler; exports.setup = setup;
|
|
152
|
+
//# sourceMappingURL=mod.js.map
|
package/dist/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/rivet/rivet/rivetkit-typescript/packages/cloudflare-workers/dist/mod.js","../src/mod.ts","../src/websocket.ts"],"names":[],"mappings":"AAAA;ACAA,kHAA8B;AAC9B,iKAAuB;AACvB;AACC;AAGA;AAAS,oCACH;ADAP;AACA;AEGA,IAAM,eAAA,YAAN,MAAM,gBAAe;AAAA,EACpB,4BAAgB,WAAA,EAAa,EAAA;AAAA,EAC7B,6BAAgB,KAAA,EAAO,EAAA;AAAA,EACvB,6BAAgB,QAAA,EAAU,EAAA;AAAA,EAC1B,6BAAgB,OAAA,EAAS,EAAA;AAAA,iBAEzB,WAAA,EAAyB,cAAA;AAAA,kBACzB,OAAA,EAA0C,KAAA;AAAA,kBAC1C,UAAA,EAAoD,KAAA;AAAA,kBACpD,QAAA,EAAgD,KAAA;AAAA,kBAChD,QAAA,EAA2C,KAAA;AAAA,kBAC3C,WAAA,EAAa,eAAA,CAAe,WAAA;AAAA,EAC5B,CAAA,MAAA;AAAA,EACA,CAAA,QAAA,EAA0D,CAAC,CAAA;AAAA,EAE3D,WAAA,CAAY,GAAA,EAAa,SAAA,EAAoC;AAC5D,IAAA,KAAK,IAAA,CAAK,CAAA,OAAA,CAAS,GAAA,EAAK,SAAS,CAAA;AAAA,EAClC;AAAA,EAEA,MAAM,CAAA,OAAA,CAAS,GAAA,EAAa,SAAA,EAAoC;AA9BjE,IAAA,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA;AA+BE,IAAA,IAAI;AACH,MA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AACA,IAAA;AAA0B,EAAA;AAE5B;AAEA;AAMA;AACC,EAAA;AACA,EAAA;AACD;AFbA;AACA;AC/EA;AAcO;AAGN,EAAA;AAAwB,IAAA;AACd,IAAA;AAC6C,IAAA;AAC3C,IAAA;AACR,EAAA;AAEL;AA4BA;AA/DA,EAAA;AAoEC,EAAA;AACA,EAAA;AACC,IAAA;AAAsB,EAAA;AAEvB,EAAA;AACC,IAAA;AAAuB,EAAA;AAExB,EAAA;AACC,IAAA;AAAmB,EAAA;AAEpB,EAAA;AACC,IAAA;AAA2D,EAAA;AAE5D,EAAA;AACC,IAAA;AAAkE,EAAA;AAEpE;AAqBO;AAIN,EAAA;AACA,EAAA;AAIA,EAAA;AAEA,EAAA;AAAO,IAAA;AAEL,MAAA;AACC,QAAA;AAAA,UAAA;AACC,2BAAA;AACS,UAAA;AACT,QAAA;AAED,QAAA;AAAa,MAAA;AAGd,MAAA;AACA,MAAA;AAIC,QAAA;AAA+B,MAAA;AAGhC,MAAA;AACC,QAAA;AAAsC,MAAA;AAGvC,MAAA;AAAW,QAAA;AACV,MAAA;AACD,IAAA;AACD,EAAA;AAEF;ADGA;AACA;AACA;AACA","file":"/home/runner/work/rivet/rivet/rivetkit-typescript/packages/cloudflare-workers/dist/mod.js","sourcesContent":[null,"import * as wasmBindings from \"@rivetkit/rivetkit-wasm\";\nimport wasmModule from \"@rivetkit/rivetkit-wasm/rivetkit_wasm_bg.wasm\";\nimport {\n\tRegistry,\n\ttype RegistryActors,\n\ttype RegistryConfigInput,\n\tsetup as rivetkitSetup,\n} from \"rivetkit\";\n// Installs the fetch-based `globalThis.WebSocket` shim required for the wasm\n// runtime's outbound tunnel to the Rivet engine. Imported for its side effect.\nimport \"./websocket\";\n\nconst DEFAULT_MANAGER_PATH = \"/api/rivet\";\n\n/** Config passed to `setup` / `createHandler`. The wasm runtime is wired automatically. */\nexport type CloudflareSetupConfig<A extends RegistryActors> = Omit<\n\tRegistryConfigInput<A>,\n\t\"runtime\" | \"wasm\"\n>;\n\n/**\n * Wraps rivetkit's `setup` with the Cloudflare Workers WebAssembly runtime wired\n * in. Returns a typed `Registry`, so you can derive a typed client with\n * `createClient<typeof registry>(...)` and pass the same registry to\n * `createHandler`.\n */\nexport function setup<A extends RegistryActors>(\n\tconfig: CloudflareSetupConfig<A>,\n): Registry<A> {\n\treturn rivetkitSetup<A>({\n\t\truntime: \"wasm\",\n\t\twasm: { bindings: wasmBindings, initInput: wasmModule },\n\t\tnoWelcome: true,\n\t\t...config,\n\t} as RegistryConfigInput<A>);\n}\n\nexport interface CreateHandlerOptions {\n\t/**\n\t * Path the Rivet manager API is mounted at. Defaults to `/api/rivet`.\n\t *\n\t * `rivet dev` and the engine poll `<managerPath>/metadata`, so changing this\n\t * also requires configuring the engine-side serverless runner URL to match.\n\t */\n\tmanagerPath?: string;\n\t/**\n\t * Handler for requests that fall outside the Rivet manager API path. Accepts\n\t * a plain `(request, env, ctx)` handler or a framework `fetch` such as\n\t * Hono's `app.fetch`.\n\t */\n\t// biome-ignore lint/suspicious/noExplicitAny: accept any fetch handler shape (e.g. Hono's app.fetch).\n\tfetch?: (request: Request, ...args: any[]) => Response | Promise<Response>;\n}\n\nexport interface CloudflareHandler {\n\tfetch(request: Request, env: unknown, ctx: unknown): Promise<Response>;\n}\n\ntype EnvRecord = Record<string, string | undefined>;\n\n// Fill connection config from the per-request `env` on first request. On\n// Cloudflare the env is not available at module scope, so values that were not\n// set explicitly in the config are sourced from `RIVET_*` Worker variables here.\nfunction applyEnv(\n\tregistry: Registry<RegistryActors>,\n\tenv: EnvRecord,\n\tmanagerPath: string,\n): void {\n\tconst config = registry.config as RegistryConfigInput<RegistryActors>;\n\tif (!config.endpoint && env.RIVET_ENDPOINT) {\n\t\tconfig.endpoint = env.RIVET_ENDPOINT;\n\t}\n\tif (!config.namespace && env.RIVET_NAMESPACE) {\n\t\tconfig.namespace = env.RIVET_NAMESPACE;\n\t}\n\tif (!config.token && env.RIVET_TOKEN) {\n\t\tconfig.token = env.RIVET_TOKEN;\n\t}\n\tif (env.RIVET_POOL && !config.envoy?.poolName) {\n\t\tconfig.envoy = { ...config.envoy, poolName: env.RIVET_POOL };\n\t}\n\tif (!config.serverless?.basePath) {\n\t\tconfig.serverless = { ...config.serverless, basePath: managerPath };\n\t}\n}\n\n/**\n * Creates a Cloudflare Workers handler that hosts Rivet Actors on the wasm\n * runtime. Accepts either a registry from this package's `setup` or a setup\n * config (which is wired through `setup` for you).\n *\n * The Rivet manager API is mounted at `managerPath` (default `/api/rivet`).\n * Requests outside that path are routed to `options.fetch` if provided, letting\n * you mount your own routes alongside Rivet. The engine endpoint is read from\n * `RIVET_ENDPOINT` (with `RIVET_NAMESPACE`, `RIVET_TOKEN`, `RIVET_POOL` optional)\n * unless set in the config.\n */\nexport function createHandler<A extends RegistryActors>(\n\tregistry: Registry<A>,\n\toptions?: CreateHandlerOptions,\n): CloudflareHandler;\nexport function createHandler<A extends RegistryActors>(\n\tconfig: CloudflareSetupConfig<A>,\n\toptions?: CreateHandlerOptions,\n): CloudflareHandler;\nexport function createHandler<A extends RegistryActors>(\n\tregistryOrConfig: Registry<A> | CloudflareSetupConfig<A>,\n\toptions: CreateHandlerOptions = {},\n): CloudflareHandler {\n\tconst managerPath = options.managerPath ?? DEFAULT_MANAGER_PATH;\n\tconst registry =\n\t\tregistryOrConfig instanceof Registry\n\t\t\t? registryOrConfig\n\t\t\t: setup(registryOrConfig);\n\tlet envApplied = false;\n\n\treturn {\n\t\tasync fetch(request, env, ctx) {\n\t\t\tif (!envApplied) {\n\t\t\t\tapplyEnv(\n\t\t\t\t\tregistry as Registry<RegistryActors>,\n\t\t\t\t\t(env ?? {}) as EnvRecord,\n\t\t\t\t\tmanagerPath,\n\t\t\t\t);\n\t\t\t\tenvApplied = true;\n\t\t\t}\n\n\t\t\tconst url = new URL(request.url);\n\t\t\tif (\n\t\t\t\turl.pathname === managerPath ||\n\t\t\t\turl.pathname.startsWith(`${managerPath}/`)\n\t\t\t) {\n\t\t\t\treturn registry.handler(request);\n\t\t\t}\n\n\t\t\tif (options.fetch) {\n\t\t\t\treturn options.fetch(request, env, ctx);\n\t\t\t}\n\n\t\t\treturn new Response(\n\t\t\t\t\"This is a RivetKit server.\\n\\nLearn more at https://rivet.dev\\n\",\n\t\t\t);\n\t\t},\n\t};\n}\n","// Cloudflare Workers do not implement an outbound `new WebSocket(url)` client\n// constructor, but the wasm actor runtime opens its tunnel to the Rivet engine\n// through `globalThis.WebSocket`. This shim translates that constructor into\n// Cloudflare's fetch-based upgrade (`fetch(url, { Upgrade })` + `response.webSocket`)\n// and installs itself on `globalThis` so both the wasm tunnel and the TypeScript\n// client path resolve a working implementation.\n\ntype WebSocketProtocolInput = string | string[] | undefined;\n\ntype CloudflareSocket = WebSocket & { accept(): void };\n\nclass FetchWebSocket {\n\tstatic readonly CONNECTING = 0;\n\tstatic readonly OPEN = 1;\n\tstatic readonly CLOSING = 2;\n\tstatic readonly CLOSED = 3;\n\n\tbinaryType: BinaryType = \"arraybuffer\";\n\tonopen: ((event: Event) => void) | null = null;\n\tonmessage: ((event: MessageEvent) => void) | null = null;\n\tonclose: ((event: CloseEvent) => void) | null = null;\n\tonerror: ((event: Event) => void) | null = null;\n\treadyState = FetchWebSocket.CONNECTING;\n\t#socket: CloudflareSocket | undefined;\n\t#pending: Array<string | ArrayBuffer | ArrayBufferView> = [];\n\n\tconstructor(url: string, protocols?: WebSocketProtocolInput) {\n\t\tvoid this.#connect(url, protocols);\n\t}\n\n\tasync #connect(url: string, protocols?: WebSocketProtocolInput) {\n\t\ttry {\n\t\t\tconst protocolList = Array.isArray(protocols)\n\t\t\t\t? protocols\n\t\t\t\t: protocols\n\t\t\t\t\t? [protocols]\n\t\t\t\t\t: [];\n\t\t\tconst headers = new Headers({ Upgrade: \"websocket\" });\n\t\t\tif (protocolList.length > 0) {\n\t\t\t\theaders.set(\"Sec-WebSocket-Protocol\", protocolList.join(\", \"));\n\t\t\t}\n\t\t\tconst response = await fetch(\n\t\t\t\turl.replace(/^ws:/, \"http:\").replace(/^wss:/, \"https:\"),\n\t\t\t\t{ headers },\n\t\t\t);\n\t\t\tconst socket = (\n\t\t\t\tresponse as unknown as { webSocket: CloudflareSocket | null }\n\t\t\t).webSocket;\n\t\t\tif (!socket) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`websocket upgrade failed with status ${response.status}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tsocket.accept();\n\t\t\tsocket.binaryType = this.binaryType;\n\t\t\tthis.#socket = socket;\n\t\t\tthis.readyState = FetchWebSocket.OPEN;\n\t\t\tsocket.addEventListener(\"message\", (event) => {\n\t\t\t\tthis.onmessage?.(event);\n\t\t\t});\n\t\t\tsocket.addEventListener(\"close\", (event) => {\n\t\t\t\tthis.readyState = FetchWebSocket.CLOSED;\n\t\t\t\tthis.onclose?.(event);\n\t\t\t});\n\t\t\tsocket.addEventListener(\"error\", (event) => {\n\t\t\t\tthis.onerror?.(event);\n\t\t\t});\n\t\t\tthis.onopen?.(new Event(\"open\"));\n\t\t\tfor (const data of this.#pending.splice(0)) {\n\t\t\t\tsocket.send(data);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"rivetkit cloudflare websocket shim failed\", error);\n\t\t\tthis.readyState = FetchWebSocket.CLOSED;\n\t\t\tthis.onerror?.(error instanceof Event ? error : new Event(\"error\"));\n\t\t\tthis.onclose?.(new CloseEvent(\"close\", { code: 1006 }));\n\t\t}\n\t}\n\n\tsend(data: string | ArrayBuffer | ArrayBufferView) {\n\t\tif (this.readyState === FetchWebSocket.CONNECTING) {\n\t\t\tthis.#pending.push(data);\n\t\t\treturn;\n\t\t}\n\t\tthis.#socket?.send(data);\n\t}\n\n\tclose(code?: number, reason?: string) {\n\t\tthis.readyState = FetchWebSocket.CLOSING;\n\t\tthis.#socket?.close(code, reason);\n\t}\n}\n\nconst globalScope = globalThis as unknown as {\n\tWebSocket: typeof WebSocket;\n\t__RIVETKIT_CF_WEBSOCKET_INSTALLED__?: boolean;\n};\n\n// Install once per isolate.\nif (!globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__) {\n\tglobalScope.WebSocket = FetchWebSocket as unknown as typeof WebSocket;\n\tglobalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__ = true;\n}\n\nexport { FetchWebSocket };\n"]}
|
package/dist/mod.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// src/mod.ts
|
|
2
|
+
import * as wasmBindings from "@rivetkit/rivetkit-wasm";
|
|
3
|
+
import wasmModule from "@rivetkit/rivetkit-wasm/rivetkit_wasm_bg.wasm";
|
|
4
|
+
import {
|
|
5
|
+
Registry,
|
|
6
|
+
setup as rivetkitSetup
|
|
7
|
+
} from "rivetkit";
|
|
8
|
+
|
|
9
|
+
// src/websocket.ts
|
|
10
|
+
var FetchWebSocket = class _FetchWebSocket {
|
|
11
|
+
static CONNECTING = 0;
|
|
12
|
+
static OPEN = 1;
|
|
13
|
+
static CLOSING = 2;
|
|
14
|
+
static CLOSED = 3;
|
|
15
|
+
binaryType = "arraybuffer";
|
|
16
|
+
onopen = null;
|
|
17
|
+
onmessage = null;
|
|
18
|
+
onclose = null;
|
|
19
|
+
onerror = null;
|
|
20
|
+
readyState = _FetchWebSocket.CONNECTING;
|
|
21
|
+
#socket;
|
|
22
|
+
#pending = [];
|
|
23
|
+
constructor(url, protocols) {
|
|
24
|
+
void this.#connect(url, protocols);
|
|
25
|
+
}
|
|
26
|
+
async #connect(url, protocols) {
|
|
27
|
+
var _a, _b, _c;
|
|
28
|
+
try {
|
|
29
|
+
const protocolList = Array.isArray(protocols) ? protocols : protocols ? [protocols] : [];
|
|
30
|
+
const headers = new Headers({ Upgrade: "websocket" });
|
|
31
|
+
if (protocolList.length > 0) {
|
|
32
|
+
headers.set("Sec-WebSocket-Protocol", protocolList.join(", "));
|
|
33
|
+
}
|
|
34
|
+
const response = await fetch(
|
|
35
|
+
url.replace(/^ws:/, "http:").replace(/^wss:/, "https:"),
|
|
36
|
+
{ headers }
|
|
37
|
+
);
|
|
38
|
+
const socket = response.webSocket;
|
|
39
|
+
if (!socket) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`websocket upgrade failed with status ${response.status}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
socket.accept();
|
|
45
|
+
socket.binaryType = this.binaryType;
|
|
46
|
+
this.#socket = socket;
|
|
47
|
+
this.readyState = _FetchWebSocket.OPEN;
|
|
48
|
+
socket.addEventListener("message", (event) => {
|
|
49
|
+
var _a2;
|
|
50
|
+
(_a2 = this.onmessage) == null ? void 0 : _a2.call(this, event);
|
|
51
|
+
});
|
|
52
|
+
socket.addEventListener("close", (event) => {
|
|
53
|
+
var _a2;
|
|
54
|
+
this.readyState = _FetchWebSocket.CLOSED;
|
|
55
|
+
(_a2 = this.onclose) == null ? void 0 : _a2.call(this, event);
|
|
56
|
+
});
|
|
57
|
+
socket.addEventListener("error", (event) => {
|
|
58
|
+
var _a2;
|
|
59
|
+
(_a2 = this.onerror) == null ? void 0 : _a2.call(this, event);
|
|
60
|
+
});
|
|
61
|
+
(_a = this.onopen) == null ? void 0 : _a.call(this, new Event("open"));
|
|
62
|
+
for (const data of this.#pending.splice(0)) {
|
|
63
|
+
socket.send(data);
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error("rivetkit cloudflare websocket shim failed", error);
|
|
67
|
+
this.readyState = _FetchWebSocket.CLOSED;
|
|
68
|
+
(_b = this.onerror) == null ? void 0 : _b.call(this, error instanceof Event ? error : new Event("error"));
|
|
69
|
+
(_c = this.onclose) == null ? void 0 : _c.call(this, new CloseEvent("close", { code: 1006 }));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
send(data) {
|
|
73
|
+
var _a;
|
|
74
|
+
if (this.readyState === _FetchWebSocket.CONNECTING) {
|
|
75
|
+
this.#pending.push(data);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
(_a = this.#socket) == null ? void 0 : _a.send(data);
|
|
79
|
+
}
|
|
80
|
+
close(code, reason) {
|
|
81
|
+
var _a;
|
|
82
|
+
this.readyState = _FetchWebSocket.CLOSING;
|
|
83
|
+
(_a = this.#socket) == null ? void 0 : _a.close(code, reason);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var globalScope = globalThis;
|
|
87
|
+
if (!globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__) {
|
|
88
|
+
globalScope.WebSocket = FetchWebSocket;
|
|
89
|
+
globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__ = true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/mod.ts
|
|
93
|
+
var DEFAULT_MANAGER_PATH = "/api/rivet";
|
|
94
|
+
function setup(config) {
|
|
95
|
+
return rivetkitSetup({
|
|
96
|
+
runtime: "wasm",
|
|
97
|
+
wasm: { bindings: wasmBindings, initInput: wasmModule },
|
|
98
|
+
noWelcome: true,
|
|
99
|
+
...config
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
function applyEnv(registry, env, managerPath) {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
const config = registry.config;
|
|
105
|
+
if (!config.endpoint && env.RIVET_ENDPOINT) {
|
|
106
|
+
config.endpoint = env.RIVET_ENDPOINT;
|
|
107
|
+
}
|
|
108
|
+
if (!config.namespace && env.RIVET_NAMESPACE) {
|
|
109
|
+
config.namespace = env.RIVET_NAMESPACE;
|
|
110
|
+
}
|
|
111
|
+
if (!config.token && env.RIVET_TOKEN) {
|
|
112
|
+
config.token = env.RIVET_TOKEN;
|
|
113
|
+
}
|
|
114
|
+
if (env.RIVET_POOL && !((_a = config.envoy) == null ? void 0 : _a.poolName)) {
|
|
115
|
+
config.envoy = { ...config.envoy, poolName: env.RIVET_POOL };
|
|
116
|
+
}
|
|
117
|
+
if (!((_b = config.serverless) == null ? void 0 : _b.basePath)) {
|
|
118
|
+
config.serverless = { ...config.serverless, basePath: managerPath };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function createHandler(registryOrConfig, options = {}) {
|
|
122
|
+
const managerPath = options.managerPath ?? DEFAULT_MANAGER_PATH;
|
|
123
|
+
const registry = registryOrConfig instanceof Registry ? registryOrConfig : setup(registryOrConfig);
|
|
124
|
+
let envApplied = false;
|
|
125
|
+
return {
|
|
126
|
+
async fetch(request, env, ctx) {
|
|
127
|
+
if (!envApplied) {
|
|
128
|
+
applyEnv(
|
|
129
|
+
registry,
|
|
130
|
+
env ?? {},
|
|
131
|
+
managerPath
|
|
132
|
+
);
|
|
133
|
+
envApplied = true;
|
|
134
|
+
}
|
|
135
|
+
const url = new URL(request.url);
|
|
136
|
+
if (url.pathname === managerPath || url.pathname.startsWith(`${managerPath}/`)) {
|
|
137
|
+
return registry.handler(request);
|
|
138
|
+
}
|
|
139
|
+
if (options.fetch) {
|
|
140
|
+
return options.fetch(request, env, ctx);
|
|
141
|
+
}
|
|
142
|
+
return new Response(
|
|
143
|
+
"This is a RivetKit server.\n\nLearn more at https://rivet.dev\n"
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
export {
|
|
149
|
+
createHandler,
|
|
150
|
+
setup
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=mod.mjs.map
|
package/dist/mod.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/mod.ts","../src/websocket.ts"],"sourcesContent":["import * as wasmBindings from \"@rivetkit/rivetkit-wasm\";\nimport wasmModule from \"@rivetkit/rivetkit-wasm/rivetkit_wasm_bg.wasm\";\nimport {\n\tRegistry,\n\ttype RegistryActors,\n\ttype RegistryConfigInput,\n\tsetup as rivetkitSetup,\n} from \"rivetkit\";\n// Installs the fetch-based `globalThis.WebSocket` shim required for the wasm\n// runtime's outbound tunnel to the Rivet engine. Imported for its side effect.\nimport \"./websocket\";\n\nconst DEFAULT_MANAGER_PATH = \"/api/rivet\";\n\n/** Config passed to `setup` / `createHandler`. The wasm runtime is wired automatically. */\nexport type CloudflareSetupConfig<A extends RegistryActors> = Omit<\n\tRegistryConfigInput<A>,\n\t\"runtime\" | \"wasm\"\n>;\n\n/**\n * Wraps rivetkit's `setup` with the Cloudflare Workers WebAssembly runtime wired\n * in. Returns a typed `Registry`, so you can derive a typed client with\n * `createClient<typeof registry>(...)` and pass the same registry to\n * `createHandler`.\n */\nexport function setup<A extends RegistryActors>(\n\tconfig: CloudflareSetupConfig<A>,\n): Registry<A> {\n\treturn rivetkitSetup<A>({\n\t\truntime: \"wasm\",\n\t\twasm: { bindings: wasmBindings, initInput: wasmModule },\n\t\tnoWelcome: true,\n\t\t...config,\n\t} as RegistryConfigInput<A>);\n}\n\nexport interface CreateHandlerOptions {\n\t/**\n\t * Path the Rivet manager API is mounted at. Defaults to `/api/rivet`.\n\t *\n\t * `rivet dev` and the engine poll `<managerPath>/metadata`, so changing this\n\t * also requires configuring the engine-side serverless runner URL to match.\n\t */\n\tmanagerPath?: string;\n\t/**\n\t * Handler for requests that fall outside the Rivet manager API path. Accepts\n\t * a plain `(request, env, ctx)` handler or a framework `fetch` such as\n\t * Hono's `app.fetch`.\n\t */\n\t// biome-ignore lint/suspicious/noExplicitAny: accept any fetch handler shape (e.g. Hono's app.fetch).\n\tfetch?: (request: Request, ...args: any[]) => Response | Promise<Response>;\n}\n\nexport interface CloudflareHandler {\n\tfetch(request: Request, env: unknown, ctx: unknown): Promise<Response>;\n}\n\ntype EnvRecord = Record<string, string | undefined>;\n\n// Fill connection config from the per-request `env` on first request. On\n// Cloudflare the env is not available at module scope, so values that were not\n// set explicitly in the config are sourced from `RIVET_*` Worker variables here.\nfunction applyEnv(\n\tregistry: Registry<RegistryActors>,\n\tenv: EnvRecord,\n\tmanagerPath: string,\n): void {\n\tconst config = registry.config as RegistryConfigInput<RegistryActors>;\n\tif (!config.endpoint && env.RIVET_ENDPOINT) {\n\t\tconfig.endpoint = env.RIVET_ENDPOINT;\n\t}\n\tif (!config.namespace && env.RIVET_NAMESPACE) {\n\t\tconfig.namespace = env.RIVET_NAMESPACE;\n\t}\n\tif (!config.token && env.RIVET_TOKEN) {\n\t\tconfig.token = env.RIVET_TOKEN;\n\t}\n\tif (env.RIVET_POOL && !config.envoy?.poolName) {\n\t\tconfig.envoy = { ...config.envoy, poolName: env.RIVET_POOL };\n\t}\n\tif (!config.serverless?.basePath) {\n\t\tconfig.serverless = { ...config.serverless, basePath: managerPath };\n\t}\n}\n\n/**\n * Creates a Cloudflare Workers handler that hosts Rivet Actors on the wasm\n * runtime. Accepts either a registry from this package's `setup` or a setup\n * config (which is wired through `setup` for you).\n *\n * The Rivet manager API is mounted at `managerPath` (default `/api/rivet`).\n * Requests outside that path are routed to `options.fetch` if provided, letting\n * you mount your own routes alongside Rivet. The engine endpoint is read from\n * `RIVET_ENDPOINT` (with `RIVET_NAMESPACE`, `RIVET_TOKEN`, `RIVET_POOL` optional)\n * unless set in the config.\n */\nexport function createHandler<A extends RegistryActors>(\n\tregistry: Registry<A>,\n\toptions?: CreateHandlerOptions,\n): CloudflareHandler;\nexport function createHandler<A extends RegistryActors>(\n\tconfig: CloudflareSetupConfig<A>,\n\toptions?: CreateHandlerOptions,\n): CloudflareHandler;\nexport function createHandler<A extends RegistryActors>(\n\tregistryOrConfig: Registry<A> | CloudflareSetupConfig<A>,\n\toptions: CreateHandlerOptions = {},\n): CloudflareHandler {\n\tconst managerPath = options.managerPath ?? DEFAULT_MANAGER_PATH;\n\tconst registry =\n\t\tregistryOrConfig instanceof Registry\n\t\t\t? registryOrConfig\n\t\t\t: setup(registryOrConfig);\n\tlet envApplied = false;\n\n\treturn {\n\t\tasync fetch(request, env, ctx) {\n\t\t\tif (!envApplied) {\n\t\t\t\tapplyEnv(\n\t\t\t\t\tregistry as Registry<RegistryActors>,\n\t\t\t\t\t(env ?? {}) as EnvRecord,\n\t\t\t\t\tmanagerPath,\n\t\t\t\t);\n\t\t\t\tenvApplied = true;\n\t\t\t}\n\n\t\t\tconst url = new URL(request.url);\n\t\t\tif (\n\t\t\t\turl.pathname === managerPath ||\n\t\t\t\turl.pathname.startsWith(`${managerPath}/`)\n\t\t\t) {\n\t\t\t\treturn registry.handler(request);\n\t\t\t}\n\n\t\t\tif (options.fetch) {\n\t\t\t\treturn options.fetch(request, env, ctx);\n\t\t\t}\n\n\t\t\treturn new Response(\n\t\t\t\t\"This is a RivetKit server.\\n\\nLearn more at https://rivet.dev\\n\",\n\t\t\t);\n\t\t},\n\t};\n}\n","// Cloudflare Workers do not implement an outbound `new WebSocket(url)` client\n// constructor, but the wasm actor runtime opens its tunnel to the Rivet engine\n// through `globalThis.WebSocket`. This shim translates that constructor into\n// Cloudflare's fetch-based upgrade (`fetch(url, { Upgrade })` + `response.webSocket`)\n// and installs itself on `globalThis` so both the wasm tunnel and the TypeScript\n// client path resolve a working implementation.\n\ntype WebSocketProtocolInput = string | string[] | undefined;\n\ntype CloudflareSocket = WebSocket & { accept(): void };\n\nclass FetchWebSocket {\n\tstatic readonly CONNECTING = 0;\n\tstatic readonly OPEN = 1;\n\tstatic readonly CLOSING = 2;\n\tstatic readonly CLOSED = 3;\n\n\tbinaryType: BinaryType = \"arraybuffer\";\n\tonopen: ((event: Event) => void) | null = null;\n\tonmessage: ((event: MessageEvent) => void) | null = null;\n\tonclose: ((event: CloseEvent) => void) | null = null;\n\tonerror: ((event: Event) => void) | null = null;\n\treadyState = FetchWebSocket.CONNECTING;\n\t#socket: CloudflareSocket | undefined;\n\t#pending: Array<string | ArrayBuffer | ArrayBufferView> = [];\n\n\tconstructor(url: string, protocols?: WebSocketProtocolInput) {\n\t\tvoid this.#connect(url, protocols);\n\t}\n\n\tasync #connect(url: string, protocols?: WebSocketProtocolInput) {\n\t\ttry {\n\t\t\tconst protocolList = Array.isArray(protocols)\n\t\t\t\t? protocols\n\t\t\t\t: protocols\n\t\t\t\t\t? [protocols]\n\t\t\t\t\t: [];\n\t\t\tconst headers = new Headers({ Upgrade: \"websocket\" });\n\t\t\tif (protocolList.length > 0) {\n\t\t\t\theaders.set(\"Sec-WebSocket-Protocol\", protocolList.join(\", \"));\n\t\t\t}\n\t\t\tconst response = await fetch(\n\t\t\t\turl.replace(/^ws:/, \"http:\").replace(/^wss:/, \"https:\"),\n\t\t\t\t{ headers },\n\t\t\t);\n\t\t\tconst socket = (\n\t\t\t\tresponse as unknown as { webSocket: CloudflareSocket | null }\n\t\t\t).webSocket;\n\t\t\tif (!socket) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`websocket upgrade failed with status ${response.status}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tsocket.accept();\n\t\t\tsocket.binaryType = this.binaryType;\n\t\t\tthis.#socket = socket;\n\t\t\tthis.readyState = FetchWebSocket.OPEN;\n\t\t\tsocket.addEventListener(\"message\", (event) => {\n\t\t\t\tthis.onmessage?.(event);\n\t\t\t});\n\t\t\tsocket.addEventListener(\"close\", (event) => {\n\t\t\t\tthis.readyState = FetchWebSocket.CLOSED;\n\t\t\t\tthis.onclose?.(event);\n\t\t\t});\n\t\t\tsocket.addEventListener(\"error\", (event) => {\n\t\t\t\tthis.onerror?.(event);\n\t\t\t});\n\t\t\tthis.onopen?.(new Event(\"open\"));\n\t\t\tfor (const data of this.#pending.splice(0)) {\n\t\t\t\tsocket.send(data);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"rivetkit cloudflare websocket shim failed\", error);\n\t\t\tthis.readyState = FetchWebSocket.CLOSED;\n\t\t\tthis.onerror?.(error instanceof Event ? error : new Event(\"error\"));\n\t\t\tthis.onclose?.(new CloseEvent(\"close\", { code: 1006 }));\n\t\t}\n\t}\n\n\tsend(data: string | ArrayBuffer | ArrayBufferView) {\n\t\tif (this.readyState === FetchWebSocket.CONNECTING) {\n\t\t\tthis.#pending.push(data);\n\t\t\treturn;\n\t\t}\n\t\tthis.#socket?.send(data);\n\t}\n\n\tclose(code?: number, reason?: string) {\n\t\tthis.readyState = FetchWebSocket.CLOSING;\n\t\tthis.#socket?.close(code, reason);\n\t}\n}\n\nconst globalScope = globalThis as unknown as {\n\tWebSocket: typeof WebSocket;\n\t__RIVETKIT_CF_WEBSOCKET_INSTALLED__?: boolean;\n};\n\n// Install once per isolate.\nif (!globalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__) {\n\tglobalScope.WebSocket = FetchWebSocket as unknown as typeof WebSocket;\n\tglobalScope.__RIVETKIT_CF_WEBSOCKET_INSTALLED__ = true;\n}\n\nexport { FetchWebSocket };\n"],"mappings":";AAAA,YAAY,kBAAkB;AAC9B,OAAO,gBAAgB;AACvB;AAAA,EACC;AAAA,EAGA,SAAS;AAAA,OACH;;;ACIP,IAAM,iBAAN,MAAM,gBAAe;AAAA,EACpB,OAAgB,aAAa;AAAA,EAC7B,OAAgB,OAAO;AAAA,EACvB,OAAgB,UAAU;AAAA,EAC1B,OAAgB,SAAS;AAAA,EAEzB,aAAyB;AAAA,EACzB,SAA0C;AAAA,EAC1C,YAAoD;AAAA,EACpD,UAAgD;AAAA,EAChD,UAA2C;AAAA,EAC3C,aAAa,gBAAe;AAAA,EAC5B;AAAA,EACA,WAA0D,CAAC;AAAA,EAE3D,YAAY,KAAa,WAAoC;AAC5D,SAAK,KAAK,SAAS,KAAK,SAAS;AAAA,EAClC;AAAA,EAEA,MAAM,SAAS,KAAa,WAAoC;AA9BjE;AA+BE,QAAI;AACH,YAAM,eAAe,MAAM,QAAQ,SAAS,IACzC,YACA,YACC,CAAC,SAAS,IACV,CAAC;AACL,YAAM,UAAU,IAAI,QAAQ,EAAE,SAAS,YAAY,CAAC;AACpD,UAAI,aAAa,SAAS,GAAG;AAC5B,gBAAQ,IAAI,0BAA0B,aAAa,KAAK,IAAI,CAAC;AAAA,MAC9D;AACA,YAAM,WAAW,MAAM;AAAA,QACtB,IAAI,QAAQ,QAAQ,OAAO,EAAE,QAAQ,SAAS,QAAQ;AAAA,QACtD,EAAE,QAAQ;AAAA,MACX;AACA,YAAM,SACL,SACC;AACF,UAAI,C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AW,GAAG,GACxC;AACD,eAAO,SAAS,QAAQ,OAAO;AAAA,MAChC;AAEA,UAAI,QAAQ,OAAO;AAClB,eAAO,QAAQ,MAAM,SAAS,KAAK,GAAG;AAAA,MACvC;AAEA,aAAO,IAAI;AAAA,QACV;AAAA,MACD;AAAA,IACD;AAAA,EACD;AACD;","names":["_a"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rivetkit/cloudflare-workers",
|
|
3
|
+
"version": "0.0.0-fix-engine-autospawn-binary-path.031e3a4",
|
|
4
|
+
"description": "Cloudflare Workers integration for RivetKit actors",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"rivetkit",
|
|
8
|
+
"cloudflare",
|
|
9
|
+
"cloudflare-workers",
|
|
10
|
+
"workers",
|
|
11
|
+
"workerd",
|
|
12
|
+
"serverless",
|
|
13
|
+
"edge",
|
|
14
|
+
"wasm",
|
|
15
|
+
"actor",
|
|
16
|
+
"microservices"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./dist/chunk-*.js",
|
|
20
|
+
"./dist/chunk-*.cjs",
|
|
21
|
+
"./src/websocket.ts"
|
|
22
|
+
],
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"package.json"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/mod.d.mts",
|
|
31
|
+
"default": "./dist/mod.mjs"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/mod.d.ts",
|
|
35
|
+
"default": "./dist/mod.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup src/mod.ts",
|
|
41
|
+
"check-types": "tsc --noEmit"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@rivetkit/rivetkit-wasm": "0.0.0-fix-engine-autospawn-binary-path.031e3a4",
|
|
45
|
+
"rivetkit": "0.0.0-fix-engine-autospawn-binary-path.031e3a4"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^22.0.0",
|
|
49
|
+
"tsup": "^8.4.0",
|
|
50
|
+
"typescript": "^5.5.2"
|
|
51
|
+
},
|
|
52
|
+
"stableVersion": "0.8.0"
|
|
53
|
+
}
|