11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst { Readable, Writable } = require('stream'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst source = Readable({ read: () => {} }); 81cb0ef41Sopenharmony_ciconst dest1 = Writable({ write: () => {} }); 91cb0ef41Sopenharmony_ciconst dest2 = Writable({ write: () => {} }); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cisource.pipe(dest1); 121cb0ef41Sopenharmony_cisource.pipe(dest2); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_cidest1.on('unpipe', common.mustCall()); 151cb0ef41Sopenharmony_cidest2.on('unpipe', common.mustCall()); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciassert.strictEqual(source._readableState.pipes[0], dest1); 181cb0ef41Sopenharmony_ciassert.strictEqual(source._readableState.pipes[1], dest2); 191cb0ef41Sopenharmony_ciassert.strictEqual(source._readableState.pipes.length, 2); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci// Should be able to unpipe them in the reverse order that they were piped. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cisource.unpipe(dest2); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciassert.deepStrictEqual(source._readableState.pipes, [dest1]); 261cb0ef41Sopenharmony_ciassert.notStrictEqual(source._readableState.pipes, dest2); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cidest2.on('unpipe', common.mustNotCall()); 291cb0ef41Sopenharmony_cisource.unpipe(dest2); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cisource.unpipe(dest1); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciassert.strictEqual(source._readableState.pipes.length, 0); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci{ 361cb0ef41Sopenharmony_ci // Test `cleanup()` if we unpipe all streams. 371cb0ef41Sopenharmony_ci const source = Readable({ read: () => {} }); 381cb0ef41Sopenharmony_ci const dest1 = Writable({ write: () => {} }); 391cb0ef41Sopenharmony_ci const dest2 = Writable({ write: () => {} }); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci let destCount = 0; 421cb0ef41Sopenharmony_ci const srcCheckEventNames = ['end', 'data']; 431cb0ef41Sopenharmony_ci const destCheckEventNames = ['close', 'finish', 'drain', 'error', 'unpipe']; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci const checkSrcCleanup = common.mustCall(() => { 461cb0ef41Sopenharmony_ci assert.strictEqual(source._readableState.pipes.length, 0); 471cb0ef41Sopenharmony_ci assert.strictEqual(source._readableState.flowing, false); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci srcCheckEventNames.forEach((eventName) => { 501cb0ef41Sopenharmony_ci assert.strictEqual( 511cb0ef41Sopenharmony_ci source.listenerCount(eventName), 0, 521cb0ef41Sopenharmony_ci `source's '${eventName}' event listeners not removed` 531cb0ef41Sopenharmony_ci ); 541cb0ef41Sopenharmony_ci }); 551cb0ef41Sopenharmony_ci }); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci function checkDestCleanup(dest) { 581cb0ef41Sopenharmony_ci const currentDestId = ++destCount; 591cb0ef41Sopenharmony_ci source.pipe(dest); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci const unpipeChecker = common.mustCall(() => { 621cb0ef41Sopenharmony_ci assert.deepStrictEqual( 631cb0ef41Sopenharmony_ci dest.listeners('unpipe'), [unpipeChecker], 641cb0ef41Sopenharmony_ci `destination{${currentDestId}} should have a 'unpipe' event ` + 651cb0ef41Sopenharmony_ci 'listener which is `unpipeChecker`' 661cb0ef41Sopenharmony_ci ); 671cb0ef41Sopenharmony_ci dest.removeListener('unpipe', unpipeChecker); 681cb0ef41Sopenharmony_ci destCheckEventNames.forEach((eventName) => { 691cb0ef41Sopenharmony_ci assert.strictEqual( 701cb0ef41Sopenharmony_ci dest.listenerCount(eventName), 0, 711cb0ef41Sopenharmony_ci `destination{${currentDestId}}'s '${eventName}' event ` + 721cb0ef41Sopenharmony_ci 'listeners not removed' 731cb0ef41Sopenharmony_ci ); 741cb0ef41Sopenharmony_ci }); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci if (--destCount === 0) 771cb0ef41Sopenharmony_ci checkSrcCleanup(); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci dest.on('unpipe', unpipeChecker); 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci checkDestCleanup(dest1); 841cb0ef41Sopenharmony_ci checkDestCleanup(dest2); 851cb0ef41Sopenharmony_ci source.unpipe(); 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci{ 891cb0ef41Sopenharmony_ci const src = Readable({ read: () => {} }); 901cb0ef41Sopenharmony_ci const dst = Writable({ write: () => {} }); 911cb0ef41Sopenharmony_ci src.pipe(dst); 921cb0ef41Sopenharmony_ci src.on('resume', common.mustCall(() => { 931cb0ef41Sopenharmony_ci src.on('pause', common.mustCall()); 941cb0ef41Sopenharmony_ci src.unpipe(dst); 951cb0ef41Sopenharmony_ci })); 961cb0ef41Sopenharmony_ci} 97