1'use strict'; 2const common = require('../common'); 3const { Writable } = require('stream'); 4const assert = require('assert'); 5 6// Ensure callback is always invoked before 7// error is emitted. Regardless if error was 8// sync or async. 9 10{ 11 let callbackCalled = false; 12 // Sync Error 13 const writable = new Writable({ 14 write: common.mustCall((buf, enc, cb) => { 15 cb(new Error()); 16 }) 17 }); 18 writable.on('error', common.mustCall(() => { 19 assert.strictEqual(callbackCalled, true); 20 })); 21 writable.write('hi', common.mustCall(() => { 22 callbackCalled = true; 23 })); 24} 25 26{ 27 let callbackCalled = false; 28 // Async Error 29 const writable = new Writable({ 30 write: common.mustCall((buf, enc, cb) => { 31 process.nextTick(cb, new Error()); 32 }) 33 }); 34 writable.on('error', common.mustCall(() => { 35 assert.strictEqual(callbackCalled, true); 36 })); 37 writable.write('hi', common.mustCall(() => { 38 callbackCalled = true; 39 })); 40} 41 42{ 43 // Sync Error 44 const writable = new Writable({ 45 write: common.mustCall((buf, enc, cb) => { 46 cb(new Error()); 47 }) 48 }); 49 50 writable.on('error', common.mustCall()); 51 52 let cnt = 0; 53 // Ensure we don't live lock on sync error 54 while (writable.write('a')) 55 cnt++; 56 57 assert.strictEqual(cnt, 0); 58} 59