@e-mc/request 0.5.3 → 0.5.4
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/LICENSE +7 -0
- package/README.md +5 -3
- package/http/host/index.d.ts +4 -4
- package/http/host/index.js +28 -28
- package/index.d.ts +4 -4
- package/index.js +157 -151
- package/package.json +5 -5
- package/util.js +14 -14
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2023 Mile Square Park
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
package/http/host/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { HttpHostConstructor } from '../../../types/lib/request';
|
|
2
|
-
|
|
3
|
-
declare const HttpHost: HttpHostConstructor;
|
|
4
|
-
|
|
1
|
+
import type { HttpHostConstructor } from '../../../types/lib/request';
|
|
2
|
+
|
|
3
|
+
declare const HttpHost: HttpHostConstructor;
|
|
4
|
+
|
|
5
5
|
export = HttpHost;
|
package/http/host/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var _a, _b, _c, _d;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
const tls = require("tls");
|
|
5
|
-
const types_1 = require("
|
|
5
|
+
const types_1 = require("@e-mc/types");
|
|
6
6
|
const kProtocol = Symbol('protocol');
|
|
7
7
|
const kSecure = Symbol('secure');
|
|
8
8
|
const kHostname = Symbol('hostname');
|
|
@@ -85,15 +85,15 @@ class HttpHost {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
this[kVersion] = 1;
|
|
88
|
-
this[kVersionData].forEach(version => version[4
|
|
88
|
+
this[kVersionData].forEach(version => version[4] = 0);
|
|
89
89
|
}
|
|
90
90
|
async hasProtocol(version) {
|
|
91
91
|
if (version > 1) {
|
|
92
92
|
const data = this[kVersionData][version - 1];
|
|
93
93
|
if (!data || !this.secure) {
|
|
94
|
-
return 0
|
|
94
|
+
return 0;
|
|
95
95
|
}
|
|
96
|
-
const status = data[3
|
|
96
|
+
const status = data[3];
|
|
97
97
|
switch (status) {
|
|
98
98
|
case 0:
|
|
99
99
|
case 1:
|
|
@@ -102,19 +102,19 @@ class HttpHost {
|
|
|
102
102
|
return this._tlsConnect || (this._tlsConnect = new Promise(resolve => {
|
|
103
103
|
const alpn = 'h' + version;
|
|
104
104
|
const socket = tls.connect(+this.port, this.hostname, { ALPNProtocols: [alpn], requestCert: true, rejectUnauthorized: false }, () => {
|
|
105
|
-
resolve(data[3
|
|
105
|
+
resolve(data[3] = alpn === socket.alpnProtocol ? 1 : 0);
|
|
106
106
|
this._tlsConnect = null;
|
|
107
107
|
});
|
|
108
108
|
socket
|
|
109
109
|
.setNoDelay(false)
|
|
110
|
-
.setTimeout(5000
|
|
110
|
+
.setTimeout(5000)
|
|
111
111
|
.on('timeout', () => {
|
|
112
112
|
if (this._tlsConnect) {
|
|
113
|
-
if (this.error(version) >= 10
|
|
114
|
-
resolve(data[3
|
|
113
|
+
if (this.error(version) >= 10) {
|
|
114
|
+
resolve(data[3] = 0);
|
|
115
115
|
}
|
|
116
116
|
else {
|
|
117
|
-
resolve(2
|
|
117
|
+
resolve(2);
|
|
118
118
|
}
|
|
119
119
|
this._tlsConnect = null;
|
|
120
120
|
}
|
|
@@ -122,36 +122,36 @@ class HttpHost {
|
|
|
122
122
|
})
|
|
123
123
|
.on('error', () => {
|
|
124
124
|
this.failed(version);
|
|
125
|
-
resolve(data[3
|
|
125
|
+
resolve(data[3] = 0);
|
|
126
126
|
this._tlsConnect = null;
|
|
127
127
|
})
|
|
128
128
|
.end();
|
|
129
129
|
}));
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
|
-
return 1
|
|
132
|
+
return 1;
|
|
133
133
|
}
|
|
134
134
|
success(version, status) {
|
|
135
135
|
const data = this[kVersionData][version - 1];
|
|
136
|
-
return status ? data[0
|
|
136
|
+
return status ? data[0] : ++data[0];
|
|
137
137
|
}
|
|
138
138
|
failed(version, status) {
|
|
139
139
|
const data = this[kVersionData][version - 1];
|
|
140
140
|
if (status) {
|
|
141
|
-
return data[1
|
|
141
|
+
return data[1];
|
|
142
142
|
}
|
|
143
143
|
this.clearAltSvc(version);
|
|
144
|
-
return ++data[1
|
|
144
|
+
return ++data[1];
|
|
145
145
|
}
|
|
146
146
|
error(version, status) {
|
|
147
147
|
const data = this[kVersionData][version - 1];
|
|
148
148
|
if (status) {
|
|
149
|
-
return data[2
|
|
149
|
+
return data[2];
|
|
150
150
|
}
|
|
151
|
-
if (data[4
|
|
151
|
+
if (data[4] !== 2) {
|
|
152
152
|
this.closeAltSvc(true);
|
|
153
153
|
}
|
|
154
|
-
return ++data[2
|
|
154
|
+
return ++data[2];
|
|
155
155
|
}
|
|
156
156
|
upgrade(version, altSvc) {
|
|
157
157
|
if (altSvc && this.secure) {
|
|
@@ -162,11 +162,11 @@ class HttpHost {
|
|
|
162
162
|
const data = this[kVersionData];
|
|
163
163
|
for (let i = data.length - 1; i >= version; --i) {
|
|
164
164
|
const host = data[i];
|
|
165
|
-
if (host[4
|
|
165
|
+
if (host[4] === 0) {
|
|
166
166
|
continue;
|
|
167
167
|
}
|
|
168
168
|
const increment = (flag) => {
|
|
169
|
-
host[4
|
|
169
|
+
host[4] = flag;
|
|
170
170
|
if (this[kVersion] < i + 1) {
|
|
171
171
|
this[kVersion] = i + 1;
|
|
172
172
|
return true;
|
|
@@ -191,7 +191,7 @@ class HttpHost {
|
|
|
191
191
|
addresses.push([
|
|
192
192
|
address,
|
|
193
193
|
port,
|
|
194
|
-
ma >= 2592000
|
|
194
|
+
ma >= 2592000 ? NaN : time + Math.min(ma * 1000, 2147483647),
|
|
195
195
|
i + 1,
|
|
196
196
|
match[3].includes('persist=1')
|
|
197
197
|
]);
|
|
@@ -228,7 +228,7 @@ class HttpHost {
|
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
didAltSvc(version) {
|
|
231
|
-
return this[kVersionData][version][4
|
|
231
|
+
return this[kVersionData][version][4] !== -1;
|
|
232
232
|
}
|
|
233
233
|
nextAltSvc() {
|
|
234
234
|
const queue = this[kAltSvcQueue].shift();
|
|
@@ -279,23 +279,23 @@ class HttpHost {
|
|
|
279
279
|
this[kAltSvcQueue] = [];
|
|
280
280
|
this[kAltSvcError] = [];
|
|
281
281
|
this[kVersionData].forEach(item => {
|
|
282
|
-
if (item[3
|
|
283
|
-
item[4
|
|
282
|
+
if (item[3] !== 0) {
|
|
283
|
+
item[4] = -1;
|
|
284
284
|
}
|
|
285
285
|
});
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
flagAltSvc(version, value) {
|
|
289
|
-
this[kVersionData][version - 1][4
|
|
289
|
+
this[kVersionData][version - 1][4] = value;
|
|
290
290
|
}
|
|
291
291
|
reset() {
|
|
292
292
|
this.clearAltSvc();
|
|
293
293
|
this[kVersionData].forEach((item, index) => {
|
|
294
|
-
item[0
|
|
295
|
-
item[1
|
|
296
|
-
item[2
|
|
294
|
+
item[0] = 0;
|
|
295
|
+
item[1] = 0;
|
|
296
|
+
item[2] = 0;
|
|
297
297
|
if (index > 0) {
|
|
298
|
-
item[3
|
|
298
|
+
item[3] = -1;
|
|
299
299
|
}
|
|
300
300
|
});
|
|
301
301
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { RequestConstructor } from '../types/lib';
|
|
2
|
-
|
|
3
|
-
declare const Request: RequestConstructor;
|
|
4
|
-
|
|
1
|
+
import type { RequestConstructor } from '../types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Request: RequestConstructor;
|
|
4
|
+
|
|
5
5
|
export = Request;
|
package/index.js
CHANGED
|
@@ -15,11 +15,11 @@ const qs = require("querystring");
|
|
|
15
15
|
const combined = require("combined-stream");
|
|
16
16
|
const yaml = require("js-yaml");
|
|
17
17
|
const which = require("which");
|
|
18
|
-
const lib_v4_1 = require("
|
|
19
|
-
const types_1 = require("
|
|
20
|
-
const module_1 = require("
|
|
21
|
-
const host_1 = require("
|
|
22
|
-
const util_1 = require("
|
|
18
|
+
const lib_v4_1 = require("@e-mc/module/lib-v4");
|
|
19
|
+
const types_1 = require("@e-mc/types");
|
|
20
|
+
const module_1 = require("@e-mc/module");
|
|
21
|
+
const host_1 = require("@e-mc/request/http/host");
|
|
22
|
+
const util_1 = require("@e-mc/request/util");
|
|
23
23
|
const kSession = Symbol('session');
|
|
24
24
|
const kHttpVersion = Symbol('httpVersion');
|
|
25
25
|
const kIpVersion = Symbol('ipVersion');
|
|
@@ -168,7 +168,7 @@ function resetHttpHost(version) {
|
|
|
168
168
|
const host = HTTP.HOST[origin];
|
|
169
169
|
if (host.secure && host.version === 1) {
|
|
170
170
|
const failed = host.failed(2, true);
|
|
171
|
-
if (failed === 0 && host.failed(2, true) < 10
|
|
171
|
+
if (failed === 0 && host.failed(2, true) < 10 || failed < 3 && host.success(2, true) > 0) {
|
|
172
172
|
host.version = version;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
@@ -263,7 +263,7 @@ class Request extends module_1.default {
|
|
|
263
263
|
return parent ? super.purgeMemory(percent, limit) : Promise.resolve(0);
|
|
264
264
|
}
|
|
265
265
|
static loadSettings(settings, password) {
|
|
266
|
-
if (!this.enabled("process.password"
|
|
266
|
+
if (!this.enabled("process.password") || super.loadSettings({ process: settings.process }, password)) {
|
|
267
267
|
const { request, download } = settings;
|
|
268
268
|
if (download && (0, types_1.isPlainObject)(download.aria2)) {
|
|
269
269
|
let { bin, update_status, max_concurrent_downloads, max_connection_per_server, bt_stop_timeout, bt_tracker_connect_timeout, bt_tracker_timeout, min_split_size, disk_cache, lowest_speed_limit, always_resume, file_allocation, conf_path } = download.aria2;
|
|
@@ -382,8 +382,8 @@ class Request extends module_1.default {
|
|
|
382
382
|
LOG_TIMEFORMAT = time_format;
|
|
383
383
|
break;
|
|
384
384
|
}
|
|
385
|
-
LOG_HTTP = this.hasLogType(1024
|
|
386
|
-
LOG_TIMEPROCESS = this.hasLogType(256
|
|
385
|
+
LOG_HTTP = this.hasLogType(1024);
|
|
386
|
+
LOG_TIMEPROCESS = this.hasLogType(256);
|
|
387
387
|
}
|
|
388
388
|
return true;
|
|
389
389
|
}
|
|
@@ -399,133 +399,133 @@ class Request extends module_1.default {
|
|
|
399
399
|
return this.readCACert(value, cache);
|
|
400
400
|
}
|
|
401
401
|
static isCert(value) {
|
|
402
|
-
return (0, types_1.isString)(value) && (value = value.trim()) ? value.
|
|
402
|
+
return (0, types_1.isString)(value) && (value = value.trim()) ? value.includes('\n') && value.startsWith('---') && value.endsWith('---') : false;
|
|
403
403
|
}
|
|
404
404
|
static fromURL(url, value) {
|
|
405
405
|
if (this.isURL(value)) {
|
|
406
406
|
return value;
|
|
407
407
|
}
|
|
408
408
|
const auth = host_1.default.formatBasicAuth(url);
|
|
409
|
-
return url.protocol + '//' + (auth && (auth + '@')) + url.hostname + (url.port ? ':' + url.port : '') + (value
|
|
409
|
+
return url.protocol + '//' + (auth && (auth + '@')) + url.hostname + (url.port ? ':' + url.port : '') + (!value.startsWith('/') ? '/' : '') + value;
|
|
410
410
|
}
|
|
411
411
|
static fromStatusCode(value) {
|
|
412
412
|
switch (+value) {
|
|
413
|
-
case 100
|
|
413
|
+
case 100:
|
|
414
414
|
return 'Continue';
|
|
415
|
-
case 101
|
|
415
|
+
case 101:
|
|
416
416
|
return 'Switching Protocol';
|
|
417
|
-
case 102
|
|
417
|
+
case 102:
|
|
418
418
|
return 'Processing';
|
|
419
|
-
case 103
|
|
419
|
+
case 103:
|
|
420
420
|
return 'Early Hints';
|
|
421
|
-
case 200
|
|
421
|
+
case 200:
|
|
422
422
|
return 'OK';
|
|
423
|
-
case 201
|
|
423
|
+
case 201:
|
|
424
424
|
return 'Created';
|
|
425
|
-
case 202
|
|
425
|
+
case 202:
|
|
426
426
|
return 'Accepted';
|
|
427
|
-
case 203
|
|
427
|
+
case 203:
|
|
428
428
|
return 'Non-Authoritative Information';
|
|
429
|
-
case 204
|
|
429
|
+
case 204:
|
|
430
430
|
return 'No Content';
|
|
431
|
-
case 205
|
|
431
|
+
case 205:
|
|
432
432
|
return 'Reset Content';
|
|
433
|
-
case 206
|
|
433
|
+
case 206:
|
|
434
434
|
return 'Partial Content';
|
|
435
|
-
case 300
|
|
435
|
+
case 300:
|
|
436
436
|
return 'Multiple Choice';
|
|
437
|
-
case 301
|
|
437
|
+
case 301:
|
|
438
438
|
return 'Moved Permanently';
|
|
439
|
-
case 302
|
|
439
|
+
case 302:
|
|
440
440
|
return 'Found';
|
|
441
|
-
case 303
|
|
441
|
+
case 303:
|
|
442
442
|
return 'See Other';
|
|
443
|
-
case 304
|
|
443
|
+
case 304:
|
|
444
444
|
return 'Not Modified';
|
|
445
|
-
case 305
|
|
445
|
+
case 305:
|
|
446
446
|
return 'Use Proxy';
|
|
447
|
-
case 307
|
|
447
|
+
case 307:
|
|
448
448
|
return 'Temporary Redirect';
|
|
449
|
-
case 308
|
|
449
|
+
case 308:
|
|
450
450
|
return 'Permanent Redirect';
|
|
451
|
-
case 400
|
|
451
|
+
case 400:
|
|
452
452
|
return 'Bad Request';
|
|
453
|
-
case 401
|
|
453
|
+
case 401:
|
|
454
454
|
return 'Upgrade Required';
|
|
455
|
-
case 402
|
|
455
|
+
case 402:
|
|
456
456
|
return 'Payment Required';
|
|
457
|
-
case 403
|
|
457
|
+
case 403:
|
|
458
458
|
return 'Forbidden';
|
|
459
|
-
case 404
|
|
459
|
+
case 404:
|
|
460
460
|
return 'Not Found';
|
|
461
|
-
case 405
|
|
461
|
+
case 405:
|
|
462
462
|
return 'Method Not Allowed';
|
|
463
|
-
case 406
|
|
463
|
+
case 406:
|
|
464
464
|
return 'Not Acceptable';
|
|
465
|
-
case 407
|
|
465
|
+
case 407:
|
|
466
466
|
return 'Proxy Authentication Required';
|
|
467
|
-
case 408
|
|
467
|
+
case 408:
|
|
468
468
|
return 'Request Timeout';
|
|
469
|
-
case 409
|
|
469
|
+
case 409:
|
|
470
470
|
return 'Conflict';
|
|
471
|
-
case 410
|
|
471
|
+
case 410:
|
|
472
472
|
return 'Gone';
|
|
473
|
-
case 411
|
|
473
|
+
case 411:
|
|
474
474
|
return 'Length Required';
|
|
475
|
-
case 412
|
|
475
|
+
case 412:
|
|
476
476
|
return 'Precondition Failed';
|
|
477
|
-
case 413
|
|
477
|
+
case 413:
|
|
478
478
|
return 'Payload Too Large';
|
|
479
|
-
case 414
|
|
479
|
+
case 414:
|
|
480
480
|
return 'URI Too Long';
|
|
481
|
-
case 415
|
|
481
|
+
case 415:
|
|
482
482
|
return 'Unsupported Media Type';
|
|
483
|
-
case 416
|
|
483
|
+
case 416:
|
|
484
484
|
return 'Range Not Satisfiable';
|
|
485
|
-
case 417
|
|
485
|
+
case 417:
|
|
486
486
|
return 'Expectation Failed';
|
|
487
|
-
case 421
|
|
487
|
+
case 421:
|
|
488
488
|
return 'Misdirected Request';
|
|
489
|
-
case 422
|
|
489
|
+
case 422:
|
|
490
490
|
return 'Unprocessable Entity';
|
|
491
|
-
case 423
|
|
491
|
+
case 423:
|
|
492
492
|
return 'Locked';
|
|
493
|
-
case 424
|
|
493
|
+
case 424:
|
|
494
494
|
return 'Failed Dependency';
|
|
495
|
-
case 426
|
|
495
|
+
case 426:
|
|
496
496
|
return 'Upgrade Required';
|
|
497
|
-
case 428
|
|
497
|
+
case 428:
|
|
498
498
|
return 'Precondition Required';
|
|
499
|
-
case 429
|
|
499
|
+
case 429:
|
|
500
500
|
return 'Too Many Requests';
|
|
501
|
-
case 431
|
|
501
|
+
case 431:
|
|
502
502
|
return 'Request Header Fields Too Large';
|
|
503
|
-
case 451
|
|
503
|
+
case 451:
|
|
504
504
|
return 'Unavailable For Legal Reasons';
|
|
505
|
-
case 500
|
|
505
|
+
case 500:
|
|
506
506
|
return 'Internal Server Error';
|
|
507
|
-
case 501
|
|
507
|
+
case 501:
|
|
508
508
|
return 'Not Implemented';
|
|
509
|
-
case 502
|
|
509
|
+
case 502:
|
|
510
510
|
return 'Bad Gateway';
|
|
511
|
-
case 503
|
|
511
|
+
case 503:
|
|
512
512
|
return 'Service Unavailable';
|
|
513
|
-
case 504
|
|
513
|
+
case 504:
|
|
514
514
|
return 'Gateway Timeout';
|
|
515
|
-
case 505
|
|
515
|
+
case 505:
|
|
516
516
|
return 'HTTP Version Not Supported';
|
|
517
|
-
case 506
|
|
517
|
+
case 506:
|
|
518
518
|
return 'Variant Also Negotiates';
|
|
519
|
-
case 507
|
|
519
|
+
case 507:
|
|
520
520
|
return 'Insufficient Storage';
|
|
521
|
-
case 508
|
|
521
|
+
case 508:
|
|
522
522
|
return 'Loop Detected';
|
|
523
|
-
case 510
|
|
523
|
+
case 510:
|
|
524
524
|
return 'Not Extended';
|
|
525
|
-
case 511
|
|
525
|
+
case 511:
|
|
526
526
|
return 'Network Authentication Required';
|
|
527
527
|
default:
|
|
528
|
-
return "Unknown"
|
|
528
|
+
return "Unknown";
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
531
|
static defineHttpAgent(options) {
|
|
@@ -583,13 +583,14 @@ class Request extends module_1.default {
|
|
|
583
583
|
this.startTime = Date.now();
|
|
584
584
|
this.readExpect = 'none';
|
|
585
585
|
this.proxy = null;
|
|
586
|
-
this._moduleName = "request"
|
|
586
|
+
this._moduleName = "request";
|
|
587
|
+
this._threadable = true;
|
|
587
588
|
this._config = {
|
|
588
|
-
timeout: 60000
|
|
589
|
-
connectTimeout: 20 * 1000
|
|
589
|
+
timeout: 60000,
|
|
590
|
+
connectTimeout: 20 * 1000,
|
|
590
591
|
redirectLimit: 10,
|
|
591
|
-
retryWait: 1000
|
|
592
|
-
retryAfter: 30 * 1000
|
|
592
|
+
retryWait: 1000,
|
|
593
|
+
retryAfter: 30 * 1000,
|
|
593
594
|
retryLimit: 5
|
|
594
595
|
};
|
|
595
596
|
this[_a] = false;
|
|
@@ -676,7 +677,7 @@ class Request extends module_1.default {
|
|
|
676
677
|
const args = [];
|
|
677
678
|
for (let i = 0; i < log.length; ++i) {
|
|
678
679
|
const item = log[i];
|
|
679
|
-
if (Array.isArray(item[2]) && item[2][0].startsWith(origin)) {
|
|
680
|
+
if (item[1] === title && Array.isArray(item[2]) && item[2][0].startsWith(origin)) {
|
|
680
681
|
item[1] = '';
|
|
681
682
|
item[2][0] = item[2][0].substring(origin.length);
|
|
682
683
|
item[4].titleBgColor = undefined;
|
|
@@ -685,12 +686,17 @@ class Request extends module_1.default {
|
|
|
685
686
|
log.splice(i--, 1);
|
|
686
687
|
}
|
|
687
688
|
}
|
|
688
|
-
output.push([title, origin, value, args]);
|
|
689
|
+
output.push([title, origin, value, args.sort((a, b) => a[2][0] < b[2][0] ? -1 : 1)]);
|
|
689
690
|
count = Math.max(count, value);
|
|
690
691
|
}
|
|
691
692
|
});
|
|
692
693
|
if (!this.host?.aborted) {
|
|
693
|
-
output.sort((a, b) =>
|
|
694
|
+
output.sort((a, b) => {
|
|
695
|
+
if (a[2] === b[2]) {
|
|
696
|
+
return a[1] < b[1] ? -1 : 1;
|
|
697
|
+
}
|
|
698
|
+
return b[2] - a[2];
|
|
699
|
+
});
|
|
694
700
|
const width = count.toString().length;
|
|
695
701
|
output.forEach(item => {
|
|
696
702
|
const [title, origin, downloads, messages] = item;
|
|
@@ -703,7 +709,7 @@ class Request extends module_1.default {
|
|
|
703
709
|
module_1.default.formatMessage(...message);
|
|
704
710
|
}
|
|
705
711
|
else {
|
|
706
|
-
this.formatMessage(1024
|
|
712
|
+
this.formatMessage(1024, title, [origin, 'downloads: ' + downloads.toString().padStart(width)], '', options);
|
|
707
713
|
messages.forEach(args => {
|
|
708
714
|
args[4].titleIndent = true;
|
|
709
715
|
module_1.default.formatMessage(...args);
|
|
@@ -732,7 +738,7 @@ class Request extends module_1.default {
|
|
|
732
738
|
}
|
|
733
739
|
init(config) {
|
|
734
740
|
if (config) {
|
|
735
|
-
const { headers, httpVersion, ipVersion, requestTimeout } = config;
|
|
741
|
+
const { headers, httpVersion, ipVersion, requestTimeout, readTimeout = requestTimeout } = config;
|
|
736
742
|
if ((0, types_1.isObject)(headers)) {
|
|
737
743
|
setOutgoingHeaders(this[kHeaders] || (this[kHeaders] = {}), headers);
|
|
738
744
|
}
|
|
@@ -742,8 +748,8 @@ class Request extends module_1.default {
|
|
|
742
748
|
if (ipVersion !== undefined) {
|
|
743
749
|
this.ipVersion = ipVersion;
|
|
744
750
|
}
|
|
745
|
-
if (
|
|
746
|
-
this.readTimeout = (0, util_1.fromSeconds)(
|
|
751
|
+
if (readTimeout !== undefined) {
|
|
752
|
+
this.readTimeout = (0, util_1.fromSeconds)(readTimeout);
|
|
747
753
|
}
|
|
748
754
|
}
|
|
749
755
|
return this;
|
|
@@ -764,7 +770,7 @@ class Request extends module_1.default {
|
|
|
764
770
|
this._config.redirectLimit = redirectLimit;
|
|
765
771
|
}
|
|
766
772
|
if (retryWait >= 0) {
|
|
767
|
-
this._config.retryWait = Math.min(retryWait, 600 * 1000
|
|
773
|
+
this._config.retryWait = Math.min(retryWait, 600 * 1000);
|
|
768
774
|
}
|
|
769
775
|
if (retryAfter >= 0) {
|
|
770
776
|
this._config.retryAfter = Math.min(retryAfter, module_1.default.MAX_TIMEOUT);
|
|
@@ -904,9 +910,9 @@ class Request extends module_1.default {
|
|
|
904
910
|
const headers = this[kHeaders];
|
|
905
911
|
return headers && getBaseHeaders(uri, headers) || (this.host ? getBaseHeaders(uri, HTTP.HEADERS) : undefined);
|
|
906
912
|
}
|
|
907
|
-
aria2c(uri, options) {
|
|
913
|
+
async aria2c(uri, options) {
|
|
908
914
|
if (!ARIA2.BIN) {
|
|
909
|
-
return Promise.reject((0, types_1.errorMessage)("aria2"
|
|
915
|
+
return Promise.reject((0, types_1.errorMessage)("aria2", "Binary not found"));
|
|
910
916
|
}
|
|
911
917
|
if (typeof uri === 'string' && module_1.default.isURL(uri)) {
|
|
912
918
|
try {
|
|
@@ -974,15 +980,15 @@ class Request extends module_1.default {
|
|
|
974
980
|
}
|
|
975
981
|
if (!(0, types_1.isString)(pathname)) {
|
|
976
982
|
if (this.host) {
|
|
977
|
-
return Promise.reject((0, types_1.errorMessage)("aria2"
|
|
983
|
+
return Promise.reject((0, types_1.errorMessage)("aria2", "Invalid parameters", 'pathname'));
|
|
978
984
|
}
|
|
979
985
|
pathname = process.cwd();
|
|
980
986
|
}
|
|
981
987
|
if ((this.host || this.hasOwnPermission()) && !this.canWrite(pathname = path.resolve(pathname.trim()))) {
|
|
982
|
-
return Promise.reject((0, types_1.errorMessage)("aria2"
|
|
988
|
+
return Promise.reject((0, types_1.errorMessage)("aria2", "Unsupported access", pathname));
|
|
983
989
|
}
|
|
984
990
|
if (!module_1.default.createDir(pathname)) {
|
|
985
|
-
return Promise.reject((0, types_1.errorMessage)("aria2"
|
|
991
|
+
return Promise.reject((0, types_1.errorMessage)("aria2", "Path is not a directory", pathname));
|
|
986
992
|
}
|
|
987
993
|
silent ?? (silent = this[kSingleton]);
|
|
988
994
|
return new Promise((resolve, reject) => {
|
|
@@ -1146,8 +1152,8 @@ class Request extends module_1.default {
|
|
|
1146
1152
|
}
|
|
1147
1153
|
args = binOpts.concat(args);
|
|
1148
1154
|
}
|
|
1149
|
-
if (args.length && module_1.default.hasLogType(32768
|
|
1150
|
-
this.formatMessage(32768
|
|
1155
|
+
if (args.length && module_1.default.hasLogType(32768)) {
|
|
1156
|
+
this.formatMessage(32768, 'ARIA2', ARIA2.BIN, args.join(' '), { ...module_1.default.LOG_STYLE_WARN });
|
|
1151
1157
|
}
|
|
1152
1158
|
opts.push(`"${escapeQuote(uri)}"`);
|
|
1153
1159
|
args = args.concat(init, opts);
|
|
@@ -1190,18 +1196,18 @@ class Request extends module_1.default {
|
|
|
1190
1196
|
break;
|
|
1191
1197
|
}
|
|
1192
1198
|
case 'ERR':
|
|
1193
|
-
fs.unlink(file, err => err && !silent && !this[kSingleton] && this.writeFail(["Unable to delete file"
|
|
1199
|
+
fs.unlink(file, err => err && !silent && !this[kSingleton] && this.writeFail(["Unable to delete file", path.basename(file)], err, { type: 32, fatal: false }));
|
|
1194
1200
|
break;
|
|
1195
1201
|
}
|
|
1196
1202
|
}
|
|
1197
1203
|
if (result.length && !silent && LOG_HTTP && LOG_TIMEPROCESS) {
|
|
1198
|
-
this.writeTimeProcess("aria2"
|
|
1204
|
+
this.writeTimeProcess("aria2", uri, startTime, { type: 1024, queue: true, messageUnit, messageUnitMinWidth: 9 });
|
|
1199
1205
|
}
|
|
1200
|
-
this.addLog(result.length ? types_1.STATUS_TYPE.INFO : types_1.STATUS_TYPE.ERROR, out, currentTime, currentTime - startTime, "aria2"
|
|
1206
|
+
this.addLog(result.length ? types_1.STATUS_TYPE.INFO : types_1.STATUS_TYPE.ERROR, out, currentTime, currentTime - startTime, "aria2", uri);
|
|
1201
1207
|
resolve(result);
|
|
1202
1208
|
}
|
|
1203
1209
|
else {
|
|
1204
|
-
reject((0, types_1.errorValue)(message || "Unknown"
|
|
1210
|
+
reject((0, types_1.errorValue)(message || "Unknown", 'Exit status: ' + code));
|
|
1205
1211
|
}
|
|
1206
1212
|
})
|
|
1207
1213
|
.on('error', err => errorResponse(pid, err));
|
|
@@ -1237,9 +1243,9 @@ class Request extends module_1.default {
|
|
|
1237
1243
|
}
|
|
1238
1244
|
else {
|
|
1239
1245
|
const current = (0, types_1.getLogCurrent)();
|
|
1240
|
-
progressBar = current?.type === 128
|
|
1246
|
+
progressBar = current?.type === 128 && current.title === "aria2";
|
|
1241
1247
|
}
|
|
1242
|
-
this.formatMessage(128
|
|
1248
|
+
this.formatMessage(128, "aria2", ['Downloading...', (0, types_1.formatTime)(startTime, true)], (PLATFORM_WIN32 ? 'taskkill /f /pid' : 'kill') + ` ${item[0]} -> ` + item[1], { ...module_1.default.LOG_STYLE_INFO, progressBar, broadcastId });
|
|
1243
1249
|
}
|
|
1244
1250
|
ARIA2.PID_QUEUE.push(item);
|
|
1245
1251
|
}
|
|
@@ -1247,17 +1253,17 @@ class Request extends module_1.default {
|
|
|
1247
1253
|
clearTimer();
|
|
1248
1254
|
}
|
|
1249
1255
|
}
|
|
1250
|
-
}, (ARIA2.UPDATE_STATUS || 30) * 1000
|
|
1256
|
+
}, (ARIA2.UPDATE_STATUS || 30) * 1000);
|
|
1251
1257
|
}
|
|
1252
1258
|
ARIA2.PID_QUEUE.push([pid, uri, this.broadcastId || undefined]);
|
|
1253
1259
|
}
|
|
1254
1260
|
});
|
|
1255
1261
|
}
|
|
1256
|
-
json(uri, options = {}) {
|
|
1262
|
+
async json(uri, options = {}) {
|
|
1257
1263
|
options.format = 'json';
|
|
1258
1264
|
return this.get(uri, options);
|
|
1259
1265
|
}
|
|
1260
|
-
pipe(uri, to, options = {}) {
|
|
1266
|
+
async pipe(uri, to, options = {}) {
|
|
1261
1267
|
options.pipeTo = to;
|
|
1262
1268
|
return this.get(uri, options);
|
|
1263
1269
|
}
|
|
@@ -1291,23 +1297,23 @@ class Request extends module_1.default {
|
|
|
1291
1297
|
headers || (headers = {});
|
|
1292
1298
|
switch (format = format.trim().toLowerCase()) {
|
|
1293
1299
|
case 'yaml':
|
|
1294
|
-
headers
|
|
1300
|
+
headers.accept = 'application/yaml, application/x-yaml, text/yaml, text/x-yaml';
|
|
1295
1301
|
break;
|
|
1296
1302
|
case 'json5':
|
|
1297
|
-
headers
|
|
1303
|
+
headers.accept = 'application/json5, application/json, text/javascript';
|
|
1298
1304
|
break;
|
|
1299
1305
|
case 'xml':
|
|
1300
|
-
headers
|
|
1306
|
+
headers.accept = 'application/xml, text/xml';
|
|
1301
1307
|
break;
|
|
1302
1308
|
case 'toml':
|
|
1303
|
-
headers
|
|
1309
|
+
headers.accept = 'application/toml';
|
|
1304
1310
|
break;
|
|
1305
1311
|
default:
|
|
1306
|
-
headers
|
|
1312
|
+
headers.accept = 'application/json, text/javascript';
|
|
1307
1313
|
format = 'json';
|
|
1308
1314
|
break;
|
|
1309
1315
|
}
|
|
1310
|
-
headers
|
|
1316
|
+
headers.accept += ', text/plain';
|
|
1311
1317
|
options.encoding = (0, types_1.getEncoding)(encoding);
|
|
1312
1318
|
options.outFormat = { out: format, parser };
|
|
1313
1319
|
}
|
|
@@ -1326,9 +1332,9 @@ class Request extends module_1.default {
|
|
|
1326
1332
|
}
|
|
1327
1333
|
const checkEncoding = (response, statusCode, contentEncoding = '') => {
|
|
1328
1334
|
switch (statusCode) {
|
|
1329
|
-
case 206
|
|
1335
|
+
case 206:
|
|
1330
1336
|
request.emit('error', new Error('[ABORT] Partial content'));
|
|
1331
|
-
case 204
|
|
1337
|
+
case 204:
|
|
1332
1338
|
return;
|
|
1333
1339
|
}
|
|
1334
1340
|
const chunkSize = outStream?.writableHighWaterMark;
|
|
@@ -1380,7 +1386,7 @@ class Request extends module_1.default {
|
|
|
1380
1386
|
request.on('response', response => {
|
|
1381
1387
|
connected = true;
|
|
1382
1388
|
const statusCode = response[':status'];
|
|
1383
|
-
if (statusCode >= 200
|
|
1389
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
1384
1390
|
if (emitter = checkEncoding(request, statusCode, response['content-encoding'])) {
|
|
1385
1391
|
for (const event in listenerMap) {
|
|
1386
1392
|
listenerMap[event].forEach(listener => {
|
|
@@ -1463,7 +1469,7 @@ class Request extends module_1.default {
|
|
|
1463
1469
|
}
|
|
1464
1470
|
}
|
|
1465
1471
|
catch (err) {
|
|
1466
|
-
this.checkPackage(err, pkg, "Unknown"
|
|
1472
|
+
this.checkPackage(err, pkg, "Unknown");
|
|
1467
1473
|
}
|
|
1468
1474
|
}
|
|
1469
1475
|
else if (keepAlive === false) {
|
|
@@ -1499,7 +1505,7 @@ class Request extends module_1.default {
|
|
|
1499
1505
|
agent
|
|
1500
1506
|
}, response => {
|
|
1501
1507
|
const statusCode = response.statusCode;
|
|
1502
|
-
if ((getting || posting) && statusCode >= 200
|
|
1508
|
+
if ((getting || posting) && statusCode >= 200 && statusCode < 300) {
|
|
1503
1509
|
let source = checkEncoding(response, statusCode, response.headers['content-encoding']);
|
|
1504
1510
|
if (source) {
|
|
1505
1511
|
source.once('finish', () => request.emit('end'));
|
|
@@ -1587,7 +1593,7 @@ class Request extends module_1.default {
|
|
|
1587
1593
|
options = contentType;
|
|
1588
1594
|
contentType = undefined;
|
|
1589
1595
|
}
|
|
1590
|
-
if (Array.isArray(data) && (!contentType || contentType === "multipart/form-data"
|
|
1596
|
+
if (Array.isArray(data) && (!contentType || contentType === "multipart/form-data")) {
|
|
1591
1597
|
parts = data;
|
|
1592
1598
|
data = undefined;
|
|
1593
1599
|
}
|
|
@@ -1615,23 +1621,23 @@ class Request extends module_1.default {
|
|
|
1615
1621
|
break;
|
|
1616
1622
|
}
|
|
1617
1623
|
}
|
|
1618
|
-
if (parts || contentType === "multipart/form-data"
|
|
1624
|
+
if (parts || contentType === "multipart/form-data" || contentType === 'form-data') {
|
|
1619
1625
|
let valid;
|
|
1620
1626
|
if ((0, types_1.isArray)(parts)) {
|
|
1621
1627
|
const write = combined.create();
|
|
1622
1628
|
const boundary = (0, types_1.generateUUID)().replace(/-/g, '');
|
|
1623
|
-
contentType = "multipart/form-data"
|
|
1629
|
+
contentType = "multipart/form-data" + `; boundary="${boundary}"`;
|
|
1624
1630
|
let contentLength = 0;
|
|
1625
1631
|
const createPart = (name, filename, type) => {
|
|
1626
|
-
const form = ["--"
|
|
1632
|
+
const form = ["--" + boundary, `Content-Disposition: form-data; name="${escapeQuote(name)}"` + (filename ? `; filename="${escapeQuote(filename)}"` : ''), "\r\n"];
|
|
1627
1633
|
if (type) {
|
|
1628
1634
|
form.splice(2, 0, 'Content-Type: ' + type);
|
|
1629
1635
|
}
|
|
1630
|
-
return form.join("\r\n"
|
|
1636
|
+
return form.join("\r\n");
|
|
1631
1637
|
};
|
|
1632
1638
|
const addValue = (name, value) => {
|
|
1633
1639
|
if (value !== undefined) {
|
|
1634
|
-
const disposition = createPart(name) + module_1.default.asString(value) + "\r\n"
|
|
1640
|
+
const disposition = createPart(name) + module_1.default.asString(value) + "\r\n";
|
|
1635
1641
|
write.append(disposition);
|
|
1636
1642
|
contentLength += Buffer.byteLength(disposition);
|
|
1637
1643
|
}
|
|
@@ -1659,7 +1665,7 @@ class Request extends module_1.default {
|
|
|
1659
1665
|
target = Buffer.concat(chunks);
|
|
1660
1666
|
}
|
|
1661
1667
|
if (!Buffer.isBuffer(target)) {
|
|
1662
|
-
throw (0, types_1.errorMessage)('File', "Unknown"
|
|
1668
|
+
throw (0, types_1.errorMessage)('File', "Unknown", "multipart/form-data");
|
|
1663
1669
|
}
|
|
1664
1670
|
if (!type || !filename) {
|
|
1665
1671
|
const result = await module_1.default.resolveMime(target);
|
|
@@ -1675,11 +1681,11 @@ class Request extends module_1.default {
|
|
|
1675
1681
|
filename = (0, types_1.generateUUID)() + '.' + ext;
|
|
1676
1682
|
}
|
|
1677
1683
|
}
|
|
1678
|
-
const disposition = createPart(name, filename, type || "application/octet-stream"
|
|
1684
|
+
const disposition = createPart(name, filename, type || "application/octet-stream");
|
|
1679
1685
|
write.append(disposition);
|
|
1680
1686
|
write.append(target);
|
|
1681
|
-
write.append("\r\n"
|
|
1682
|
-
contentLength += Buffer.byteLength(disposition) + target.length + 2
|
|
1687
|
+
write.append("\r\n");
|
|
1688
|
+
contentLength += Buffer.byteLength(disposition) + target.length + 2;
|
|
1683
1689
|
valid = true;
|
|
1684
1690
|
}
|
|
1685
1691
|
catch (err) {
|
|
@@ -1692,13 +1698,13 @@ class Request extends module_1.default {
|
|
|
1692
1698
|
}
|
|
1693
1699
|
}
|
|
1694
1700
|
if (valid) {
|
|
1695
|
-
write.append("--"
|
|
1696
|
-
headers['content-length'] = (contentLength + 38
|
|
1701
|
+
write.append("--" + boundary + "--" + "\r\n");
|
|
1702
|
+
headers['content-length'] = (contentLength + 38).toString();
|
|
1697
1703
|
data = write;
|
|
1698
1704
|
}
|
|
1699
1705
|
}
|
|
1700
1706
|
if (!valid) {
|
|
1701
|
-
return Promise.reject((0, types_1.errorValue)('No files were detected', "multipart/form-data"
|
|
1707
|
+
return Promise.reject((0, types_1.errorValue)('No files were detected', "multipart/form-data"));
|
|
1702
1708
|
}
|
|
1703
1709
|
}
|
|
1704
1710
|
else {
|
|
@@ -1706,12 +1712,12 @@ class Request extends module_1.default {
|
|
|
1706
1712
|
contentType = 'application/' + contentType.trim();
|
|
1707
1713
|
}
|
|
1708
1714
|
if ((0, types_1.isPlainObject)(data)) {
|
|
1709
|
-
if (contentType === "application/x-www-form-urlencoded"
|
|
1715
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
1710
1716
|
data = qs.stringify(data);
|
|
1711
1717
|
}
|
|
1712
1718
|
else {
|
|
1713
1719
|
data = JSON.stringify(data);
|
|
1714
|
-
contentType || (contentType = "application/json"
|
|
1720
|
+
contentType || (contentType = "application/json");
|
|
1715
1721
|
}
|
|
1716
1722
|
}
|
|
1717
1723
|
else {
|
|
@@ -1722,10 +1728,10 @@ class Request extends module_1.default {
|
|
|
1722
1728
|
options.method = 'POST';
|
|
1723
1729
|
options.httpVersion = 1;
|
|
1724
1730
|
options.postData = data;
|
|
1725
|
-
headers['content-type'] = contentType || "text/plain"
|
|
1731
|
+
headers['content-type'] = contentType || "text/plain";
|
|
1726
1732
|
return this.get(uri, options);
|
|
1727
1733
|
}
|
|
1728
|
-
get(uri, options = {}) {
|
|
1734
|
+
async get(uri, options = {}) {
|
|
1729
1735
|
const opts = (typeof options === 'string' ? { format: options, encoding: 'utf-8' } : options);
|
|
1730
1736
|
if (this.readExpect === 'string') {
|
|
1731
1737
|
opts.encoding = (0, types_1.getEncoding)(opts?.encoding);
|
|
@@ -1760,7 +1766,7 @@ class Request extends module_1.default {
|
|
|
1760
1766
|
}
|
|
1761
1767
|
if (pipeTo) {
|
|
1762
1768
|
if ((0, types_1.isString)(pipeTo)) {
|
|
1763
|
-
outStream = fs.createWriteStream(pipeTo, { emitClose: false, highWaterMark: request.host.localhost ? 65536
|
|
1769
|
+
outStream = fs.createWriteStream(pipeTo, { emitClose: false, highWaterMark: request.host.localhost ? 65536 : 4096 });
|
|
1764
1770
|
request.outStream = outStream;
|
|
1765
1771
|
}
|
|
1766
1772
|
else {
|
|
@@ -1776,9 +1782,9 @@ class Request extends module_1.default {
|
|
|
1776
1782
|
({ httpVersion, outAbort } = request);
|
|
1777
1783
|
const isAborted = () => client.destroyed || httpVersion === 2 && client.aborted;
|
|
1778
1784
|
const isRetry = (value) => (0, util_1.isRetryable)(value) && ++retries <= this._config.retryLimit;
|
|
1779
|
-
const isUnsupported = (value) => value === 421
|
|
1785
|
+
const isUnsupported = (value) => value === 421 || value === 505;
|
|
1780
1786
|
const isDowngrade = (err) => err instanceof Error && (err.code === 'ERR_HTTP2_ERROR' || isUnsupported(Math.abs(err.errno)));
|
|
1781
|
-
const formatWarning = (message) => LOG_HTTP && !silent && this.formatMessage(1024
|
|
1787
|
+
const formatWarning = (message) => LOG_HTTP && !silent && this.formatMessage(1024, 'HTTP' + httpVersion, [message, host.origin], url.toString(), { titleBgColor: 'bgGrey', titleColor: 'yellow' });
|
|
1782
1788
|
const formatNgFlags = (value, statusCode, location) => location ? `Using HTTP 1.1 for URL redirect (${location})` : formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
1783
1789
|
const abortResponse = () => {
|
|
1784
1790
|
if (closed) {
|
|
@@ -1817,7 +1823,7 @@ class Request extends module_1.default {
|
|
|
1817
1823
|
if (this.readTimeout > 0) {
|
|
1818
1824
|
timeout = setTimeout(() => {
|
|
1819
1825
|
abortResponse();
|
|
1820
|
-
throwError((0, types_1.errorValue)("Timeout was exceeded"
|
|
1826
|
+
throwError((0, types_1.errorValue)("Timeout was exceeded", href.toString()));
|
|
1821
1827
|
}, this.readTimeout);
|
|
1822
1828
|
}
|
|
1823
1829
|
if (log) {
|
|
@@ -1877,19 +1883,19 @@ class Request extends module_1.default {
|
|
|
1877
1883
|
}
|
|
1878
1884
|
}
|
|
1879
1885
|
if (mibsTime) {
|
|
1880
|
-
const unit = (Buffer.byteLength(buffer, encoding) * 8) / ((0, types_1.convertTime)(process.hrtime(mibsTime)) * 1000
|
|
1886
|
+
const unit = (Buffer.byteLength(buffer, encoding) * 8) / ((0, types_1.convertTime)(process.hrtime(mibsTime)) * 1000);
|
|
1881
1887
|
if (unit < 1) {
|
|
1882
|
-
messageUnit = Math.ceil(unit * 1000
|
|
1888
|
+
messageUnit = Math.ceil(unit * 1000) + 'KiB/s';
|
|
1883
1889
|
}
|
|
1884
|
-
else if (unit < 1000
|
|
1890
|
+
else if (unit < 1000) {
|
|
1885
1891
|
messageUnit = unit.toPrecision(3) + 'MiB/s';
|
|
1886
1892
|
}
|
|
1887
1893
|
else {
|
|
1888
|
-
messageUnit = (unit / 1000
|
|
1894
|
+
messageUnit = (unit / 1000).toPrecision(3) + 'GiB/s';
|
|
1889
1895
|
}
|
|
1890
1896
|
}
|
|
1891
1897
|
if (typeof buffer === 'string') {
|
|
1892
|
-
if (buffer
|
|
1898
|
+
if (buffer.startsWith('\uFEFF') && encoding !== 'utf16le') {
|
|
1893
1899
|
buffer = buffer.substring(1);
|
|
1894
1900
|
}
|
|
1895
1901
|
if (outFormat) {
|
|
@@ -1919,7 +1925,7 @@ class Request extends module_1.default {
|
|
|
1919
1925
|
}
|
|
1920
1926
|
catch (err) {
|
|
1921
1927
|
if (!silent && !this[kSingleton] && !(packageName && this.checkPackage(err, packageName))) {
|
|
1922
|
-
this.writeFail(['Unable to parse URI response', format], err, 1024
|
|
1928
|
+
this.writeFail(['Unable to parse URI response', format], err, 1024);
|
|
1923
1929
|
}
|
|
1924
1930
|
result = null;
|
|
1925
1931
|
}
|
|
@@ -1939,7 +1945,7 @@ class Request extends module_1.default {
|
|
|
1939
1945
|
}
|
|
1940
1946
|
resolve(result);
|
|
1941
1947
|
if (log) {
|
|
1942
|
-
this.writeTimeProcess('HTTP' + httpVersion, request.statusMessage || url.toString(), startTime, { type: 1024
|
|
1948
|
+
this.writeTimeProcess('HTTP' + httpVersion, request.statusMessage || url.toString(), startTime, { type: 1024, queue: !!this.host, titleBgColor, messageUnit, messageUnitMinWidth: 9, delayTime, bypassLog: true });
|
|
1943
1949
|
}
|
|
1944
1950
|
});
|
|
1945
1951
|
host.success(httpVersion);
|
|
@@ -2014,7 +2020,7 @@ class Request extends module_1.default {
|
|
|
2014
2020
|
host.failed(2);
|
|
2015
2021
|
if (host.version > 1) {
|
|
2016
2022
|
if (!silent) {
|
|
2017
|
-
this.formatMessage(1024
|
|
2023
|
+
this.formatMessage(1024, 'HTTP2', ['Unsupported protocol', host.origin], message, { failed: true });
|
|
2018
2024
|
}
|
|
2019
2025
|
host.version = 1;
|
|
2020
2026
|
}
|
|
@@ -2027,18 +2033,18 @@ class Request extends module_1.default {
|
|
|
2027
2033
|
return;
|
|
2028
2034
|
}
|
|
2029
2035
|
const statusCode = headers[':status'];
|
|
2030
|
-
if (statusCode < 300
|
|
2036
|
+
if (statusCode < 300) {
|
|
2031
2037
|
acceptResponse(headers);
|
|
2032
2038
|
}
|
|
2033
|
-
else if (statusCode < 400
|
|
2039
|
+
else if (statusCode < 400) {
|
|
2034
2040
|
redirectResponse(statusCode, headers.location);
|
|
2035
2041
|
}
|
|
2036
|
-
else if (statusCode === 401
|
|
2037
|
-
statusCode === 402
|
|
2038
|
-
statusCode === 403
|
|
2039
|
-
statusCode === 404
|
|
2040
|
-
statusCode === 407
|
|
2041
|
-
statusCode === 410
|
|
2042
|
+
else if (statusCode === 401 ||
|
|
2043
|
+
statusCode === 402 ||
|
|
2044
|
+
statusCode === 403 ||
|
|
2045
|
+
statusCode === 404 ||
|
|
2046
|
+
statusCode === 407 ||
|
|
2047
|
+
statusCode === 410) {
|
|
2042
2048
|
throwError(formatStatus(statusCode), outAbort);
|
|
2043
2049
|
}
|
|
2044
2050
|
else if (isRetry(statusCode)) {
|
|
@@ -2074,10 +2080,10 @@ class Request extends module_1.default {
|
|
|
2074
2080
|
return;
|
|
2075
2081
|
}
|
|
2076
2082
|
switch (!isDowngrade(err) && await host.hasProtocol(2)) {
|
|
2077
|
-
case 1
|
|
2083
|
+
case 1:
|
|
2078
2084
|
errorResponse(err);
|
|
2079
2085
|
break;
|
|
2080
|
-
case 2
|
|
2086
|
+
case 2:
|
|
2081
2087
|
retryDownload(false, err);
|
|
2082
2088
|
break;
|
|
2083
2089
|
default:
|
|
@@ -2093,10 +2099,10 @@ class Request extends module_1.default {
|
|
|
2093
2099
|
return;
|
|
2094
2100
|
}
|
|
2095
2101
|
const statusCode = res.statusCode;
|
|
2096
|
-
if (statusCode < 300
|
|
2102
|
+
if (statusCode < 300) {
|
|
2097
2103
|
acceptResponse(res.headers);
|
|
2098
2104
|
}
|
|
2099
|
-
else if (statusCode < 400
|
|
2105
|
+
else if (statusCode < 400) {
|
|
2100
2106
|
redirectResponse(statusCode, res.headers.location);
|
|
2101
2107
|
}
|
|
2102
2108
|
else if (isRetry(statusCode)) {
|
|
@@ -2126,7 +2132,7 @@ class Request extends module_1.default {
|
|
|
2126
2132
|
retryTimeout();
|
|
2127
2133
|
}
|
|
2128
2134
|
else {
|
|
2129
|
-
throwError(formatStatus(408
|
|
2135
|
+
throwError(formatStatus(408));
|
|
2130
2136
|
}
|
|
2131
2137
|
});
|
|
2132
2138
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/anpham6/e-mc.git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
13
13
|
"directory": "src/request"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"squared-functions"
|
|
18
18
|
],
|
|
19
19
|
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
-
"license": "
|
|
20
|
+
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.5.
|
|
24
|
-
"@e-mc/types": "0.5.
|
|
23
|
+
"@e-mc/module": "0.5.4",
|
|
24
|
+
"@e-mc/types": "0.5.4",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|
package/util.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseHeader = void 0;
|
|
4
|
-
const module_1 = require("
|
|
5
|
-
const types_1 = require("
|
|
4
|
+
const module_1 = require("@e-mc/module");
|
|
5
|
+
const types_1 = require("@e-mc/types");
|
|
6
6
|
const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
|
|
7
7
|
function parseHeader(headers, name) {
|
|
8
8
|
const value = headers[name];
|
|
@@ -87,19 +87,19 @@ function checkRetryable(err) {
|
|
|
87
87
|
exports.checkRetryable = checkRetryable;
|
|
88
88
|
function isRetryable(value, timeout) {
|
|
89
89
|
switch (value) {
|
|
90
|
-
case 408
|
|
91
|
-
case 504
|
|
92
|
-
case 522
|
|
93
|
-
case 524
|
|
90
|
+
case 408:
|
|
91
|
+
case 504:
|
|
92
|
+
case 522:
|
|
93
|
+
case 524:
|
|
94
94
|
if (timeout) {
|
|
95
95
|
return true;
|
|
96
96
|
}
|
|
97
|
-
case 429
|
|
98
|
-
case 499
|
|
99
|
-
case 500
|
|
100
|
-
case 502
|
|
101
|
-
case 503
|
|
102
|
-
case 521
|
|
97
|
+
case 429:
|
|
98
|
+
case 499:
|
|
99
|
+
case 500:
|
|
100
|
+
case 502:
|
|
101
|
+
case 503:
|
|
102
|
+
case 521:
|
|
103
103
|
if (!timeout) {
|
|
104
104
|
return true;
|
|
105
105
|
}
|
|
@@ -138,9 +138,9 @@ exports.asFloat = asFloat;
|
|
|
138
138
|
function fromSeconds(value) {
|
|
139
139
|
switch (typeof value) {
|
|
140
140
|
case 'string':
|
|
141
|
-
return safeInt(
|
|
141
|
+
return safeInt(parseInt(value) * 1000);
|
|
142
142
|
case 'number':
|
|
143
|
-
return safeInt(value * 1000
|
|
143
|
+
return safeInt(Math.trunc(value) * 1000);
|
|
144
144
|
default:
|
|
145
145
|
return NaN;
|
|
146
146
|
}
|