From e2fc14d3c72f68eb50260af4bee3b18fce8f294b Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 24 Feb 2023 16:48:07 +0100 Subject: [PATCH 1/3] stream: always delay construct callback by a nextTick Signed-off-by: Matteo Collina Fixes: https://github.com/nodejs/node/issues/46765 --- lib/internal/streams/destroy.js | 8 ++++++-- test/parallel/test-stream2-transform.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 76433594662d91..3c2edf5a30a0c9 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -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); } } diff --git a/test/parallel/test-stream2-transform.js b/test/parallel/test-stream2-transform.js index 849cfb3538b306..6f57384eb02443 100644 --- a/test/parallel/test-stream2-transform.js +++ b/test/parallel/test-stream2-transform.js @@ -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')); +} From e65f74351cb638c950bd7ee5c2f26516167d997d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 24 Feb 2023 17:17:01 +0100 Subject: [PATCH 2/3] fixup Signed-off-by: Matteo Collina --- lib/internal/streams/destroy.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 3c2edf5a30a0c9..49cf941ab36ec8 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -271,14 +271,12 @@ function constructNT(stream) { } } - function jump(err) { - process.nextTick(onConstruct, err); - } - try { - stream._construct(jump); + stream._construct((err) => { + process.nextTick(onConstruct, err); + }) } catch (err) { - jump(err); + process.nextTick(onConstruct, err); } } From fb1fba4ec19620f5741479241025b02d5709a20d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 24 Feb 2023 19:07:35 +0100 Subject: [PATCH 3/3] Update lib/internal/streams/destroy.js Co-authored-by: Antoine du Hamel --- lib/internal/streams/destroy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 49cf941ab36ec8..358422e12a33e0 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -274,7 +274,7 @@ function constructNT(stream) { try { stream._construct((err) => { process.nextTick(onConstruct, err); - }) + }); } catch (err) { process.nextTick(onConstruct, err); }