stream: add drain()/drainSync() for stream/iter - #65598
Open
Ethan-Arrowood wants to merge 2 commits into
Open
Conversation
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>
Collaborator
|
Review requested:
|
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>
jasnell
reviewed
Aug 27, 2026
jasnell
approved these changes
Aug 27, 2026
benjamingr
approved these changes
Aug 27, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
drain()anddrainSync()tonode:stream/iter.This will enable us to replace
for awaitloops withawait drain(stream)across the repo.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!