stream: do not error async iterators on destroy(null)

Fixes: https://github.com/nodejs/node/issues/23890

PR-URL: https://github.com/nodejs/node/pull/23901
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina 2018-10-26 12:38:08 +02:00 committed by Michaël Zasso
parent 398418dd26
commit b1e1fe4e07
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
2 changed files with 19 additions and 6 deletions

View File

@ -153,7 +153,7 @@ const createReadableStreamAsyncIterator = (stream) => {
});
finished(stream, (err) => {
if (err) {
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
const reject = iterator[kLastReject];
// reject if we are waiting for data in the Promise
// returned by next() and store the error

View File

@ -335,11 +335,8 @@ async function tests() {
readable.destroy();
try {
await readable[Symbol.asyncIterator]().next();
} catch (e) {
assert.strictEqual(e.code, 'ERR_STREAM_PREMATURE_CLOSE');
}
const { done } = await readable[Symbol.asyncIterator]().next();
assert.strictEqual(done, true);
})();
await (async function() {
@ -380,6 +377,22 @@ async function tests() {
for await (const b of r) {
}
})();
await (async () => {
console.log('destroy mid-stream does not error');
const r = new Readable({
objectMode: true,
read() {
this.push('asdf');
this.push('hehe');
}
});
// eslint-disable-next-line no-unused-vars
for await (const a of r) {
r.destroy(null);
}
})();
}
// to avoid missing some tests if a promise does not resolve