1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const stream = require('stream'); 6 7{ 8 // Invoke end callback on failure. 9 const writable = new stream.Writable(); 10 11 const _err = new Error('kaboom'); 12 writable._write = (chunk, encoding, cb) => { 13 process.nextTick(cb, _err); 14 }; 15 16 writable.on('error', common.mustCall((err) => { 17 assert.strictEqual(err, _err); 18 })); 19 writable.write('asd'); 20 writable.end(common.mustCall((err) => { 21 assert.strictEqual(err, _err); 22 })); 23 writable.end(common.mustCall((err) => { 24 assert.strictEqual(err, _err); 25 })); 26} 27 28{ 29 // Don't invoke end callback twice 30 const writable = new stream.Writable(); 31 32 writable._write = (chunk, encoding, cb) => { 33 process.nextTick(cb); 34 }; 35 36 let called = false; 37 writable.end('asd', common.mustCall((err) => { 38 called = true; 39 assert.strictEqual(err, undefined); 40 })); 41 42 writable.on('error', common.mustCall((err) => { 43 assert.strictEqual(err.message, 'kaboom'); 44 })); 45 writable.on('finish', common.mustCall(() => { 46 assert.strictEqual(called, true); 47 writable.emit('error', new Error('kaboom')); 48 })); 49} 50 51{ 52 const w = new stream.Writable({ 53 write(chunk, encoding, callback) { 54 setImmediate(callback); 55 }, 56 finish(callback) { 57 setImmediate(callback); 58 } 59 }); 60 w.end('testing ended state', common.mustCall((err) => { 61 assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END'); 62 })); 63 assert.strictEqual(w.destroyed, false); 64 assert.strictEqual(w.writableEnded, true); 65 w.end(common.mustCall((err) => { 66 assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END'); 67 })); 68 assert.strictEqual(w.destroyed, false); 69 assert.strictEqual(w.writableEnded, true); 70 w.end('end', common.mustCall((err) => { 71 assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END'); 72 })); 73 assert.strictEqual(w.destroyed, true); 74 w.on('error', common.mustCall((err) => { 75 assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END'); 76 })); 77 w.on('finish', common.mustNotCall()); 78} 79