Skip to content
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

stream: async iterator destroy compat #29176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,26 @@ const ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf({
},

return() {
// destroy(err, cb) is a private API.
// We can guarantee we have that here, because we control the
// Readable class this is attached to.
return new Promise((resolve, reject) => {
this[kStream].destroy(null, (err) => {
if (err) {
const stream = this[kStream];

// TODO(ronag): Remove this check once finished() handles
// already ended and/or destroyed streams.
const ended = stream.destroyed || stream.readableEnded ||
(stream._readableState && stream._readableState.endEmitted);
if (ended) {
resolve(createIterResult(undefined, true));
return;
}

finished(stream, (err) => {
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
ronag marked this conversation as resolved.
Show resolved Hide resolved
reject(err);
return;
} else {
resolve(createIterResult(undefined, true));
}
resolve(createIterResult(undefined, true));
});
stream.destroy();
});
},
}, AsyncIteratorPrototype);
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,46 @@ async function tests() {
}
}

{
// AsyncIterator return should end even when destroy
// does not implement the callback API.

const r = new Readable({
objectMode: true,
read() {
}
});

const originalDestroy = r.destroy;
r.destroy = (err) => {
originalDestroy.call(r, err);
};
const it = r[Symbol.asyncIterator]();
const p = it.return();
r.push(null);
p.then(common.mustCall());
}


{
// AsyncIterator return should not error with
// premature close.

const r = new Readable({
objectMode: true,
read() {
}
});

const originalDestroy = r.destroy;
r.destroy = (err) => {
originalDestroy.call(r, err);
};
const it = r[Symbol.asyncIterator]();
const p = it.return();
r.emit('close');
p.then(common.mustCall()).catch(common.mustNotCall());
}

// To avoid missing some tests if a promise does not resolve
tests().then(common.mustCall());