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AC,OAAO,CAAC,SAAS,CAAC,CAAC;gBAEtC,WAAO,IAAI,OAAO,CAAmB,UAAC,OAAO,EAAE,MAAM;wBAGjD,KAAI,CAAC,GAAG,CAAC,cAAc,CAAC,KAAK,EAAE,UAAC,GAAQ,EAAE,MAA6B;4BACnE,IAAI,GAAG,EAAE;gCACL,KAAK,CAAC,4BAA4B,CAAC,CAAC;gCACpC,KAAK,CAAC,GAAG,CAAC,CAAC;gCACX,MAAM,CAAC,GAAG,CAAC,CAAC;gCACZ,OAAO;6BACV;4BACD,IAAM,eAAe,GAAqB;gCACtC,MAAM,EAAE,KAAK,CAAC,gBAA0B;gCACxC,KAAK,EAAE;;wCACH,WAAO,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,EAAC;;qCAC7C;gCACD,MAAM,QAAA;6BACT,CAAC;4BACF,OAAO,CAAC,eAAe,CAAC,CAAC;wBAC7B,CAAC,CAAC,CAAC;oBACP,CAAC,CAAC,EAAC;;;KACN;IAGO,uBAAQ,GAAhB,UAAiB,KAAU;QACvB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC;IACzC,CAAC;IACL,WAAC;AAAD,CAAC,AAlWD,CAA0B,SAAG,GAkW5B;AAlWY,oBAAI","sourcesContent":["// ==LICENSE-BEGIN==\n// Copyright 2017 European Digital Reading Lab. All rights reserved.\n// Licensed to the Readium Foundation under one or more contributor license agreements.\n// Use of this source code is governed by a BSD-style license\n// that can be found in the LICENSE file exposed on Github (readium) in the project repository.\n// ==LICENSE-END==\n\nimport * as debug_ from \"debug\";\nimport * as request from \"request\";\nimport * as requestPromise from \"request-promise-native\";\nimport * as yauzl from \"yauzl\";\n\nimport { isHTTP } from \"../http/UrlUtils\";\nimport { streamToBufferPromise } from \"../stream/BufferUtils\";\nimport { IStreamAndLength, IZip, Zip } from \"./zip\";\nimport { HttpZipReader } from \"./zip2RandomAccessReader_Http\";\n\nconst debug = debug_(\"r2:utils#zip/zip2\");\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface IStringKeyedObject { [key: string]: any; }\n\nexport class Zip2 extends Zip {\n\n public static async loadPromise(filePath: string): Promise<IZip> {\n if (isHTTP(filePath)) {\n return Zip2.loadPromiseHTTP(filePath);\n }\n\n return new Promise<IZip>((resolve, reject) => {\n\n yauzl.open(filePath, { lazyEntries: true, autoClose: false }, (err, zip) => {\n if (err || !zip) {\n debug(\"yauzl init ERROR\");\n debug(err);\n reject(err);\n return;\n }\n const zip2 = new Zip2(filePath, zip);\n\n zip.on(\"error\", (erro) => {\n debug(\"yauzl ERROR\");\n debug(erro);\n reject(erro);\n });\n\n zip.readEntry(); // next (lazyEntries)\n zip.on(\"entry\", (entry) => {\n // if (/\\/$/.test(entry.fileName)) {\n if (entry.fileName[entry.fileName.length - 1] === \"/\") {\n // skip directories / folders\n } else {\n // debug(entry.fileName);\n zip2.addEntry(entry);\n }\n zip.readEntry(); // next (lazyEntries)\n });\n\n zip.on(\"end\", () => {\n debug(\"yauzl END\");\n resolve(zip2);\n });\n\n zip.on(\"close\", () => {\n debug(\"yauzl CLOSE\");\n });\n });\n });\n }\n\n private static async loadPromiseHTTP(filePath: string): Promise<IZip> {\n\n // No response streaming! :(\n // https://github.com/request/request-promise/issues/90\n const needsStreamingResponse = true;\n\n return new Promise<IZip>(async (resolve, reject) => {\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const failure = (err: any) => {\n debug(err);\n reject(err);\n };\n\n const success = async (res: request.RequestResponse) => {\n if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {\n failure(\"HTTP CODE \" + res.statusCode);\n return;\n }\n\n debug(filePath);\n debug(res.headers);\n\n // if (!res.headers[\"content-type\"]\n // || res.headers[\"content-type\"] !== \"application/epub+zip\") {\n // reject(\"content-type not supported!\");\n // return;\n // }\n\n // TODO: if the HTTP server does not provide Content-Length,\n // then fallback on download, but interrupt (req.abort())\n // if response payload reaches the max limit\n if (!res.headers[\"content-length\"]) {\n reject(\"content-length not supported!\");\n return;\n }\n const httpZipByteLength = parseInt(res.headers[\"content-length\"] as string, 10);\n debug(`Content-Length: ${httpZipByteLength}`);\n\n if (!res.headers[\"accept-ranges\"]\n // Note: some servers have several headers with the same value!\n // (erm, https://raw.githubusercontent.com)\n // (comma-separated values, so we can't match \"bytes\" exactly)\n || res.headers[\"accept-ranges\"].indexOf(\"bytes\") < 0) {\n\n if (httpZipByteLength > (2 * 1024 * 1024)) {\n reject(\"accept-ranges not supported, file too big to download: \" + httpZipByteLength);\n return;\n }\n debug(\"Downloading: \" + filePath);\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const failure_ = (err: any) => {\n\n debug(err);\n reject(err);\n };\n\n const success_ = async (ress: request.RequestResponse) => {\n if (ress.statusCode && (ress.statusCode < 200 || ress.statusCode >= 300)) {\n failure_(\"HTTP CODE \" + ress.statusCode);\n return;\n }\n\n // debug(filePath);\n // debug(res.headers);\n let buffer: Buffer;\n try {\n buffer = await streamToBufferPromise(ress);\n } catch (err) {\n debug(err);\n reject(err);\n return;\n }\n\n yauzl.fromBuffer(buffer,\n { lazyEntries: true },\n (err, zip) => {\n if (err || !zip) {\n debug(\"yauzl init ERROR\");\n debug(err);\n reject(err);\n return;\n }\n const zip2 = new Zip2(filePath, zip);\n\n zip.on(\"error\", (erro) => {\n debug(\"yauzl ERROR\");\n debug(erro);\n reject(erro);\n });\n\n zip.readEntry(); // next (lazyEntries)\n zip.on(\"entry\", (entry) => {\n if (entry.fileName[entry.fileName.length - 1] === \"/\") {\n // skip directories / folders\n } else {\n // debug(entry.fileName);\n zip2.addEntry(entry);\n }\n zip.readEntry(); // next (lazyEntries)\n });\n\n zip.on(\"end\", () => {\n debug(\"yauzl END\");\n resolve(zip2);\n });\n\n zip.on(\"close\", () => {\n debug(\"yauzl CLOSE\");\n });\n });\n };\n\n if (needsStreamingResponse) {\n request.get({\n headers: {},\n method: \"GET\",\n uri: filePath,\n })\n .on(\"response\", async (res) => {\n try {\n await success_(res);\n }\n catch (successError) {\n failure_(successError);\n return;\n }\n })\n .on(\"error\", failure_);\n } else {\n let ress: requestPromise.FullResponse;\n try {\n // tslint:disable-next-line:await-promise no-floating-promises\n ress = await requestPromise({\n headers: {},\n method: \"GET\",\n resolveWithFullResponse: true,\n uri: filePath,\n });\n } catch (err) {\n failure_(err);\n return;\n }\n\n await success_(ress);\n }\n\n return;\n }\n\n const httpZipReader = new HttpZipReader(filePath, httpZipByteLength);\n yauzl.fromRandomAccessReader(httpZipReader, httpZipByteLength,\n { lazyEntries: true, autoClose: false },\n (err, zip) => {\n if (err || !zip) {\n debug(\"yauzl init ERROR\");\n debug(err);\n reject(err);\n return;\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (zip as any).httpZipReader = httpZipReader;\n const zip2 = new Zip2(filePath, zip);\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n zip.on(\"error\", (erro: any) => {\n debug(\"yauzl ERROR\");\n debug(erro);\n reject(erro);\n });\n\n zip.readEntry(); // next (lazyEntries)\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n zip.on(\"entry\", (entry: any) => {\n if (entry.fileName[entry.fileName.length - 1] === \"/\") {\n // skip directories / folders\n } else {\n // debug(entry.fileName);\n zip2.addEntry(entry);\n }\n zip.readEntry(); // next (lazyEntries)\n });\n\n zip.on(\"end\", () => {\n debug(\"yauzl END\");\n resolve(zip2);\n });\n\n zip.on(\"close\", () => {\n debug(\"yauzl CLOSE\");\n });\n });\n };\n\n if (needsStreamingResponse) {\n request.get({\n headers: {},\n method: \"HEAD\",\n uri: filePath,\n })\n .on(\"response\", async (res) => {\n try {\n await success(res);\n }\n catch (successError) {\n failure(successError);\n return;\n }\n })\n .on(\"error\", failure);\n } else {\n // TODO: instead of a HEAD request, if not supported then\n // GET with immediate req.abort() in the response callback\n let res: requestPromise.FullResponse;\n try {\n // tslint:disable-next-line:await-promise no-floating-promises\n res = await requestPromise({\n headers: {},\n method: \"HEAD\",\n resolveWithFullResponse: true,\n uri: filePath,\n });\n } catch (err) {\n failure(err);\n return;\n }\n\n await success(res);\n }\n });\n }\n\n private entries: IStringKeyedObject;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private constructor(readonly filePath: string, readonly zip: any) {\n super();\n\n // see addEntry()\n this.entries = {};\n }\n\n public freeDestroy(): void {\n debug(\"freeDestroy: Zip2 -- \" + this.filePath);\n if (this.zip) {\n this.zip.close();\n }\n }\n\n public entriesCount(): number {\n return this.zip.entryCount;\n }\n\n public hasEntries(): boolean {\n return this.entriesCount() > 0;\n }\n\n public hasEntry(entryPath: string): boolean {\n return this.hasEntries() && this.entries[entryPath];\n }\n\n public async getEntries(): Promise<string[]> {\n\n if (!this.hasEntries()) {\n return Promise.resolve([]);\n }\n return Promise.resolve(Object.keys(this.entries));\n }\n\n public async entryStreamPromise(entryPath: string): Promise<IStreamAndLength> {\n\n // debug(`entryStreamPromise: ${entryPath}`);\n\n if (!this.hasEntries() || !this.hasEntry(entryPath)) {\n return Promise.reject(\"no such path in zip: \" + entryPath);\n }\n\n const entry = this.entries[entryPath];\n\n return new Promise<IStreamAndLength>((resolve, reject) => {\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.zip.openReadStream(entry, (err: any, stream: NodeJS.ReadableStream) => {\n if (err) {\n debug(\"yauzl openReadStream ERROR\");\n debug(err);\n reject(err);\n return;\n }\n const streamAndLength: IStreamAndLength = {\n length: entry.uncompressedSize as number,\n reset: async () => {\n return this.entryStreamPromise(entryPath);\n },\n stream,\n };\n resolve(streamAndLength);\n });\n });\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private addEntry(entry: any) {\n this.entries[entry.fileName] = entry;\n }\n}\n"]}
\No newline at end of file