From 5c30fecbfa3b5b68a27bef038056eb2befe9f75a Mon Sep 17 00:00:00 2001 From: haze Date: Mon, 18 Mar 2024 05:33:04 -0400 Subject: [PATCH] stream: add `new` when constructing `ERR_MULTIPLE_CALLBACK` commit c71e548b65d912a976b65ea10ad6ee7d66b6e997 changed NodeError from a function to a class, and missed a spot where `ERR_MULTIPLE_CALLBACK` was being instantiated. This commit fixes that by adding the new keyword to that instance. Co-authored-by: Luigi Pinca PR-URL: https://github.com/nodejs/node/pull/52110 Reviewed-By: Robert Nagy Reviewed-By: Debadree Chatterjee Reviewed-By: Luigi Pinca Reviewed-By: Matteo Collina --- lib/internal/streams/writable.js | 2 +- ...ream-err-multiple-callback-construction.js | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-stream-err-multiple-callback-construction.js diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 0dbf56d7a69ca9..4de88c48eebdb9 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -876,7 +876,7 @@ function needFinish(state) { function onFinish(stream, state, err) { if ((state[kState] & kPrefinished) !== 0) { - errorOrDestroy(stream, err ?? ERR_MULTIPLE_CALLBACK()); + errorOrDestroy(stream, err ?? new ERR_MULTIPLE_CALLBACK()); return; } state.pendingcb--; diff --git a/test/parallel/test-stream-err-multiple-callback-construction.js b/test/parallel/test-stream-err-multiple-callback-construction.js new file mode 100644 index 00000000000000..829af3ffe7265f --- /dev/null +++ b/test/parallel/test-stream-err-multiple-callback-construction.js @@ -0,0 +1,25 @@ +'use strict'; +const common = require('../common'); +const stream = require('stream'); +const assert = require('assert'); + +class TestWritable extends stream.Writable { + _write(_chunk, _encoding, callback) { + callback(); + } + + _final(callback) { + process.nextTick(callback); + process.nextTick(callback); + } +} + +const writable = new TestWritable(); + +writable.on('finish', common.mustCall()); +writable.on('error', common.mustCall((error) => { + assert.strictEqual(error.message, 'Callback called multiple times'); +})); + +writable.write('some data'); +writable.end();