UNPKG

23.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Zip2 = void 0;
4var tslib_1 = require("tslib");
5var debug_ = require("debug");
6var request = require("request");
7var requestPromise = require("request-promise-native");
8var yauzl = require("yauzl");
9var UrlUtils_1 = require("../http/UrlUtils");
10var BufferUtils_1 = require("../stream/BufferUtils");
11var zip_1 = require("./zip");
12var zip2RandomAccessReader_Http_1 = require("./zip2RandomAccessReader_Http");
13var debug = debug_("r2:utils#zip/zip2");
14var Zip2 = (function (_super) {
15 tslib_1.__extends(Zip2, _super);
16 function Zip2(filePath, zip) {
17 var _this = _super.call(this) || this;
18 _this.filePath = filePath;
19 _this.zip = zip;
20 _this.entries = {};
21 return _this;
22 }
23 Zip2.loadPromise = function (filePath) {
24 return tslib_1.__awaiter(this, void 0, void 0, function () {
25 return tslib_1.__generator(this, function (_a) {
26 if ((0, UrlUtils_1.isHTTP)(filePath)) {
27 return [2, Zip2.loadPromiseHTTP(filePath)];
28 }
29 return [2, new Promise(function (resolve, reject) {
30 yauzl.open(filePath, { lazyEntries: true, autoClose: false }, function (err, zip) {
31 if (err || !zip) {
32 debug("yauzl init ERROR");
33 debug(err);
34 reject(err);
35 return;
36 }
37 var zip2 = new Zip2(filePath, zip);
38 zip.on("error", function (erro) {
39 debug("yauzl ERROR");
40 debug(erro);
41 reject(erro);
42 });
43 zip.readEntry();
44 zip.on("entry", function (entry) {
45 if (entry.fileName[entry.fileName.length - 1] === "/") {
46 }
47 else {
48 zip2.addEntry(entry);
49 }
50 zip.readEntry();
51 });
52 zip.on("end", function () {
53 debug("yauzl END");
54 resolve(zip2);
55 });
56 zip.on("close", function () {
57 debug("yauzl CLOSE");
58 });
59 });
60 })];
61 });
62 });
63 };
64 Zip2.loadPromiseHTTP = function (filePath) {
65 return tslib_1.__awaiter(this, void 0, void 0, function () {
66 var needsStreamingResponse;
67 var _this = this;
68 return tslib_1.__generator(this, function (_a) {
69 needsStreamingResponse = true;
70 return [2, new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
71 var failure, success, res, err_1;
72 var _this = this;
73 return tslib_1.__generator(this, function (_a) {
74 switch (_a.label) {
75 case 0:
76 failure = function (err) {
77 debug(err);
78 reject(err);
79 };
80 success = function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
81 var httpZipByteLength, failure_1, success_1, ress, err_2, httpZipReader;
82 var _this = this;
83 return tslib_1.__generator(this, function (_a) {
84 switch (_a.label) {
85 case 0:
86 if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
87 failure("HTTP CODE " + res.statusCode);
88 return [2];
89 }
90 debug(filePath);
91 debug(res.headers);
92 if (!res.headers["content-length"]) {
93 reject("content-length not supported!");
94 return [2];
95 }
96 httpZipByteLength = parseInt(res.headers["content-length"], 10);
97 debug("Content-Length: ".concat(httpZipByteLength));
98 if (!(!res.headers["accept-ranges"]
99 || res.headers["accept-ranges"].indexOf("bytes") < 0)) return [3, 8];
100 if (httpZipByteLength > (2 * 1024 * 1024)) {
101 reject("accept-ranges not supported, file too big to download: " + httpZipByteLength);
102 return [2];
103 }
104 debug("Downloading: " + filePath);
105 failure_1 = function (err) {
106 debug(err);
107 reject(err);
108 };
109 success_1 = function (ress) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
110 var buffer, err_3;
111 return tslib_1.__generator(this, function (_a) {
112 switch (_a.label) {
113 case 0:
114 if (ress.statusCode && (ress.statusCode < 200 || ress.statusCode >= 300)) {
115 failure_1("HTTP CODE " + ress.statusCode);
116 return [2];
117 }
118 _a.label = 1;
119 case 1:
120 _a.trys.push([1, 3, , 4]);
121 return [4, (0, BufferUtils_1.streamToBufferPromise)(ress)];
122 case 2:
123 buffer = _a.sent();
124 return [3, 4];
125 case 3:
126 err_3 = _a.sent();
127 debug(err_3);
128 reject(err_3);
129 return [2];
130 case 4:
131 yauzl.fromBuffer(buffer, { lazyEntries: true }, function (err, zip) {
132 if (err || !zip) {
133 debug("yauzl init ERROR");
134 debug(err);
135 reject(err);
136 return;
137 }
138 var zip2 = new Zip2(filePath, zip);
139 zip.on("error", function (erro) {
140 debug("yauzl ERROR");
141 debug(erro);
142 reject(erro);
143 });
144 zip.readEntry();
145 zip.on("entry", function (entry) {
146 if (entry.fileName[entry.fileName.length - 1] === "/") {
147 }
148 else {
149 zip2.addEntry(entry);
150 }
151 zip.readEntry();
152 });
153 zip.on("end", function () {
154 debug("yauzl END");
155 resolve(zip2);
156 });
157 zip.on("close", function () {
158 debug("yauzl CLOSE");
159 });
160 });
161 return [2];
162 }
163 });
164 }); };
165 if (!needsStreamingResponse) return [3, 1];
166 request.get({
167 headers: {},
168 method: "GET",
169 uri: filePath,
170 })
171 .on("response", function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
172 var successError_1;
173 return tslib_1.__generator(this, function (_a) {
174 switch (_a.label) {
175 case 0:
176 _a.trys.push([0, 2, , 3]);
177 return [4, success_1(res)];
178 case 1:
179 _a.sent();
180 return [3, 3];
181 case 2:
182 successError_1 = _a.sent();
183 failure_1(successError_1);
184 return [2];
185 case 3: return [2];
186 }
187 });
188 }); })
189 .on("error", failure_1);
190 return [3, 7];
191 case 1:
192 ress = void 0;
193 _a.label = 2;
194 case 2:
195 _a.trys.push([2, 4, , 5]);
196 return [4, requestPromise({
197 headers: {},
198 method: "GET",
199 resolveWithFullResponse: true,
200 uri: filePath,
201 })];
202 case 3:
203 ress = _a.sent();
204 return [3, 5];
205 case 4:
206 err_2 = _a.sent();
207 failure_1(err_2);
208 return [2];
209 case 5: return [4, success_1(ress)];
210 case 6:
211 _a.sent();
212 _a.label = 7;
213 case 7: return [2];
214 case 8:
215 httpZipReader = new zip2RandomAccessReader_Http_1.HttpZipReader(filePath, httpZipByteLength);
216 yauzl.fromRandomAccessReader(httpZipReader, httpZipByteLength, { lazyEntries: true, autoClose: false }, function (err, zip) {
217 if (err || !zip) {
218 debug("yauzl init ERROR");
219 debug(err);
220 reject(err);
221 return;
222 }
223 zip.httpZipReader = httpZipReader;
224 var zip2 = new Zip2(filePath, zip);
225 zip.on("error", function (erro) {
226 debug("yauzl ERROR");
227 debug(erro);
228 reject(erro);
229 });
230 zip.readEntry();
231 zip.on("entry", function (entry) {
232 if (entry.fileName[entry.fileName.length - 1] === "/") {
233 }
234 else {
235 zip2.addEntry(entry);
236 }
237 zip.readEntry();
238 });
239 zip.on("end", function () {
240 debug("yauzl END");
241 resolve(zip2);
242 });
243 zip.on("close", function () {
244 debug("yauzl CLOSE");
245 });
246 });
247 return [2];
248 }
249 });
250 }); };
251 if (!needsStreamingResponse) return [3, 1];
252 request.get({
253 headers: {},
254 method: "HEAD",
255 uri: filePath,
256 })
257 .on("response", function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
258 var successError_2;
259 return tslib_1.__generator(this, function (_a) {
260 switch (_a.label) {
261 case 0:
262 _a.trys.push([0, 2, , 3]);
263 return [4, success(res)];
264 case 1:
265 _a.sent();
266 return [3, 3];
267 case 2:
268 successError_2 = _a.sent();
269 failure(successError_2);
270 return [2];
271 case 3: return [2];
272 }
273 });
274 }); })
275 .on("error", failure);
276 return [3, 7];
277 case 1:
278 res = void 0;
279 _a.label = 2;
280 case 2:
281 _a.trys.push([2, 4, , 5]);
282 return [4, requestPromise({
283 headers: {},
284 method: "HEAD",
285 resolveWithFullResponse: true,
286 uri: filePath,
287 })];
288 case 3:
289 res = _a.sent();
290 return [3, 5];
291 case 4:
292 err_1 = _a.sent();
293 failure(err_1);
294 return [2];
295 case 5: return [4, success(res)];
296 case 6:
297 _a.sent();
298 _a.label = 7;
299 case 7: return [2];
300 }
301 });
302 }); })];
303 });
304 });
305 };
306 Zip2.prototype.freeDestroy = function () {
307 debug("freeDestroy: Zip2 -- " + this.filePath);
308 if (this.zip) {
309 this.zip.close();
310 }
311 };
312 Zip2.prototype.entriesCount = function () {
313 return this.zip.entryCount;
314 };
315 Zip2.prototype.hasEntries = function () {
316 return this.entriesCount() > 0;
317 };
318 Zip2.prototype.hasEntry = function (entryPath) {
319 return this.hasEntries() && this.entries[entryPath];
320 };
321 Zip2.prototype.getEntries = function () {
322 return tslib_1.__awaiter(this, void 0, void 0, function () {
323 return tslib_1.__generator(this, function (_a) {
324 if (!this.hasEntries()) {
325 return [2, Promise.resolve([])];
326 }
327 return [2, Promise.resolve(Object.keys(this.entries))];
328 });
329 });
330 };
331 Zip2.prototype.entryStreamPromise = function (entryPath) {
332 return tslib_1.__awaiter(this, void 0, void 0, function () {
333 var entry;
334 var _this = this;
335 return tslib_1.__generator(this, function (_a) {
336 if (!this.hasEntries() || !this.hasEntry(entryPath)) {
337 return [2, Promise.reject("no such path in zip: " + entryPath)];
338 }
339 entry = this.entries[entryPath];
340 return [2, new Promise(function (resolve, reject) {
341 _this.zip.openReadStream(entry, function (err, stream) {
342 if (err) {
343 debug("yauzl openReadStream ERROR");
344 debug(err);
345 reject(err);
346 return;
347 }
348 var streamAndLength = {
349 length: entry.uncompressedSize,
350 reset: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
351 return tslib_1.__generator(this, function (_a) {
352 return [2, this.entryStreamPromise(entryPath)];
353 });
354 }); },
355 stream: stream,
356 };
357 resolve(streamAndLength);
358 });
359 })];
360 });
361 });
362 };
363 Zip2.prototype.addEntry = function (entry) {
364 this.entries[entry.fileName] = entry;
365 };
366 return Zip2;
367}(zip_1.Zip));
368exports.Zip2 = Zip2;
369//# sourceMappingURL=zip2.js.map
\No newline at end of file