Skip to content

Commit 654b747

Browse files
mcollinadanielleadams
authored andcommitted
stream: always delay construct callback by a nextTick
Signed-off-by: Matteo Collina <[email protected]> Fixes: #46765 PR-URL: #46818 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 0e78fd5 commit 654b747

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/internal/streams/destroy.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,11 @@ function constructNT(stream) {
272272
}
273273

274274
try {
275-
stream._construct(onConstruct);
275+
stream._construct((err) => {
276+
process.nextTick(onConstruct, err);
277+
});
276278
} catch (err) {
277-
onConstruct(err);
279+
process.nextTick(onConstruct, err);
278280
}
279281
}
280282

test/parallel/test-stream2-transform.js

+24
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,27 @@ const { PassThrough, Transform } = require('stream');
468468
assert.strictEqual(ended, true);
469469
}));
470470
}
471+
472+
{
473+
const s = new Transform({
474+
objectMode: true,
475+
construct(callback) {
476+
this.push('header from constructor');
477+
callback();
478+
},
479+
transform: (row, encoding, callback) => {
480+
callback(null, row);
481+
},
482+
});
483+
484+
const expected = [
485+
'header from constructor',
486+
'firstLine',
487+
'secondLine',
488+
];
489+
s.on('data', common.mustCall((data) => {
490+
assert.strictEqual(data.toString(), expected.shift());
491+
}, 3));
492+
s.write('firstLine');
493+
process.nextTick(() => s.write('secondLine'));
494+
}

0 commit comments

Comments
 (0)