1'use strict'; 2const common = require('../common'); 3 4// Tests for the regression in _stream_writable discussed in 5// https://github.com/nodejs/node/pull/31756 6 7// Specifically, when a write callback is invoked synchronously 8// with an error, and autoDestroy is not being used, the error 9// should still be emitted on nextTick. 10 11const { Writable } = require('stream'); 12 13class MyStream extends Writable { 14 #cb = undefined; 15 16 constructor() { 17 super({ autoDestroy: false }); 18 } 19 20 _write(_, __, cb) { 21 this.#cb = cb; 22 } 23 24 close() { 25 // Synchronously invoke the callback with an error. 26 this.#cb(new Error('foo')); 27 } 28} 29 30const stream = new MyStream(); 31 32const mustError = common.mustCall(2); 33 34stream.write('test', () => {}); 35 36// Both error callbacks should be invoked. 37 38stream.on('error', mustError); 39 40stream.close(); 41 42// Without the fix in #31756, the error handler 43// added after the call to close will not be invoked. 44stream.on('error', mustError); 45