-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
stream: improve Readable.from error handling #37158
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 |
---|---|---|
|
@@ -43,9 +43,6 @@ function from(Readable, iterable, opts) { | |
// being called before last iteration completion. | ||
let reading = false; | ||
|
||
// Flag for when iterator needs to be explicitly closed. | ||
let needToClose = false; | ||
|
||
readable._read = function() { | ||
if (!reading) { | ||
reading = true; | ||
|
@@ -54,19 +51,23 @@ function from(Readable, iterable, opts) { | |
}; | ||
|
||
readable._destroy = function(error, cb) { | ||
if (needToClose) { | ||
needToClose = false; | ||
PromisePrototypeThen( | ||
close(), | ||
() => process.nextTick(cb, error), | ||
(e) => process.nextTick(cb, error || e), | ||
); | ||
} else { | ||
cb(error); | ||
} | ||
PromisePrototypeThen( | ||
close(error), | ||
() => process.nextTick(cb, error), // nextTick is here in case cb throws | ||
(e) => process.nextTick(cb, e || error), | ||
); | ||
}; | ||
|
||
async function close() { | ||
async function close(error) { | ||
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. This is tricky and I want feedback on the semantics, basically:
|
||
const hadError = (error !== undefined) && (error !== null); | ||
const hasThrow = typeof iterator.throw === 'function'; | ||
if (hadError && hasThrow) { | ||
const { value, done } = await iterator.throw(error); | ||
await value; | ||
if (done) { | ||
return; | ||
} | ||
} | ||
if (typeof iterator.return === 'function') { | ||
const { value } = await iterator.return(); | ||
await value; | ||
|
@@ -75,13 +76,9 @@ function from(Readable, iterable, opts) { | |
|
||
async function next() { | ||
try { | ||
needToClose = false; | ||
const { value, done } = await iterator.next(); | ||
needToClose = !done; | ||
if (done) { | ||
readable.push(null); | ||
} else if (readable.destroyed) { | ||
await close(); | ||
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. This check is no longer necessary without needToClose since |
||
} else { | ||
const res = await value; | ||
if (res === null) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.