UNPKG

r2-utils-js

Version:

Readium 2 'utils' for NodeJS (TypeScript)

90 lines 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Zip1 = void 0; const tslib_1 = require("tslib"); const debug_ = require("debug"); const StreamZip = require("node-stream-zip"); const zip_1 = require("./zip"); const debug = debug_("r2:utils#zip/zip1"); class Zip1 extends zip_1.Zip { static loadPromise(filePath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const zip = new StreamZip({ file: filePath, storeEntries: true, }); zip.on("error", (err) => { debug("--ZIP error: " + filePath); debug(err); reject(err); }); zip.on("entry", (_entry) => { }); zip.on("extract", (entry, file) => { debug("--ZIP extract:"); debug(entry.name); debug(file); }); zip.on("ready", () => { resolve(new Zip1(filePath, zip)); }); }); }); } constructor(filePath, zip) { super(); this.filePath = filePath; this.zip = zip; } freeDestroy() { debug("freeDestroy: Zip1 -- " + this.filePath); if (this.zip) { this.zip.close(); } } entriesCount() { return this.zip.entriesCount; } hasEntries() { return this.entriesCount() > 0; } hasEntry(entryPath) { return this.hasEntries() && this.zip.entries()[entryPath]; } getEntries() { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.hasEntries()) { return Promise.resolve([]); } return Promise.resolve(Object.keys(this.zip.entries())); }); } entryStreamPromise(entryPath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.hasEntries() || !this.hasEntry(entryPath)) { return Promise.reject("no such path in zip: " + entryPath); } return new Promise((resolve, reject) => { this.zip.stream(entryPath, (err, stream) => { if (err) { reject(err); return; } const entry = this.zip.entries()[entryPath]; const streamAndLength = { length: entry.size, reset: () => tslib_1.__awaiter(this, void 0, void 0, function* () { return this.entryStreamPromise(entryPath); }), stream, }; resolve(streamAndLength); }); }); }); } } exports.Zip1 = Zip1; //# sourceMappingURL=zip1.js.map