-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Multiple fixes for async iterators #23901
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -127,7 +127,10 @@ const createReadableStreamAsyncIterator = (stream) => { | |||||||||||||||||||||||||||||||||
| [kLastResolve]: { value: null, writable: true }, | ||||||||||||||||||||||||||||||||||
| [kLastReject]: { value: null, writable: true }, | ||||||||||||||||||||||||||||||||||
| [kError]: { value: null, writable: true }, | ||||||||||||||||||||||||||||||||||
| [kEnded]: { value: false, writable: true }, | ||||||||||||||||||||||||||||||||||
| [kEnded]: { | ||||||||||||||||||||||||||||||||||
| value: stream._readableState.endEmitted, | ||||||||||||||||||||||||||||||||||
| writable: true | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| [kLastPromise]: { value: null, writable: true }, | ||||||||||||||||||||||||||||||||||
| // the function passed to new Promise | ||||||||||||||||||||||||||||||||||
| // is cached so we avoid allocating a new | ||||||||||||||||||||||||||||||||||
|
|
@@ -150,7 +153,7 @@ const createReadableStreamAsyncIterator = (stream) => { | |||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| finished(stream, (err) => { | ||||||||||||||||||||||||||||||||||
| if (err) { | ||||||||||||||||||||||||||||||||||
| if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| await (async function() { | |
| console.log('.next() on destroyed stream'); | |
| const readable = new Readable({ | |
| read() { | |
| // no-op | |
| } | |
| }); | |
| readable.destroy(); | |
| try { | |
| await readable[Symbol.asyncIterator]().next(); | |
| } catch (e) { | |
| assert.strictEqual(e.code, 'ERR_STREAM_PREMATURE_CLOSE'); | |
| } | |
| })(); |
We see precedent for invalidated iterator still operating gracefully in ECMA-262 iterators:
const array = [0, 1, 2, 3];
const iterated = [];
for (const a of array) {
iterated.push(a);
if (a === 1) {
array.splice(0);
}
// loop ends immediately.
}
// iterated is [0, 1]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.
First, end-of-stream is one of the most downloaded utilities on npm, and keeping the API and behavior similar has been a goal. Second, from a consumer perspective a stream that was ended or destroyed is significantly different. That could have easily been a flag as an argument. I think it errors because in the context of pipeline/pump, in fact it is an error: the pipeline did not complete correctly but was interrupted (as an example, gzipping a file).
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.
I’ll check again that test asap, it does not sound correct with this change.
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.
Okay, as long as async iteration isn't affected it works for me.
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.
@TimothyGu the test was botched anyway, and the catch clause was never executed in this case. I've fixed it.
Uh oh!
There was an error while loading. Please reload this page.