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
  • Loading branch information
mcollina committed Feb 24, 2023
1 parent b6c56cb commit db2554c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,14 @@ function constructNT(stream) {
}
}

function jump (err) {
process.nextTick(onConstruct, err);
}

try {
stream._construct(onConstruct);
stream._construct(jump);
} catch (err) {
onConstruct(err);
jump(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 db2554c

Please sign in to comment.