Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/presets/cloudflare/runtime/cloudflare-durable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export default createHandler<Env>({
// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare#durable-objects
if (hasWebSocket && request.headers.get("upgrade") === "websocket") {
// Ensure Cloudflare bindings are available on `peer.context` inside WebSocket handlers.
// crossws will forward `request.context` to `peer.context`.
let wsContext = (request as any).context;
if (!wsContext) {
wsContext = {};
Object.defineProperty(request as any, "context", { enumerable: true, value: wsContext });
}
wsContext.cloudflare ||= {};
wsContext.cloudflare.env ||= env;
wsContext.cloudflare.context ||= context;
return ws!.handleUpgrade(request, env, context);
}
},
Expand Down
10 changes: 10 additions & 0 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export default createHandler<Env>({
// Websocket upgrade
// https://crossws.unjs.io/adapters/cloudflare
if (hasWebSocket && request.headers.get("upgrade") === "websocket") {
// Ensure Cloudflare bindings are available on `peer.context` inside WebSocket handlers.
// crossws will forward `request.context` to `peer.context`.
let wsContext = (request as any).context;
if (!wsContext) {
wsContext = {};
Object.defineProperty(request as any, "context", { enumerable: true, value: wsContext });
}
wsContext.cloudflare ||= {};
wsContext.cloudflare.env ||= env;
wsContext.cloudflare.context ||= context;
return ws!.handleUpgrade(request as any, env, context);
}
},
Expand Down