Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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