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 <hello@matteocollina.com>
Fixes: #46765
PR-URL: #46818
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
mcollina authored and danielleadams committed Apr 11, 2023
1 parent 0e78fd5 commit 654b747
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 654b747

Please sign in to comment.