-
-
Notifications
You must be signed in to change notification settings - Fork 32k
stream: fix handling generators in Readable.from #32844
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
11 commits
Select commit
Hold shift + click to select a range
2fa5848
stream: close iterator in Readable.from
vadzim 980a7c8
fixup: explicit `yielded` event in closeStreamWhileNextIsPending
vadzim b772927
fixup: test fot iterator closing after null has been yielded
vadzim 57bb126
fixup: escape unhandled rejected promise
vadzim 8a2974f
fixup: test for closing iterator on yielded null
vadzim 8528ad4
fixup: fix variable name
vadzim e821b98
fixup: run callback with process.nextTick
vadzim e9becca
remove unnesessary closure
vadzim 509be2c
fixup: do not doublecheck needToClose flag
vadzim 7aac752
fixup: make _destroy not async
vadzim bffbf58
fixup: trigger build
vadzim 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,198 @@ | ||
'use strict'; | ||
|
||
const { mustCall, mustNotCall } = require('../common'); | ||
const { Readable } = require('stream'); | ||
const { strictEqual } = require('assert'); | ||
|
||
async function asyncSupport() { | ||
const finallyMustCall = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
async function* infiniteGenerate() { | ||
try { | ||
while (true) yield 'a'; | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncSupport() { | ||
const finallyMustCall = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
function* infiniteGenerate() { | ||
try { | ||
while (true) yield 'a'; | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncPromiseSupport() { | ||
const returnMustBeAwaited = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
function* infiniteGenerate() { | ||
try { | ||
while (true) yield Promise.resolve('a'); | ||
} finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return { then(cb) { | ||
returnMustBeAwaited(); | ||
cb(); | ||
} }; | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncRejectedSupport() { | ||
const returnMustBeAwaited = mustCall(); | ||
const bodyMustNotCall = mustNotCall(); | ||
const catchMustCall = mustCall(); | ||
const secondNextMustNotCall = mustNotCall(); | ||
|
||
function* generate() { | ||
try { | ||
yield Promise.reject('a'); | ||
secondNextMustNotCall(); | ||
} finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return { then(cb) { | ||
returnMustBeAwaited(); | ||
cb(); | ||
} }; | ||
} | ||
} | ||
|
||
const stream = Readable.from(generate()); | ||
|
||
try { | ||
for await (const chunk of stream) { | ||
bodyMustNotCall(chunk); | ||
} | ||
} catch { | ||
catchMustCall(); | ||
} | ||
} | ||
|
||
async function noReturnAfterThrow() { | ||
const returnMustNotCall = mustNotCall(); | ||
const bodyMustNotCall = mustNotCall(); | ||
const catchMustCall = mustCall(); | ||
const nextMustCall = mustCall(); | ||
|
||
const stream = Readable.from({ | ||
[Symbol.asyncIterator]() { return this; }, | ||
async next() { | ||
nextMustCall(); | ||
throw new Error('a'); | ||
}, | ||
async return() { | ||
returnMustNotCall(); | ||
return { done: true }; | ||
}, | ||
}); | ||
|
||
try { | ||
for await (const chunk of stream) { | ||
bodyMustNotCall(chunk); | ||
} | ||
} catch { | ||
catchMustCall(); | ||
} | ||
} | ||
|
||
async function closeStreamWhileNextIsPending() { | ||
const finallyMustCall = mustCall(); | ||
const dataMustCall = mustCall(); | ||
|
||
let resolveDestroy; | ||
const destroyed = | ||
new Promise((resolve) => { resolveDestroy = mustCall(resolve); }); | ||
let resolveYielded; | ||
const yielded = | ||
new Promise((resolve) => { resolveYielded = mustCall(resolve); }); | ||
|
||
async function* infiniteGenerate() { | ||
try { | ||
while (true) { | ||
yield 'a'; | ||
resolveYielded(); | ||
await destroyed; | ||
} | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
stream.on('data', (data) => { | ||
dataMustCall(); | ||
strictEqual(data, 'a'); | ||
}); | ||
|
||
yielded.then(() => { | ||
stream.destroy(); | ||
resolveDestroy(); | ||
}); | ||
} | ||
|
||
async function closeAfterNullYielded() { | ||
const finallyMustCall = mustCall(); | ||
const dataMustCall = mustCall(3); | ||
|
||
function* infiniteGenerate() { | ||
try { | ||
yield 'a'; | ||
yield 'a'; | ||
yield 'a'; | ||
while (true) yield null; | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
stream.on('data', (chunk) => { | ||
dataMustCall(); | ||
strictEqual(chunk, 'a'); | ||
}); | ||
} | ||
|
||
Promise.all([ | ||
asyncSupport(), | ||
syncSupport(), | ||
syncPromiseSupport(), | ||
syncRejectedSupport(), | ||
noReturnAfterThrow(), | ||
closeStreamWhileNextIsPending(), | ||
closeAfterNullYielded(), | ||
]).then(mustCall()); |
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.