1'use strict'; 2 3const common = require('../common'); 4const { Writable } = require('stream'); 5const assert = require('assert'); 6 7// basic 8{ 9 // Find it on Writable.prototype 10 assert(Object.hasOwn(Writable.prototype, 'writableFinished')); 11} 12 13// event 14{ 15 const writable = new Writable(); 16 17 writable._write = (chunk, encoding, cb) => { 18 // The state finished should start in false. 19 assert.strictEqual(writable.writableFinished, false); 20 cb(); 21 }; 22 23 writable.on('finish', common.mustCall(() => { 24 assert.strictEqual(writable.writableFinished, true); 25 })); 26 27 writable.end('testing finished state', common.mustCall(() => { 28 assert.strictEqual(writable.writableFinished, true); 29 })); 30} 31 32{ 33 // Emit finish asynchronously. 34 35 const w = new Writable({ 36 write(chunk, encoding, cb) { 37 cb(); 38 } 39 }); 40 41 w.end(); 42 w.on('finish', common.mustCall()); 43} 44 45{ 46 // Emit prefinish synchronously. 47 48 const w = new Writable({ 49 write(chunk, encoding, cb) { 50 cb(); 51 } 52 }); 53 54 let sync = true; 55 w.on('prefinish', common.mustCall(() => { 56 assert.strictEqual(sync, true); 57 })); 58 w.end(); 59 sync = false; 60} 61 62{ 63 // Emit prefinish synchronously w/ final. 64 65 const w = new Writable({ 66 write(chunk, encoding, cb) { 67 cb(); 68 }, 69 final(cb) { 70 cb(); 71 } 72 }); 73 74 let sync = true; 75 w.on('prefinish', common.mustCall(() => { 76 assert.strictEqual(sync, true); 77 })); 78 w.end(); 79 sync = false; 80} 81 82 83{ 84 // Call _final synchronously. 85 86 let sync = true; 87 const w = new Writable({ 88 write(chunk, encoding, cb) { 89 cb(); 90 }, 91 final: common.mustCall((cb) => { 92 assert.strictEqual(sync, true); 93 cb(); 94 }) 95 }); 96 97 w.end(); 98 sync = false; 99} 100