Skip to content

Commit

Permalink
stream: use null for the error argument
Browse files Browse the repository at this point in the history
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 <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
lpinca committed Aug 23, 2022
1 parent 8e87299 commit 2b32985
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/internal/streams/writable.js
Expand Up @@ -497,7 +497,7 @@ function afterWrite(stream, state, count, cb) {

while (count-- > 0) {
state.pendingcb--;
cb();
cb(null);
}

if (state.destroyed) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-writable-end-cb-error.js
Expand Up @@ -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) => {
Expand Down
11 changes: 8 additions & 3 deletions test/parallel/test-stream2-writable.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -225,15 +226,19 @@ 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');

{
// 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);
}));
}

{
Expand Down

0 comments on commit 2b32985

Please sign in to comment.