tar-transform
Version:
extract, transform and re-pack tarball entries in form of stream
159 lines (152 loc) • 5.08 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}
var tar = require('tar-stream');
var stream = require('stream');
function drain(writable) {
if (writable.destroyed) {
return Promise.reject(new Error("premature close"));
}
return new Promise((resolve, reject) => {
const done = () => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
writable.removeListener("close", onClose);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
writable.removeListener("drain", onDrain);
};
const onClose = () => {
done();
reject(new Error("premature close"));
};
const onDrain = () => {
done();
resolve();
};
writable.on("close", onClose);
writable.on("drain", onDrain);
});
}
const TAR_EXTRACT_PRIVATE_PROP_KEY = Symbol("tarPackPrivateProp");
function getGzipOption(options) {
if (!options || options.gzip === undefined) {
return "auto";
}
const { gzip } = options;
if (typeof gzip === "boolean" ||
gzip === "auto" ||
(typeof gzip === "object" && gzip !== null)) {
return gzip;
}
throw new Error("TarExtract constructor option gzip is wrong");
}
function getPrivateTarExtract(options) {
const gzip = getGzipOption(options);
const extract = tar.extract();
const writableStreamPromise = gzip === true || typeof gzip === "object"
? new Promise(function (resolve) { resolve(_interopNamespace(require('zlib'))); }).then(zlib => zlib.createGunzip(gzip === true ? undefined : gzip))
: gzip === false
? undefined
: new Promise(function (resolve) { resolve(require('./gunzip-maybe-3c89e432.js')); }).then(({ default: gunzip }) => gunzip());
const tarExtract = writableStreamPromise
? {
extract,
setup: false,
writable: undefined,
pending: writableStreamPromise.then(w => {
w.pipe(extract);
tarExtract.writable = w;
tarExtract.pending = undefined;
return w;
}),
}
: {
extract,
setup: false,
writable: extract,
pending: undefined,
};
return tarExtract;
}
class TarExtract extends stream.Duplex {
constructor(options) {
super({ ...options, readableObjectMode: true, writableObjectMode: false });
this[TAR_EXTRACT_PRIVATE_PROP_KEY] = getPrivateTarExtract(options);
}
async _write(chunk, encoding, callback) {
try {
const { writable, pending } = this[TAR_EXTRACT_PRIVATE_PROP_KEY];
const writer = writable || (await pending);
if (!writer) {
throw new Error("invalid state in TarExtract: writer is unavailable");
}
let resolve;
let reject;
const p = new Promise((rsv, rjt) => {
resolve = rsv;
reject = rjt;
});
if (!writer.write(chunk, err => {
if (err)
reject(err);
else
resolve();
})) {
await drain(writer);
}
await p;
callback();
}
catch (err) {
callback(err);
}
}
_read() {
const { setup, extract } = this[TAR_EXTRACT_PRIVATE_PROP_KEY];
if (!setup) {
this[TAR_EXTRACT_PRIVATE_PROP_KEY].setup = true;
extract.on("entry", (headers, stream, next) => {
const res = { headers, stream };
this.push(res);
// stream.once("end", () => {
// console.log("stream once [end]");
// next();
// });
next();
});
extract.on("finish", () => {
this.push(null);
});
}
}
async _final(callback) {
const { writable, pending } = this[TAR_EXTRACT_PRIVATE_PROP_KEY];
const writer = writable || (await pending);
if (!writer) {
throw new Error("invalid state in TarExtract: writer is unavailable");
}
writer.end(callback);
}
}
function extract(...args) {
return new TarExtract(...args);
}
exports.TarExtract = TarExtract;
exports.extract = extract;
//# sourceMappingURL=extract.js.map
;