Skip to content

Commit 2b32985

Browse files
authored
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: #44290 PR-URL: #44312 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 8e87299 commit 2b32985

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lib/internal/streams/writable.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ function afterWrite(stream, state, count, cb) {
497497

498498
while (count-- > 0) {
499499
state.pendingcb--;
500-
cb();
500+
cb(null);
501501
}
502502

503503
if (state.destroyed) {
@@ -640,8 +640,10 @@ Writable.prototype.end = function(chunk, encoding, cb) {
640640
}
641641

642642
if (typeof cb === 'function') {
643-
if (err || state.finished) {
643+
if (err) {
644644
process.nextTick(cb, err);
645+
} else if (state.finished) {
646+
process.nextTick(cb, null);
645647
} else {
646648
state[kOnFinished].push(cb);
647649
}
@@ -742,7 +744,7 @@ function finish(stream, state) {
742744

743745
const onfinishCallbacks = state[kOnFinished].splice(0);
744746
for (let i = 0; i < onfinishCallbacks.length; i++) {
745-
onfinishCallbacks[i]();
747+
onfinishCallbacks[i](null);
746748
}
747749

748750
stream.emit('finish');

test/parallel/test-stream-writable-end-cb-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const stream = require('stream');
3636
let called = false;
3737
writable.end('asd', common.mustCall((err) => {
3838
called = true;
39-
assert.strictEqual(err, undefined);
39+
assert.strictEqual(err, null);
4040
}));
4141

4242
writable.on('error', common.mustCall((err) => {

test/parallel/test-stream2-writable.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ for (let i = 0; i < chunks.length; i++) {
194194
{
195195
// Verify write callbacks
196196
const callbacks = chunks.map(function(chunk, i) {
197-
return [i, function() {
197+
return [i, function(err) {
198+
assert.strictEqual(err, null);
198199
callbacks._called[i] = chunk;
199200
}];
200201
}).reduce(function(set, x) {
@@ -225,15 +226,19 @@ for (let i = 0; i < chunks.length; i++) {
225226
{
226227
// Verify end() callback
227228
const tw = new TestWriter();
228-
tw.end(common.mustCall());
229+
tw.end(common.mustCall(function(err) {
230+
assert.strictEqual(err, null);
231+
}));
229232
}
230233

231234
const helloWorldBuffer = Buffer.from('hello world');
232235

233236
{
234237
// Verify end() callback with chunk
235238
const tw = new TestWriter();
236-
tw.end(helloWorldBuffer, common.mustCall());
239+
tw.end(helloWorldBuffer, common.mustCall(function(err) {
240+
assert.strictEqual(err, null);
241+
}));
237242
}
238243

239244
{

0 commit comments

Comments
 (0)