Skip to content

stream: add dump()/dumpSync() for stream/iter - #65598

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

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

Conversation

@Ethan-Arrowood

@Ethan-ArrowoodEthan-Arrowood commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Adds dump() and dumpSync() to node:stream/iter.

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

- for await (const _ of stream) { /* drain */ } // eslint-disable-line no-unused-vars+ await dump(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!

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/quic
  • @nodejs/streams

@nodejs-github-botnodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Aug 27, 2026
@Ethan-ArrowoodEthan-Arrowood added the quic Issues and PRs related to the QUIC transport implementation. label Aug 27, 2026
Comment threadlib/stream/iter.js Outdated
@codecov

codecovBot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (1e9fd95) to head (2db8a62).
⚠️ Report is 25 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #65598 +/- ##
==========================================
- Coverage 90.17% 90.15% -0.02% 
==========================================
Files 769 769 Lines 261448 261545 +97 Branches 49674 49668 -6 ==========================================
+ Hits 235759 235806 +47 - Misses 16736 16775 +39 - Partials 8953 8964 +11 
Files with missing linesCoverage Δ
lib/internal/streams/iter/consumers.js97.12% <100.00%> (+0.35%)⬆️
lib/stream/iter.js100.00% <100.00%> (ø)

... and 34 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-ArrowoodEthan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 27, 2026
@github-actionsgithub-actionsBot 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

@pimterrypimterry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very onboard with the concept, implementation looks good, small point on a test case.

Larger question though: can we rename this? Drain is a separate related but different concept on the writer API. Giving them the same name is a bit confusing, and hits practical issues as well (like the various tests here that have to rename existing drain references that mean something else).

dump() or discard()?

// drain: does not retain data
// =============================================================================

async function testDrainDoesNotRetain() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't really do what it says it does - as is it roughly duplicates testDrainConsumesToCompletion I think. It allocates the buffer once outside the source, so it can't check what's retained anyway. Needs a buffer per chunk with a WeakRef & GC dance if you want to test this I think.

@Ethan-Arrowood

Copy link
Copy Markdown
ContributorAuthor

I'm okay with considering a different name. Honestly, I stuck with drain() because it was in the comments of the code blocks this is meant to replace for await (const _ of stream) { /* drain */ }. But Undici uses dump(). I also like discard().

Let the bikeshedding begin!

@jasnell

Copy link
Copy Markdown
Member

consume()?

@Ethan-Arrowood

Copy link
Copy Markdown
ContributorAuthor

I feel like consume implies its being used somehow. Like all the other methods "consume" the stream and then return something.

I think dump() is the way to go. Undici set some precedent with body.dump().

Can people +1 this comment if they agree on dump()? Otherwise, please bikeshed whatever other name. I'd prefer not to block this too long on the name.

@Ethan-ArrowoodEthan-Arrowood changed the title stream: add drain()/drainSync() for stream/iterstream: add dump()/dumpSync() for stream/iterSep 4, 2026
@Ethan-Arrowood

Copy link
Copy Markdown
ContributorAuthor

We agreed on dump() on a recent QUIC team meeting call. I've renamed it now and updated everything except the branch name. Please re review and I'll get the CI kicked off. Thank you!

@Ethan-ArrowoodEthan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 4, 2026
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@bjohansebasbjohansebas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGMT!

@bjohansebasbjohansebas added the request-ci Add this label to start a Jenkins CI on a PR. label Sep 4, 2026

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment threaddoc/api/quic.md Outdated
Comment threadlib/internal/streams/iter/consumers.js Outdated
@Ethan-Arrowood

Copy link
Copy Markdown
ContributorAuthor

Good catch, fixing now.

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.
dump() 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. dumpSync() is the synchronous form.
The name follows undici's body.dump(). drain() was the obvious
alternative but this module already exports ondrain() and
drainableProtocol() for writer-side backpressure, which is the
opposite side of the pipe.
Assisted-by: Claude Opus 5
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
Replaces the `for await (const _ of source) {}` discard idiom with
dump(), 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 21 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 dump() does.
Assisted-by: Claude Opus 5
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
@Ethan-Arrowood

Copy link
Copy Markdown
ContributorAuthor

My bad @pimterry . I manually reviewed the updates now for any erroneous find/replaces, looks all good to me.

@jasnelljasnell added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-squash PRs the Commit Queue should land as one squashed commit. labels Sep 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@jasnelljasnell added the commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. label Sep 4, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs with CI started, the required approvals, and no outstanding review comments.commit-queue-rebasePRs the Commit Queue should land as multiple self-contained commits.needs-ciPRs that need a full CI run.quicIssues and PRs related to the QUIC transport implementation.streamIssues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@Ethan-Arrowood@nodejs-github-bot@jasnell@mcollina@benjamingr@pimterry@metcoder95@bjohansebas