From 2b32985c626b57d7ea3e68d6ce088ea3183e3a58 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 23 Aug 2022 09:51:53 +0200 Subject: [PATCH] stream: use null for the error argument When no error occurs, use `null` instead of `undefined` for the `error` argument of the `writable.write()` and `writable.end()` callbacks. Fixes: https://github.com/nodejs/node/issues/44290 PR-URL: https://github.com/nodejs/node/pull/44312 Reviewed-By: Robert Nagy Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: Minwoo Jung --- lib/internal/streams/writable.js | 8 +++++--- test/parallel/test-stream-writable-end-cb-error.js | 2 +- test/parallel/test-stream2-writable.js | 11 ++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 1054631ddfee1a..c4d95df9ac7153 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -497,7 +497,7 @@ function afterWrite(stream, state, count, cb) { while (count-- > 0) { state.pendingcb--; - cb(); + cb(null); } if (state.destroyed) { @@ -640,8 +640,10 @@ Writable.prototype.end = function(chunk, encoding, cb) { } if (typeof cb === 'function') { - if (err || state.finished) { + if (err) { process.nextTick(cb, err); + } else if (state.finished) { + process.nextTick(cb, null); } else { state[kOnFinished].push(cb); } @@ -742,7 +744,7 @@ function finish(stream, state) { const onfinishCallbacks = state[kOnFinished].splice(0); for (let i = 0; i < onfinishCallbacks.length; i++) { - onfinishCallbacks[i](); + onfinishCallbacks[i](null); } stream.emit('finish'); diff --git a/test/parallel/test-stream-writable-end-cb-error.js b/test/parallel/test-stream-writable-end-cb-error.js index f871c50dbe972c..f140d939bc024c 100644 --- a/test/parallel/test-stream-writable-end-cb-error.js +++ b/test/parallel/test-stream-writable-end-cb-error.js @@ -36,7 +36,7 @@ const stream = require('stream'); let called = false; writable.end('asd', common.mustCall((err) => { called = true; - assert.strictEqual(err, undefined); + assert.strictEqual(err, null); })); writable.on('error', common.mustCall((err) => { diff --git a/test/parallel/test-stream2-writable.js b/test/parallel/test-stream2-writable.js index 3d16fb62df3313..6d233ae6b68107 100644 --- a/test/parallel/test-stream2-writable.js +++ b/test/parallel/test-stream2-writable.js @@ -194,7 +194,8 @@ for (let i = 0; i < chunks.length; i++) { { // Verify write callbacks const callbacks = chunks.map(function(chunk, i) { - return [i, function() { + return [i, function(err) { + assert.strictEqual(err, null); callbacks._called[i] = chunk; }]; }).reduce(function(set, x) { @@ -225,7 +226,9 @@ for (let i = 0; i < chunks.length; i++) { { // Verify end() callback const tw = new TestWriter(); - tw.end(common.mustCall()); + tw.end(common.mustCall(function(err) { + assert.strictEqual(err, null); + })); } const helloWorldBuffer = Buffer.from('hello world'); @@ -233,7 +236,9 @@ const helloWorldBuffer = Buffer.from('hello world'); { // Verify end() callback with chunk const tw = new TestWriter(); - tw.end(helloWorldBuffer, common.mustCall()); + tw.end(helloWorldBuffer, common.mustCall(function(err) { + assert.strictEqual(err, null); + })); } {