@e-mc/request 0.8.28 → 0.8.29
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/http/adapter/index.js +592 -0
- package/http/host/altsvc/index.js +92 -0
- package/index.js +11 -9
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
12
|
+
- https://www.unpkg.com/@e-mc/types@0.8.29/lib/index.d.ts
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -82,9 +82,9 @@ interface RequestConstructor extends ModuleConstructor {
|
|
|
82
82
|
|
|
83
83
|
## References
|
|
84
84
|
|
|
85
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
86
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
87
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
85
|
+
- https://www.unpkg.com/@e-mc/types@0.8.29/lib/http.d.ts
|
|
86
|
+
- https://www.unpkg.com/@e-mc/types@0.8.29/lib/request.d.ts
|
|
87
|
+
- https://www.unpkg.com/@e-mc/types@0.8.29/lib/settings.d.ts
|
|
88
88
|
|
|
89
89
|
## LICENSE
|
|
90
90
|
|
|
@@ -0,0 +1,592 @@
|
|
|
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
|
+
try {
|
|
359
|
+
result = require('smol-toml').parse(buffer);
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
result = require(packageName = 'toml').parse(buffer);
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
default:
|
|
366
|
+
result = JSON.parse(buffer);
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
if (!(0, types_1.isObject)(result)) {
|
|
370
|
+
result = null;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
if (this.state.verbose && !(packageName && this.instance.checkPackage(err, packageName))) {
|
|
375
|
+
this.instance.writeFail(["Unable to parse URI response", format], err, 1024);
|
|
376
|
+
}
|
|
377
|
+
result = null;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (result === undefined) {
|
|
382
|
+
result = buffer;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
if (updating) {
|
|
387
|
+
this.updateProgress(0, this.contentLength);
|
|
388
|
+
}
|
|
389
|
+
if (enabled && this.instance.readExpect === 'always' && !empty) {
|
|
390
|
+
this.terminate("No data received");
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
dataLength = this.contentLength || (typeof this.pipeTo === 'string' ? (0, util_1.getSize)(this.pipeTo) : 0);
|
|
394
|
+
result = encoding && !pipeline ? '' : null;
|
|
395
|
+
}
|
|
396
|
+
this.endResponse(result, dataLength, log);
|
|
397
|
+
});
|
|
398
|
+
this.host.success(this.httpVersion);
|
|
399
|
+
}
|
|
400
|
+
updateProgress(dataLength, contentLength) {
|
|
401
|
+
const { host, moduleName } = this.instance;
|
|
402
|
+
host.updateProgress(moduleName, this.opts.progressId, dataLength, contentLength, this.dataTime);
|
|
403
|
+
}
|
|
404
|
+
endResponse(result, dataLength = 0, logging = this.state.log) {
|
|
405
|
+
if (logging) {
|
|
406
|
+
this.instance.writeTimeProcess('HTTP' + this.httpVersion, this.opts.statusMessage || this.uri.toString(), this.startTime, {
|
|
407
|
+
type: 1024,
|
|
408
|
+
queue: !!this.instance.host,
|
|
409
|
+
titleBgColor: !result ? 'bgBlue' : undefined,
|
|
410
|
+
messageUnit: this.formatMibs(dataLength),
|
|
411
|
+
messageUnitMinWidth: 9,
|
|
412
|
+
delayTime: this.delayTime,
|
|
413
|
+
bypassLog: module_1.hasLogType(32768)
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
this.resolve(result);
|
|
417
|
+
}
|
|
418
|
+
redirectResponse(statusCode, location) {
|
|
419
|
+
if (location) {
|
|
420
|
+
if (this.opts.followRedirect === false) {
|
|
421
|
+
this.terminate(this.formatStatus(statusCode, "Follow redirect was disabled"));
|
|
422
|
+
}
|
|
423
|
+
else if (++this.redirects <= this.redirectLimit) {
|
|
424
|
+
this.abortResponse();
|
|
425
|
+
if (this.opts.method?.toUpperCase() === 'PUT' && statusCode !== 307 && statusCode !== 308) {
|
|
426
|
+
this.#options.method = 'GET';
|
|
427
|
+
}
|
|
428
|
+
this.setOpts((0, util_1.fromURL)(this.opts.url, location));
|
|
429
|
+
this.init();
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
this.terminate(this.formatStatus(statusCode, "Exceeded redirect limit"));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
this.terminate(this.formatStatus(statusCode, "Missing redirect location"));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
abortResponse() {
|
|
440
|
+
if (this.closed) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
this.aborted = true;
|
|
444
|
+
this.client.destroy();
|
|
445
|
+
this.instance.reset(this);
|
|
446
|
+
this.cleanup();
|
|
447
|
+
}
|
|
448
|
+
errorResponse(err) {
|
|
449
|
+
if (HttpAdapter.wasAborted(err)) {
|
|
450
|
+
this.terminate(err);
|
|
451
|
+
}
|
|
452
|
+
else if ((0, util_1.checkRetryable)(err) && ++this.retries <= this.retryLimit) {
|
|
453
|
+
this.abortResponse();
|
|
454
|
+
if (HttpAdapter.isConnectionError(err)) {
|
|
455
|
+
this.retryTimeout();
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
setTimeout(() => {
|
|
459
|
+
this.init();
|
|
460
|
+
}, this.retryWait);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
this.host.error(this.httpVersion);
|
|
465
|
+
this.terminate(err);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
retryResponse(statusCode, retryAfter) {
|
|
469
|
+
this.abortResponse();
|
|
470
|
+
if (retryAfter && this.retryAfter > 0) {
|
|
471
|
+
let offset = +retryAfter || new Date(retryAfter);
|
|
472
|
+
if (offset instanceof Date) {
|
|
473
|
+
offset = Math.max(0, offset.getTime() - Date.now());
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
offset *= 1000;
|
|
477
|
+
}
|
|
478
|
+
if (offset > 0) {
|
|
479
|
+
if (offset <= this.retryAfter) {
|
|
480
|
+
this.sendWarning(`Retry After (${retryAfter})`);
|
|
481
|
+
setTimeout(() => {
|
|
482
|
+
this.init();
|
|
483
|
+
}, offset);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
this.terminate(this.formatStatus(statusCode));
|
|
487
|
+
}
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
|
|
492
|
+
if ((0, util_1.isRetryable)(statusCode, true)) {
|
|
493
|
+
setImmediate(this.init.bind(this));
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
setTimeout(() => {
|
|
497
|
+
this.init();
|
|
498
|
+
}, this.retryWait);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
isRetry(value) {
|
|
502
|
+
return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
|
|
503
|
+
}
|
|
504
|
+
retryTimeout() {
|
|
505
|
+
this.sendWarning(this.#formatRetry("HTTP connection timeout"));
|
|
506
|
+
this.init();
|
|
507
|
+
}
|
|
508
|
+
terminate(err) {
|
|
509
|
+
if (this.closed) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
this.cleanup();
|
|
513
|
+
this.close();
|
|
514
|
+
this.reject(typeof err === 'string' ? new Error(err) : err);
|
|
515
|
+
}
|
|
516
|
+
sendWarning(message) {
|
|
517
|
+
if (this.state.verbose) {
|
|
518
|
+
const { host, url } = this.opts;
|
|
519
|
+
this.instance.formatMessage(1024, 'HTTP' + this.httpVersion, [message, host.origin], url.toString(), { ...module_1.LOG_STYLE_WARN });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
formatStatus(value, hint) {
|
|
523
|
+
return value + ': ' + (hint || (0, util_1.fromStatusCode)(value)) + ` (${this.uri.toString()})`;
|
|
524
|
+
}
|
|
525
|
+
formatMibs(dataLength) {
|
|
526
|
+
if (dataLength > 0 && this.dataTime) {
|
|
527
|
+
return (0, util_1.getTransferRate)(dataLength, Math.max(1, (0, types_1.convertTime)(process.hrtime.bigint() - this.dataTime, false) * 1000));
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
close() {
|
|
531
|
+
this.closed = true;
|
|
532
|
+
this.instance.reset(this);
|
|
533
|
+
if (this.aborted && !this.client.aborted) {
|
|
534
|
+
this.abortController?.abort();
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
cleanup() {
|
|
538
|
+
if ((0, types_1.isString)(this.pipeTo) && this.outStream) {
|
|
539
|
+
(0, util_1.cleanupStream)(this.outStream, this.pipeTo);
|
|
540
|
+
this.outStream = null;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
#formatNgFlags(value, statusCode, location) {
|
|
544
|
+
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
545
|
+
}
|
|
546
|
+
#formatRetry(message) {
|
|
547
|
+
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
548
|
+
}
|
|
549
|
+
set abortController(value) {
|
|
550
|
+
this.opts.outAbort = value;
|
|
551
|
+
}
|
|
552
|
+
get abortController() {
|
|
553
|
+
return this.opts.outAbort || null;
|
|
554
|
+
}
|
|
555
|
+
set outStream(value) {
|
|
556
|
+
this.#outStream = value;
|
|
557
|
+
if (value) {
|
|
558
|
+
this.opts.outStream = value;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
get outStream() {
|
|
562
|
+
return this.#outStream;
|
|
563
|
+
}
|
|
564
|
+
get destroyed() {
|
|
565
|
+
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
|
566
|
+
}
|
|
567
|
+
get host() {
|
|
568
|
+
return this.opts.host;
|
|
569
|
+
}
|
|
570
|
+
get httpVersion() {
|
|
571
|
+
return this.opts.httpVersion;
|
|
572
|
+
}
|
|
573
|
+
get pipeTo() {
|
|
574
|
+
return this.opts.pipeTo;
|
|
575
|
+
}
|
|
576
|
+
get silent() {
|
|
577
|
+
return this.opts.silent === false;
|
|
578
|
+
}
|
|
579
|
+
get retryLimit() {
|
|
580
|
+
return this.state.config.retryLimit;
|
|
581
|
+
}
|
|
582
|
+
get retryWait() {
|
|
583
|
+
return this.state.config.retryWait;
|
|
584
|
+
}
|
|
585
|
+
get retryAfter() {
|
|
586
|
+
return this.state.config.retryAfter;
|
|
587
|
+
}
|
|
588
|
+
get redirectLimit() {
|
|
589
|
+
return this.state.config.redirectLimit;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
module.exports = HttpAdapter;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
class HttpHostAltSvc {
|
|
3
|
+
host;
|
|
4
|
+
#location = {};
|
|
5
|
+
#available = [];
|
|
6
|
+
#errors = [];
|
|
7
|
+
#versionData;
|
|
8
|
+
constructor(host, versionData) {
|
|
9
|
+
this.host = host;
|
|
10
|
+
this.#versionData = versionData;
|
|
11
|
+
}
|
|
12
|
+
did(version) {
|
|
13
|
+
return this.#versionData[version].status !== -1;
|
|
14
|
+
}
|
|
15
|
+
next() {
|
|
16
|
+
const queue = this.#available.shift();
|
|
17
|
+
if (queue) {
|
|
18
|
+
const { hostname, port, version, expires } = queue;
|
|
19
|
+
const ms = expires - Date.now();
|
|
20
|
+
if (ms < 0) {
|
|
21
|
+
return this.next();
|
|
22
|
+
}
|
|
23
|
+
let timeout = null;
|
|
24
|
+
if (!isNaN(ms)) {
|
|
25
|
+
timeout = setTimeout(() => {
|
|
26
|
+
if (!this.next()) {
|
|
27
|
+
this.host.version = 1;
|
|
28
|
+
}
|
|
29
|
+
}, ms);
|
|
30
|
+
}
|
|
31
|
+
this.#location = { hostname, port, version, timeout, origin: this.host.protocol + '//' + hostname + ':' + port };
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
close(error) {
|
|
37
|
+
const { hostname, port, version, timeout } = this.#location;
|
|
38
|
+
if (hostname) {
|
|
39
|
+
this.#location = {};
|
|
40
|
+
if (timeout) {
|
|
41
|
+
clearTimeout(timeout);
|
|
42
|
+
}
|
|
43
|
+
if (error) {
|
|
44
|
+
this.#errors.push({ hostname, port, version });
|
|
45
|
+
return this.next();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
clear(version) {
|
|
51
|
+
if (version) {
|
|
52
|
+
if (this.#available.length === 0) {
|
|
53
|
+
this.flag(version, 0);
|
|
54
|
+
}
|
|
55
|
+
else if (!this.close(true)) {
|
|
56
|
+
this.flag(version, -1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.close();
|
|
61
|
+
this.#available = [];
|
|
62
|
+
this.#errors = [];
|
|
63
|
+
for (const item of this.#versionData) {
|
|
64
|
+
if (item.alpn !== 0) {
|
|
65
|
+
item.status = -1;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
flag(version, value) {
|
|
71
|
+
this.#versionData[version - 1].status = value;
|
|
72
|
+
}
|
|
73
|
+
valid(hostname, port, version) {
|
|
74
|
+
return !this.errors.find(item => item.hostname === hostname && item.port === port && item.version === version);
|
|
75
|
+
}
|
|
76
|
+
set available(value) {
|
|
77
|
+
this.#available = value;
|
|
78
|
+
}
|
|
79
|
+
get errors() {
|
|
80
|
+
return this.#errors;
|
|
81
|
+
}
|
|
82
|
+
get hostname() {
|
|
83
|
+
return this.#location.hostname;
|
|
84
|
+
}
|
|
85
|
+
get port() {
|
|
86
|
+
return this.#location.port;
|
|
87
|
+
}
|
|
88
|
+
get origin() {
|
|
89
|
+
return this.#location.origin;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
module.exports = HttpHostAltSvc;
|
package/index.js
CHANGED
|
@@ -159,11 +159,17 @@ function getProxySettings(request, agentTimeout) {
|
|
|
159
159
|
return null;
|
|
160
160
|
}
|
|
161
161
|
function closeTorrent(pid) {
|
|
162
|
-
const index = ARIA2.PID_QUEUE.findIndex(value =>
|
|
162
|
+
const index = ARIA2.PID_QUEUE.findIndex(value => value[0] === pid);
|
|
163
163
|
if (index !== -1) {
|
|
164
164
|
ARIA2.PID_QUEUE.splice(index, 1);
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
+
function resetAria2() {
|
|
168
|
+
if (ARIA2.PID_TIMER) {
|
|
169
|
+
clearInterval(ARIA2.PID_TIMER);
|
|
170
|
+
ARIA2.PID_TIMER = null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
167
173
|
function clearDnsLookup() {
|
|
168
174
|
DNS.CACHE = Object.create(null);
|
|
169
175
|
DNS.TIMEOUT.forEach(value => clearTimeout(value));
|
|
@@ -181,7 +187,7 @@ function resetHttpHost(version) {
|
|
|
181
187
|
const host = HTTP.HOST[origin];
|
|
182
188
|
if (host.secure && host.version === 1) {
|
|
183
189
|
const failed = host.failed(2, true);
|
|
184
|
-
if (failed === 0 && host.
|
|
190
|
+
if (failed === 0 && host.error(2, true) < 10 || failed < 3 && host.success(2, true) > 0) {
|
|
185
191
|
host.version = version;
|
|
186
192
|
}
|
|
187
193
|
}
|
|
@@ -1374,13 +1380,9 @@ class Request extends module_1.default {
|
|
|
1374
1380
|
stderr.setEncoding('utf-8').on('data', (value) => message += value);
|
|
1375
1381
|
if (pid !== undefined && module_1.default.isFile(uri, 'torrent')) {
|
|
1376
1382
|
if (!ARIA2.PID_TIMER) {
|
|
1377
|
-
const clearTimer = () => {
|
|
1378
|
-
clearInterval(ARIA2.PID_TIMER);
|
|
1379
|
-
ARIA2.PID_TIMER = null;
|
|
1380
|
-
};
|
|
1381
1383
|
ARIA2.PID_TIMER = setInterval(() => {
|
|
1382
1384
|
if (ARIA2.PID_QUEUE.length === 0) {
|
|
1383
|
-
|
|
1385
|
+
resetAria2();
|
|
1384
1386
|
return;
|
|
1385
1387
|
}
|
|
1386
1388
|
for (const item of ARIA2.PID_QUEUE) {
|
|
@@ -1415,7 +1417,7 @@ class Request extends module_1.default {
|
|
|
1415
1417
|
ARIA2.PID_QUEUE.push(item);
|
|
1416
1418
|
}
|
|
1417
1419
|
else {
|
|
1418
|
-
|
|
1420
|
+
resetAria2();
|
|
1419
1421
|
}
|
|
1420
1422
|
}
|
|
1421
1423
|
}, (ARIA2.UPDATE_STATUS || 30) * 1000);
|
|
@@ -2073,7 +2075,7 @@ class Request extends module_1.default {
|
|
|
2073
2075
|
result = buffer;
|
|
2074
2076
|
}
|
|
2075
2077
|
}
|
|
2076
|
-
else if (enabled && this.readExpect === 'always') {
|
|
2078
|
+
else if (enabled && !empty && this.readExpect === 'always') {
|
|
2077
2079
|
throwError('No data received');
|
|
2078
2080
|
return;
|
|
2079
2081
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.29",
|
|
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.8.
|
|
24
|
-
"@e-mc/types": "0.8.
|
|
23
|
+
"@e-mc/module": "0.8.29",
|
|
24
|
+
"@e-mc/types": "0.8.29",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"picomatch": "^3.0.1",
|