@e-mc/request 0.5.14 → 0.5.16
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 +3 -0
- 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
|
@@ -975,13 +975,14 @@ class Request extends module_1.default {
|
|
|
975
975
|
return Promise.reject(err);
|
|
976
976
|
}
|
|
977
977
|
}
|
|
978
|
-
let pathname, headers, binOpts, silent;
|
|
978
|
+
let pathname, headers, binOpts, signal, silent;
|
|
979
979
|
if (options) {
|
|
980
980
|
if (typeof options === 'string') {
|
|
981
981
|
pathname = options;
|
|
982
982
|
}
|
|
983
983
|
else {
|
|
984
|
-
({ pathname,
|
|
984
|
+
({ pathname, binOpts, signal, silent } = options);
|
|
985
|
+
headers = (0, util_1.parseOutgoingHeaders)(options.headers);
|
|
985
986
|
if ((0, types_1.isArray)(binOpts)) {
|
|
986
987
|
let next = false;
|
|
987
988
|
binOpts = binOpts.filter(opt => !((0, types_1.isString)(opt) && /^-[a-z][\S\s]*$/i.test(opt.trim()))).map((opt) => {
|
|
@@ -1280,7 +1281,13 @@ class Request extends module_1.default {
|
|
|
1280
1281
|
}
|
|
1281
1282
|
for (const item of ARIA2.PID_QUEUE) {
|
|
1282
1283
|
try {
|
|
1283
|
-
|
|
1284
|
+
if (pid === item[0] && (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
1285
|
+
process.kill(item[0]);
|
|
1286
|
+
closeTorrent(item[0]);
|
|
1287
|
+
}
|
|
1288
|
+
else {
|
|
1289
|
+
process.kill(item[0], 0);
|
|
1290
|
+
}
|
|
1284
1291
|
}
|
|
1285
1292
|
catch {
|
|
1286
1293
|
closeTorrent(item[0]);
|
|
@@ -1338,7 +1345,7 @@ class Request extends module_1.default {
|
|
|
1338
1345
|
open(uri, options) {
|
|
1339
1346
|
var _l;
|
|
1340
1347
|
var _m, _o;
|
|
1341
|
-
let { host, url, httpVersion, method = 'GET', encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
|
|
1348
|
+
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);
|
|
1342
1349
|
const getting = method === 'GET';
|
|
1343
1350
|
const posting = method === 'POST';
|
|
1344
1351
|
if (format) {
|
|
@@ -1585,6 +1592,9 @@ class Request extends module_1.default {
|
|
|
1585
1592
|
const ac = new AbortController();
|
|
1586
1593
|
this[kDownloading].add(options.outAbort = ac);
|
|
1587
1594
|
stream.addAbortSignal(ac.signal, request);
|
|
1595
|
+
if (options.signal) {
|
|
1596
|
+
stream.addAbortSignal(options.signal, request);
|
|
1597
|
+
}
|
|
1588
1598
|
this.signal.addEventListener('abort', () => ac.abort(), { once: true });
|
|
1589
1599
|
}
|
|
1590
1600
|
if (posting) {
|
|
@@ -1634,7 +1644,7 @@ class Request extends module_1.default {
|
|
|
1634
1644
|
else {
|
|
1635
1645
|
options = {};
|
|
1636
1646
|
}
|
|
1637
|
-
const headers = options.headers || (options.headers = {});
|
|
1647
|
+
const headers = (0, util_1.parseOutgoingHeaders)(options.headers || (options.headers = {}));
|
|
1638
1648
|
for (const attr in headers) {
|
|
1639
1649
|
switch (attr.toLowerCase()) {
|
|
1640
1650
|
case 'content-type':
|
|
@@ -1800,7 +1810,7 @@ class Request extends module_1.default {
|
|
|
1800
1810
|
}
|
|
1801
1811
|
const client = this.open(href, request);
|
|
1802
1812
|
const { host, url, encoding, outFormat } = request;
|
|
1803
|
-
let buffer, aborted;
|
|
1813
|
+
let buffer, aborted = false;
|
|
1804
1814
|
({ httpVersion, outAbort } = request);
|
|
1805
1815
|
const isAborted = () => client.destroyed || httpVersion === 2 && client.aborted;
|
|
1806
1816
|
const isRetry = (value) => (0, util_1.isRetryable)(value) && ++retries <= this._config.retryLimit;
|
|
@@ -1817,12 +1827,6 @@ class Request extends module_1.default {
|
|
|
1817
1827
|
}
|
|
1818
1828
|
buffer = null;
|
|
1819
1829
|
aborted = true;
|
|
1820
|
-
if (outAbort) {
|
|
1821
|
-
if (!client.aborted) {
|
|
1822
|
-
outAbort.abort();
|
|
1823
|
-
}
|
|
1824
|
-
this[kDownloading].delete(outAbort);
|
|
1825
|
-
}
|
|
1826
1830
|
client.destroy();
|
|
1827
1831
|
};
|
|
1828
1832
|
const retryTimeout = () => {
|
|
@@ -1846,7 +1850,6 @@ class Request extends module_1.default {
|
|
|
1846
1850
|
var _l;
|
|
1847
1851
|
if (this.readTimeout > 0) {
|
|
1848
1852
|
timeout = setTimeout(() => {
|
|
1849
|
-
abortResponse();
|
|
1850
1853
|
throwError((0, types_1.errorValue)("Timeout was exceeded", href.toString()));
|
|
1851
1854
|
}, this.readTimeout);
|
|
1852
1855
|
}
|
|
@@ -1975,9 +1978,9 @@ class Request extends module_1.default {
|
|
|
1975
1978
|
host.success(httpVersion);
|
|
1976
1979
|
};
|
|
1977
1980
|
const redirectResponse = (statusCode, location) => {
|
|
1978
|
-
abortResponse();
|
|
1979
1981
|
if (location) {
|
|
1980
1982
|
if (++redirects <= this._config.redirectLimit) {
|
|
1983
|
+
abortResponse();
|
|
1981
1984
|
downloadUri.call(this, Request.fromURL(url, location));
|
|
1982
1985
|
}
|
|
1983
1986
|
else {
|
|
@@ -1989,8 +1992,8 @@ class Request extends module_1.default {
|
|
|
1989
1992
|
}
|
|
1990
1993
|
};
|
|
1991
1994
|
const errorResponse = (err) => {
|
|
1992
|
-
abortResponse();
|
|
1993
1995
|
if ((0, util_1.checkRetryable)(err) && ++retries <= this._config.retryLimit) {
|
|
1996
|
+
abortResponse();
|
|
1994
1997
|
if (isConnectionTimeout(err)) {
|
|
1995
1998
|
retryTimeout();
|
|
1996
1999
|
}
|
|
@@ -2133,7 +2136,6 @@ class Request extends module_1.default {
|
|
|
2133
2136
|
retryResponse(statusCode, res.headers['retry-after']);
|
|
2134
2137
|
}
|
|
2135
2138
|
else {
|
|
2136
|
-
abortResponse();
|
|
2137
2139
|
throwError(formatStatus(statusCode));
|
|
2138
2140
|
}
|
|
2139
2141
|
})
|
|
@@ -2151,8 +2153,8 @@ class Request extends module_1.default {
|
|
|
2151
2153
|
if (aborted) {
|
|
2152
2154
|
return;
|
|
2153
2155
|
}
|
|
2154
|
-
abortResponse();
|
|
2155
2156
|
if (++retries <= this._config.retryLimit) {
|
|
2157
|
+
abortResponse();
|
|
2156
2158
|
retryTimeout();
|
|
2157
2159
|
}
|
|
2158
2160
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.16",
|
|
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.5.
|
|
24
|
-
"@e-mc/types": "0.5.
|
|
23
|
+
"@e-mc/module": "0.5.16",
|
|
24
|
+
"@e-mc/types": "0.5.16",
|
|
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,7 +1,10 @@
|
|
|
1
|
+
import type { AuthValue } from '@e-mc/types/lib/http';
|
|
2
|
+
|
|
1
3
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
|
|
2
4
|
|
|
3
5
|
declare namespace util {
|
|
4
6
|
function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
|
|
7
|
+
function parseOutgoingHeaders(headers: OutgoingHttpHeaders | Headers | undefined): OutgoingHttpHeaders | undefined;
|
|
5
8
|
function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
|
|
6
9
|
function getBasicAuth(auth: AuthValue): string;
|
|
7
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;
|