Skip to content

stream: add drain()/drainSync() for stream/iter - #65598

Open
Ethan-Arrowood wants to merge 2 commits into
nodejs:mainfrom
Ethan-Arrowood:stream-iter-drain
Open

stream: add drain()/drainSync() for stream/iter#65598
Ethan-Arrowood wants to merge 2 commits into
nodejs:mainfrom
Ethan-Arrowood:stream-iter-drain

Conversation

@Ethan-Arrowood

@Ethan-Arrowood Ethan-Arrowood commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Adds drain() and drainSync() to node:stream/iter.

This will enable us to replace for await loops with await drain(stream) across the repo.

- for await (const _ of stream) { /* drain */ } // eslint-disable-line no-unused-vars
+ await drain(stream);

I used two commits: one for the feature, another for the migration so it's easy to review.

I only migrated tests that were already using iter streams. But there are more that could benefit from this util.

This api is inspired by things like Undici's body.dump() method.

Let me know what you think of this API addition!

Every consumer in node:stream/iter retains what it reads, so there is
no way to read a streamable to completion while keeping nothing. To
clear a stream, callers write `for await (const _ of source) {}`. This
is apparent throughout multiple test suites, including the stream/iter
tests themselves.

These are especially useful for QUIC where a receiver doesn't want the
payload, but still has to read it to completion to relieve
backpressure. bytes() does that too, but allocates the whole payload to
discard it.

drain() pulls every batch and drops it, so peak memory is one batch
regardless of volume. It takes the same signal and limit options as
the other consumers, rejects if the source errors mid-stream, and
fulfills with undefined. drainSync() is the synchronous form.

Assisted-by: Claude Opus 5
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/quic
  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Aug 27, 2026
Replaces the `for await (const _ of source) {}` discard idiom with
drain(), which says what it does and does not need an eslint-disable
for the unused loop variable. 89 loops across 60 files, plus 32 now
redundant eslint-disable directives and comments removed.

Only files that already declare --experimental-stream-iter are
converted, so no test gains an experimental flag it did not already
opt into. That leaves 19 files with the old idiom; converting those
would change what those tests run under and belongs in a separate
discussion.

Loops that break early are left alone: those cancel the source rather
than reading it to completion, which is not what drain() does.

Four QUIC tests already bound a local `drain` to the writer-side
drainableProtocol promise. Those locals are renamed to drainPromise;
in test-quic-stream-writer-api.mjs the local shadowed the import and
broke at runtime rather than at parse time.

Assisted-by: Claude Opus 5
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
@Ethan-Arrowood Ethan-Arrowood added the quic Issues and PRs related to the QUIC implementation / HTTP/3. label Aug 27, 2026
Comment thread lib/stream/iter.js
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.08%. Comparing base (d6e67a5) to head (ef6ed85).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65598      +/-   ##
==========================================
+ Coverage   90.07%   90.08%   +0.01%     
==========================================
  Files         751      751              
  Lines      254881   254964      +83     
  Branches    48111    48145      +34     
==========================================
+ Hits       229582   229684     +102     
+ Misses      16486    16460      -26     
- Partials     8813     8820       +7     
Files with missing lines Coverage Δ
lib/internal/streams/iter/consumers.js 97.01% <100.00%> (+0.38%) ⬆️
lib/stream/iter.js 100.00% <100.00%> (ø)

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Ethan-Arrowood Ethan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 27, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 27, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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

Labels

needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC implementation / HTTP/3. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants