-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
stream: add compose operator #44937
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
Merged
nodejs-github-bot
merged 7 commits into
nodejs:main
from
rluvaton:feat/add-compose-stream-operator
Oct 31, 2022
Merged
stream: add compose operator #44937
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
076bbe5
stream: add compose operator
rluvaton da9255f
stream: rename import
rluvaton 273b0b6
stream: add abort signal support
rluvaton 2e4898e
stream: add validation and refactor tests
rluvaton fed2ac3
stream: fix lint and add started adding docs
rluvaton da244b9
stream: added an example in the streams docs
rluvaton 7e55077
stream: fix lint error
rluvaton 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
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
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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { | ||
| Readable, Transform, | ||
| } = require('stream'); | ||
| const assert = require('assert'); | ||
|
|
||
| { | ||
| // with async generator | ||
| const stream = Readable.from(['a', 'b', 'c', 'd']).compose(async function *(stream) { | ||
| let str = ''; | ||
| for await (const chunk of stream) { | ||
| str += chunk; | ||
|
|
||
| if (str.length === 2) { | ||
| yield str; | ||
| str = ''; | ||
| } | ||
| } | ||
| }); | ||
| const result = ['ab', 'cd']; | ||
| (async () => { | ||
| for await (const item of stream) { | ||
| assert.strictEqual(item, result.shift()); | ||
| } | ||
| })().then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| // With Transformer | ||
| const stream = Readable.from(['a', 'b', 'c', 'd']).compose(new Transform({ | ||
| objectMode: true, | ||
| transform: common.mustCall((chunk, encoding, callback) => { | ||
| callback(null, chunk); | ||
| }, 4) | ||
| })); | ||
| const result = ['a', 'b', 'c', 'd']; | ||
| (async () => { | ||
| for await (const item of stream) { | ||
| assert.strictEqual(item, result.shift()); | ||
| } | ||
| })().then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| // Throwing an error during `compose` (before waiting for data) | ||
| const stream = Readable.from([1, 2, 3, 4, 5]).compose(async function *(stream) { // eslint-disable-line require-yield | ||
|
|
||
| throw new Error('boom'); | ||
| }); | ||
|
|
||
| assert.rejects(async () => { | ||
| for await (const item of stream) { | ||
| assert.fail('should not reach here, got ' + item); | ||
| } | ||
| }, /boom/).then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| // Throwing an error during `compose` (when waiting for data) | ||
| const stream = Readable.from([1, 2, 3, 4, 5]).compose(async function *(stream) { | ||
| for await (const chunk of stream) { | ||
| if (chunk === 3) { | ||
| throw new Error('boom'); | ||
| } | ||
| yield chunk; | ||
| } | ||
| }); | ||
|
|
||
| assert.rejects( | ||
| stream.toArray(), | ||
| /boom/, | ||
| ).then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| // Throwing an error during `compose` (after finishing all readable data) | ||
| const stream = Readable.from([1, 2, 3, 4, 5]).compose(async function *(stream) { // eslint-disable-line require-yield | ||
|
|
||
| // eslint-disable-next-line no-unused-vars,no-empty | ||
| for await (const chunk of stream) { | ||
| } | ||
|
|
||
| throw new Error('boom'); | ||
| }); | ||
| assert.rejects( | ||
| stream.toArray(), | ||
| /boom/, | ||
| ).then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| // AbortSignal | ||
| const ac = new AbortController(); | ||
| const stream = Readable.from([1, 2, 3, 4, 5]) | ||
| .compose(async function *(source) { | ||
| // Should not reach here | ||
| for await (const chunk of source) { | ||
| yield chunk; | ||
| } | ||
| }, { signal: ac.signal }); | ||
|
|
||
| ac.abort(); | ||
|
|
||
| assert.rejects(async () => { | ||
| for await (const item of stream) { | ||
| assert.fail('should not reach here, got ' + item); | ||
| } | ||
| }, { | ||
| name: 'AbortError', | ||
| }).then(common.mustCall()); | ||
| } | ||
|
|
||
| { | ||
| assert.throws( | ||
| () => Readable.from(['a']).compose(Readable.from(['b'])), | ||
| { code: 'ERR_INVALID_ARG_VALUE' } | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| assert.throws( | ||
| () => Readable.from(['a']).compose(), | ||
| { code: 'ERR_INVALID_ARG_TYPE' } | ||
| ); | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.