Skip to content

fix(dev): proxy websocket upgrades for devProxy routes with ws - #4480

Open
danielroe wants to merge 1 commit into
mainfrom
fix/devproxy-ws-upgrade
Open

fix(dev): proxy websocket upgrades for devProxy routes with ws#4480
danielroe wants to merge 1 commit into
mainfrom
fix/devproxy-ws-upgrade

Conversation

@danielroe

Copy link
Copy Markdown
Member

🔗 Linked issue

resolves #4269

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

this is also reproducible on v2 (see nuxt/cli#107).

the issue is that devProxy with ws: true proxies HTTP fine but the upgrade falls through to the dev worker and gets answered as a normal request

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroe
danielroe requested a review from pi0 as a code owner July 25, 2026 08:30
@vercel

vercel Bot commented Jul 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview, Comment Jul 25, 2026 8:30am

Request Review

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

NitroDevApp now routes WebSocket upgrades through matching devProxy rules marked ws. NitroDevServer checks this path before forwarding upgrades to the worker, with unit tests covering successful proxying, subpaths, query strings, and fallback behavior.

Dev proxy WebSocket handling

Layer / File(s) Summary
Register and route WebSocket proxies
src/dev/app.ts
Stores ws-enabled proxy instances, matches exact or normalized prefix paths, forwards upgrades, and destroys sockets after proxy errors.
Delegate upgrades before worker handling
src/dev/server.ts
Calls proxyUpgrade before the existing worker upgrade capability check and forwarding.
Validate upgrade forwarding and fallback
test/unit/dev-proxy-ws.test.ts
Tests successful upgrades, echoed frames, preserved paths and query strings, cleanup, and fallback for routes without ws: true.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits and clearly describes the websocket proxy fix for devProxy routes.
Description check ✅ Passed The description is directly related to the devProxy websocket upgrade bug and proposed fix.
Linked Issues check ✅ Passed The changes match #4269 by routing matching WS upgrades through devProxy before falling back to the worker.
Out of Scope Changes check ✅ Passed The patch stays focused on devProxy websocket upgrade handling and related tests, with no obvious unrelated changes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/devproxy-ws-upgrade

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.

@pkg-pr-new

pkg-pr-new Bot commented Jul 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4480

commit: cd32e1b

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
test/unit/dev-proxy-ws.test.ts (1)

33-33: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use an options object for request options.

Change upgradeRequest(port, path) to accept { path } as its second argument and update the call sites. As per coding guidelines, “For multi-argument functions, use an options object as the second parameter.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/dev-proxy-ws.test.ts` at line 33, Update upgradeRequest to accept
an options object containing path as its second parameter instead of a separate
path argument, then revise every call site to pass that object while preserving
the existing request behavior.

Source: Coding guidelines

src/dev/app.ts (2)

121-121: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use an options object for proxyUpgrade.

Change the new positional socket, head parameters to a second options object, then update callers in src/dev/server.ts and the test. As per coding guidelines, “For multi-argument functions, use an options object as the second parameter.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/dev/app.ts` at line 121, Update proxyUpgrade to accept an options object
as its second parameter containing the current socket and head values, then
adjust all callers in server.ts and the test to pass those fields through the
object while preserving existing behavior.

Source: Coding guidelines


116-120: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove unrequested explanatory comments.

  • src/dev/app.ts#L116-L120: remove the behavior-restating JSDoc.
  • test/unit/dev-proxy-ws.test.ts#L20-L20: remove the helper-description comment.

As per coding guidelines, “Do not add comments explaining what the line does unless prompted.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/dev/app.ts` around lines 116 - 120, Remove the behavior-restating JSDoc
immediately preceding the WebSocket proxy helper in src/dev/app.ts (lines
116-120), and remove the helper-description comment in
test/unit/dev-proxy-ws.test.ts (line 20); retain the associated code unchanged.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/unit/dev-proxy-ws.test.ts`:
- Around line 41-42: Update the request setup in the WebSocket upgrade test so
the timeout created by setTimeout is cleared before rejecting on req’s error
event. Preserve the existing “upgrade timed out” rejection and error propagation
while ensuring both fallback failures do not leave the five-second timer active.

---

Nitpick comments:
In `@src/dev/app.ts`:
- Line 121: Update proxyUpgrade to accept an options object as its second
parameter containing the current socket and head values, then adjust all callers
in server.ts and the test to pass those fields through the object while
preserving existing behavior.
- Around line 116-120: Remove the behavior-restating JSDoc immediately preceding
the WebSocket proxy helper in src/dev/app.ts (lines 116-120), and remove the
helper-description comment in test/unit/dev-proxy-ws.test.ts (line 20); retain
the associated code unchanged.

In `@test/unit/dev-proxy-ws.test.ts`:
- Line 33: Update upgradeRequest to accept an options object containing path as
its second parameter instead of a separate path argument, then revise every call
site to pass that object while preserving the existing request behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2c774a78-b11f-4014-8249-4632afd344d1

📥 Commits

Reviewing files that changed from the base of the PR and between 77b77ff and cd32e1b.

📒 Files selected for processing (3)
  • src/dev/app.ts
  • src/dev/server.ts
  • test/unit/dev-proxy-ws.test.ts

Comment on lines +41 to +42
const timeout = setTimeout(() => reject(new Error("upgrade timed out")), 5000);
req.on("error", reject);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win

Clear the timeout on request errors.

The two expected fallback failures reject through error but retain their five-second timers, unnecessarily extending the test process.

Proposed fix
-    req.on("error", reject);
+    req.on("error", (error) => {
+      clearTimeout(timeout);
+      reject(error);
+    });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const timeout = setTimeout(() => reject(new Error("upgrade timed out")), 5000);
req.on("error", reject);
const timeout = setTimeout(() => reject(new Error("upgrade timed out")), 5000);
req.on("error", (error) => {
clearTimeout(timeout);
reject(error);
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/dev-proxy-ws.test.ts` around lines 41 - 42, Update the request
setup in the WebSocket upgrade test so the timeout created by setTimeout is
cleared before rejecting on req’s error event. Preserve the existing “upgrade
timed out” rejection and error propagation while ensuring both fallback failures
do not leave the five-second timer active.

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.

nitro.devProxy[...].ws: true is silently ignored — WebSocket upgrades bypass devProxy and hit the worker

1 participant