Skip to content

Commit

Permalink
stream: normalize async iterator stream destroy
Browse files Browse the repository at this point in the history
PR-URL: #31316
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
ronag authored and codebytere committed Feb 17, 2020
1 parent 20d0a0e commit 5a95fa4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ const kStream = Symbol('stream');

let Readable;

function destroy(stream, err) {
// request.destroy just do .end - .abort is what we want
if (typeof stream.abort === 'function') return stream.abort();
if (stream.req &&
typeof stream.req.abort === 'function') return stream.req.abort();
if (typeof stream.destroy === 'function') return stream.destroy(err);
if (typeof stream.close === 'function') return stream.close();
}

function createIterResult(value, done) {
return { value, done };
}
Expand Down Expand Up @@ -141,7 +150,7 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
resolve(createIterResult(undefined, true));
}
});
stream.destroy();
destroy(stream);
});
},
}, AsyncIteratorPrototype);
Expand All @@ -156,11 +165,7 @@ const createReadableStreamAsyncIterator = (stream) => {

const src = stream;
stream = new Readable({ objectMode: true }).wrap(src);
finished(stream, (err) => {
if (typeof src.destroy === 'function') {
src.destroy(err);
}
});
finished(stream, (err) => destroy(src, err));
}

const iterator = ObjectCreate(ReadableStreamAsyncIteratorPrototype, {
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ async function tests() {
}));
}

{
// Non standard stream cleanup

const readable = new Readable({ autoDestroy: false, read() {} });
readable.push('asd');
readable.push('asd');
readable.destroy = null;
readable.close = common.mustCall(() => {
readable.emit('close');
});

await (async () => {
for await (const d of readable) {
d;
return;
}
})();
}

{
const readable = new Readable({ objectMode: true, read() {} });
readable.push(0);
Expand Down

0 comments on commit 5a95fa4

Please sign in to comment.