-
-
Notifications
You must be signed in to change notification settings - Fork 35k
stream: validate streams in pipeline #62214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
efekrskl
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
efekrskl:feat/validate-streams-in-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−7
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,6 @@ const { | |
| const { eos } = require('internal/streams/end-of-stream'); | ||
| const { once } = require('internal/util'); | ||
| const destroyImpl = require('internal/streams/destroy'); | ||
| const Duplex = require('internal/streams/duplex'); | ||
| const { | ||
| AbortError, | ||
| aggregateTwoErrors, | ||
|
|
@@ -36,6 +35,8 @@ const { | |
| isIterable, | ||
| isReadable, | ||
| isReadableNodeStream, | ||
| isWritableNodeStream, | ||
| isWritableStream, | ||
| isNodeStream, | ||
| isTransformStream, | ||
| isWebStream, | ||
|
|
@@ -159,7 +160,8 @@ async function pumpToWeb(readable, writable, finish, { end }) { | |
| try { | ||
| for await (const chunk of readable) { | ||
| await writer.ready; | ||
| writer.write(chunk).catch(() => {}); | ||
| writer.write(chunk).catch(() => { | ||
| }); | ||
| } | ||
|
|
||
| await writer.ready; | ||
|
|
@@ -179,6 +181,68 @@ async function pumpToWeb(readable, writable, finish, { end }) { | |
| } | ||
| } | ||
|
|
||
| function isValidPipelineSource(stream) { | ||
| return ( | ||
| isIterable(stream) || | ||
| isReadableNodeStream(stream) || | ||
| isReadableStream(stream) || | ||
| isTransformStream(stream) | ||
| ); | ||
| } | ||
|
|
||
| function isValidPipelineTransform(stream) { | ||
| return ( | ||
| isTransformStream(stream) || | ||
| (isNodeStream(stream) && | ||
| isReadableNodeStream(stream) && | ||
| isWritableNodeStream(stream)) | ||
| ); | ||
| } | ||
|
|
||
| function isValidPipelineDestination(stream) { | ||
| return ( | ||
| isWritableNodeStream(stream) || | ||
| isWritableStream(stream) || | ||
| isTransformStream(stream) | ||
| ); | ||
| } | ||
|
|
||
| function validatePipelineStream(stream, i, len) { | ||
| if (typeof stream === 'function') { | ||
| return; | ||
| } | ||
|
|
||
| if (i === 0) { | ||
| if (!isValidPipelineSource(stream)) { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| 'source', | ||
| ['Readable', 'Iterable', 'AsyncIterable', 'ReadableStream', 'TransformStream'], | ||
| stream, | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (i === len - 1) { | ||
| if (!isValidPipelineDestination(stream)) { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| 'destination', | ||
| ['Writable', 'WritableStream', 'TransformStream'], | ||
| stream, | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (!isValidPipelineTransform(stream)) { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| `transform[${i - 1}]`, | ||
| ['Duplex', 'Transform', 'TransformStream'], | ||
| stream, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function pipeline(...streams) { | ||
| return pipelineImpl(streams, once(popCallback(streams))); | ||
| } | ||
|
|
@@ -192,6 +256,10 @@ function pipelineImpl(streams, callback, opts) { | |
| throw new ERR_MISSING_ARGS('streams'); | ||
| } | ||
|
|
||
| for (let i = 0; i < streams.length; i++) { | ||
| validatePipelineStream(streams[i], i, streams.length); | ||
| } | ||
|
|
||
| const ac = new AbortController(); | ||
| const signal = ac.signal; | ||
| const outerSignal = opts?.signal; | ||
|
|
@@ -298,10 +366,8 @@ function pipelineImpl(streams, callback, opts) { | |
| throw new ERR_INVALID_RETURN_VALUE( | ||
| 'Iterable, AsyncIterable or Stream', 'source', ret); | ||
| } | ||
| } else if (isIterable(stream) || isReadableNodeStream(stream) || isTransformStream(stream)) { | ||
| ret = stream; | ||
| } else { | ||
| ret = Duplex.from(stream); | ||
| ret = stream; | ||
| } | ||
| } else if (typeof stream === 'function') { | ||
| if (isTransformStream(ret)) { | ||
|
|
@@ -402,8 +468,6 @@ function pipelineImpl(streams, callback, opts) { | |
| 'val', ['Readable', 'Iterable', 'AsyncIterable', 'ReadableStream', 'TransformStream'], ret); | ||
| } | ||
| ret = stream; | ||
| } else { | ||
| ret = Duplex.from(stream); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, unless I'm missing something with the early validation this becomes dead code. I'm less confident on this one though 😅 |
||
| } | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I see by validating and throwing early this becomes dead code. This is the branch for the "source stream" and the only case this was being used was a non-readable node stream which will now throw