Skip to content

Conversation

@emretinaztepe
Copy link

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.

@emretinaztepe emretinaztepe requested a review from pi0 as a code owner December 30, 2025 00:40
@vercel
Copy link

vercel bot commented Dec 30, 2025

@emretinaztepe is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 30, 2025

📝 Walkthrough

Walkthrough

Adds 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

Cohort / File(s) Change Summary
Cloudflare WebSocket Context Initialization
src/presets/cloudflare/runtime/cloudflare-durable.ts, src/presets/cloudflare/runtime/cloudflare-module.ts
Adds runtime logic to initialize per-request context for WebSocket upgrades. Both files now retrieve or create request.context.cloudflare and populate env and context properties before delegating to existing WebSocket upgrade handlers. Ensures bindings are accessible in crossws-enabled WebSocket paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with 'fix:' prefix and clearly describes the main change of passing Cloudflare env & context in websocket context.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem and the solution of passing Cloudflare environment and context to CrossWS peers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a 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.context with Cloudflare bindings before delegating to the WebSocket upgrade handler. The use of Object.defineProperty with enumerable: true and 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.ts to 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.context to peer.context" may be inaccurate. According to crossws documentation, context should be forwarded via ctx.props (ExecutionContext properties) rather than request.context. Verify if the current approach actually works as intended or if context.props should be used instead.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a1198e and a1fd9d9.

📒 Files selected for processing (2)
  • src/presets/cloudflare/runtime/cloudflare-durable.ts
  • src/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}: Use pathe for cross-platform path operations instead of Node.js node:path
Use ESM and modern JavaScript
Do not add comments explaining what the line does unless prompted

Files:

  • src/presets/cloudflare/runtime/cloudflare-module.ts
  • src/presets/cloudflare/runtime/cloudflare-durable.ts
src/**/*.{ts,js}

📄 CodeRabbit inference engine (AGENTS.md)

Use unstorage for storage abstraction

Files:

  • src/presets/cloudflare/runtime/cloudflare-module.ts
  • src/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.ts and correctly ensures env and context are accessible within WebSocket handlers via peer.context.

Note: The code duplication concern and refactoring suggestion were already mentioned in the review of cloudflare-durable.ts (lines 55-64).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant