fast-extract
Version:
Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)
59 lines (58 loc) • 1.68 kB
JavaScript
import TarIterator from 'tar-iterator';
import { PassThrough, Transform } from '../../compat/stream.js';
let TarTransform = class TarTransform extends Transform {
_transform(chunk, encoding, callback) {
if (this._stream) {
this._stream.write(chunk, encoding, callback);
return;
}
this._stream = new PassThrough();
this._iterator = new TarIterator(this._stream);
this._iterator.forEach((entry)=>{
this.push(entry);
}, {
concurrency: 1
}, (err)=>{
if (!this._iterator) return;
err || this.push(null);
this._stream = null;
this._iterator.destroy();
this._iterator = null;
this._callback ? this._callback(err) : this.end(err);
this._callback = null;
});
this._stream.write(chunk, encoding, callback);
}
_flush(callback) {
if (!this._stream) {
callback();
return;
}
this._callback = callback;
this._stream.end();
this._stream = null;
}
destroy(error) {
if (this._stream) {
this._stream.end();
this._stream = null;
}
if (this._iterator) {
const iterator = this._iterator;
this._iterator = null;
iterator.destroy(error);
this.end(error);
}
return this;
}
constructor(options){
options = options ? {
...options,
objectMode: true
} : {
objectMode: true
};
super(options);
}
};
export { TarTransform as default };