Skip to content
Closed
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
4 changes: 4 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const { getHighWaterMark } = require('internal/streams/state');
const {
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_MULTIPLE_CALLBACK,
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_DESTROYED,
ERR_STREAM_NULL_VALUES,
Expand Down Expand Up @@ -445,6 +446,9 @@ function onwrite(stream, er) {
var sync = state.sync;
var cb = state.writecb;

if (typeof cb !== 'function')
throw new ERR_MULTIPLE_CALLBACK();

onwriteStateUpdate(state);

if (er)
Expand Down
49 changes: 49 additions & 0 deletions test/parallel/test-stream-writable-write-cb-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common');
const { Writable } = require('stream');

{
// Sync + Sync
const writable = new Writable({
write: common.mustCall((buf, enc, cb) => {
cb();
common.expectsError(cb, {
code: 'ERR_MULTIPLE_CALLBACK',
type: Error
});
})
});
writable.write('hi');
}

{
// Sync + Async
const writable = new Writable({
write: common.mustCall((buf, enc, cb) => {
cb();
process.nextTick(() => {
common.expectsError(cb, {
code: 'ERR_MULTIPLE_CALLBACK',
type: Error
});
});
})
});
writable.write('hi');
}

{
// Async + Async
const writable = new Writable({
write: common.mustCall((buf, enc, cb) => {
process.nextTick(cb);
process.nextTick(() => {
common.expectsError(cb, {
code: 'ERR_MULTIPLE_CALLBACK',
type: Error
});
});
})
});
writable.write('hi');
}