UNPKG

fast-extract

Version:

Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)

61 lines (60 loc) 2.06 kB
// Writable stream with flush callback support for Node 0.8+ import { Writable } from 'extract-base-iterator'; import { bufferFrom } from './buffer.js'; // Signal buffer for pre-_final Node versions const SIGNAL_FLUSH = bufferFrom([ 0 ]); let FlushWriteStream = class FlushWriteStream extends Writable { // biome-ignore lint/suspicious/noExplicitAny: Object mode allows any chunk type _write(chunk, enc, cb) { if (chunk === SIGNAL_FLUSH) { if (this._flushFn) { this._flushFn(cb); } else { cb(); } } else { this._writeFn.call(this, chunk, enc, cb); } } // biome-ignore lint/suspicious/noExplicitAny: Matches stream.Writable.end signature end(chunk, enc, cb) { if (!this._flushFn) return super.end(chunk, enc, cb); // Normalize arguments if (typeof chunk === 'function') [chunk, cb] = [ null, chunk ]; else if (typeof enc === 'function') [enc, cb] = [ null, enc ]; if (chunk) this.write(chunk); // Wait for SIGNAL_FLUSH write to complete before ending stream // biome-ignore lint/suspicious/noExplicitAny: Access internal _writableState if (!this._writableState.ending) { this.write(SIGNAL_FLUSH, ()=>{ super.end(cb); }); return this; } return super.end(cb); } destroy(err) { if (this.destroyed) return this; this.destroyed = true; if (err) this.emit('error', err); this.emit('close'); return this; } constructor(opts, writeFn, flushFn){ super(opts || {}), this.destroyed = false; this._writeFn = writeFn; this._flushFn = flushFn; } }; export default function flushWriteStream(opts, writeFn, flushFn) { if (typeof opts === 'function') return new FlushWriteStream(undefined, opts, writeFn); return new FlushWriteStream(opts, writeFn, flushFn); }