Skip to content

fix(node): send the head for stream bodies that end without data - #339

Merged
pi0 merged 2 commits into
h3js:mainfrom
danielroe:fix/empty-stream-body
Oct 27, 2026
Merged

pi0 merged 2 commits into
h3js:mainfrom
danielroe:fix/empty-stream-body

Conversation

@danielroe

@danielroe danielroe commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

resolves #338

in Nitro 3 dev mode, every empty non-204/304 response proxied from the worker hangs until the client times out

this broke CI in nuxt/nuxt#36671 and will probably break any user too...

this started happening when env-runner started passing FastResponse to srvx, and there's a good diagnosis in #338 from @onmax....

the bug originated in 4051f22 (v0.11.12), which started deferring the head until 'readable', and it was then activated by unjs/httpxy#186 and unjs/env-runner#92...

so, this PR also listens for 'end' before writing the head, and checks readableEnded up front

Summary by CodeRabbit

  • Bug Fixes
    • Empty or already-ended streams now return an empty response while preserving the configured status, status text, and headers.
    • Responses now finish correctly when a stream ends before becoming readable or ends after processing begins.
    • Streams that error after ending return a 500 status.
  • Tests
    • Added coverage for empty, already-ended, and delayed-ending streams, including error handling.

@danielroe
danielroe requested a review from pi0 as a code owner October 26, 2026 23:12
@vercel

vercel Bot commented Oct 26, 2026

Copy link
Copy Markdown

@danielroe is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fa5b02a8-4f6b-4dac-8517-154727b2032a
Commits

Reviewing files that changed from the base of the PR and between a9a0754 and d1b68fe.

Files selected for processing: 2
  • src/adapters/_node/send.ts
  • test/node-streams.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Walkthrough

pipeBody handles Node streams that have already ended or end without emitting readable. Tests cover empty IncomingMessage bodies and Node readable streams that end before or after response handling begins.

Changes: Empty stream responses

Layer / File(s) Summary
Handle and verify stream end
src/adapters/_node/send.ts, test/node-streams.test.ts
pipeBody writes the configured response head and ends the response when a stream has already ended or ends without emitting readable. Tests check response status, status text, headers, empty bodies, error status, and listener cleanup.

Priority: Not assessed

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Merge Risk: Moderate · up to 9792c

Empty Node stream bodies now get a response head instead of hanging, which fixes the reported issue. An unusual stream that errors after ending could still crash the process because no error listener remains. Keep an error listener until the stream closes before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(node): send the head for stream bodies that end without data" matches the main change. The raw summary shows that pipeBody in src/adapters/_node/send.ts now listens for end and writes…
Linked Issues check Passed Issue #338 requires an ended empty Node stream to receive a response. pipeBody now checks readableEnded, listens for end, writes the configured head, and ends the response. The tests cover an em…
Out of Scope Changes check Passed The source change is limited to empty or already-ended Node stream handling in pipeBody. The added tests cover the linked issue and adjacent event timing cases. No unrelated implementation or test c…
Full details: Title check

Explanation

The title "fix(node): send the head for stream bodies that end without data" matches the main change. The raw summary shows that pipeBody in src/adapters/_node/send.ts now listens for end and writes the intended headers when a Node stream ends without data. The linked objectives describe the same hang, and the added tests cover empty IncomingMessage bodies and already-ended streams. The title is specific and accurate, and it follows the conventional commit style.

Full details: Linked Issues check

Explanation

Issue #338 requires an ended empty Node stream to receive a response. pipeBody now checks readableEnded, listens for end, writes the configured head, and ends the response. The tests cover an empty IncomingMessage, an already-ended stream, and a stream that ends later. The tests verify status, status text, headers, and an empty body.

Full details: Out of Scope Changes check

Explanation

The source change is limited to empty or already-ended Node stream handling in pipeBody. The added tests cover the linked issue and adjacent event timing cases. No unrelated implementation or test changes appear in the reviewed diff.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the stream at dawn
The empty bytes have all withdrawn
The headers reach the waiting door
The response ends; it waits no more
I twitch my nose and hop along
A quiet end can still be strong

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

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/adapters/_node/send.ts 90.90% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@pkg-pr-new

pkg-pr-new Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/srvx@339

commit: 9792ce4

Also add a deterministic test for streams that emit 'end' without 'readable'.
@pi0
pi0 merged commit 2af7a1b into h3js:main Oct 27, 2026
14 of 16 checks passed
@danielroe
danielroe deleted the fix/empty-stream-body branch October 27, 2026 11:55
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.

Node: FastResponse with an empty Node stream body never responds

2 participants