11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { Writable } = require('stream'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// basic 81cb0ef41Sopenharmony_ci{ 91cb0ef41Sopenharmony_ci // Find it on Writable.prototype 101cb0ef41Sopenharmony_ci assert(Object.hasOwn(Writable.prototype, 'writableFinished')); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci// event 141cb0ef41Sopenharmony_ci{ 151cb0ef41Sopenharmony_ci const writable = new Writable(); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci writable._write = (chunk, encoding, cb) => { 181cb0ef41Sopenharmony_ci // The state finished should start in false. 191cb0ef41Sopenharmony_ci assert.strictEqual(writable.writableFinished, false); 201cb0ef41Sopenharmony_ci cb(); 211cb0ef41Sopenharmony_ci }; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci writable.on('finish', common.mustCall(() => { 241cb0ef41Sopenharmony_ci assert.strictEqual(writable.writableFinished, true); 251cb0ef41Sopenharmony_ci })); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci writable.end('testing finished state', common.mustCall(() => { 281cb0ef41Sopenharmony_ci assert.strictEqual(writable.writableFinished, true); 291cb0ef41Sopenharmony_ci })); 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci{ 331cb0ef41Sopenharmony_ci // Emit finish asynchronously. 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci const w = new Writable({ 361cb0ef41Sopenharmony_ci write(chunk, encoding, cb) { 371cb0ef41Sopenharmony_ci cb(); 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci w.end(); 421cb0ef41Sopenharmony_ci w.on('finish', common.mustCall()); 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci{ 461cb0ef41Sopenharmony_ci // Emit prefinish synchronously. 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci const w = new Writable({ 491cb0ef41Sopenharmony_ci write(chunk, encoding, cb) { 501cb0ef41Sopenharmony_ci cb(); 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci }); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci let sync = true; 551cb0ef41Sopenharmony_ci w.on('prefinish', common.mustCall(() => { 561cb0ef41Sopenharmony_ci assert.strictEqual(sync, true); 571cb0ef41Sopenharmony_ci })); 581cb0ef41Sopenharmony_ci w.end(); 591cb0ef41Sopenharmony_ci sync = false; 601cb0ef41Sopenharmony_ci} 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci{ 631cb0ef41Sopenharmony_ci // Emit prefinish synchronously w/ final. 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci const w = new Writable({ 661cb0ef41Sopenharmony_ci write(chunk, encoding, cb) { 671cb0ef41Sopenharmony_ci cb(); 681cb0ef41Sopenharmony_ci }, 691cb0ef41Sopenharmony_ci final(cb) { 701cb0ef41Sopenharmony_ci cb(); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci }); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci let sync = true; 751cb0ef41Sopenharmony_ci w.on('prefinish', common.mustCall(() => { 761cb0ef41Sopenharmony_ci assert.strictEqual(sync, true); 771cb0ef41Sopenharmony_ci })); 781cb0ef41Sopenharmony_ci w.end(); 791cb0ef41Sopenharmony_ci sync = false; 801cb0ef41Sopenharmony_ci} 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci{ 841cb0ef41Sopenharmony_ci // Call _final synchronously. 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci let sync = true; 871cb0ef41Sopenharmony_ci const w = new Writable({ 881cb0ef41Sopenharmony_ci write(chunk, encoding, cb) { 891cb0ef41Sopenharmony_ci cb(); 901cb0ef41Sopenharmony_ci }, 911cb0ef41Sopenharmony_ci final: common.mustCall((cb) => { 921cb0ef41Sopenharmony_ci assert.strictEqual(sync, true); 931cb0ef41Sopenharmony_ci cb(); 941cb0ef41Sopenharmony_ci }) 951cb0ef41Sopenharmony_ci }); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci w.end(); 981cb0ef41Sopenharmony_ci sync = false; 991cb0ef41Sopenharmony_ci} 100