@e-mc/request 0.6.12 → 0.6.14
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 +1 -1
- package/index.js +19 -17
- package/package.json +3 -3
- package/util.d.ts +2 -1
- package/util.js +23 -5
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright 2023 An Pham
|
|
2
2
|
|
|
3
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
4
|
|
package/index.js
CHANGED
|
@@ -1006,13 +1006,14 @@ class Request extends module_1.default {
|
|
|
1006
1006
|
return Promise.reject(err);
|
|
1007
1007
|
}
|
|
1008
1008
|
}
|
|
1009
|
-
let pathname, headers, binOpts, silent;
|
|
1009
|
+
let pathname, headers, binOpts, signal, silent;
|
|
1010
1010
|
if (options) {
|
|
1011
1011
|
if (typeof options === 'string') {
|
|
1012
1012
|
pathname = options;
|
|
1013
1013
|
}
|
|
1014
1014
|
else {
|
|
1015
|
-
({ pathname,
|
|
1015
|
+
({ pathname, binOpts, signal, silent } = options);
|
|
1016
|
+
headers = (0, util_1.parseOutgoingHeaders)(options.headers);
|
|
1016
1017
|
if ((0, types_1.isArray)(binOpts)) {
|
|
1017
1018
|
let next = false;
|
|
1018
1019
|
binOpts = binOpts.filter(opt => !((0, types_1.isString)(opt) && /^-[a-z][\S\s]*$/i.test(opt.trim()))).map((opt) => {
|
|
@@ -1311,7 +1312,13 @@ class Request extends module_1.default {
|
|
|
1311
1312
|
}
|
|
1312
1313
|
for (const item of ARIA2.PID_QUEUE) {
|
|
1313
1314
|
try {
|
|
1314
|
-
|
|
1315
|
+
if (pid === item[0] && (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
1316
|
+
process.kill(item[0]);
|
|
1317
|
+
closeTorrent(item[0]);
|
|
1318
|
+
}
|
|
1319
|
+
else {
|
|
1320
|
+
process.kill(item[0], 0);
|
|
1321
|
+
}
|
|
1315
1322
|
}
|
|
1316
1323
|
catch {
|
|
1317
1324
|
closeTorrent(item[0]);
|
|
@@ -1369,7 +1376,7 @@ class Request extends module_1.default {
|
|
|
1369
1376
|
open(uri, options) {
|
|
1370
1377
|
var _l;
|
|
1371
1378
|
var _m, _o;
|
|
1372
|
-
let { host, url, httpVersion, method = 'GET', encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
|
|
1379
|
+
let { host, url, httpVersion, method = 'GET', encoding, format, headers: outgoing, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options, headers = (0, util_1.parseOutgoingHeaders)(outgoing);
|
|
1373
1380
|
const getting = method === 'GET';
|
|
1374
1381
|
const posting = method === 'POST';
|
|
1375
1382
|
if (format) {
|
|
@@ -1624,6 +1631,9 @@ class Request extends module_1.default {
|
|
|
1624
1631
|
const ac = new AbortController();
|
|
1625
1632
|
this[kDownloading].add(options.outAbort = ac);
|
|
1626
1633
|
stream.addAbortSignal(ac.signal, request);
|
|
1634
|
+
if (options.signal) {
|
|
1635
|
+
stream.addAbortSignal(options.signal, request);
|
|
1636
|
+
}
|
|
1627
1637
|
this.signal.addEventListener('abort', () => ac.abort(), { once: true });
|
|
1628
1638
|
}
|
|
1629
1639
|
if (posting) {
|
|
@@ -1673,7 +1683,7 @@ class Request extends module_1.default {
|
|
|
1673
1683
|
else {
|
|
1674
1684
|
options = {};
|
|
1675
1685
|
}
|
|
1676
|
-
const headers = options.headers || (options.headers = {});
|
|
1686
|
+
const headers = (0, util_1.parseOutgoingHeaders)(options.headers || (options.headers = {}));
|
|
1677
1687
|
for (const attr in headers) {
|
|
1678
1688
|
const name = attr.toLowerCase();
|
|
1679
1689
|
if (name === 'content-type' || name === 'content-length') {
|
|
@@ -1836,7 +1846,7 @@ class Request extends module_1.default {
|
|
|
1836
1846
|
}
|
|
1837
1847
|
const client = this.open(href, request);
|
|
1838
1848
|
const { host, url, encoding, outFormat } = request;
|
|
1839
|
-
let buffer, aborted;
|
|
1849
|
+
let buffer, aborted = false;
|
|
1840
1850
|
({ httpVersion, outAbort } = request);
|
|
1841
1851
|
const isAborted = () => client.destroyed || httpVersion === 2 && client.aborted;
|
|
1842
1852
|
const isRetry = (value) => (0, util_1.isRetryable)(value) && ++retries <= this._config.retryLimit;
|
|
@@ -1853,12 +1863,6 @@ class Request extends module_1.default {
|
|
|
1853
1863
|
}
|
|
1854
1864
|
buffer = null;
|
|
1855
1865
|
aborted = true;
|
|
1856
|
-
if (outAbort) {
|
|
1857
|
-
if (!client.aborted) {
|
|
1858
|
-
outAbort.abort();
|
|
1859
|
-
}
|
|
1860
|
-
this[kDownloading].delete(outAbort);
|
|
1861
|
-
}
|
|
1862
1866
|
client.destroy();
|
|
1863
1867
|
};
|
|
1864
1868
|
const retryTimeout = () => {
|
|
@@ -1883,7 +1887,6 @@ class Request extends module_1.default {
|
|
|
1883
1887
|
var _l;
|
|
1884
1888
|
if (readTimeout > 0) {
|
|
1885
1889
|
timeout = setTimeout(() => {
|
|
1886
|
-
abortResponse();
|
|
1887
1890
|
throwError((0, types_1.errorValue)("Timeout was exceeded", href.toString()));
|
|
1888
1891
|
}, readTimeout);
|
|
1889
1892
|
}
|
|
@@ -2012,9 +2015,9 @@ class Request extends module_1.default {
|
|
|
2012
2015
|
host.success(httpVersion);
|
|
2013
2016
|
};
|
|
2014
2017
|
const redirectResponse = (statusCode, location) => {
|
|
2015
|
-
abortResponse();
|
|
2016
2018
|
if (location) {
|
|
2017
2019
|
if (++redirects <= this._config.redirectLimit) {
|
|
2020
|
+
abortResponse();
|
|
2018
2021
|
downloadUri.call(this, Request.fromURL(url, location));
|
|
2019
2022
|
}
|
|
2020
2023
|
else {
|
|
@@ -2026,8 +2029,8 @@ class Request extends module_1.default {
|
|
|
2026
2029
|
}
|
|
2027
2030
|
};
|
|
2028
2031
|
const errorResponse = (err) => {
|
|
2029
|
-
abortResponse();
|
|
2030
2032
|
if ((0, util_1.checkRetryable)(err) && ++retries <= this._config.retryLimit) {
|
|
2033
|
+
abortResponse();
|
|
2031
2034
|
if (isConnectionTimeout(err)) {
|
|
2032
2035
|
retryTimeout();
|
|
2033
2036
|
}
|
|
@@ -2170,7 +2173,6 @@ class Request extends module_1.default {
|
|
|
2170
2173
|
retryResponse(statusCode, res.headers['retry-after']);
|
|
2171
2174
|
}
|
|
2172
2175
|
else {
|
|
2173
|
-
abortResponse();
|
|
2174
2176
|
throwError(formatStatus(statusCode));
|
|
2175
2177
|
}
|
|
2176
2178
|
})
|
|
@@ -2188,8 +2190,8 @@ class Request extends module_1.default {
|
|
|
2188
2190
|
if (aborted) {
|
|
2189
2191
|
return;
|
|
2190
2192
|
}
|
|
2191
|
-
abortResponse();
|
|
2192
2193
|
if (++retries <= this._config.retryLimit) {
|
|
2194
|
+
abortResponse();
|
|
2193
2195
|
retryTimeout();
|
|
2194
2196
|
}
|
|
2195
2197
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.14",
|
|
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.6.
|
|
24
|
-
"@e-mc/types": "0.6.
|
|
23
|
+
"@e-mc/module": "0.6.14",
|
|
24
|
+
"@e-mc/types": "0.6.14",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"which": "^2.0.2"
|
package/util.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import type { AuthValue } from '@e-mc/types/lib/http';
|
|
2
2
|
|
|
3
3
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
|
|
4
4
|
|
|
5
5
|
declare namespace util {
|
|
6
6
|
function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
|
|
7
|
+
function parseOutgoingHeaders(headers: OutgoingHttpHeaders | Headers | undefined): OutgoingHttpHeaders | undefined;
|
|
7
8
|
function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
|
|
8
9
|
function getBasicAuth(auth: AuthValue): string;
|
|
9
10
|
function getBasicAuth(username: unknown, password?: unknown): string;
|
package/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseHeader = void 0;
|
|
3
|
+
exports.fromSeconds = exports.asFloat = exports.asInt = exports.trimPath = exports.isRetryable = exports.checkRetryable = exports.hasBasicAuth = exports.getBasicAuth = exports.normalizeHeaders = exports.parseOutgoingHeaders = exports.parseHeader = void 0;
|
|
4
4
|
const util = require("util");
|
|
5
5
|
const module_1 = require("@e-mc/module");
|
|
6
6
|
const types_1 = require("@e-mc/types");
|
|
@@ -25,6 +25,24 @@ function parseHeader(headers, name) {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
exports.parseHeader = parseHeader;
|
|
28
|
+
function parseOutgoingHeaders(headers) {
|
|
29
|
+
if (headers) {
|
|
30
|
+
if (globalThis.Headers && headers instanceof Headers) {
|
|
31
|
+
const result = {};
|
|
32
|
+
headers.forEach((value, key) => {
|
|
33
|
+
if (key === 'set-cookie') {
|
|
34
|
+
(result[key] || (result[key] = [])).push(value);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
result[key] = value;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
return headers;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.parseOutgoingHeaders = parseOutgoingHeaders;
|
|
28
46
|
function normalizeHeaders(headers) {
|
|
29
47
|
const result = Object.create(null);
|
|
30
48
|
for (const name in headers) {
|
|
@@ -37,11 +55,11 @@ function normalizeHeaders(headers) {
|
|
|
37
55
|
value = value.trim();
|
|
38
56
|
break;
|
|
39
57
|
default:
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
break;
|
|
58
|
+
if (!(0, types_1.isArray)(value)) {
|
|
59
|
+
continue;
|
|
43
60
|
}
|
|
44
|
-
|
|
61
|
+
value = value.map(out => module_1.default.asString(out).trim());
|
|
62
|
+
break;
|
|
45
63
|
}
|
|
46
64
|
if (value) {
|
|
47
65
|
result[trimPath(name.trim().toLowerCase())] = value;
|