@e-mc/request 0.5.2 → 0.6.0
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/http/host/index.js +5 -8
- package/index.js +276 -240
- package/package.json +3 -3
package/http/host/index.js
CHANGED
|
@@ -107,7 +107,7 @@ class HttpHost {
|
|
|
107
107
|
});
|
|
108
108
|
socket
|
|
109
109
|
.setNoDelay(false)
|
|
110
|
-
.setTimeout(
|
|
110
|
+
.setTimeout(10000 /* HOST.TIMEOUT */)
|
|
111
111
|
.on('timeout', () => {
|
|
112
112
|
if (this._tlsConnect) {
|
|
113
113
|
if (this.error(version) >= 10 /* HOST.MAX_ERROR */) {
|
|
@@ -173,10 +173,11 @@ class HttpHost {
|
|
|
173
173
|
}
|
|
174
174
|
return false;
|
|
175
175
|
};
|
|
176
|
-
const pattern = new RegExp(`h${i + 1}
|
|
176
|
+
const pattern = new RegExp(`h${i + 1}(?:-\\d+)?="([^:]*):(\\d+)"([^,]*)`, 'g');
|
|
177
177
|
const addresses = [];
|
|
178
178
|
const time = Date.now();
|
|
179
179
|
const hostname = this[kHostname];
|
|
180
|
+
const excluded = this[kAltSvcError];
|
|
180
181
|
let match;
|
|
181
182
|
while (match = pattern.exec(altSvc)) {
|
|
182
183
|
const port = match[2];
|
|
@@ -187,7 +188,7 @@ class HttpHost {
|
|
|
187
188
|
}
|
|
188
189
|
const address = match[1] || hostname;
|
|
189
190
|
const ma = +(/ma=(\d+)/.exec(match[3])?.[1] || 86400);
|
|
190
|
-
if (!
|
|
191
|
+
if (!excluded.includes(`h${i + 1}:${address}:${port}`)) {
|
|
191
192
|
addresses.push([
|
|
192
193
|
address,
|
|
193
194
|
port,
|
|
@@ -278,11 +279,7 @@ class HttpHost {
|
|
|
278
279
|
this.closeAltSvc();
|
|
279
280
|
this[kAltSvcQueue] = [];
|
|
280
281
|
this[kAltSvcError] = [];
|
|
281
|
-
this[kVersionData].forEach(item =>
|
|
282
|
-
if (item[3 /* HOST_VERSION.ALPN */] !== 0) {
|
|
283
|
-
item[4 /* HOST_VERSION.ALT_SVC */] = -1;
|
|
284
|
-
}
|
|
285
|
-
});
|
|
282
|
+
this[kVersionData].forEach(item => item[3 /* HOST_VERSION.ALPN */] && (item[4 /* HOST_VERSION.ALT_SVC */] = -1));
|
|
286
283
|
}
|
|
287
284
|
}
|
|
288
285
|
flagAltSvc(version, value) {
|
package/index.js
CHANGED
|
@@ -53,6 +53,8 @@ const DNS = {
|
|
|
53
53
|
};
|
|
54
54
|
const ARIA2 = {
|
|
55
55
|
BIN: which.sync(PLATFORM_WIN32 ? 'aria2c.exe' : 'aria2c', { nothrow: true }) || '',
|
|
56
|
+
EXEC_UID: undefined,
|
|
57
|
+
EXEC_GID: undefined,
|
|
56
58
|
PID_TIMER: null,
|
|
57
59
|
PID_QUEUE: [],
|
|
58
60
|
UPDATE_STATUS: 0,
|
|
@@ -76,6 +78,14 @@ let AGENT_TIMEOUT = 0;
|
|
|
76
78
|
let LOG_HTTP = false;
|
|
77
79
|
let LOG_TIMEPROCESS = true;
|
|
78
80
|
let LOG_TIMEFORMAT = 'readable';
|
|
81
|
+
let LIB_ZSTD = null;
|
|
82
|
+
try {
|
|
83
|
+
require('zstd-codec/lib/zstd-stream').run(codec => {
|
|
84
|
+
LIB_ZSTD = codec;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
}
|
|
79
89
|
function getBaseHeaders(uri, headers) {
|
|
80
90
|
uri = (0, util_1.trimPath)(uri);
|
|
81
91
|
let result;
|
|
@@ -122,7 +132,7 @@ function setOutgoingHeaders(output, headers) {
|
|
|
122
132
|
}
|
|
123
133
|
function getProxySettings(request, agentTimeout) {
|
|
124
134
|
const proxy = request.proxy;
|
|
125
|
-
if (
|
|
135
|
+
if (proxy?.address && proxy.port) {
|
|
126
136
|
const port = (0, util_1.asInt)(proxy.port);
|
|
127
137
|
const address = (!module_1.default.isURL(proxy.address) ? port === 80 ? 'http://' : 'https://' : '') + proxy.address;
|
|
128
138
|
try {
|
|
@@ -144,11 +154,9 @@ function getProxySettings(request, agentTimeout) {
|
|
|
144
154
|
return null;
|
|
145
155
|
}
|
|
146
156
|
function closeTorrent(pid) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
ARIA2.PID_QUEUE.splice(index, 1);
|
|
151
|
-
}
|
|
157
|
+
const index = ARIA2.PID_QUEUE.findIndex(value => pid === value[0]);
|
|
158
|
+
if (index !== -1) {
|
|
159
|
+
ARIA2.PID_QUEUE.splice(index, 1);
|
|
152
160
|
}
|
|
153
161
|
}
|
|
154
162
|
function clearDnsLookup() {
|
|
@@ -266,7 +274,7 @@ class Request extends module_1.default {
|
|
|
266
274
|
if (!this.enabled("process.password" /* KEY_NAME.PROCESS_PASSWORD */) || super.loadSettings({ process: settings.process }, password)) {
|
|
267
275
|
const { request, download } = settings;
|
|
268
276
|
if (download && (0, types_1.isPlainObject)(download.aria2)) {
|
|
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;
|
|
277
|
+
let { bin, exec, 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;
|
|
270
278
|
const parseSize = (value, zero) => {
|
|
271
279
|
if (zero && (value === '0' || value === 0)) {
|
|
272
280
|
return 0;
|
|
@@ -281,6 +289,11 @@ class Request extends module_1.default {
|
|
|
281
289
|
else if (bin && this.isPath(bin = path.resolve(bin), true)) {
|
|
282
290
|
ARIA2.BIN = bin;
|
|
283
291
|
}
|
|
292
|
+
if ((0, types_1.isPlainObject)(exec)) {
|
|
293
|
+
let { uid, gid } = exec;
|
|
294
|
+
ARIA2.EXEC_UID = (uid = (0, util_1.asInt)(uid)) >= 0 ? uid : undefined;
|
|
295
|
+
ARIA2.EXEC_GID = (gid = (0, util_1.asInt)(gid)) >= 0 ? gid : undefined;
|
|
296
|
+
}
|
|
284
297
|
if ((0, types_1.isPlainObject)(update_status)) {
|
|
285
298
|
const { interval, broadcast_only } = update_status;
|
|
286
299
|
if (typeof broadcast_only === 'boolean') {
|
|
@@ -409,124 +422,141 @@ class Request extends module_1.default {
|
|
|
409
422
|
return url.protocol + '//' + (auth && (auth + '@')) + url.hostname + (url.port ? ':' + url.port : '') + (value[0] !== '/' ? '/' : '') + value;
|
|
410
423
|
}
|
|
411
424
|
static fromStatusCode(value) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
case 507 /* HTTP_STATUS.INSUFFICIENT_STORAGE */:
|
|
520
|
-
return 'Insufficient Storage';
|
|
521
|
-
case 508 /* HTTP_STATUS.LOOP_DETECTED */:
|
|
522
|
-
return 'Loop Detected';
|
|
523
|
-
case 510 /* HTTP_STATUS.NOT_EXTENDED */:
|
|
524
|
-
return 'Not Extended';
|
|
525
|
-
case 511 /* HTTP_STATUS.NETWORK_AUTHENTICATION_REQUIRED */:
|
|
526
|
-
return 'Network Authentication Required';
|
|
527
|
-
default:
|
|
528
|
-
return "Unknown" /* ERR_MESSAGE.UNKNOWN */;
|
|
425
|
+
if ((value = +value) < 200) {
|
|
426
|
+
switch (value) {
|
|
427
|
+
case 100 /* HTTP_STATUS.CONTINUE */:
|
|
428
|
+
return 'Continue';
|
|
429
|
+
case 101 /* HTTP_STATUS.SWITCHING_PROTOCOL */:
|
|
430
|
+
return 'Switching Protocol';
|
|
431
|
+
case 102 /* HTTP_STATUS.PROCESSING */:
|
|
432
|
+
return 'Processing';
|
|
433
|
+
case 103 /* HTTP_STATUS.EARLY_HINTS */:
|
|
434
|
+
return 'Early Hints';
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else if (value < 300) {
|
|
438
|
+
switch (value) {
|
|
439
|
+
case 200 /* HTTP_STATUS.OK */:
|
|
440
|
+
return 'OK';
|
|
441
|
+
case 201 /* HTTP_STATUS.CREATED */:
|
|
442
|
+
return 'Created';
|
|
443
|
+
case 202 /* HTTP_STATUS.ACCEPTED */:
|
|
444
|
+
return 'Accepted';
|
|
445
|
+
case 203 /* HTTP_STATUS.NON_AUTHORITATIVE_INFORMATION */:
|
|
446
|
+
return 'Non-Authoritative Information';
|
|
447
|
+
case 204 /* HTTP_STATUS.NO_CONTENT */:
|
|
448
|
+
return 'No Content';
|
|
449
|
+
case 205 /* HTTP_STATUS.RESET_CONTENT */:
|
|
450
|
+
return 'Reset Content';
|
|
451
|
+
case 206 /* HTTP_STATUS.PARTIAL_CONTENT */:
|
|
452
|
+
return 'Partial Content';
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
else if (value < 400) {
|
|
456
|
+
switch (value) {
|
|
457
|
+
case 300 /* HTTP_STATUS.MULTIPLE_CHOICES */:
|
|
458
|
+
return 'Multiple Choice';
|
|
459
|
+
case 301 /* HTTP_STATUS.MOVED_PERMANENTLY */:
|
|
460
|
+
return 'Moved Permanently';
|
|
461
|
+
case 302 /* HTTP_STATUS.FOUND */:
|
|
462
|
+
return 'Found';
|
|
463
|
+
case 303 /* HTTP_STATUS.SEE_OTHER */:
|
|
464
|
+
return 'See Other';
|
|
465
|
+
case 304 /* HTTP_STATUS.NOT_MODIFIED */:
|
|
466
|
+
return 'Not Modified';
|
|
467
|
+
case 305 /* HTTP_STATUS.USE_PROXY */:
|
|
468
|
+
return 'Use Proxy';
|
|
469
|
+
case 307 /* HTTP_STATUS.TEMPORARY_REDIRECT */:
|
|
470
|
+
return 'Temporary Redirect';
|
|
471
|
+
case 308 /* HTTP_STATUS.PERMANENT_REDIRECT */:
|
|
472
|
+
return 'Permanent Redirect';
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
else if (value < 500) {
|
|
476
|
+
switch (value) {
|
|
477
|
+
case 400 /* HTTP_STATUS.BAD_REQUEST */:
|
|
478
|
+
return 'Bad Request';
|
|
479
|
+
case 401 /* HTTP_STATUS.UNAUTHORIZED */:
|
|
480
|
+
return 'Upgrade Required';
|
|
481
|
+
case 402 /* HTTP_STATUS.PAYMENT_REQUIRED */:
|
|
482
|
+
return 'Payment Required';
|
|
483
|
+
case 403 /* HTTP_STATUS.FORBIDDEN */:
|
|
484
|
+
return 'Forbidden';
|
|
485
|
+
case 404 /* HTTP_STATUS.NOT_FOUND */:
|
|
486
|
+
return 'Not Found';
|
|
487
|
+
case 405 /* HTTP_STATUS.METHOD_NOT_ALLOWED */:
|
|
488
|
+
return 'Method Not Allowed';
|
|
489
|
+
case 406 /* HTTP_STATUS.NOT_ACCEPTABLE */:
|
|
490
|
+
return 'Not Acceptable';
|
|
491
|
+
case 407 /* HTTP_STATUS.PROXY_AUTHENTICATION_REQUIRED */:
|
|
492
|
+
return 'Proxy Authentication Required';
|
|
493
|
+
case 408 /* HTTP_STATUS.REQUEST_TIMEOUT */:
|
|
494
|
+
return 'Request Timeout';
|
|
495
|
+
case 409 /* HTTP_STATUS.CONFLICT */:
|
|
496
|
+
return 'Conflict';
|
|
497
|
+
case 410 /* HTTP_STATUS.GONE */:
|
|
498
|
+
return 'Gone';
|
|
499
|
+
case 411 /* HTTP_STATUS.LENGTH_REQUIRED */:
|
|
500
|
+
return 'Length Required';
|
|
501
|
+
case 412 /* HTTP_STATUS.PRECONDITION_FAILED */:
|
|
502
|
+
return 'Precondition Failed';
|
|
503
|
+
case 413 /* HTTP_STATUS.PAYLOAD_TOO_LARGE */:
|
|
504
|
+
return 'Payload Too Large';
|
|
505
|
+
case 414 /* HTTP_STATUS.REQUEST_URI_TOO_LONG */:
|
|
506
|
+
return 'URI Too Long';
|
|
507
|
+
case 415 /* HTTP_STATUS.UNSUPPORTED_MEDIA_TYPE */:
|
|
508
|
+
return 'Unsupported Media Type';
|
|
509
|
+
case 416 /* HTTP_STATUS.RANGE_NOT_SATISFIABLE */:
|
|
510
|
+
return 'Range Not Satisfiable';
|
|
511
|
+
case 417 /* HTTP_STATUS.EXPECTATION_FAILED */:
|
|
512
|
+
return 'Expectation Failed';
|
|
513
|
+
case 421 /* HTTP_STATUS.MISDIRECTED_REQUEST */:
|
|
514
|
+
return 'Misdirected Request';
|
|
515
|
+
case 422 /* HTTP_STATUS.UNPROCESSABLE_ENTITY */:
|
|
516
|
+
return 'Unprocessable Entity';
|
|
517
|
+
case 423 /* HTTP_STATUS.LOCKED */:
|
|
518
|
+
return 'Locked';
|
|
519
|
+
case 424 /* HTTP_STATUS.FAILED_DEPENDENCY */:
|
|
520
|
+
return 'Failed Dependency';
|
|
521
|
+
case 426 /* HTTP_STATUS.UPGRADE_REQUIRED */:
|
|
522
|
+
return 'Upgrade Required';
|
|
523
|
+
case 428 /* HTTP_STATUS.PRECONDITION_REQUIRED */:
|
|
524
|
+
return 'Precondition Required';
|
|
525
|
+
case 429 /* HTTP_STATUS.TOO_MANY_REQUESTS */:
|
|
526
|
+
return 'Too Many Requests';
|
|
527
|
+
case 431 /* HTTP_STATUS.REQUEST_HEADER_FIELDS_TOO_LARGE */:
|
|
528
|
+
return 'Request Header Fields Too Large';
|
|
529
|
+
case 451 /* HTTP_STATUS.UNAVAILABLE_FOR_LEGAL_REASONS */:
|
|
530
|
+
return 'Unavailable For Legal Reasons';
|
|
531
|
+
}
|
|
529
532
|
}
|
|
533
|
+
else {
|
|
534
|
+
switch (value) {
|
|
535
|
+
case 500 /* HTTP_STATUS.INTERNAL_SERVER_ERROR */:
|
|
536
|
+
return 'Internal Server Error';
|
|
537
|
+
case 501 /* HTTP_STATUS.NOT_IMPLEMENTED */:
|
|
538
|
+
return 'Not Implemented';
|
|
539
|
+
case 502 /* HTTP_STATUS.BAD_GATEWAY */:
|
|
540
|
+
return 'Bad Gateway';
|
|
541
|
+
case 503 /* HTTP_STATUS.SERVICE_UNAVAILABLE */:
|
|
542
|
+
return 'Service Unavailable';
|
|
543
|
+
case 504 /* HTTP_STATUS.GATEWAY_TIMEOUT */:
|
|
544
|
+
return 'Gateway Timeout';
|
|
545
|
+
case 505 /* HTTP_STATUS.HTTP_VERSION_NOT_SUPPORTED */:
|
|
546
|
+
return 'HTTP Version Not Supported';
|
|
547
|
+
case 506 /* HTTP_STATUS.VARIANT_ALSO_NEGOTIATES */:
|
|
548
|
+
return 'Variant Also Negotiates';
|
|
549
|
+
case 507 /* HTTP_STATUS.INSUFFICIENT_STORAGE */:
|
|
550
|
+
return 'Insufficient Storage';
|
|
551
|
+
case 508 /* HTTP_STATUS.LOOP_DETECTED */:
|
|
552
|
+
return 'Loop Detected';
|
|
553
|
+
case 510 /* HTTP_STATUS.NOT_EXTENDED */:
|
|
554
|
+
return 'Not Extended';
|
|
555
|
+
case 511 /* HTTP_STATUS.NETWORK_AUTHENTICATION_REQUIRED */:
|
|
556
|
+
return 'Network Authentication Required';
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return "Unknown" /* ERR_MESSAGE.UNKNOWN */;
|
|
530
560
|
}
|
|
531
561
|
static defineHttpAgent(options) {
|
|
532
562
|
const { keepAlive, timeout = 0 } = options;
|
|
@@ -609,11 +639,8 @@ class Request extends module_1.default {
|
|
|
609
639
|
this.readTimeout = (value = (0, util_1.fromSeconds)(read_timeout)) >= 0 ? value : READ_TIMEOUT;
|
|
610
640
|
this.keepAlive = typeof (value = agent?.keep_alive) === 'boolean' ? value : KEEP_ALIVE;
|
|
611
641
|
this.acceptEncoding = typeof (value = use?.accept_encoding) === 'boolean' ? value : ACCEPT_ENCODING;
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
case 2:
|
|
615
|
-
this[kHttpVersion] = value;
|
|
616
|
-
break;
|
|
642
|
+
if ((value = (0, util_1.asInt)(use?.http_version)) === 1 || value === 2) {
|
|
643
|
+
this[kHttpVersion] = value;
|
|
617
644
|
}
|
|
618
645
|
this[kIpVersion] = (value = (0, util_1.asInt)(data.dns?.family)) && (value === 4 || value === 6) ? value : 0;
|
|
619
646
|
if ((value = (0, util_1.fromSeconds)(agent?.timeout)) >= 0) {
|
|
@@ -796,28 +823,26 @@ class Request extends module_1.default {
|
|
|
796
823
|
return this;
|
|
797
824
|
}
|
|
798
825
|
addDns(hostname, address, family) {
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
826
|
+
switch (family) {
|
|
827
|
+
case 'IPv4':
|
|
828
|
+
family = 4;
|
|
829
|
+
break;
|
|
830
|
+
case 'IPv6':
|
|
831
|
+
family = 6;
|
|
832
|
+
break;
|
|
833
|
+
default:
|
|
834
|
+
if (net.isIPv4(address)) {
|
|
802
835
|
family = 4;
|
|
803
|
-
|
|
804
|
-
|
|
836
|
+
}
|
|
837
|
+
else if (net.isIPv6(address)) {
|
|
805
838
|
family = 6;
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
else if (net.isIPv6(address)) {
|
|
812
|
-
family = 6;
|
|
813
|
-
}
|
|
814
|
-
else {
|
|
815
|
-
return;
|
|
816
|
-
}
|
|
817
|
-
break;
|
|
818
|
-
}
|
|
819
|
-
setDnsCache(hostname, this[kConnectDns][hostname] = [{ address, family }]);
|
|
839
|
+
}
|
|
840
|
+
else {
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
break;
|
|
820
844
|
}
|
|
845
|
+
setDnsCache(hostname, this[kConnectDns][hostname] = [{ address, family }]);
|
|
821
846
|
}
|
|
822
847
|
lookupDns(hostname) {
|
|
823
848
|
var _l;
|
|
@@ -887,17 +912,18 @@ class Request extends module_1.default {
|
|
|
887
912
|
}
|
|
888
913
|
proxyOf(uri, localhost) {
|
|
889
914
|
const proxy = this.proxy || this.host && HTTP.PROXY;
|
|
890
|
-
if (proxy) {
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
915
|
+
if (!proxy) {
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const { include, exclude } = proxy;
|
|
919
|
+
if (!include && !exclude && !localhost) {
|
|
920
|
+
return proxy;
|
|
921
|
+
}
|
|
922
|
+
if ((0, types_1.isArray)(include)) {
|
|
923
|
+
return include.some(value => uri.startsWith(value)) ? proxy : undefined;
|
|
924
|
+
}
|
|
925
|
+
if (Array.isArray(exclude) && !exclude.some(value => uri.startsWith(value))) {
|
|
926
|
+
return proxy;
|
|
901
927
|
}
|
|
902
928
|
}
|
|
903
929
|
headersOf(uri) {
|
|
@@ -1158,7 +1184,7 @@ class Request extends module_1.default {
|
|
|
1158
1184
|
closeTorrent(pid);
|
|
1159
1185
|
reject(err);
|
|
1160
1186
|
};
|
|
1161
|
-
const { pid, stdout, stderr } = child_process.spawn(module_1.default.sanitizeCmd(ARIA2.BIN), args, { cwd: pathname, shell: true, signal: this.signal })
|
|
1187
|
+
const { pid, stdout, stderr } = child_process.spawn(module_1.default.sanitizeCmd(ARIA2.BIN), args, { cwd: pathname, shell: true, signal: this.signal, uid: ARIA2.EXEC_UID, gid: ARIA2.EXEC_GID })
|
|
1162
1188
|
.on('exit', code => {
|
|
1163
1189
|
closeTorrent(pid);
|
|
1164
1190
|
if (aborted) {
|
|
@@ -1291,23 +1317,23 @@ class Request extends module_1.default {
|
|
|
1291
1317
|
headers || (headers = {});
|
|
1292
1318
|
switch (format = format.trim().toLowerCase()) {
|
|
1293
1319
|
case 'yaml':
|
|
1294
|
-
headers
|
|
1320
|
+
headers.accept = 'application/yaml, application/x-yaml, text/yaml, text/x-yaml';
|
|
1295
1321
|
break;
|
|
1296
1322
|
case 'json5':
|
|
1297
|
-
headers
|
|
1323
|
+
headers.accept = 'application/json5, application/json, text/javascript';
|
|
1298
1324
|
break;
|
|
1299
1325
|
case 'xml':
|
|
1300
|
-
headers
|
|
1326
|
+
headers.accept = 'application/xml, text/xml';
|
|
1301
1327
|
break;
|
|
1302
1328
|
case 'toml':
|
|
1303
|
-
headers
|
|
1329
|
+
headers.accept = 'application/toml';
|
|
1304
1330
|
break;
|
|
1305
1331
|
default:
|
|
1306
|
-
headers
|
|
1332
|
+
headers.accept = 'application/json, text/javascript';
|
|
1307
1333
|
format = 'json';
|
|
1308
1334
|
break;
|
|
1309
1335
|
}
|
|
1310
|
-
headers
|
|
1336
|
+
headers.accept += ', text/plain';
|
|
1311
1337
|
options.encoding = (0, types_1.getEncoding)(encoding);
|
|
1312
1338
|
options.outFormat = { out: format, parser };
|
|
1313
1339
|
}
|
|
@@ -1346,6 +1372,11 @@ class Request extends module_1.default {
|
|
|
1346
1372
|
case 'deflate-raw':
|
|
1347
1373
|
pipeTo = zlib.createInflateRaw({ chunkSize });
|
|
1348
1374
|
break;
|
|
1375
|
+
case 'zstd':
|
|
1376
|
+
if (LIB_ZSTD) {
|
|
1377
|
+
pipeTo = new LIB_ZSTD.ZstdDecompressTransform({ writableHighWaterMark: chunkSize });
|
|
1378
|
+
}
|
|
1379
|
+
break;
|
|
1349
1380
|
}
|
|
1350
1381
|
if (pipeTo) {
|
|
1351
1382
|
if (outStream) {
|
|
@@ -1357,21 +1388,22 @@ class Request extends module_1.default {
|
|
|
1357
1388
|
return pipeTo;
|
|
1358
1389
|
}
|
|
1359
1390
|
};
|
|
1360
|
-
const { hostname, origin } = host;
|
|
1391
|
+
const { hostname, origin, secure, localhost } = host;
|
|
1361
1392
|
const pathname = url.pathname + (socketPath ? '' : url.search);
|
|
1362
|
-
const proxy = this.proxyOf(uri,
|
|
1393
|
+
const proxy = this.proxyOf(uri, localhost);
|
|
1394
|
+
const version = this.httpVersion;
|
|
1363
1395
|
let request, ca, cert, key, minVersion, baseHeaders = this.headersOf(uri);
|
|
1364
|
-
if (getting && this.acceptEncoding && !
|
|
1365
|
-
(_l = (headers || (headers = {})))['accept-encoding'] || (_l['accept-encoding'] = 'gzip, deflate, br');
|
|
1396
|
+
if (getting && this.acceptEncoding && !localhost && !baseHeaders?.['accept-encoding']) {
|
|
1397
|
+
(_l = (headers || (headers = {})))['accept-encoding'] || (_l['accept-encoding'] = 'gzip, deflate, br' + (LIB_ZSTD ? ', zstd' : ''));
|
|
1366
1398
|
}
|
|
1367
|
-
if (
|
|
1368
|
-
const
|
|
1369
|
-
if (
|
|
1370
|
-
({ ca, cert, key, version: minVersion } =
|
|
1399
|
+
if (secure) {
|
|
1400
|
+
const certs = this[kCerts]?.[0][origin] || (this.host ? TLS.TEXT[origin] : null);
|
|
1401
|
+
if (certs) {
|
|
1402
|
+
({ ca, cert, key, version: minVersion } = certs);
|
|
1371
1403
|
}
|
|
1372
1404
|
}
|
|
1373
|
-
if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 &&
|
|
1374
|
-
request = ((_m = this[kSession][0])[origin] || (_m[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
|
|
1405
|
+
if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && version !== 1 || secure && version === 2 && host.failed(2, true) === 0)) {
|
|
1406
|
+
request = ((_m = this[kSession][0])[origin] || (_m[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion, settings: localhost ? { initialWindowSize: 4294967295 /* CONSTANTS.INITIAL_WINDOW_SIZE */, maxFrameSize: 16777215 /* CONSTANTS.MAX_FRAME_SIZE */, enablePush: false } : { enablePush: false } }))).request({ ...baseHeaders, ...host_1.default.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
|
|
1375
1407
|
if (getting) {
|
|
1376
1408
|
const listenerMap = {};
|
|
1377
1409
|
const onEvent = request.on.bind(request);
|
|
@@ -1451,7 +1483,7 @@ class Request extends module_1.default {
|
|
|
1451
1483
|
let agent;
|
|
1452
1484
|
if (!socketPath) {
|
|
1453
1485
|
if (proxy) {
|
|
1454
|
-
const pkg =
|
|
1486
|
+
const pkg = secure ? 'https-proxy-agent' : 'http-proxy-agent';
|
|
1455
1487
|
try {
|
|
1456
1488
|
const { protocol, hostname: proxyname, port, username, password, href } = proxy.host;
|
|
1457
1489
|
keepAlive ?? (keepAlive = proxy.keepAlive || false);
|
|
@@ -1467,16 +1499,16 @@ class Request extends module_1.default {
|
|
|
1467
1499
|
}
|
|
1468
1500
|
}
|
|
1469
1501
|
else if (keepAlive === false) {
|
|
1470
|
-
agent = new (
|
|
1502
|
+
agent = new (secure ? https.Agent : http.Agent)({ keepAlive: false });
|
|
1471
1503
|
}
|
|
1472
1504
|
else if (keepAlive === true || agentTimeout > 0) {
|
|
1473
|
-
agent = new (
|
|
1505
|
+
agent = new (secure ? https.Agent : http.Agent)({ keepAlive: true, timeout: agentTimeout });
|
|
1474
1506
|
}
|
|
1475
1507
|
else if (agentTimeout !== 0) {
|
|
1476
1508
|
keepAlive = this.keepAlive || false;
|
|
1477
1509
|
agentTimeout ?? (agentTimeout = this.agentTimeout);
|
|
1478
1510
|
if (keepAlive || agentTimeout > 0) {
|
|
1479
|
-
agent = new (
|
|
1511
|
+
agent = new (secure ? https.Agent : http.Agent)({ keepAlive, timeout: agentTimeout });
|
|
1480
1512
|
}
|
|
1481
1513
|
}
|
|
1482
1514
|
}
|
|
@@ -1484,7 +1516,7 @@ class Request extends module_1.default {
|
|
|
1484
1516
|
if (baseHeaders || basicAuth) {
|
|
1485
1517
|
headers = { ...baseHeaders, ...basicAuth, ...headers };
|
|
1486
1518
|
}
|
|
1487
|
-
request = (
|
|
1519
|
+
request = (secure ? https : http).request(socketPath ? { socketPath, path: pathname } : {
|
|
1488
1520
|
protocol: host.protocol,
|
|
1489
1521
|
hostname,
|
|
1490
1522
|
port: host.port,
|
|
@@ -1500,7 +1532,8 @@ class Request extends module_1.default {
|
|
|
1500
1532
|
}, response => {
|
|
1501
1533
|
const statusCode = response.statusCode;
|
|
1502
1534
|
if ((getting || posting) && statusCode >= 200 /* HTTP_STATUS.OK */ && statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */) {
|
|
1503
|
-
|
|
1535
|
+
const incoming = response.headers;
|
|
1536
|
+
let source = checkEncoding(response, statusCode, incoming['content-encoding']);
|
|
1504
1537
|
if (source) {
|
|
1505
1538
|
source.once('finish', () => request.emit('end'));
|
|
1506
1539
|
}
|
|
@@ -1531,8 +1564,13 @@ class Request extends module_1.default {
|
|
|
1531
1564
|
if (response !== source) {
|
|
1532
1565
|
source.on('error', err => request.emit('error', err));
|
|
1533
1566
|
}
|
|
1534
|
-
if (!posting
|
|
1535
|
-
|
|
1567
|
+
if (!posting) {
|
|
1568
|
+
if (version === 2 && incoming.upgrade?.includes('h2')) {
|
|
1569
|
+
host.version = 2;
|
|
1570
|
+
}
|
|
1571
|
+
else if (!host.didAltSvc(1)) {
|
|
1572
|
+
host.upgrade(1, incoming['alt-svc']);
|
|
1573
|
+
}
|
|
1536
1574
|
}
|
|
1537
1575
|
if (LOG_HTTP) {
|
|
1538
1576
|
this[kConnectHttp][0][origin] = (this[kConnectHttp][0][origin] || 0) + 1;
|
|
@@ -1608,11 +1646,9 @@ class Request extends module_1.default {
|
|
|
1608
1646
|
}
|
|
1609
1647
|
const headers = options.headers || (options.headers = {});
|
|
1610
1648
|
for (const attr in headers) {
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
delete headers[attr];
|
|
1615
|
-
break;
|
|
1649
|
+
const name = attr.toLowerCase();
|
|
1650
|
+
if (name === 'content-type' || name === 'content-length') {
|
|
1651
|
+
delete headers[attr];
|
|
1616
1652
|
}
|
|
1617
1653
|
}
|
|
1618
1654
|
if (parts || contentType === "multipart/form-data" /* MIME.MULTIPART */ || contentType === 'form-data') {
|
|
@@ -1643,53 +1679,54 @@ class Request extends module_1.default {
|
|
|
1643
1679
|
}
|
|
1644
1680
|
}
|
|
1645
1681
|
for (let { name, data: target, value, contentType: type, filename } of parts) {
|
|
1646
|
-
if ((0, types_1.isString)(name)) {
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1682
|
+
if (!(0, types_1.isString)(name)) {
|
|
1683
|
+
continue;
|
|
1684
|
+
}
|
|
1685
|
+
if (target) {
|
|
1686
|
+
try {
|
|
1687
|
+
if (typeof target === 'string') {
|
|
1688
|
+
filename || (filename = path.basename(target));
|
|
1689
|
+
type || (type = module_1.default.lookupMime(filename));
|
|
1690
|
+
target = fs.readFileSync(target);
|
|
1691
|
+
}
|
|
1692
|
+
else if (target instanceof stream.Readable) {
|
|
1693
|
+
const chunks = [];
|
|
1694
|
+
for await (const chunk of target) {
|
|
1695
|
+
chunks.push(chunk);
|
|
1653
1696
|
}
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1697
|
+
target = Buffer.concat(chunks);
|
|
1698
|
+
}
|
|
1699
|
+
if (!Buffer.isBuffer(target)) {
|
|
1700
|
+
throw (0, types_1.errorMessage)('File', "Unknown" /* ERR_MESSAGE.UNKNOWN */, "multipart/form-data" /* MIME.MULTIPART */);
|
|
1701
|
+
}
|
|
1702
|
+
if (!type || !filename) {
|
|
1703
|
+
const result = await module_1.default.resolveMime(target);
|
|
1704
|
+
let ext;
|
|
1705
|
+
if (result) {
|
|
1706
|
+
type || (type = result.mime);
|
|
1707
|
+
ext = result.ext;
|
|
1660
1708
|
}
|
|
1661
|
-
if (
|
|
1662
|
-
|
|
1709
|
+
else if (type) {
|
|
1710
|
+
ext = module_1.default.lookupMime(type, true);
|
|
1663
1711
|
}
|
|
1664
|
-
if (
|
|
1665
|
-
|
|
1666
|
-
let ext;
|
|
1667
|
-
if (result) {
|
|
1668
|
-
type || (type = result.mime);
|
|
1669
|
-
ext = result.ext;
|
|
1670
|
-
}
|
|
1671
|
-
else if (type) {
|
|
1672
|
-
ext = module_1.default.lookupMime(type, true);
|
|
1673
|
-
}
|
|
1674
|
-
if (ext && !filename) {
|
|
1675
|
-
filename = (0, types_1.generateUUID)() + '.' + ext;
|
|
1676
|
-
}
|
|
1712
|
+
if (ext && !filename) {
|
|
1713
|
+
filename = (0, types_1.generateUUID)() + '.' + ext;
|
|
1677
1714
|
}
|
|
1678
|
-
const disposition = createPart(name, filename, type || "application/octet-stream" /* MIME.OCTET_STREAM */);
|
|
1679
|
-
write.append(disposition);
|
|
1680
|
-
write.append(target);
|
|
1681
|
-
write.append("\r\n" /* FORM_DATA.CRLF */);
|
|
1682
|
-
contentLength += Buffer.byteLength(disposition) + target.length + 2 /* FORM_DATA.CRLF_LENGTH */;
|
|
1683
|
-
valid = true;
|
|
1684
|
-
}
|
|
1685
|
-
catch (err) {
|
|
1686
|
-
return Promise.reject(err);
|
|
1687
1715
|
}
|
|
1716
|
+
const disposition = createPart(name, filename, type || "application/octet-stream" /* MIME.OCTET_STREAM */);
|
|
1717
|
+
write.append(disposition);
|
|
1718
|
+
write.append(target);
|
|
1719
|
+
write.append("\r\n" /* FORM_DATA.CRLF */);
|
|
1720
|
+
contentLength += Buffer.byteLength(disposition) + target.length + 2 /* FORM_DATA.CRLF_LENGTH */;
|
|
1721
|
+
valid = true;
|
|
1688
1722
|
}
|
|
1689
|
-
|
|
1690
|
-
|
|
1723
|
+
catch (err) {
|
|
1724
|
+
return Promise.reject(err);
|
|
1691
1725
|
}
|
|
1692
1726
|
}
|
|
1727
|
+
else {
|
|
1728
|
+
addValue(name, value);
|
|
1729
|
+
}
|
|
1693
1730
|
}
|
|
1694
1731
|
if (valid) {
|
|
1695
1732
|
write.append("--" /* FORM_DATA.BOUNDARY_HASH */ + boundary + "--" /* FORM_DATA.BOUNDARY_HASH */ + "\r\n" /* FORM_DATA.CRLF */);
|
|
@@ -1705,17 +1742,15 @@ class Request extends module_1.default {
|
|
|
1705
1742
|
if (contentType && !contentType.includes('/')) {
|
|
1706
1743
|
contentType = 'application/' + contentType.trim();
|
|
1707
1744
|
}
|
|
1708
|
-
if ((0, types_1.isPlainObject)(data)) {
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
data = JSON.stringify(data);
|
|
1714
|
-
contentType || (contentType = "application/json" /* MIME.JSON */);
|
|
1715
|
-
}
|
|
1745
|
+
if (!(0, types_1.isPlainObject)(data)) {
|
|
1746
|
+
data = module_1.default.asString(data);
|
|
1747
|
+
}
|
|
1748
|
+
else if (contentType === "application/x-www-form-urlencoded" /* MIME.URLENCODED */) {
|
|
1749
|
+
data = qs.stringify(data);
|
|
1716
1750
|
}
|
|
1717
1751
|
else {
|
|
1718
|
-
data =
|
|
1752
|
+
data = JSON.stringify(data);
|
|
1753
|
+
contentType || (contentType = "application/json" /* MIME.JSON */);
|
|
1719
1754
|
}
|
|
1720
1755
|
headers['content-length'] = Buffer.byteLength(data, (0, types_1.getEncoding)(dataEncoding)).toString();
|
|
1721
1756
|
}
|
|
@@ -1811,14 +1846,15 @@ class Request extends module_1.default {
|
|
|
1811
1846
|
const buffering = request.connected?.call(client, headers);
|
|
1812
1847
|
const pipeline = pipeTo ? !(0, types_1.isString)(pipeTo) : false;
|
|
1813
1848
|
const enabled = buffering !== false && !pipeline;
|
|
1849
|
+
const readTimeout = this.readTimeout;
|
|
1814
1850
|
let mibsTime, delayTime;
|
|
1815
|
-
if (log ||
|
|
1851
|
+
if (log || readTimeout > 0) {
|
|
1816
1852
|
client.once('readable', () => {
|
|
1817
|
-
if (
|
|
1853
|
+
if (readTimeout > 0) {
|
|
1818
1854
|
timeout = setTimeout(() => {
|
|
1819
1855
|
abortResponse();
|
|
1820
1856
|
throwError((0, types_1.errorValue)("Timeout was exceeded" /* ERR_MESSAGE.TIMEOUT */, href.toString()));
|
|
1821
|
-
},
|
|
1857
|
+
}, readTimeout);
|
|
1822
1858
|
}
|
|
1823
1859
|
if (log) {
|
|
1824
1860
|
if (buffering === false) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.
|
|
24
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/module": "0.6.0",
|
|
24
|
+
"@e-mc/types": "0.6.0",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|