@e-mc/request 0.7.18 → 0.7.20
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/adapter/index.js +12 -14
- package/index.js +23 -18
- package/package.json +3 -3
package/http/adapter/index.js
CHANGED
|
@@ -5,8 +5,6 @@ const yaml = require("js-yaml");
|
|
|
5
5
|
const types_1 = require("@e-mc/types");
|
|
6
6
|
const module_1 = require("@e-mc/module");
|
|
7
7
|
const util_1 = require("@e-mc/request/util");
|
|
8
|
-
const kOutStream = Symbol('outStream');
|
|
9
|
-
const kOptions = Symbol('options');
|
|
10
8
|
let LOG_TIMEFORMAT = 'readable';
|
|
11
9
|
class HttpAdapter {
|
|
12
10
|
instance;
|
|
@@ -53,13 +51,13 @@ class HttpAdapter {
|
|
|
53
51
|
resolve;
|
|
54
52
|
reject;
|
|
55
53
|
startTime;
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
#outStream = null;
|
|
55
|
+
#options;
|
|
58
56
|
constructor(instance, state, uri, options) {
|
|
59
57
|
this.instance = instance;
|
|
60
58
|
this.state = state;
|
|
61
59
|
this.uri = uri;
|
|
62
|
-
this
|
|
60
|
+
this.#options = options;
|
|
63
61
|
this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
|
|
64
62
|
this.setOpts();
|
|
65
63
|
}
|
|
@@ -99,14 +97,14 @@ class HttpAdapter {
|
|
|
99
97
|
this.retryResponse(statusCode, headers['retry-after']);
|
|
100
98
|
}
|
|
101
99
|
else if (HttpAdapter.isUnsupported(statusCode)) {
|
|
102
|
-
this.retryDownload(true, this
|
|
100
|
+
this.retryDownload(true, this.#formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
|
|
103
101
|
}
|
|
104
102
|
else {
|
|
105
103
|
switch (flags) {
|
|
106
104
|
case http2.constants.NGHTTP2_PROTOCOL_ERROR:
|
|
107
105
|
case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
|
|
108
106
|
case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
|
|
109
|
-
this.retryDownload(true, this
|
|
107
|
+
this.retryDownload(true, this.#formatNgFlags(flags, statusCode, headers.location));
|
|
110
108
|
break;
|
|
111
109
|
default:
|
|
112
110
|
this.retryDownload(false, this.formatStatus(statusCode));
|
|
@@ -194,7 +192,7 @@ class HttpAdapter {
|
|
|
194
192
|
if (uri) {
|
|
195
193
|
this.uri = uri;
|
|
196
194
|
}
|
|
197
|
-
this.opts = this.instance.opts(this.uri, this
|
|
195
|
+
this.opts = this.instance.opts(this.uri, this.#options);
|
|
198
196
|
}
|
|
199
197
|
setWriteStream() {
|
|
200
198
|
const pipeTo = this.pipeTo;
|
|
@@ -462,7 +460,7 @@ class HttpAdapter {
|
|
|
462
460
|
return;
|
|
463
461
|
}
|
|
464
462
|
}
|
|
465
|
-
this.sendWarning(this
|
|
463
|
+
this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
|
|
466
464
|
if ((0, util_1.isRetryable)(statusCode, true)) {
|
|
467
465
|
setImmediate(this.init.bind(this));
|
|
468
466
|
}
|
|
@@ -476,7 +474,7 @@ class HttpAdapter {
|
|
|
476
474
|
return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
|
|
477
475
|
}
|
|
478
476
|
retryTimeout() {
|
|
479
|
-
this.sendWarning(this
|
|
477
|
+
this.sendWarning(this.#formatRetry("HTTP connection timeout"));
|
|
480
478
|
this.init();
|
|
481
479
|
}
|
|
482
480
|
terminate(err) {
|
|
@@ -509,10 +507,10 @@ class HttpAdapter {
|
|
|
509
507
|
this.outStream = null;
|
|
510
508
|
}
|
|
511
509
|
}
|
|
512
|
-
formatNgFlags(value, statusCode, location) {
|
|
510
|
+
#formatNgFlags(value, statusCode, location) {
|
|
513
511
|
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
514
512
|
}
|
|
515
|
-
formatRetry(message) {
|
|
513
|
+
#formatRetry(message) {
|
|
516
514
|
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
517
515
|
}
|
|
518
516
|
set abortController(value) {
|
|
@@ -522,13 +520,13 @@ class HttpAdapter {
|
|
|
522
520
|
return this.opts.outAbort || null;
|
|
523
521
|
}
|
|
524
522
|
set outStream(value) {
|
|
525
|
-
this
|
|
523
|
+
this.#outStream = value;
|
|
526
524
|
if (value) {
|
|
527
525
|
this.opts.outStream = value;
|
|
528
526
|
}
|
|
529
527
|
}
|
|
530
528
|
get outStream() {
|
|
531
|
-
return this
|
|
529
|
+
return this.#outStream;
|
|
532
530
|
}
|
|
533
531
|
get destroyed() {
|
|
534
532
|
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
package/index.js
CHANGED
|
@@ -385,7 +385,7 @@ class Request extends module_1.default {
|
|
|
385
385
|
if ((lowest_speed_limit = parseSize(lowest_speed_limit, true)) !== undefined) {
|
|
386
386
|
ARIA2.LOWEST_SPEED_LIMIT = lowest_speed_limit;
|
|
387
387
|
}
|
|
388
|
-
if (always_resume) {
|
|
388
|
+
if (typeof always_resume === 'boolean') {
|
|
389
389
|
ARIA2.ALWAYS_RESUME = always_resume;
|
|
390
390
|
}
|
|
391
391
|
if ((0, types_1.isString)(file_allocation)) {
|
|
@@ -399,7 +399,7 @@ class Request extends module_1.default {
|
|
|
399
399
|
break;
|
|
400
400
|
}
|
|
401
401
|
}
|
|
402
|
-
if (conf_path && this.isPath(conf_path = path.resolve(conf_path))) {
|
|
402
|
+
if (conf_path === '' || (0, types_1.isString)(conf_path) && this.isPath(conf_path = path.resolve(conf_path))) {
|
|
403
403
|
ARIA2.CONF_PATH = conf_path;
|
|
404
404
|
}
|
|
405
405
|
}
|
|
@@ -1027,7 +1027,7 @@ class Request extends module_1.default {
|
|
|
1027
1027
|
case 'string': {
|
|
1028
1028
|
const value = opt.trim();
|
|
1029
1029
|
if (value.startsWith('--')) {
|
|
1030
|
-
const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)
|
|
1030
|
+
const match = /^(--[a-z]+[a-z0-9-]*)(=)?\s*(.*)$/s.exec(value);
|
|
1031
1031
|
if (match) {
|
|
1032
1032
|
switch (match[1]) {
|
|
1033
1033
|
case '--daemon':
|
|
@@ -1083,8 +1083,8 @@ class Request extends module_1.default {
|
|
|
1083
1083
|
({ protocol, origin, username, password, href: uri } = uri);
|
|
1084
1084
|
}
|
|
1085
1085
|
const escapeQuote = (value) => {
|
|
1086
|
-
value = value.replace(/"/g, '\\"');
|
|
1087
|
-
return PLATFORM_WIN32 ? value : value.replace(/
|
|
1086
|
+
value = value.replace(/(?<!\\)"/g, '\\"');
|
|
1087
|
+
return PLATFORM_WIN32 ? value : value.replace(/(?<!\\)\$/g, '\\$');
|
|
1088
1088
|
};
|
|
1089
1089
|
const init = [
|
|
1090
1090
|
`--dir="${escapeQuote(pathname)}"`,
|
|
@@ -1229,10 +1229,13 @@ class Request extends module_1.default {
|
|
|
1229
1229
|
}
|
|
1230
1230
|
if (binOpts) {
|
|
1231
1231
|
for (const leading of binOpts) {
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1232
|
+
if (leading.startsWith('-')) {
|
|
1233
|
+
const pattern = new RegExp(`^${leading}(?:=|$)`);
|
|
1234
|
+
for (let i = 0; i < opts.length; ++i) {
|
|
1235
|
+
if (pattern.test(opts[i])) {
|
|
1236
|
+
opts.splice(i--, i);
|
|
1237
|
+
break;
|
|
1238
|
+
}
|
|
1236
1239
|
}
|
|
1237
1240
|
}
|
|
1238
1241
|
}
|
|
@@ -1249,18 +1252,14 @@ class Request extends module_1.default {
|
|
|
1249
1252
|
opts.push(`"${escapeQuote(uri)}"`);
|
|
1250
1253
|
args = args.concat(init, opts);
|
|
1251
1254
|
const startTime = Date.now();
|
|
1252
|
-
let out = '', message = ''
|
|
1255
|
+
let out = '', message = '';
|
|
1253
1256
|
const errorResponse = (pid, err) => {
|
|
1254
|
-
aborted = true;
|
|
1255
1257
|
closeTorrent(pid);
|
|
1256
1258
|
reject(err);
|
|
1257
1259
|
};
|
|
1258
1260
|
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 })
|
|
1259
1261
|
.on('exit', code => {
|
|
1260
1262
|
closeTorrent(pid);
|
|
1261
|
-
if (aborted) {
|
|
1262
|
-
return;
|
|
1263
|
-
}
|
|
1264
1263
|
if (this.aborted) {
|
|
1265
1264
|
errorResponse(pid, (0, types_1.createAbortError)());
|
|
1266
1265
|
return;
|
|
@@ -1291,10 +1290,15 @@ class Request extends module_1.default {
|
|
|
1291
1290
|
break;
|
|
1292
1291
|
}
|
|
1293
1292
|
}
|
|
1294
|
-
if (result.length
|
|
1295
|
-
|
|
1293
|
+
if (result.length > 0) {
|
|
1294
|
+
if (!silent && LOG_HTTP && LOG_TIMEPROCESS) {
|
|
1295
|
+
this.writeTimeProcess("aria2", uri, startTime, { type: 1024, queue: true, messageUnit, messageUnitMinWidth: 9, bypassLog: true });
|
|
1296
|
+
}
|
|
1297
|
+
this.addLog(types_1.STATUS_TYPE.INFO, out, currentTime, currentTime - startTime, "aria2", uri);
|
|
1298
|
+
}
|
|
1299
|
+
else {
|
|
1300
|
+
this.addLog(types_1.STATUS_TYPE.ERROR, 'No files were successfully downloaded', currentTime, currentTime - startTime, "aria2", uri);
|
|
1296
1301
|
}
|
|
1297
|
-
this.addLog(result.length ? types_1.STATUS_TYPE.INFO : types_1.STATUS_TYPE.ERROR, out, currentTime, currentTime - startTime, "aria2", uri);
|
|
1298
1302
|
resolve(result);
|
|
1299
1303
|
}
|
|
1300
1304
|
else {
|
|
@@ -1688,7 +1692,7 @@ class Request extends module_1.default {
|
|
|
1688
1692
|
else {
|
|
1689
1693
|
options = {};
|
|
1690
1694
|
}
|
|
1691
|
-
const headers = (0, util_1.parseOutgoingHeaders)(options.headers ||
|
|
1695
|
+
const headers = (0, util_1.parseOutgoingHeaders)(options.headers) || {};
|
|
1692
1696
|
for (const attr in headers) {
|
|
1693
1697
|
const name = attr.toLowerCase();
|
|
1694
1698
|
if (name === 'content-type' || name === 'content-length') {
|
|
@@ -1802,6 +1806,7 @@ class Request extends module_1.default {
|
|
|
1802
1806
|
options.httpVersion = 1;
|
|
1803
1807
|
options.postData = data;
|
|
1804
1808
|
headers['content-type'] = contentType || "text/plain";
|
|
1809
|
+
options.headers = headers;
|
|
1805
1810
|
return this.get(uri, options);
|
|
1806
1811
|
}
|
|
1807
1812
|
async get(uri, options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.20",
|
|
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.7.
|
|
24
|
-
"@e-mc/types": "0.7.
|
|
23
|
+
"@e-mc/module": "0.7.20",
|
|
24
|
+
"@e-mc/types": "0.7.20",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|