Skip to content

Commit

Permalink
stream: always delay construct callback by a nextTick
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mcollina authored and targos committed Mar 14, 2023
1 parent 8b1afe3 commit 4ad48d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ function constructNT(stream) {
}

try {
stream._construct(onConstruct);
stream._construct((err) => {
process.nextTick(onConstruct, err);
});
} catch (err) {
onConstruct(err);
process.nextTick(onConstruct, err);
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-stream2-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,27 @@ const { PassThrough, Transform } = require('stream');
assert.strictEqual(ended, true);
}));
}

{
const s = new Transform({
objectMode: true,
construct(callback) {
this.push('header from constructor');
callback();
},
transform: (row, encoding, callback) => {
callback(null, row);
},
});

const expected = [
'header from constructor',
'firstLine',
'secondLine',
];
s.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString(), expected.shift());
}, 3));
s.write('firstLine');
process.nextTick(() => s.write('secondLine'));
}

0 comments on commit 4ad48d9

Please sign in to comment.