From a48c9ca0dbe333b7560592cbd880d78b0f8e8906 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 7 Mar 2024 17:28:25 +0100 Subject: [PATCH] stream: do not defer construction by one microtick MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/51993 PR-URL: https://github.com/nodejs/node/pull/52005 Reviewed-By: Robert Nagy Reviewed-By: Michaƫl Zasso --- lib/internal/streams/destroy.js | 6 +--- .../test-fs-writestream-open-write.js | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-fs-writestream-open-write.js diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 28802cae5eff32..96e61491f08cfc 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -291,7 +291,7 @@ function constructNT(stream) { } else if (err) { errorOrDestroy(stream, err, true); } else { - process.nextTick(emitConstructNT, stream); + stream.emit(kConstruct); } } @@ -304,10 +304,6 @@ function constructNT(stream) { } } -function emitConstructNT(stream) { - stream.emit(kConstruct); -} - function isRequest(stream) { return stream?.setHeader && typeof stream.abort === 'function'; } diff --git a/test/parallel/test-fs-writestream-open-write.js b/test/parallel/test-fs-writestream-open-write.js new file mode 100644 index 00000000000000..af02d90ae6efd9 --- /dev/null +++ b/test/parallel/test-fs-writestream-open-write.js @@ -0,0 +1,28 @@ +'use strict'; + +const common = require('../common'); +const tmpdir = require('../common/tmpdir'); +const { strictEqual } = require('assert'); +const fs = require('fs'); + +// Regression test for https://github.com/nodejs/node/issues/51993 + +tmpdir.refresh(); + +const file = tmpdir.resolve('test-fs-writestream-open-write.txt'); + +const w = fs.createWriteStream(file); + +w.on('open', common.mustCall(() => { + w.write('hello'); + + process.nextTick(() => { + w.write('world'); + w.end(); + }); +})); + +w.on('close', common.mustCall(() => { + strictEqual(fs.readFileSync(file, 'utf8'), 'helloworld'); + fs.unlinkSync(file); +}));