fast-extract
Version:
Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)
62 lines (61 loc) • 2.2 kB
JavaScript
import { PassThrough, Transform } from 'extract-base-iterator';
export default function createIteratorTransform(IteratorClass) {
let IteratorTransform = class IteratorTransform extends Transform {
_transform(chunk, encoding, callback) {
if (this._stream) {
this._stream.write(chunk, encoding, callback);
return;
}
this._stream = new PassThrough();
this._iterator = new IteratorClass(this._stream);
const onEntry = (entry)=>{
this.push(entry);
};
const onDone = (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._iterator.forEach(onEntry, {
concurrency: this._concurrency
}, onDone);
this._stream.write(chunk, encoding, callback);
}
_flush(callback) {
if (!this._stream) return callback();
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){
var _ref;
const concurrency = (_ref = options === null || options === void 0 ? void 0 : options.concurrency) !== null && _ref !== void 0 ? _ref : Infinity;
options = options ? {
...options,
objectMode: true
} : {
objectMode: true
};
super(options);
this._concurrency = concurrency;
}
};
return IteratorTransform;
}