UNPKG

tar-iterator

Version:

Extract contents from tar archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms

47 lines (46 loc) 1.51 kB
import once from 'call-once-fn'; import BaseIterator from 'extract-base-iterator'; import fs from 'fs'; import * as tarStream from 'tar-stream-compat'; import Lock from './lib/Lock.js'; import nextEntry from './nextEntry.js'; let TarIterator = class TarIterator extends BaseIterator { end(err) { if (this.lock) { this.lock.err = err; this.lock.release(); this.lock = null; } else { BaseIterator.prototype.end.call(this, err); // call in lock release so end is properly handled } this.extract = null; } constructor(source, options = {}){ super(options); this.lock = new Lock(); this.lock.iterator = this; let cancelled = false; const setup = ()=>{ cancelled = true; }; this.processing.push(setup); this.extract = tarStream.extract(); const pipe = (cb)=>{ try { if (typeof source === 'string') source = fs.createReadStream(source); } catch (err) { cb(err); } const end = once(cb); source.on('data', ()=>end()); source.on('error', end); source.pipe(this.extract); }; pipe((err)=>{ this.processing.remove(setup); if (this.done || cancelled) return; // done err ? this.end(err) : this.push(nextEntry.bind(null, null)); }); } }; export { TarIterator as default };