11cb0ef41Sopenharmony_ciconst Stream = require('stream') 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciclass MuteStream extends Stream { 41cb0ef41Sopenharmony_ci #isTTY = null 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci constructor (opts = {}) { 71cb0ef41Sopenharmony_ci super(opts) 81cb0ef41Sopenharmony_ci this.writable = this.readable = true 91cb0ef41Sopenharmony_ci this.muted = false 101cb0ef41Sopenharmony_ci this.on('pipe', this._onpipe) 111cb0ef41Sopenharmony_ci this.replace = opts.replace 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci // For readline-type situations 141cb0ef41Sopenharmony_ci // This much at the start of a line being redrawn after a ctrl char 151cb0ef41Sopenharmony_ci // is seen (such as backspace) won't be redrawn as the replacement 161cb0ef41Sopenharmony_ci this._prompt = opts.prompt || null 171cb0ef41Sopenharmony_ci this._hadControl = false 181cb0ef41Sopenharmony_ci } 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci #destSrc (key, def) { 211cb0ef41Sopenharmony_ci if (this._dest) { 221cb0ef41Sopenharmony_ci return this._dest[key] 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci if (this._src) { 251cb0ef41Sopenharmony_ci return this._src[key] 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci return def 281cb0ef41Sopenharmony_ci } 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci #proxy (method, ...args) { 311cb0ef41Sopenharmony_ci if (typeof this._dest?.[method] === 'function') { 321cb0ef41Sopenharmony_ci this._dest[method](...args) 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci if (typeof this._src?.[method] === 'function') { 351cb0ef41Sopenharmony_ci this._src[method](...args) 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci get isTTY () { 401cb0ef41Sopenharmony_ci if (this.#isTTY !== null) { 411cb0ef41Sopenharmony_ci return this.#isTTY 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci return this.#destSrc('isTTY', false) 441cb0ef41Sopenharmony_ci } 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // basically just get replace the getter/setter with a regular value 471cb0ef41Sopenharmony_ci set isTTY (val) { 481cb0ef41Sopenharmony_ci this.#isTTY = val 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci get rows () { 521cb0ef41Sopenharmony_ci return this.#destSrc('rows') 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci get columns () { 561cb0ef41Sopenharmony_ci return this.#destSrc('columns') 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci mute () { 601cb0ef41Sopenharmony_ci this.muted = true 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci unmute () { 641cb0ef41Sopenharmony_ci this.muted = false 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci _onpipe (src) { 681cb0ef41Sopenharmony_ci this._src = src 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci pipe (dest, options) { 721cb0ef41Sopenharmony_ci this._dest = dest 731cb0ef41Sopenharmony_ci return super.pipe(dest, options) 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci pause () { 771cb0ef41Sopenharmony_ci if (this._src) { 781cb0ef41Sopenharmony_ci return this._src.pause() 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci resume () { 831cb0ef41Sopenharmony_ci if (this._src) { 841cb0ef41Sopenharmony_ci return this._src.resume() 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci write (c) { 891cb0ef41Sopenharmony_ci if (this.muted) { 901cb0ef41Sopenharmony_ci if (!this.replace) { 911cb0ef41Sopenharmony_ci return true 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci // eslint-disable-next-line no-control-regex 941cb0ef41Sopenharmony_ci if (c.match(/^\u001b/)) { 951cb0ef41Sopenharmony_ci if (c.indexOf(this._prompt) === 0) { 961cb0ef41Sopenharmony_ci c = c.slice(this._prompt.length) 971cb0ef41Sopenharmony_ci c = c.replace(/./g, this.replace) 981cb0ef41Sopenharmony_ci c = this._prompt + c 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci this._hadControl = true 1011cb0ef41Sopenharmony_ci return this.emit('data', c) 1021cb0ef41Sopenharmony_ci } else { 1031cb0ef41Sopenharmony_ci if (this._prompt && this._hadControl && 1041cb0ef41Sopenharmony_ci c.indexOf(this._prompt) === 0) { 1051cb0ef41Sopenharmony_ci this._hadControl = false 1061cb0ef41Sopenharmony_ci this.emit('data', this._prompt) 1071cb0ef41Sopenharmony_ci c = c.slice(this._prompt.length) 1081cb0ef41Sopenharmony_ci } 1091cb0ef41Sopenharmony_ci c = c.toString().replace(/./g, this.replace) 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci this.emit('data', c) 1131cb0ef41Sopenharmony_ci } 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci end (c) { 1161cb0ef41Sopenharmony_ci if (this.muted) { 1171cb0ef41Sopenharmony_ci if (c && this.replace) { 1181cb0ef41Sopenharmony_ci c = c.toString().replace(/./g, this.replace) 1191cb0ef41Sopenharmony_ci } else { 1201cb0ef41Sopenharmony_ci c = null 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci } 1231cb0ef41Sopenharmony_ci if (c) { 1241cb0ef41Sopenharmony_ci this.emit('data', c) 1251cb0ef41Sopenharmony_ci } 1261cb0ef41Sopenharmony_ci this.emit('end') 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci destroy (...args) { 1301cb0ef41Sopenharmony_ci return this.#proxy('destroy', ...args) 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci destroySoon (...args) { 1341cb0ef41Sopenharmony_ci return this.#proxy('destroySoon', ...args) 1351cb0ef41Sopenharmony_ci } 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci close (...args) { 1381cb0ef41Sopenharmony_ci return this.#proxy('close', ...args) 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci} 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_cimodule.exports = MuteStream 143