stream: only use legacy close listeners if not willEmitClose

Some streams that willEmitClose unecessarily fallback to legacy
events.

PR-URL: https://github.com/nodejs/node/pull/36649
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Robert Nagy
2020-12-27 21:48:40 +01:00
committed by Node.js GitHub Bot
parent a5b43e9404
commit faee7399b2
2 changed files with 27 additions and 2 deletions

View File

@@ -129,7 +129,9 @@ function eos(stream, options, callback) {
if (isRequest(stream)) {
stream.on('complete', onfinish);
stream.on('abort', onclose);
if (!willEmitClose) {
stream.on('abort', onclose);
}
if (stream.req) onrequest();
else stream.on('request', onrequest);
} else if (writable && !wState) { // legacy streams
@@ -138,7 +140,7 @@ function eos(stream, options, callback) {
}
// Not all streams will emit 'close' after 'aborted'.
if (typeof stream.aborted === 'boolean') {
if (!willEmitClose && typeof stream.aborted === 'boolean') {
stream.on('aborted', onclose);
}

View File

@@ -492,3 +492,26 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
.on('response', common.mustCall());
});
}
{
const w = new Writable({
write(chunk, encoding, callback) {
process.nextTick(callback);
}
});
w.aborted = false;
w.end();
let closed = false;
w.on('finish', () => {
assert.strictEqual(closed, false);
w.emit('aborted');
});
w.on('close', common.mustCall(() => {
closed = true;
}));
finished(w, common.mustCall(() => {
assert.strictEqual(closed, true);
}));
}