@e-mc/request 0.9.26 → 0.9.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/index.js +10 -8
- package/package.json +3 -3
- package/http/adapter/index.js +0 -587
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.28/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -202,9 +202,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
|
|
|
202
202
|
|
|
203
203
|
## References
|
|
204
204
|
|
|
205
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
206
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
207
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
205
|
+
- https://www.unpkg.com/@e-mc/types@0.9.28/lib/http.d.ts
|
|
206
|
+
- https://www.unpkg.com/@e-mc/types@0.9.28/lib/request.d.ts
|
|
207
|
+
- https://www.unpkg.com/@e-mc/types@0.9.28/lib/settings.d.ts
|
|
208
208
|
|
|
209
209
|
* https://www.npmjs.com/package/@types/node
|
|
210
210
|
|
package/index.js
CHANGED
|
@@ -156,11 +156,17 @@ function getProxySettings(request, agentTimeout) {
|
|
|
156
156
|
return null;
|
|
157
157
|
}
|
|
158
158
|
function closeTorrent(pid) {
|
|
159
|
-
const index = ARIA2.PID_QUEUE.findIndex(value =>
|
|
159
|
+
const index = ARIA2.PID_QUEUE.findIndex(value => value[0] === pid);
|
|
160
160
|
if (index !== -1) {
|
|
161
161
|
ARIA2.PID_QUEUE.splice(index, 1);
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
+
function resetAria2() {
|
|
165
|
+
if (ARIA2.PID_TIMER) {
|
|
166
|
+
clearInterval(ARIA2.PID_TIMER);
|
|
167
|
+
ARIA2.PID_TIMER = null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
164
170
|
function clearDnsLookup() {
|
|
165
171
|
DNS.CACHE = Object.create(null);
|
|
166
172
|
DNS.TIMEOUT.forEach(value => clearTimeout(value));
|
|
@@ -178,7 +184,7 @@ function resetHttpHost(version) {
|
|
|
178
184
|
const host = HTTP.HOST[origin];
|
|
179
185
|
if (host.secure && host.version === 1) {
|
|
180
186
|
const failed = host.failed(2, true);
|
|
181
|
-
if (failed === 0 && host.
|
|
187
|
+
if (failed === 0 && host.error(2, true) < 10 || failed < 3 && host.success(2, true) > 0) {
|
|
182
188
|
host.version = version;
|
|
183
189
|
}
|
|
184
190
|
}
|
|
@@ -1366,13 +1372,9 @@ class Request extends module_1 {
|
|
|
1366
1372
|
stderr.setEncoding('utf-8').on('data', (value) => message += value);
|
|
1367
1373
|
if (pid !== undefined && module_1.isFile(uri, 'torrent')) {
|
|
1368
1374
|
if (!ARIA2.PID_TIMER) {
|
|
1369
|
-
const clearTimer = () => {
|
|
1370
|
-
clearInterval(ARIA2.PID_TIMER);
|
|
1371
|
-
ARIA2.PID_TIMER = null;
|
|
1372
|
-
};
|
|
1373
1375
|
ARIA2.PID_TIMER = setInterval(() => {
|
|
1374
1376
|
if (ARIA2.PID_QUEUE.length === 0) {
|
|
1375
|
-
|
|
1377
|
+
resetAria2();
|
|
1376
1378
|
return;
|
|
1377
1379
|
}
|
|
1378
1380
|
for (const item of ARIA2.PID_QUEUE) {
|
|
@@ -1392,7 +1394,7 @@ class Request extends module_1 {
|
|
|
1392
1394
|
if (ARIA2.UPDATE_STATUS && !silent) {
|
|
1393
1395
|
const item = ARIA2.PID_QUEUE.shift();
|
|
1394
1396
|
if (!item) {
|
|
1395
|
-
|
|
1397
|
+
resetAria2();
|
|
1396
1398
|
return;
|
|
1397
1399
|
}
|
|
1398
1400
|
if ((!ARIA2.UPDATE_BROADCAST_ONLY || item[2]) && this.host?.logState !== 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.28",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.9.
|
|
24
|
-
"@e-mc/types": "0.9.
|
|
23
|
+
"@e-mc/module": "0.9.28",
|
|
24
|
+
"@e-mc/types": "0.9.28",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.1",
|
|
27
27
|
"picomatch": "^4.0.3",
|
package/http/adapter/index.js
DELETED
|
@@ -1,587 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const fs = require("node:fs");
|
|
3
|
-
const http2 = require("node:http2");
|
|
4
|
-
const yaml = require("js-yaml");
|
|
5
|
-
const types_1 = require("@e-mc/types");
|
|
6
|
-
const module_1 = require("@e-mc/module");
|
|
7
|
-
const util_1 = require("@e-mc/request/util");
|
|
8
|
-
const kHttpAdapter = Symbol.for('request:http:adapter:constructor');
|
|
9
|
-
let LOG_TIMEFORMAT = 'readable';
|
|
10
|
-
const isConstructor = (value) => typeof value === 'function' && !!value.prototype?.constructor.name;
|
|
11
|
-
class HttpAdapter {
|
|
12
|
-
instance;
|
|
13
|
-
state;
|
|
14
|
-
uri;
|
|
15
|
-
static [kHttpAdapter] = true;
|
|
16
|
-
static constructorOf(value) {
|
|
17
|
-
return isConstructor(value) && value[kHttpAdapter] === true;
|
|
18
|
-
}
|
|
19
|
-
static isUnsupported(value) {
|
|
20
|
-
return value === 421 || value === 505;
|
|
21
|
-
}
|
|
22
|
-
static isDowngrade(err) {
|
|
23
|
-
return (0, types_1.isErrorCode)(err, 'ERR_HTTP2_ERROR') || err instanceof Error && this.isUnsupported(Math.abs(err.errno));
|
|
24
|
-
}
|
|
25
|
-
static wasAborted(err) {
|
|
26
|
-
return err instanceof Error && err.message.startsWith("Aborted");
|
|
27
|
-
}
|
|
28
|
-
static isConnectionError(err) {
|
|
29
|
-
return (0, types_1.isErrorCode)(err, 'ETIMEDOUT', 'ECONNRESET');
|
|
30
|
-
}
|
|
31
|
-
static defineHostConfig({ settings }) {
|
|
32
|
-
const time_format = settings?.time_format;
|
|
33
|
-
switch (time_format) {
|
|
34
|
-
case 'readable':
|
|
35
|
-
case 'relative':
|
|
36
|
-
case 'none':
|
|
37
|
-
LOG_TIMEFORMAT = time_format;
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
contentLength = 0;
|
|
42
|
-
retries = 0;
|
|
43
|
-
redirects = 0;
|
|
44
|
-
closed = false;
|
|
45
|
-
aborted = false;
|
|
46
|
-
timeout = null;
|
|
47
|
-
dataTime = null;
|
|
48
|
-
delayTime = undefined;
|
|
49
|
-
opts;
|
|
50
|
-
client;
|
|
51
|
-
resolve;
|
|
52
|
-
reject;
|
|
53
|
-
startTime;
|
|
54
|
-
#outStream = null;
|
|
55
|
-
#options;
|
|
56
|
-
constructor(instance, state, uri, options) {
|
|
57
|
-
this.instance = instance;
|
|
58
|
-
this.state = state;
|
|
59
|
-
this.uri = uri;
|
|
60
|
-
this.#options = options;
|
|
61
|
-
this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
|
|
62
|
-
this.setOpts();
|
|
63
|
-
}
|
|
64
|
-
async start() {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
this.resolve = resolve;
|
|
67
|
-
this.reject = reject;
|
|
68
|
-
this.init();
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
init() {
|
|
72
|
-
this.aborted = false;
|
|
73
|
-
this.setWriteStream();
|
|
74
|
-
this.client = this.instance.open(this.uri, this.opts);
|
|
75
|
-
if (this.opts.httpVersion === 2) {
|
|
76
|
-
this.client
|
|
77
|
-
.on('response', (headers, flags) => {
|
|
78
|
-
if (this.destroyed) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const statusCode = headers[':status'];
|
|
82
|
-
if (statusCode === 204 || statusCode === 304) {
|
|
83
|
-
this.acceptResponse(headers, true);
|
|
84
|
-
}
|
|
85
|
-
else if (statusCode < 300) {
|
|
86
|
-
this.acceptResponse(headers, false);
|
|
87
|
-
}
|
|
88
|
-
else if (statusCode === 301 ||
|
|
89
|
-
statusCode === 302 ||
|
|
90
|
-
statusCode === 303 ||
|
|
91
|
-
statusCode === 307 ||
|
|
92
|
-
statusCode === 308) {
|
|
93
|
-
this.redirectResponse(statusCode, headers.location);
|
|
94
|
-
}
|
|
95
|
-
else if (statusCode === 305 ||
|
|
96
|
-
statusCode === 401 ||
|
|
97
|
-
statusCode === 402 ||
|
|
98
|
-
statusCode === 403 ||
|
|
99
|
-
statusCode === 404 ||
|
|
100
|
-
statusCode === 407 ||
|
|
101
|
-
statusCode === 410) {
|
|
102
|
-
this.terminate(this.formatStatus(statusCode));
|
|
103
|
-
}
|
|
104
|
-
else if (this.isRetry(statusCode)) {
|
|
105
|
-
this.retryResponse(statusCode, headers['retry-after']);
|
|
106
|
-
}
|
|
107
|
-
else if (HttpAdapter.isUnsupported(statusCode)) {
|
|
108
|
-
this.retryDownload(true, this.#formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
switch (flags) {
|
|
112
|
-
case http2.constants.NGHTTP2_PROTOCOL_ERROR:
|
|
113
|
-
case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
|
|
114
|
-
case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
|
|
115
|
-
this.retryDownload(true, this.#formatNgFlags(flags, statusCode, headers.location));
|
|
116
|
-
break;
|
|
117
|
-
default:
|
|
118
|
-
this.retryDownload(false, this.formatStatus(statusCode));
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
})
|
|
123
|
-
.on('unknownProtocol', () => {
|
|
124
|
-
if (this.aborted) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
this.retryDownload(true, 'Unknown protocol (HTTP/2)');
|
|
128
|
-
})
|
|
129
|
-
.on('aborted', () => {
|
|
130
|
-
this.aborted = true;
|
|
131
|
-
this.terminate((0, types_1.createAbortError)());
|
|
132
|
-
})
|
|
133
|
-
.on('error', async (err) => {
|
|
134
|
-
if (this.aborted) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
if (HttpAdapter.wasAborted(err)) {
|
|
138
|
-
this.errorResponse(err);
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
switch (!HttpAdapter.isDowngrade(err) && await this.host.hasProtocol(2)) {
|
|
142
|
-
case 1:
|
|
143
|
-
this.errorResponse(err);
|
|
144
|
-
break;
|
|
145
|
-
case 2:
|
|
146
|
-
this.retryDownload(false, err);
|
|
147
|
-
break;
|
|
148
|
-
default:
|
|
149
|
-
this.retryDownload(true, err);
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
this.client
|
|
157
|
-
.on('response', res => {
|
|
158
|
-
if (this.destroyed) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const statusCode = res.statusCode;
|
|
162
|
-
if (statusCode === 204 || statusCode === 304) {
|
|
163
|
-
this.acceptResponse(res.headers, true);
|
|
164
|
-
}
|
|
165
|
-
else if (statusCode < 300) {
|
|
166
|
-
this.acceptResponse(res.headers, false);
|
|
167
|
-
}
|
|
168
|
-
else if (statusCode === 301 ||
|
|
169
|
-
statusCode === 302 ||
|
|
170
|
-
statusCode === 303 ||
|
|
171
|
-
statusCode === 307 ||
|
|
172
|
-
statusCode === 308) {
|
|
173
|
-
this.redirectResponse(statusCode, res.headers.location);
|
|
174
|
-
}
|
|
175
|
-
else if (this.isRetry(statusCode)) {
|
|
176
|
-
this.retryResponse(statusCode, res.headers['retry-after']);
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
this.terminate(this.formatStatus(statusCode));
|
|
180
|
-
}
|
|
181
|
-
})
|
|
182
|
-
.on('abort', () => {
|
|
183
|
-
this.aborted = true;
|
|
184
|
-
this.terminate((0, types_1.createAbortError)());
|
|
185
|
-
})
|
|
186
|
-
.on('error', err => {
|
|
187
|
-
if (this.aborted) {
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
this.errorResponse(err);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
this.client.on('timeout', () => {
|
|
194
|
-
if (this.aborted) {
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
if (++this.retries <= this.retryLimit) {
|
|
198
|
-
this.abortResponse();
|
|
199
|
-
this.retryTimeout();
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
this.terminate(this.formatStatus(408));
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
setOpts(uri) {
|
|
207
|
-
if (uri) {
|
|
208
|
-
this.uri = uri;
|
|
209
|
-
}
|
|
210
|
-
this.opts = this.instance.opts(this.uri, this.#options);
|
|
211
|
-
}
|
|
212
|
-
setWriteStream() {
|
|
213
|
-
const pipeTo = this.pipeTo;
|
|
214
|
-
if (typeof pipeTo === 'string') {
|
|
215
|
-
try {
|
|
216
|
-
this.outStream = fs.createWriteStream(pipeTo, { emitClose: false, highWaterMark: this.host.streamSize });
|
|
217
|
-
}
|
|
218
|
-
catch (err) {
|
|
219
|
-
this.terminate(err);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
else if (pipeTo) {
|
|
223
|
-
this.outStream = pipeTo;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
retryDownload(downgrade, message) {
|
|
227
|
-
if (this.aborted) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
this.abortResponse();
|
|
231
|
-
if (downgrade) {
|
|
232
|
-
const host = this.host;
|
|
233
|
-
host.failed(2);
|
|
234
|
-
if (host.version > 1) {
|
|
235
|
-
if (this.state.verbose) {
|
|
236
|
-
this.instance.formatMessage(1024, 'HTTP2', ["Unsupported protocol", host.origin], message, { failed: true });
|
|
237
|
-
}
|
|
238
|
-
host.version = 1;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
this.opts.httpVersion = 1;
|
|
242
|
-
this.init();
|
|
243
|
-
}
|
|
244
|
-
acceptResponse(headers, empty = false) {
|
|
245
|
-
const opts = this.opts;
|
|
246
|
-
if ('outHeaders' in opts) {
|
|
247
|
-
opts.outHeaders = headers;
|
|
248
|
-
}
|
|
249
|
-
if ('outFilename' in opts) {
|
|
250
|
-
opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
|
|
251
|
-
}
|
|
252
|
-
const pipeline = this.pipeTo ? !(0, types_1.isString)(this.pipeTo) : false;
|
|
253
|
-
const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
|
|
254
|
-
const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
|
|
255
|
-
this.contentLength = parseInt(headers['content-length'] || '0');
|
|
256
|
-
const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0 && !empty;
|
|
257
|
-
const readTimeout = this.instance.readTimeout;
|
|
258
|
-
let log = this.state.log, buffer = null, dataLength = 0;
|
|
259
|
-
this.client.once('readable', () => {
|
|
260
|
-
if (readTimeout > 0) {
|
|
261
|
-
this.timeout = setTimeout(() => {
|
|
262
|
-
this.terminate((0, types_1.errorValue)("Timeout was exceeded", this.uri.toString()));
|
|
263
|
-
}, readTimeout);
|
|
264
|
-
}
|
|
265
|
-
if (log) {
|
|
266
|
-
switch (this.instance.settings?.time_format || LOG_TIMEFORMAT) {
|
|
267
|
-
case 'readable':
|
|
268
|
-
this.delayTime = process.hrtime.bigint() - this.startTime;
|
|
269
|
-
break;
|
|
270
|
-
case 'relative':
|
|
271
|
-
this.delayTime = Date.now() - this.instance.startTime;
|
|
272
|
-
break;
|
|
273
|
-
case 'none':
|
|
274
|
-
if (!this.silent) {
|
|
275
|
-
log = false;
|
|
276
|
-
}
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
this.dataTime = process.hrtime.bigint();
|
|
281
|
-
if (updating) {
|
|
282
|
-
this.updateProgress(0, 0);
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
if (enabled && !empty) {
|
|
286
|
-
const encoding = opts.encoding;
|
|
287
|
-
this.client.on('data', (chunk) => {
|
|
288
|
-
if (buffer) {
|
|
289
|
-
if (typeof buffer === 'string') {
|
|
290
|
-
buffer += typeof chunk === 'string' ? chunk : chunk.toString(encoding);
|
|
291
|
-
}
|
|
292
|
-
else if (Array.isArray(buffer)) {
|
|
293
|
-
buffer.push(typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk);
|
|
294
|
-
}
|
|
295
|
-
else {
|
|
296
|
-
buffer = Buffer.concat([buffer, typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk]);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
buffer = typeof chunk === 'string' ? chunk : [chunk];
|
|
301
|
-
}
|
|
302
|
-
if (updating) {
|
|
303
|
-
dataLength += Buffer.byteLength(chunk, encoding);
|
|
304
|
-
this.updateProgress(dataLength, this.contentLength);
|
|
305
|
-
}
|
|
306
|
-
if (maxBufferSize > 0) {
|
|
307
|
-
if (!updating) {
|
|
308
|
-
dataLength += Buffer.byteLength(chunk, encoding);
|
|
309
|
-
}
|
|
310
|
-
if (dataLength > maxBufferSize) {
|
|
311
|
-
this.terminate((0, types_1.errorValue)("Size limit was exceeded", this.uri.toString()));
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
if (opts.trailers) {
|
|
317
|
-
this.client.once('trailers', (trailers) => {
|
|
318
|
-
opts.trailers.call(this.client, trailers);
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
this.client.once('end', () => {
|
|
322
|
-
if (this.closed || this.aborted) {
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
this.close();
|
|
326
|
-
const encoding = opts.encoding;
|
|
327
|
-
let result;
|
|
328
|
-
if (buffer) {
|
|
329
|
-
if (Array.isArray(buffer)) {
|
|
330
|
-
buffer = Buffer.concat(buffer);
|
|
331
|
-
if (encoding) {
|
|
332
|
-
buffer = buffer.toString(encoding);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
dataLength = Buffer.byteLength(buffer, encoding);
|
|
336
|
-
if (updating) {
|
|
337
|
-
this.updateProgress(dataLength, dataLength);
|
|
338
|
-
}
|
|
339
|
-
if (typeof buffer === 'string') {
|
|
340
|
-
if (buffer.startsWith('\uFEFF') && encoding !== 'utf16le' && encoding !== 'utf-16le') {
|
|
341
|
-
buffer = buffer.substring(1);
|
|
342
|
-
}
|
|
343
|
-
if (opts.outFormat) {
|
|
344
|
-
const { out: format, parser } = opts.outFormat;
|
|
345
|
-
let packageName;
|
|
346
|
-
try {
|
|
347
|
-
switch (format) {
|
|
348
|
-
case 'yaml':
|
|
349
|
-
result = yaml.load(buffer, parser);
|
|
350
|
-
break;
|
|
351
|
-
case 'json5':
|
|
352
|
-
result = require(packageName = 'json5').parse(buffer);
|
|
353
|
-
break;
|
|
354
|
-
case 'xml':
|
|
355
|
-
result = new (require(packageName = 'fast-xml-parser').XMLParser)(parser).parse(buffer);
|
|
356
|
-
break;
|
|
357
|
-
case 'toml':
|
|
358
|
-
result = require(packageName = 'toml').parse(buffer);
|
|
359
|
-
break;
|
|
360
|
-
default:
|
|
361
|
-
result = JSON.parse(buffer);
|
|
362
|
-
break;
|
|
363
|
-
}
|
|
364
|
-
if (!(0, types_1.isObject)(result)) {
|
|
365
|
-
result = null;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
catch (err) {
|
|
369
|
-
if (this.state.verbose && !(packageName && this.instance.checkPackage(err, packageName))) {
|
|
370
|
-
this.instance.writeFail(["Unable to parse URI response", format], err, 1024);
|
|
371
|
-
}
|
|
372
|
-
result = null;
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
if (result === undefined) {
|
|
377
|
-
result = buffer;
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
if (updating) {
|
|
382
|
-
this.updateProgress(0, this.contentLength);
|
|
383
|
-
}
|
|
384
|
-
if (enabled && this.instance.readExpect === 'always' && !empty) {
|
|
385
|
-
this.terminate("No data received");
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
dataLength = this.contentLength || (typeof this.pipeTo === 'string' ? (0, util_1.getSize)(this.pipeTo) : 0);
|
|
389
|
-
result = encoding && !pipeline ? '' : null;
|
|
390
|
-
}
|
|
391
|
-
this.endResponse(result, dataLength, log);
|
|
392
|
-
});
|
|
393
|
-
this.host.success(this.httpVersion);
|
|
394
|
-
}
|
|
395
|
-
updateProgress(dataLength, contentLength) {
|
|
396
|
-
const { host, moduleName } = this.instance;
|
|
397
|
-
host.updateProgress(moduleName, this.opts.progressId, dataLength, contentLength, this.dataTime);
|
|
398
|
-
}
|
|
399
|
-
endResponse(result, dataLength = 0, logging = this.state.log) {
|
|
400
|
-
if (logging) {
|
|
401
|
-
this.instance.writeTimeProcess('HTTP' + this.httpVersion, this.opts.statusMessage || this.uri.toString(), this.startTime, {
|
|
402
|
-
type: 1024,
|
|
403
|
-
queue: !!this.instance.host,
|
|
404
|
-
titleBgColor: !result ? 'bgBlue' : undefined,
|
|
405
|
-
messageUnit: this.formatMibs(dataLength),
|
|
406
|
-
messageUnitMinWidth: 9,
|
|
407
|
-
delayTime: this.delayTime,
|
|
408
|
-
bypassLog: module_1.hasLogType(32768)
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
this.resolve(result);
|
|
412
|
-
}
|
|
413
|
-
redirectResponse(statusCode, location) {
|
|
414
|
-
if (location) {
|
|
415
|
-
if (this.opts.followRedirect === false) {
|
|
416
|
-
this.terminate(this.formatStatus(statusCode, "Follow redirect was disabled"));
|
|
417
|
-
}
|
|
418
|
-
else if (++this.redirects <= this.redirectLimit) {
|
|
419
|
-
this.abortResponse();
|
|
420
|
-
if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
|
|
421
|
-
this.#options.method = 'GET';
|
|
422
|
-
}
|
|
423
|
-
this.setOpts((0, util_1.fromURL)(this.opts.url, location));
|
|
424
|
-
this.init();
|
|
425
|
-
}
|
|
426
|
-
else {
|
|
427
|
-
this.terminate(this.formatStatus(statusCode, "Exceeded redirect limit"));
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
else {
|
|
431
|
-
this.terminate(this.formatStatus(statusCode, "Missing redirect location"));
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
abortResponse() {
|
|
435
|
-
if (this.closed) {
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
this.aborted = true;
|
|
439
|
-
this.client.destroy();
|
|
440
|
-
this.instance.reset(this);
|
|
441
|
-
this.cleanup();
|
|
442
|
-
}
|
|
443
|
-
errorResponse(err) {
|
|
444
|
-
if (HttpAdapter.wasAborted(err)) {
|
|
445
|
-
this.terminate(err);
|
|
446
|
-
}
|
|
447
|
-
else if ((0, util_1.checkRetryable)(err) && ++this.retries <= this.retryLimit) {
|
|
448
|
-
this.abortResponse();
|
|
449
|
-
if (HttpAdapter.isConnectionError(err)) {
|
|
450
|
-
this.retryTimeout();
|
|
451
|
-
}
|
|
452
|
-
else {
|
|
453
|
-
setTimeout(() => {
|
|
454
|
-
this.init();
|
|
455
|
-
}, this.retryWait);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
else {
|
|
459
|
-
this.host.error(this.httpVersion);
|
|
460
|
-
this.terminate(err);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
retryResponse(statusCode, retryAfter) {
|
|
464
|
-
this.abortResponse();
|
|
465
|
-
if (retryAfter && this.retryAfter > 0) {
|
|
466
|
-
let offset = +retryAfter || new Date(retryAfter);
|
|
467
|
-
if (offset instanceof Date) {
|
|
468
|
-
offset = Math.max(0, offset.getTime() - Date.now());
|
|
469
|
-
}
|
|
470
|
-
else {
|
|
471
|
-
offset *= 1000;
|
|
472
|
-
}
|
|
473
|
-
if (offset > 0) {
|
|
474
|
-
if (offset <= this.retryAfter) {
|
|
475
|
-
this.sendWarning(`Retry After (${retryAfter})`);
|
|
476
|
-
setTimeout(() => {
|
|
477
|
-
this.init();
|
|
478
|
-
}, offset);
|
|
479
|
-
}
|
|
480
|
-
else {
|
|
481
|
-
this.terminate(this.formatStatus(statusCode));
|
|
482
|
-
}
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
|
|
487
|
-
if ((0, util_1.isRetryable)(statusCode, true)) {
|
|
488
|
-
setImmediate(this.init.bind(this));
|
|
489
|
-
}
|
|
490
|
-
else {
|
|
491
|
-
setTimeout(() => {
|
|
492
|
-
this.init();
|
|
493
|
-
}, this.retryWait);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
isRetry(value) {
|
|
497
|
-
return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
|
|
498
|
-
}
|
|
499
|
-
retryTimeout() {
|
|
500
|
-
this.sendWarning(this.#formatRetry("HTTP connection timeout"));
|
|
501
|
-
this.init();
|
|
502
|
-
}
|
|
503
|
-
terminate(err) {
|
|
504
|
-
if (this.closed) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
this.cleanup();
|
|
508
|
-
this.close();
|
|
509
|
-
this.reject(typeof err === 'string' ? new Error(err) : err);
|
|
510
|
-
}
|
|
511
|
-
sendWarning(message) {
|
|
512
|
-
if (this.state.verbose) {
|
|
513
|
-
const { host, url } = this.opts;
|
|
514
|
-
this.instance.formatMessage(1024, 'HTTP' + this.httpVersion, [message, host.origin], url.toString(), { ...module_1.LOG_STYLE_WARN });
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
formatStatus(value, hint) {
|
|
518
|
-
return value + ': ' + (hint || (0, util_1.fromStatusCode)(value)) + ` (${this.uri.toString()})`;
|
|
519
|
-
}
|
|
520
|
-
formatMibs(dataLength) {
|
|
521
|
-
if (dataLength > 0 && this.dataTime) {
|
|
522
|
-
return (0, util_1.getTransferRate)(dataLength, Math.max(1, (0, types_1.convertTime)(process.hrtime.bigint() - this.dataTime, false) * 1000));
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
close() {
|
|
526
|
-
this.closed = true;
|
|
527
|
-
this.instance.reset(this);
|
|
528
|
-
if (this.aborted && !this.client.aborted) {
|
|
529
|
-
this.abortController?.abort();
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
cleanup() {
|
|
533
|
-
if ((0, types_1.isString)(this.pipeTo) && this.outStream) {
|
|
534
|
-
(0, util_1.cleanupStream)(this.outStream, this.pipeTo);
|
|
535
|
-
this.outStream = null;
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
#formatNgFlags(value, statusCode, location) {
|
|
539
|
-
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
540
|
-
}
|
|
541
|
-
#formatRetry(message) {
|
|
542
|
-
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
543
|
-
}
|
|
544
|
-
set abortController(value) {
|
|
545
|
-
this.opts.outAbort = value;
|
|
546
|
-
}
|
|
547
|
-
get abortController() {
|
|
548
|
-
return this.opts.outAbort || null;
|
|
549
|
-
}
|
|
550
|
-
set outStream(value) {
|
|
551
|
-
this.#outStream = value;
|
|
552
|
-
if (value) {
|
|
553
|
-
this.opts.outStream = value;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
get outStream() {
|
|
557
|
-
return this.#outStream;
|
|
558
|
-
}
|
|
559
|
-
get destroyed() {
|
|
560
|
-
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
|
561
|
-
}
|
|
562
|
-
get host() {
|
|
563
|
-
return this.opts.host;
|
|
564
|
-
}
|
|
565
|
-
get httpVersion() {
|
|
566
|
-
return this.opts.httpVersion;
|
|
567
|
-
}
|
|
568
|
-
get pipeTo() {
|
|
569
|
-
return this.opts.pipeTo;
|
|
570
|
-
}
|
|
571
|
-
get silent() {
|
|
572
|
-
return this.opts.silent === false;
|
|
573
|
-
}
|
|
574
|
-
get retryLimit() {
|
|
575
|
-
return this.state.config.retryLimit;
|
|
576
|
-
}
|
|
577
|
-
get retryWait() {
|
|
578
|
-
return this.state.config.retryWait;
|
|
579
|
-
}
|
|
580
|
-
get retryAfter() {
|
|
581
|
-
return this.state.config.retryAfter;
|
|
582
|
-
}
|
|
583
|
-
get redirectLimit() {
|
|
584
|
-
return this.state.config.redirectLimit;
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
module.exports = HttpAdapter;
|