@e-mc/request 0.8.23 → 0.8.24
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/README.md +4 -4
- package/http/adapter/index.js +28 -23
- package/index.js +1 -1
- package/package.json +3 -3
- package/util.d.ts +1 -0
- package/util.js +23 -5
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
12
|
+
- https://www.unpkg.com/@e-mc/types@0.8.24/lib/index.d.ts
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -82,9 +82,9 @@ interface RequestConstructor extends ModuleConstructor {
|
|
|
82
82
|
|
|
83
83
|
## References
|
|
84
84
|
|
|
85
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
86
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
87
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
85
|
+
- https://www.unpkg.com/@e-mc/types@0.8.24/lib/http.d.ts
|
|
86
|
+
- https://www.unpkg.com/@e-mc/types@0.8.24/lib/request.d.ts
|
|
87
|
+
- https://www.unpkg.com/@e-mc/types@0.8.24/lib/settings.d.ts
|
|
88
88
|
|
|
89
89
|
## LICENSE
|
|
90
90
|
|
package/http/adapter/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _a;
|
|
3
2
|
const fs = require("node:fs");
|
|
4
3
|
const http2 = require("node:http2");
|
|
5
4
|
const yaml = require("js-yaml");
|
|
6
5
|
const types_1 = require("@e-mc/types");
|
|
7
6
|
const module_1 = require("@e-mc/module");
|
|
8
7
|
const util_1 = require("@e-mc/request/util");
|
|
9
|
-
const kOutStream = Symbol('outStream');
|
|
10
|
-
const kOptions = Symbol('options');
|
|
11
8
|
let LOG_TIMEFORMAT = 'readable';
|
|
12
9
|
class HttpAdapter {
|
|
10
|
+
instance;
|
|
11
|
+
state;
|
|
12
|
+
uri;
|
|
13
13
|
static isUnsupported(value) {
|
|
14
14
|
return value === 421 || value === 505;
|
|
15
15
|
}
|
|
@@ -38,20 +38,26 @@ class HttpAdapter {
|
|
|
38
38
|
break;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
+
contentLength = 0;
|
|
42
|
+
retries = 0;
|
|
43
|
+
redirects = 0;
|
|
44
|
+
closed = false;
|
|
45
|
+
aborted = false;
|
|
46
|
+
timeout = null;
|
|
47
|
+
dataTime = null;
|
|
48
|
+
delayTime = undefined;
|
|
49
|
+
opts;
|
|
50
|
+
client;
|
|
51
|
+
resolve;
|
|
52
|
+
reject;
|
|
53
|
+
startTime;
|
|
54
|
+
#outStream = null;
|
|
55
|
+
#options;
|
|
41
56
|
constructor(instance, state, uri, options) {
|
|
42
57
|
this.instance = instance;
|
|
43
58
|
this.state = state;
|
|
44
59
|
this.uri = uri;
|
|
45
|
-
this
|
|
46
|
-
this.retries = 0;
|
|
47
|
-
this.redirects = 0;
|
|
48
|
-
this.closed = false;
|
|
49
|
-
this.aborted = false;
|
|
50
|
-
this.timeout = null;
|
|
51
|
-
this.dataTime = null;
|
|
52
|
-
this.delayTime = undefined;
|
|
53
|
-
this[_a] = null;
|
|
54
|
-
this[kOptions] = options;
|
|
60
|
+
this.#options = options;
|
|
55
61
|
this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
|
|
56
62
|
this.setOpts();
|
|
57
63
|
}
|
|
@@ -91,14 +97,14 @@ class HttpAdapter {
|
|
|
91
97
|
this.retryResponse(statusCode, headers['retry-after']);
|
|
92
98
|
}
|
|
93
99
|
else if (HttpAdapter.isUnsupported(statusCode)) {
|
|
94
|
-
this.retryDownload(true, this
|
|
100
|
+
this.retryDownload(true, this.#formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
|
|
95
101
|
}
|
|
96
102
|
else {
|
|
97
103
|
switch (flags) {
|
|
98
104
|
case http2.constants.NGHTTP2_PROTOCOL_ERROR:
|
|
99
105
|
case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
|
|
100
106
|
case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
|
|
101
|
-
this.retryDownload(true, this
|
|
107
|
+
this.retryDownload(true, this.#formatNgFlags(flags, statusCode, headers.location));
|
|
102
108
|
break;
|
|
103
109
|
default:
|
|
104
110
|
this.retryDownload(false, this.formatStatus(statusCode));
|
|
@@ -186,7 +192,7 @@ class HttpAdapter {
|
|
|
186
192
|
if (uri) {
|
|
187
193
|
this.uri = uri;
|
|
188
194
|
}
|
|
189
|
-
this.opts = this.instance.opts(this.uri, this
|
|
195
|
+
this.opts = this.instance.opts(this.uri, this.#options);
|
|
190
196
|
}
|
|
191
197
|
setWriteStream() {
|
|
192
198
|
const pipeTo = this.pipeTo;
|
|
@@ -454,7 +460,7 @@ class HttpAdapter {
|
|
|
454
460
|
return;
|
|
455
461
|
}
|
|
456
462
|
}
|
|
457
|
-
this.sendWarning(this
|
|
463
|
+
this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
|
|
458
464
|
if ((0, util_1.isRetryable)(statusCode, true)) {
|
|
459
465
|
setImmediate(this.init.bind(this));
|
|
460
466
|
}
|
|
@@ -468,7 +474,7 @@ class HttpAdapter {
|
|
|
468
474
|
return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
|
|
469
475
|
}
|
|
470
476
|
retryTimeout() {
|
|
471
|
-
this.sendWarning(this
|
|
477
|
+
this.sendWarning(this.#formatRetry("HTTP connection timeout"));
|
|
472
478
|
this.init();
|
|
473
479
|
}
|
|
474
480
|
terminate(err) {
|
|
@@ -501,10 +507,10 @@ class HttpAdapter {
|
|
|
501
507
|
this.outStream = null;
|
|
502
508
|
}
|
|
503
509
|
}
|
|
504
|
-
formatNgFlags(value, statusCode, location) {
|
|
510
|
+
#formatNgFlags(value, statusCode, location) {
|
|
505
511
|
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
506
512
|
}
|
|
507
|
-
formatRetry(message) {
|
|
513
|
+
#formatRetry(message) {
|
|
508
514
|
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
509
515
|
}
|
|
510
516
|
set abortController(value) {
|
|
@@ -514,13 +520,13 @@ class HttpAdapter {
|
|
|
514
520
|
return this.opts.outAbort || null;
|
|
515
521
|
}
|
|
516
522
|
set outStream(value) {
|
|
517
|
-
this
|
|
523
|
+
this.#outStream = value;
|
|
518
524
|
if (value) {
|
|
519
525
|
this.opts.outStream = value;
|
|
520
526
|
}
|
|
521
527
|
}
|
|
522
528
|
get outStream() {
|
|
523
|
-
return this
|
|
529
|
+
return this.#outStream;
|
|
524
530
|
}
|
|
525
531
|
get destroyed() {
|
|
526
532
|
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
|
@@ -550,5 +556,4 @@ class HttpAdapter {
|
|
|
550
556
|
return this.state.config.redirectLimit;
|
|
551
557
|
}
|
|
552
558
|
}
|
|
553
|
-
_a = kOutStream;
|
|
554
559
|
module.exports = HttpAdapter;
|
package/index.js
CHANGED
|
@@ -1752,7 +1752,7 @@ class Request extends module_1.default {
|
|
|
1752
1752
|
else {
|
|
1753
1753
|
options = {};
|
|
1754
1754
|
}
|
|
1755
|
-
const headers =
|
|
1755
|
+
const headers = (0, util_1.parseOutgoingHeaders)(options.headers) || {};
|
|
1756
1756
|
for (const attr in headers) {
|
|
1757
1757
|
const name = attr.toLowerCase();
|
|
1758
1758
|
if (name === 'content-type' || name === 'content-length') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.24",
|
|
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.8.
|
|
24
|
-
"@e-mc/types": "0.8.
|
|
23
|
+
"@e-mc/module": "0.8.24",
|
|
24
|
+
"@e-mc/types": "0.8.24",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"picomatch": "^3.0.1",
|
package/util.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ 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;
|