fast-extract
Version:
Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)
48 lines (47 loc) • 1.49 kB
JavaScript
import { Transform } from 'extract-base-iterator';
import throttle from 'lodash.throttle';
let EntryProgressTransform = class EntryProgressTransform extends Transform {
_transform(entry, encoding, callback) {
if (this.progress) this.progress(entry);
this.push(entry, encoding);
callback();
}
_flush(callback) {
if (this.progress) {
const p = this.progress;
this.progress = null;
p(null);
const throttled = p;
if (throttled.cancel) throttled.cancel();
}
callback();
}
constructor(options){
options = options ? {
...options,
objectMode: true
} : {
objectMode: true
};
super(options), this.progress = null;
const internalOptions = options;
let done = false;
const progressFn = function progressFn(entry) {
if (done) return; // throttle can call after done
if (!entry) {
done = true;
return done;
}
if (internalOptions.progress) internalOptions.progress({
...entry,
progress: 'extract'
});
return undefined;
};
const time = internalOptions.time;
this.progress = time !== undefined ? throttle(progressFn, time, {
leading: true
}) : progressFn;
}
};
export { EntryProgressTransform as default };