Skip to content

Commit 190ddce

Browse files
ronagdanielleadams
authored andcommitted
stream: only use legacy close listeners if not willEmitClose
Some streams that willEmitClose unecessarily fallback to legacy events. PR-URL: #36649 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 651e7d2 commit 190ddce

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/internal/streams/end-of-stream.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ function eos(stream, options, callback) {
129129

130130
if (isRequest(stream)) {
131131
stream.on('complete', onfinish);
132-
stream.on('abort', onclose);
132+
if (!willEmitClose) {
133+
stream.on('abort', onclose);
134+
}
133135
if (stream.req) onrequest();
134136
else stream.on('request', onrequest);
135137
} else if (writable && !wState) { // legacy streams
@@ -138,7 +140,7 @@ function eos(stream, options, callback) {
138140
}
139141

140142
// Not all streams will emit 'close' after 'aborted'.
141-
if (typeof stream.aborted === 'boolean') {
143+
if (!willEmitClose && typeof stream.aborted === 'boolean') {
142144
stream.on('aborted', onclose);
143145
}
144146

test/parallel/test-stream-finished.js

+23
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,26 @@ testClosed((opts) => new Writable({ write() {}, ...opts }));
492492
.on('response', common.mustCall());
493493
});
494494
}
495+
496+
497+
{
498+
const w = new Writable({
499+
write(chunk, encoding, callback) {
500+
process.nextTick(callback);
501+
}
502+
});
503+
w.aborted = false;
504+
w.end();
505+
let closed = false;
506+
w.on('finish', () => {
507+
assert.strictEqual(closed, false);
508+
w.emit('aborted');
509+
});
510+
w.on('close', common.mustCall(() => {
511+
closed = true;
512+
}));
513+
514+
finished(w, common.mustCall(() => {
515+
assert.strictEqual(closed, true);
516+
}));
517+
}

0 commit comments

Comments
 (0)