-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
stream: add map method to Readable: #40815
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f5dfa00
stream: add map method to Readable
benjamingr eae40bc
test code review
benjamingr 7678628
fixup
ronag 55eb7ff
fixup: explicit listener
ronag 66fbd9f
fixup
ronag 416e462
fixup
ronag 5a22655
Update lib/internal/streams/operators.js
ronag 2e7c469
fixup
ronag 751b429
fixup
ronag d91ddda
fixup
ronag 0679c79
fixup
ronag a8e9dd6
fixup
ronag 2db5501
fixup
ronag 7045b68
Apply suggestions from code review
ronag c61f6ba
fixup
ronag 7e617c2
Update lib/stream.js
ronag 47d9891
fixup
ronag 2d7cd8c
fixup
ronag 686d9e6
ifxuo
ronag 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use strict'; | ||
|
||
const { AbortController } = require('internal/abort_controller'); | ||
const { | ||
codes: { | ||
ERR_INVALID_ARG_TYPE, | ||
}, | ||
AbortError, | ||
} = require('internal/errors'); | ||
const { validateInteger } = require('internal/validators'); | ||
|
||
const { | ||
MathFloor, | ||
Promise, | ||
PromiseReject, | ||
PromisePrototypeCatch, | ||
Symbol, | ||
} = primordials; | ||
|
||
const kEmpty = Symbol('kEmpty'); | ||
const kEof = Symbol('kEof'); | ||
|
||
async function * map(fn, options) { | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (typeof fn !== 'function') { | ||
throw new ERR_INVALID_ARG_TYPE( | ||
'fn', ['Function', 'AsyncFunction'], this); | ||
} | ||
|
||
if (options != null && typeof options !== 'object') { | ||
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.
|
||
throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
} | ||
|
||
let concurrency = 1; | ||
if (options?.concurrency != null) { | ||
concurrency = MathFloor(options.concurrency); | ||
} | ||
|
||
validateInteger(concurrency, 'concurrency', 1); | ||
|
||
const ac = new AbortController(); | ||
const stream = this; | ||
const queue = []; | ||
const signal = ac.signal; | ||
const signalOpt = { signal }; | ||
|
||
const abort = () => ac.abort(); | ||
options?.signal?.addEventListener('abort', abort); | ||
|
||
let next; | ||
let resume; | ||
let done = false; | ||
|
||
function onDone() { | ||
done = true; | ||
} | ||
|
||
async function pump() { | ||
try { | ||
for await (let val of stream) { | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (done) { | ||
return; | ||
} | ||
|
||
if (signal.aborted) { | ||
throw new AbortError(); | ||
} | ||
|
||
try { | ||
val = fn(val, signalOpt); | ||
} catch (err) { | ||
val = PromiseReject(err); | ||
} | ||
|
||
if (val === kEmpty) { | ||
continue; | ||
} | ||
|
||
if (typeof val?.catch === 'function') { | ||
val.catch(onDone); | ||
} | ||
|
||
queue.push(val); | ||
if (next) { | ||
next(); | ||
next = null; | ||
} | ||
|
||
if (!done && queue.length && queue.length >= concurrency) { | ||
await new Promise((resolve) => { | ||
resume = resolve; | ||
}); | ||
} | ||
} | ||
queue.push(kEof); | ||
} catch (err) { | ||
const val = PromiseReject(err); | ||
PromisePrototypeCatch(val, onDone); | ||
queue.push(val); | ||
} finally { | ||
done = true; | ||
if (next) { | ||
next(); | ||
next = null; | ||
} | ||
options?.signal?.removeEventListener('abort', abort); | ||
} | ||
} | ||
|
||
pump(); | ||
|
||
try { | ||
while (true) { | ||
Linkgoron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (queue.length > 0) { | ||
const val = await queue[0]; | ||
|
||
if (val === kEof) { | ||
return; | ||
} | ||
|
||
if (signal.aborted) { | ||
throw new AbortError(); | ||
} | ||
|
||
if (val !== kEmpty) { | ||
yield val; | ||
} | ||
|
||
queue.shift(); | ||
if (resume) { | ||
resume(); | ||
resume = null; | ||
} | ||
} | ||
|
||
await new Promise((resolve) => { | ||
next = resolve; | ||
}); | ||
} | ||
} finally { | ||
ac.abort(); | ||
|
||
done = true; | ||
if (resume) { | ||
resume(); | ||
resume = null; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
map, | ||
}; |
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,108 @@ | ||
'use strict'; | ||
benjamingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const common = require('../common'); | ||
const { | ||
Readable, | ||
} = require('stream'); | ||
const assert = require('assert'); | ||
const { setTimeout } = require('timers/promises'); | ||
|
||
{ | ||
// Map works on synchronous streams with a synchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x + x); | ||
const result = [2, 4, 6, 8, 10]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Map works on synchronous streams with an asynchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => { | ||
await Promise.resolve(); | ||
return x + x; | ||
}); | ||
const result = [2, 4, 6, 8, 10]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Map works on asynchronous streams with a asynchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => { | ||
return x + x; | ||
}).map((x) => x + x); | ||
const result = [4, 8, 12, 16, 20]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Concurrency + AbortSignal | ||
const ac = new AbortController(); | ||
let calls = 0; | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (_, { signal }) => { | ||
calls++; | ||
await setTimeout(100, { signal }); | ||
}, { signal: ac.signal, concurrency: 2 }); | ||
// pump | ||
assert.rejects(async () => { | ||
for await (const item of stream) { | ||
// nope | ||
console.log(item); | ||
} | ||
}, { | ||
name: 'AbortError', | ||
}).then(common.mustCall()); | ||
|
||
setImmediate(() => { | ||
ac.abort(); | ||
assert.strictEqual(calls, 2); | ||
}); | ||
} | ||
|
||
{ | ||
// Concurrency result order | ||
const stream = Readable.from([1, 2]).map(async (item, { signal }) => { | ||
await setTimeout(10 - item, { signal }); | ||
return item; | ||
}, { concurrency: 2 }); | ||
|
||
(async () => { | ||
const expected = [1, 2]; | ||
for await (const item of stream) { | ||
assert.strictEqual(item, expected.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Error cases | ||
assert.rejects(async () => { | ||
// eslint-disable-next-line no-unused-vars | ||
for await (const unused of Readable.from([1]).map(1)); | ||
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
assert.rejects(async () => { | ||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of Readable.from([1]).map((x) => x, { | ||
concurrency: 'Foo' | ||
})); | ||
}, /ERR_OUT_OF_RANGE/).then(common.mustCall()); | ||
assert.rejects(async () => { | ||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of Readable.from([1]).map((x) => x, 1)); | ||
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
} | ||
{ | ||
// Test result is a Readable | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x); | ||
assert.strictEqual(stream.readable, true); | ||
} |
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.