stream: give error message if write()
cb called twice
Otherwise, this condition would result in an error that just reads `cb is not a function`, and which additionally could have lost stack trace context through a `process.nextTick()` call. PR-URL: https://github.com/nodejs/node/pull/19510 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
cdfe47b323
commit
d111d7b91c
@ -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,
|
||||
@ -449,6 +450,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)
|
||||
|
49
test/parallel/test-stream-writable-write-cb-twice.js
Normal file
49
test/parallel/test-stream-writable-write-cb-twice.js
Normal file
@ -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');
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user