-
-
Notifications
You must be signed in to change notification settings - Fork 769
fix: pass cloudflare env & context in websocket context #3904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: pass cloudflare env & context in websocket context #3904
Conversation
|
@emretinaztepe is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughAdds initialization of per-request context for WebSocket upgrades in Cloudflare runtime environments. The changes ensure Cloudflare bindings (env and context) are available within WebSocket handlers by populating request.context prior to delegating to the WebSocket upgrade handler. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/presets/cloudflare/runtime/cloudflare-durable.ts (1)
55-64: Logic looks correct for passing Cloudflare bindings to WebSocket handlers.The implementation properly initializes
request.contextwith Cloudflare bindings before delegating to the WebSocket upgrade handler. The use ofObject.definePropertywithenumerable: trueand the||=operators ensure safe initialization.However, this exact logic is duplicated in
src/presets/cloudflare/runtime/cloudflare-module.ts(lines 28-37). Consider extracting this initialization into a shared helper function in_module-handler.tsto reduce duplication:// In _module-handler.ts function initializeWebSocketContext( request: Request, env: any, context: any ): void { 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; }Then use in both files:
initializeWebSocketContext(request, env, context); return ws!.handleUpgrade(request, env, context);Note: The comment stating "crossws will forward
request.contexttopeer.context" may be inaccurate. According to crossws documentation, context should be forwarded viactx.props(ExecutionContext properties) rather thanrequest.context. Verify if the current approach actually works as intended or ifcontext.propsshould be used instead.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/presets/cloudflare/runtime/cloudflare-durable.tssrc/presets/cloudflare/runtime/cloudflare-module.ts
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.{ts,js,tsx,jsx}
📄 CodeRabbit inference engine (AGENTS.md)
src/**/*.{ts,js,tsx,jsx}: Usepathefor cross-platform path operations instead of Node.jsnode:path
Use ESM and modern JavaScript
Do not add comments explaining what the line does unless prompted
Files:
src/presets/cloudflare/runtime/cloudflare-module.tssrc/presets/cloudflare/runtime/cloudflare-durable.ts
src/**/*.{ts,js}
📄 CodeRabbit inference engine (AGENTS.md)
Use
unstoragefor storage abstraction
Files:
src/presets/cloudflare/runtime/cloudflare-module.tssrc/presets/cloudflare/runtime/cloudflare-durable.ts
🧠 Learnings (3)
📚 Learning: 2025-12-24T11:45:17.416Z
Learnt from: CR
Repo: nitrojs/nitro PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-24T11:45:17.416Z
Learning: Applies to src/runtime/**/*.{ts,js} : In runtime code, prefer Web APIs (fetch, Request, Response, URL, etc.) over Node.js APIs
Applied to files:
src/presets/cloudflare/runtime/cloudflare-module.ts
📚 Learning: 2025-12-03T19:09:13.905Z
Learnt from: medz
Repo: nitrojs/nitro PR: 3834
File: src/presets/cloudflare/server-entry.ts:63-63
Timestamp: 2025-12-03T19:09:13.905Z
Learning: In the Nitro Cloudflare preset (src/presets/cloudflare/server-entry.ts), native errors from oxc-parser and Node.js readFile operations should be allowed to bubble up naturally without wrapping, as their native error messages are more user-friendly and provide better diagnostic information.
Applied to files:
src/presets/cloudflare/runtime/cloudflare-module.ts
📚 Learning: 2025-12-24T11:45:17.416Z
Learnt from: CR
Repo: nitrojs/nitro PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-24T11:45:17.416Z
Learning: Applies to src/runtime/**/*.{ts,js} : Keep runtime code minimal and side-effect free to reduce bundle size
Applied to files:
src/presets/cloudflare/runtime/cloudflare-module.ts
🧬 Code graph analysis (2)
src/presets/cloudflare/runtime/cloudflare-module.ts (1)
src/runtime/internal/vite/node-runner.mjs (1)
env(174-174)
src/presets/cloudflare/runtime/cloudflare-durable.ts (1)
src/runtime/internal/vite/node-runner.mjs (1)
env(174-174)
🔇 Additional comments (1)
src/presets/cloudflare/runtime/cloudflare-module.ts (1)
28-37: LGTM! Implementation correctly initializes Cloudflare bindings for WebSocket context.The logic mirrors the implementation in
cloudflare-durable.tsand correctly ensuresenvandcontextare accessible within WebSocket handlers viapeer.context.Note: The code duplication concern and refactoring suggestion were already mentioned in the review of
cloudflare-durable.ts(lines 55-64).
Currently, there is no way to access or pass Cloudflare env and bindings for websockets.
This PR passes the Cloudflare environment and context to the CrossWS peer, making bindings accessible within WS handlers.