UNPKG

tar-transform

Version:

extract, transform and re-pack tarball entries in form of stream

162 lines (154 loc) 4.74 kB
import { Transform } from 'stream'; function streamToBuffer(stream) { const chunks = []; return new Promise((resolve, reject) => { const data = (chunk) => chunks.push(chunk); const end = () => { stream.removeListener("data", data); stream.removeListener("error", reject); stream.removeListener("end", end); resolve(Buffer.concat(chunks)); }; stream.on("data", data); stream.on("error", reject); stream.on("end", end); }); } async function streamToString(stream, encoding = "utf8") { const buffer = await streamToBuffer(stream); return buffer.toString(encoding); } async function bufferContentOfTarEntry(entry) { const { content, stream } = entry; if (typeof content === "string" && !stream) { return Buffer.from(content); } else if (content instanceof Buffer && !stream) { return content; } else if (!!stream && !content) { return streamToBuffer(stream); } else { throw new Error("invalid content of TarEntry"); } } async function stringContentOfTarEntry(entry, encoding) { const { content, stream } = entry; if (typeof content === "string") { if (stream) { throw new Error("invalid content of TarEntry"); } return content; } else { const buf = await bufferContentOfTarEntry(entry); return buf.toString(encoding); } } function headersWithNewName(headers, newName) { const newHeader = { ...headers, name: newName }; if (newHeader.pax) { newHeader.pax.path = newName; } return newHeader; } var util = /*#__PURE__*/Object.freeze({ __proto__: null, streamToBuffer: streamToBuffer, streamToString: streamToString, bufferContentOfTarEntry: bufferContentOfTarEntry, stringContentOfTarEntry: stringContentOfTarEntry, headersWithNewName: headersWithNewName }); var _a; const KEY_READABLE_STREAM = Symbol("readableStream"); const KEY_ON_ENTRY = Symbol("onEntry"); const KEY_ON_END = Symbol("onEnd"); class TarEntryTransformer { constructor(opts = {}) { this[_a] = undefined; this.util = util; this[KEY_ON_ENTRY] = opts.onEntry; this[KEY_ON_END] = opts.onEnd; this.ctx = opts.initCtx; } get readableStream() { const v = this[KEY_READABLE_STREAM]; if (!v) throw new Error("readableStream of instance of TarEntryTransformer not set"); return v; } set readableStream(v) { if (this[KEY_READABLE_STREAM]) throw new Error("readableStream of instance of TarEntryTransformer already set"); this[KEY_READABLE_STREAM] = v; } get onEntryImpl() { const f = this[KEY_ON_ENTRY]; if (!f) throw new Error("TarEntryTransformer.prototype.onEntry not implemented"); return f; } onEntry(entry) { return this.onEntryImpl.call(this, entry); } onEnd() { const end = this[KEY_ON_END]; if (typeof end === "function") { return end.call(this); } } push(entry) { this.readableStream.push(entry); return true; } pass(entry) { if (entry.stream) { entry.stream.resume(); } return true; } } _a = KEY_READABLE_STREAM; const KEY_TRANSFORMER = Symbol("transformer"); class TarTransform extends Transform { constructor(transformer) { super({ readableObjectMode: true, writableObjectMode: true }); if (transformer) { const t = transformer instanceof TarEntryTransformer ? transformer : new TarEntryTransformer(transformer); t.readableStream = this; this[KEY_TRANSFORMER] = t; } else { this[KEY_TRANSFORMER] = undefined; } } get transformer() { const v = this[KEY_TRANSFORMER]; if (!v) throw new Error("transformer of instance of TarTransform not assigned"); return v; } _transform(chunk, encoding, callback) { const t = this.transformer; Promise.resolve(t.onEntry(chunk)) .then(() => { callback(); }) .catch(err => callback(err)); } _flush(callback) { Promise.resolve(this.transformer.onEnd()).then(() => callback()); } transformTarEntry(chunk) { this.push(chunk); } } function transform(transformer) { return new TarTransform(transformer); } export { TarTransform as T, streamToString as a, TarEntryTransformer as b, streamToBuffer as s, transform as t }; //# sourceMappingURL=transform-ce45aa22.js.map