11cb0ef41Sopenharmony_ci'use strict' 21cb0ef41Sopenharmony_ciconst { Minipass } = require('minipass') 31cb0ef41Sopenharmony_ciconst EE = require('events').EventEmitter 41cb0ef41Sopenharmony_ciconst fs = require('fs') 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst writev = fs.writev 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst _autoClose = Symbol('_autoClose') 91cb0ef41Sopenharmony_ciconst _close = Symbol('_close') 101cb0ef41Sopenharmony_ciconst _ended = Symbol('_ended') 111cb0ef41Sopenharmony_ciconst _fd = Symbol('_fd') 121cb0ef41Sopenharmony_ciconst _finished = Symbol('_finished') 131cb0ef41Sopenharmony_ciconst _flags = Symbol('_flags') 141cb0ef41Sopenharmony_ciconst _flush = Symbol('_flush') 151cb0ef41Sopenharmony_ciconst _handleChunk = Symbol('_handleChunk') 161cb0ef41Sopenharmony_ciconst _makeBuf = Symbol('_makeBuf') 171cb0ef41Sopenharmony_ciconst _mode = Symbol('_mode') 181cb0ef41Sopenharmony_ciconst _needDrain = Symbol('_needDrain') 191cb0ef41Sopenharmony_ciconst _onerror = Symbol('_onerror') 201cb0ef41Sopenharmony_ciconst _onopen = Symbol('_onopen') 211cb0ef41Sopenharmony_ciconst _onread = Symbol('_onread') 221cb0ef41Sopenharmony_ciconst _onwrite = Symbol('_onwrite') 231cb0ef41Sopenharmony_ciconst _open = Symbol('_open') 241cb0ef41Sopenharmony_ciconst _path = Symbol('_path') 251cb0ef41Sopenharmony_ciconst _pos = Symbol('_pos') 261cb0ef41Sopenharmony_ciconst _queue = Symbol('_queue') 271cb0ef41Sopenharmony_ciconst _read = Symbol('_read') 281cb0ef41Sopenharmony_ciconst _readSize = Symbol('_readSize') 291cb0ef41Sopenharmony_ciconst _reading = Symbol('_reading') 301cb0ef41Sopenharmony_ciconst _remain = Symbol('_remain') 311cb0ef41Sopenharmony_ciconst _size = Symbol('_size') 321cb0ef41Sopenharmony_ciconst _write = Symbol('_write') 331cb0ef41Sopenharmony_ciconst _writing = Symbol('_writing') 341cb0ef41Sopenharmony_ciconst _defaultFlag = Symbol('_defaultFlag') 351cb0ef41Sopenharmony_ciconst _errored = Symbol('_errored') 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciclass ReadStream extends Minipass { 381cb0ef41Sopenharmony_ci constructor (path, opt) { 391cb0ef41Sopenharmony_ci opt = opt || {} 401cb0ef41Sopenharmony_ci super(opt) 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci this.readable = true 431cb0ef41Sopenharmony_ci this.writable = false 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci if (typeof path !== 'string') { 461cb0ef41Sopenharmony_ci throw new TypeError('path must be a string') 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci this[_errored] = false 501cb0ef41Sopenharmony_ci this[_fd] = typeof opt.fd === 'number' ? opt.fd : null 511cb0ef41Sopenharmony_ci this[_path] = path 521cb0ef41Sopenharmony_ci this[_readSize] = opt.readSize || 16 * 1024 * 1024 531cb0ef41Sopenharmony_ci this[_reading] = false 541cb0ef41Sopenharmony_ci this[_size] = typeof opt.size === 'number' ? opt.size : Infinity 551cb0ef41Sopenharmony_ci this[_remain] = this[_size] 561cb0ef41Sopenharmony_ci this[_autoClose] = typeof opt.autoClose === 'boolean' ? 571cb0ef41Sopenharmony_ci opt.autoClose : true 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci if (typeof this[_fd] === 'number') { 601cb0ef41Sopenharmony_ci this[_read]() 611cb0ef41Sopenharmony_ci } else { 621cb0ef41Sopenharmony_ci this[_open]() 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci get fd () { 671cb0ef41Sopenharmony_ci return this[_fd] 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci get path () { 711cb0ef41Sopenharmony_ci return this[_path] 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci write () { 751cb0ef41Sopenharmony_ci throw new TypeError('this is a readable stream') 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci end () { 791cb0ef41Sopenharmony_ci throw new TypeError('this is a readable stream') 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci [_open] () { 831cb0ef41Sopenharmony_ci fs.open(this[_path], 'r', (er, fd) => this[_onopen](er, fd)) 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci [_onopen] (er, fd) { 871cb0ef41Sopenharmony_ci if (er) { 881cb0ef41Sopenharmony_ci this[_onerror](er) 891cb0ef41Sopenharmony_ci } else { 901cb0ef41Sopenharmony_ci this[_fd] = fd 911cb0ef41Sopenharmony_ci this.emit('open', fd) 921cb0ef41Sopenharmony_ci this[_read]() 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci [_makeBuf] () { 971cb0ef41Sopenharmony_ci return Buffer.allocUnsafe(Math.min(this[_readSize], this[_remain])) 981cb0ef41Sopenharmony_ci } 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci [_read] () { 1011cb0ef41Sopenharmony_ci if (!this[_reading]) { 1021cb0ef41Sopenharmony_ci this[_reading] = true 1031cb0ef41Sopenharmony_ci const buf = this[_makeBuf]() 1041cb0ef41Sopenharmony_ci /* istanbul ignore if */ 1051cb0ef41Sopenharmony_ci if (buf.length === 0) { 1061cb0ef41Sopenharmony_ci return process.nextTick(() => this[_onread](null, 0, buf)) 1071cb0ef41Sopenharmony_ci } 1081cb0ef41Sopenharmony_ci fs.read(this[_fd], buf, 0, buf.length, null, (er, br, b) => 1091cb0ef41Sopenharmony_ci this[_onread](er, br, b)) 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci [_onread] (er, br, buf) { 1141cb0ef41Sopenharmony_ci this[_reading] = false 1151cb0ef41Sopenharmony_ci if (er) { 1161cb0ef41Sopenharmony_ci this[_onerror](er) 1171cb0ef41Sopenharmony_ci } else if (this[_handleChunk](br, buf)) { 1181cb0ef41Sopenharmony_ci this[_read]() 1191cb0ef41Sopenharmony_ci } 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci [_close] () { 1231cb0ef41Sopenharmony_ci if (this[_autoClose] && typeof this[_fd] === 'number') { 1241cb0ef41Sopenharmony_ci const fd = this[_fd] 1251cb0ef41Sopenharmony_ci this[_fd] = null 1261cb0ef41Sopenharmony_ci fs.close(fd, er => er ? this.emit('error', er) : this.emit('close')) 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci [_onerror] (er) { 1311cb0ef41Sopenharmony_ci this[_reading] = true 1321cb0ef41Sopenharmony_ci this[_close]() 1331cb0ef41Sopenharmony_ci this.emit('error', er) 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci [_handleChunk] (br, buf) { 1371cb0ef41Sopenharmony_ci let ret = false 1381cb0ef41Sopenharmony_ci // no effect if infinite 1391cb0ef41Sopenharmony_ci this[_remain] -= br 1401cb0ef41Sopenharmony_ci if (br > 0) { 1411cb0ef41Sopenharmony_ci ret = super.write(br < buf.length ? buf.slice(0, br) : buf) 1421cb0ef41Sopenharmony_ci } 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci if (br === 0 || this[_remain] <= 0) { 1451cb0ef41Sopenharmony_ci ret = false 1461cb0ef41Sopenharmony_ci this[_close]() 1471cb0ef41Sopenharmony_ci super.end() 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci return ret 1511cb0ef41Sopenharmony_ci } 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci emit (ev, data) { 1541cb0ef41Sopenharmony_ci switch (ev) { 1551cb0ef41Sopenharmony_ci case 'prefinish': 1561cb0ef41Sopenharmony_ci case 'finish': 1571cb0ef41Sopenharmony_ci break 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci case 'drain': 1601cb0ef41Sopenharmony_ci if (typeof this[_fd] === 'number') { 1611cb0ef41Sopenharmony_ci this[_read]() 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci break 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci case 'error': 1661cb0ef41Sopenharmony_ci if (this[_errored]) { 1671cb0ef41Sopenharmony_ci return 1681cb0ef41Sopenharmony_ci } 1691cb0ef41Sopenharmony_ci this[_errored] = true 1701cb0ef41Sopenharmony_ci return super.emit(ev, data) 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci default: 1731cb0ef41Sopenharmony_ci return super.emit(ev, data) 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci} 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ciclass ReadStreamSync extends ReadStream { 1791cb0ef41Sopenharmony_ci [_open] () { 1801cb0ef41Sopenharmony_ci let threw = true 1811cb0ef41Sopenharmony_ci try { 1821cb0ef41Sopenharmony_ci this[_onopen](null, fs.openSync(this[_path], 'r')) 1831cb0ef41Sopenharmony_ci threw = false 1841cb0ef41Sopenharmony_ci } finally { 1851cb0ef41Sopenharmony_ci if (threw) { 1861cb0ef41Sopenharmony_ci this[_close]() 1871cb0ef41Sopenharmony_ci } 1881cb0ef41Sopenharmony_ci } 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci [_read] () { 1921cb0ef41Sopenharmony_ci let threw = true 1931cb0ef41Sopenharmony_ci try { 1941cb0ef41Sopenharmony_ci if (!this[_reading]) { 1951cb0ef41Sopenharmony_ci this[_reading] = true 1961cb0ef41Sopenharmony_ci do { 1971cb0ef41Sopenharmony_ci const buf = this[_makeBuf]() 1981cb0ef41Sopenharmony_ci /* istanbul ignore next */ 1991cb0ef41Sopenharmony_ci const br = buf.length === 0 ? 0 2001cb0ef41Sopenharmony_ci : fs.readSync(this[_fd], buf, 0, buf.length, null) 2011cb0ef41Sopenharmony_ci if (!this[_handleChunk](br, buf)) { 2021cb0ef41Sopenharmony_ci break 2031cb0ef41Sopenharmony_ci } 2041cb0ef41Sopenharmony_ci } while (true) 2051cb0ef41Sopenharmony_ci this[_reading] = false 2061cb0ef41Sopenharmony_ci } 2071cb0ef41Sopenharmony_ci threw = false 2081cb0ef41Sopenharmony_ci } finally { 2091cb0ef41Sopenharmony_ci if (threw) { 2101cb0ef41Sopenharmony_ci this[_close]() 2111cb0ef41Sopenharmony_ci } 2121cb0ef41Sopenharmony_ci } 2131cb0ef41Sopenharmony_ci } 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci [_close] () { 2161cb0ef41Sopenharmony_ci if (this[_autoClose] && typeof this[_fd] === 'number') { 2171cb0ef41Sopenharmony_ci const fd = this[_fd] 2181cb0ef41Sopenharmony_ci this[_fd] = null 2191cb0ef41Sopenharmony_ci fs.closeSync(fd) 2201cb0ef41Sopenharmony_ci this.emit('close') 2211cb0ef41Sopenharmony_ci } 2221cb0ef41Sopenharmony_ci } 2231cb0ef41Sopenharmony_ci} 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ciclass WriteStream extends EE { 2261cb0ef41Sopenharmony_ci constructor (path, opt) { 2271cb0ef41Sopenharmony_ci opt = opt || {} 2281cb0ef41Sopenharmony_ci super(opt) 2291cb0ef41Sopenharmony_ci this.readable = false 2301cb0ef41Sopenharmony_ci this.writable = true 2311cb0ef41Sopenharmony_ci this[_errored] = false 2321cb0ef41Sopenharmony_ci this[_writing] = false 2331cb0ef41Sopenharmony_ci this[_ended] = false 2341cb0ef41Sopenharmony_ci this[_needDrain] = false 2351cb0ef41Sopenharmony_ci this[_queue] = [] 2361cb0ef41Sopenharmony_ci this[_path] = path 2371cb0ef41Sopenharmony_ci this[_fd] = typeof opt.fd === 'number' ? opt.fd : null 2381cb0ef41Sopenharmony_ci this[_mode] = opt.mode === undefined ? 0o666 : opt.mode 2391cb0ef41Sopenharmony_ci this[_pos] = typeof opt.start === 'number' ? opt.start : null 2401cb0ef41Sopenharmony_ci this[_autoClose] = typeof opt.autoClose === 'boolean' ? 2411cb0ef41Sopenharmony_ci opt.autoClose : true 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ci // truncating makes no sense when writing into the middle 2441cb0ef41Sopenharmony_ci const defaultFlag = this[_pos] !== null ? 'r+' : 'w' 2451cb0ef41Sopenharmony_ci this[_defaultFlag] = opt.flags === undefined 2461cb0ef41Sopenharmony_ci this[_flags] = this[_defaultFlag] ? defaultFlag : opt.flags 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci if (this[_fd] === null) { 2491cb0ef41Sopenharmony_ci this[_open]() 2501cb0ef41Sopenharmony_ci } 2511cb0ef41Sopenharmony_ci } 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci emit (ev, data) { 2541cb0ef41Sopenharmony_ci if (ev === 'error') { 2551cb0ef41Sopenharmony_ci if (this[_errored]) { 2561cb0ef41Sopenharmony_ci return 2571cb0ef41Sopenharmony_ci } 2581cb0ef41Sopenharmony_ci this[_errored] = true 2591cb0ef41Sopenharmony_ci } 2601cb0ef41Sopenharmony_ci return super.emit(ev, data) 2611cb0ef41Sopenharmony_ci } 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ci get fd () { 2641cb0ef41Sopenharmony_ci return this[_fd] 2651cb0ef41Sopenharmony_ci } 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci get path () { 2681cb0ef41Sopenharmony_ci return this[_path] 2691cb0ef41Sopenharmony_ci } 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci [_onerror] (er) { 2721cb0ef41Sopenharmony_ci this[_close]() 2731cb0ef41Sopenharmony_ci this[_writing] = true 2741cb0ef41Sopenharmony_ci this.emit('error', er) 2751cb0ef41Sopenharmony_ci } 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci [_open] () { 2781cb0ef41Sopenharmony_ci fs.open(this[_path], this[_flags], this[_mode], 2791cb0ef41Sopenharmony_ci (er, fd) => this[_onopen](er, fd)) 2801cb0ef41Sopenharmony_ci } 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci [_onopen] (er, fd) { 2831cb0ef41Sopenharmony_ci if (this[_defaultFlag] && 2841cb0ef41Sopenharmony_ci this[_flags] === 'r+' && 2851cb0ef41Sopenharmony_ci er && er.code === 'ENOENT') { 2861cb0ef41Sopenharmony_ci this[_flags] = 'w' 2871cb0ef41Sopenharmony_ci this[_open]() 2881cb0ef41Sopenharmony_ci } else if (er) { 2891cb0ef41Sopenharmony_ci this[_onerror](er) 2901cb0ef41Sopenharmony_ci } else { 2911cb0ef41Sopenharmony_ci this[_fd] = fd 2921cb0ef41Sopenharmony_ci this.emit('open', fd) 2931cb0ef41Sopenharmony_ci if (!this[_writing]) { 2941cb0ef41Sopenharmony_ci this[_flush]() 2951cb0ef41Sopenharmony_ci } 2961cb0ef41Sopenharmony_ci } 2971cb0ef41Sopenharmony_ci } 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ci end (buf, enc) { 3001cb0ef41Sopenharmony_ci if (buf) { 3011cb0ef41Sopenharmony_ci this.write(buf, enc) 3021cb0ef41Sopenharmony_ci } 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci this[_ended] = true 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci // synthetic after-write logic, where drain/finish live 3071cb0ef41Sopenharmony_ci if (!this[_writing] && !this[_queue].length && 3081cb0ef41Sopenharmony_ci typeof this[_fd] === 'number') { 3091cb0ef41Sopenharmony_ci this[_onwrite](null, 0) 3101cb0ef41Sopenharmony_ci } 3111cb0ef41Sopenharmony_ci return this 3121cb0ef41Sopenharmony_ci } 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci write (buf, enc) { 3151cb0ef41Sopenharmony_ci if (typeof buf === 'string') { 3161cb0ef41Sopenharmony_ci buf = Buffer.from(buf, enc) 3171cb0ef41Sopenharmony_ci } 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci if (this[_ended]) { 3201cb0ef41Sopenharmony_ci this.emit('error', new Error('write() after end()')) 3211cb0ef41Sopenharmony_ci return false 3221cb0ef41Sopenharmony_ci } 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci if (this[_fd] === null || this[_writing] || this[_queue].length) { 3251cb0ef41Sopenharmony_ci this[_queue].push(buf) 3261cb0ef41Sopenharmony_ci this[_needDrain] = true 3271cb0ef41Sopenharmony_ci return false 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci this[_writing] = true 3311cb0ef41Sopenharmony_ci this[_write](buf) 3321cb0ef41Sopenharmony_ci return true 3331cb0ef41Sopenharmony_ci } 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ci [_write] (buf) { 3361cb0ef41Sopenharmony_ci fs.write(this[_fd], buf, 0, buf.length, this[_pos], (er, bw) => 3371cb0ef41Sopenharmony_ci this[_onwrite](er, bw)) 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci [_onwrite] (er, bw) { 3411cb0ef41Sopenharmony_ci if (er) { 3421cb0ef41Sopenharmony_ci this[_onerror](er) 3431cb0ef41Sopenharmony_ci } else { 3441cb0ef41Sopenharmony_ci if (this[_pos] !== null) { 3451cb0ef41Sopenharmony_ci this[_pos] += bw 3461cb0ef41Sopenharmony_ci } 3471cb0ef41Sopenharmony_ci if (this[_queue].length) { 3481cb0ef41Sopenharmony_ci this[_flush]() 3491cb0ef41Sopenharmony_ci } else { 3501cb0ef41Sopenharmony_ci this[_writing] = false 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ci if (this[_ended] && !this[_finished]) { 3531cb0ef41Sopenharmony_ci this[_finished] = true 3541cb0ef41Sopenharmony_ci this[_close]() 3551cb0ef41Sopenharmony_ci this.emit('finish') 3561cb0ef41Sopenharmony_ci } else if (this[_needDrain]) { 3571cb0ef41Sopenharmony_ci this[_needDrain] = false 3581cb0ef41Sopenharmony_ci this.emit('drain') 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci } 3611cb0ef41Sopenharmony_ci } 3621cb0ef41Sopenharmony_ci } 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci [_flush] () { 3651cb0ef41Sopenharmony_ci if (this[_queue].length === 0) { 3661cb0ef41Sopenharmony_ci if (this[_ended]) { 3671cb0ef41Sopenharmony_ci this[_onwrite](null, 0) 3681cb0ef41Sopenharmony_ci } 3691cb0ef41Sopenharmony_ci } else if (this[_queue].length === 1) { 3701cb0ef41Sopenharmony_ci this[_write](this[_queue].pop()) 3711cb0ef41Sopenharmony_ci } else { 3721cb0ef41Sopenharmony_ci const iovec = this[_queue] 3731cb0ef41Sopenharmony_ci this[_queue] = [] 3741cb0ef41Sopenharmony_ci writev(this[_fd], iovec, this[_pos], 3751cb0ef41Sopenharmony_ci (er, bw) => this[_onwrite](er, bw)) 3761cb0ef41Sopenharmony_ci } 3771cb0ef41Sopenharmony_ci } 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ci [_close] () { 3801cb0ef41Sopenharmony_ci if (this[_autoClose] && typeof this[_fd] === 'number') { 3811cb0ef41Sopenharmony_ci const fd = this[_fd] 3821cb0ef41Sopenharmony_ci this[_fd] = null 3831cb0ef41Sopenharmony_ci fs.close(fd, er => er ? this.emit('error', er) : this.emit('close')) 3841cb0ef41Sopenharmony_ci } 3851cb0ef41Sopenharmony_ci } 3861cb0ef41Sopenharmony_ci} 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ciclass WriteStreamSync extends WriteStream { 3891cb0ef41Sopenharmony_ci [_open] () { 3901cb0ef41Sopenharmony_ci let fd 3911cb0ef41Sopenharmony_ci // only wrap in a try{} block if we know we'll retry, to avoid 3921cb0ef41Sopenharmony_ci // the rethrow obscuring the error's source frame in most cases. 3931cb0ef41Sopenharmony_ci if (this[_defaultFlag] && this[_flags] === 'r+') { 3941cb0ef41Sopenharmony_ci try { 3951cb0ef41Sopenharmony_ci fd = fs.openSync(this[_path], this[_flags], this[_mode]) 3961cb0ef41Sopenharmony_ci } catch (er) { 3971cb0ef41Sopenharmony_ci if (er.code === 'ENOENT') { 3981cb0ef41Sopenharmony_ci this[_flags] = 'w' 3991cb0ef41Sopenharmony_ci return this[_open]() 4001cb0ef41Sopenharmony_ci } else { 4011cb0ef41Sopenharmony_ci throw er 4021cb0ef41Sopenharmony_ci } 4031cb0ef41Sopenharmony_ci } 4041cb0ef41Sopenharmony_ci } else { 4051cb0ef41Sopenharmony_ci fd = fs.openSync(this[_path], this[_flags], this[_mode]) 4061cb0ef41Sopenharmony_ci } 4071cb0ef41Sopenharmony_ci 4081cb0ef41Sopenharmony_ci this[_onopen](null, fd) 4091cb0ef41Sopenharmony_ci } 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci [_close] () { 4121cb0ef41Sopenharmony_ci if (this[_autoClose] && typeof this[_fd] === 'number') { 4131cb0ef41Sopenharmony_ci const fd = this[_fd] 4141cb0ef41Sopenharmony_ci this[_fd] = null 4151cb0ef41Sopenharmony_ci fs.closeSync(fd) 4161cb0ef41Sopenharmony_ci this.emit('close') 4171cb0ef41Sopenharmony_ci } 4181cb0ef41Sopenharmony_ci } 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci [_write] (buf) { 4211cb0ef41Sopenharmony_ci // throw the original, but try to close if it fails 4221cb0ef41Sopenharmony_ci let threw = true 4231cb0ef41Sopenharmony_ci try { 4241cb0ef41Sopenharmony_ci this[_onwrite](null, 4251cb0ef41Sopenharmony_ci fs.writeSync(this[_fd], buf, 0, buf.length, this[_pos])) 4261cb0ef41Sopenharmony_ci threw = false 4271cb0ef41Sopenharmony_ci } finally { 4281cb0ef41Sopenharmony_ci if (threw) { 4291cb0ef41Sopenharmony_ci try { 4301cb0ef41Sopenharmony_ci this[_close]() 4311cb0ef41Sopenharmony_ci } catch { 4321cb0ef41Sopenharmony_ci // ok error 4331cb0ef41Sopenharmony_ci } 4341cb0ef41Sopenharmony_ci } 4351cb0ef41Sopenharmony_ci } 4361cb0ef41Sopenharmony_ci } 4371cb0ef41Sopenharmony_ci} 4381cb0ef41Sopenharmony_ci 4391cb0ef41Sopenharmony_ciexports.ReadStream = ReadStream 4401cb0ef41Sopenharmony_ciexports.ReadStreamSync = ReadStreamSync 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ciexports.WriteStream = WriteStream 4431cb0ef41Sopenharmony_ciexports.WriteStreamSync = WriteStreamSync 444