UNPKG

5.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.HttpZipReader = void 0;
4var tslib_1 = require("tslib");
5var debug_ = require("debug");
6var request = require("request");
7var requestPromise = require("request-promise-native");
8var stream_1 = require("stream");
9var yauzl = require("yauzl");
10var BufferUtils_1 = require("../stream/BufferUtils");
11var debug = debug_("r2:utils#zip/zip2RandomAccessReader_Http");
12var HttpZipReader = (function (_super) {
13 tslib_1.__extends(HttpZipReader, _super);
14 function HttpZipReader(url, byteLength) {
15 var _this = _super.call(this) || this;
16 _this.url = url;
17 _this.byteLength = byteLength;
18 _this.firstBuffer = undefined;
19 _this.firstBufferStart = 0;
20 _this.firstBufferEnd = 0;
21 return _this;
22 }
23 HttpZipReader.prototype._readStreamForRange = function (start, end) {
24 var _this = this;
25 if (this.firstBuffer && start >= this.firstBufferStart && end <= this.firstBufferEnd) {
26 var begin = start - this.firstBufferStart;
27 var stop_1 = end - this.firstBufferStart;
28 return (0, BufferUtils_1.bufferToStream)(this.firstBuffer.slice(begin, stop_1));
29 }
30 var stream = new stream_1.PassThrough();
31 var lastByteIndex = end - 1;
32 var range = "".concat(start, "-").concat(lastByteIndex);
33 var failure = function (err) {
34 debug(err);
35 };
36 var success = function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
37 var buffer, err_1;
38 return tslib_1.__generator(this, function (_a) {
39 switch (_a.label) {
40 case 0:
41 if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
42 failure("HTTP CODE " + res.statusCode);
43 return [2];
44 }
45 if (!this.firstBuffer) return [3, 1];
46 res.pipe(stream);
47 return [3, 6];
48 case 1:
49 buffer = void 0;
50 _a.label = 2;
51 case 2:
52 _a.trys.push([2, 4, , 5]);
53 return [4, (0, BufferUtils_1.streamToBufferPromise)(res)];
54 case 3:
55 buffer = _a.sent();
56 return [3, 5];
57 case 4:
58 err_1 = _a.sent();
59 debug(err_1);
60 stream.end();
61 return [2];
62 case 5:
63 debug("streamToBufferPromise: ".concat(buffer.length));
64 this.firstBuffer = buffer;
65 this.firstBufferStart = start;
66 this.firstBufferEnd = end;
67 stream.write(buffer);
68 stream.end();
69 _a.label = 6;
70 case 6: return [2];
71 }
72 });
73 }); };
74 var needsStreamingResponse = true;
75 if (needsStreamingResponse) {
76 request.get({
77 headers: { Range: "bytes=".concat(range) },
78 method: "GET",
79 uri: this.url,
80 })
81 .on("response", function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
82 var successError_1;
83 return tslib_1.__generator(this, function (_a) {
84 switch (_a.label) {
85 case 0:
86 _a.trys.push([0, 2, , 3]);
87 return [4, success(res)];
88 case 1:
89 _a.sent();
90 return [3, 3];
91 case 2:
92 successError_1 = _a.sent();
93 failure(successError_1);
94 return [2];
95 case 3: return [2];
96 }
97 });
98 }); })
99 .on("error", failure);
100 }
101 else {
102 (function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
103 var res, err_2;
104 return tslib_1.__generator(this, function (_a) {
105 switch (_a.label) {
106 case 0:
107 _a.trys.push([0, 2, , 3]);
108 return [4, requestPromise({
109 headers: { Range: "bytes=".concat(range) },
110 method: "GET",
111 resolveWithFullResponse: true,
112 uri: this.url,
113 })];
114 case 1:
115 res = _a.sent();
116 return [3, 3];
117 case 2:
118 err_2 = _a.sent();
119 failure(err_2);
120 return [2];
121 case 3: return [4, success(res)];
122 case 4:
123 _a.sent();
124 return [2];
125 }
126 });
127 }); })();
128 }
129 return stream;
130 };
131 return HttpZipReader;
132}(yauzl.RandomAccessReader));
133exports.HttpZipReader = HttpZipReader;
134//# sourceMappingURL=zip2RandomAccessReader_Http.js.map
\No newline at end of file