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