@e-mc/request 0.10.8 → 0.10.10
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 +35 -25
- package/index.js +11 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.10.10/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -205,9 +205,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
|
|
|
205
205
|
|
|
206
206
|
## References
|
|
207
207
|
|
|
208
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
209
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
210
|
-
- https://www.unpkg.com/@e-mc/types@0.10.
|
|
208
|
+
- https://www.unpkg.com/@e-mc/types@0.10.10/lib/http.d.ts
|
|
209
|
+
- https://www.unpkg.com/@e-mc/types@0.10.10/lib/request.d.ts
|
|
210
|
+
- https://www.unpkg.com/@e-mc/types@0.10.10/lib/settings.d.ts
|
|
211
211
|
|
|
212
212
|
* https://www.npmjs.com/package/@types/node
|
|
213
213
|
|
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;
|
|
@@ -359,6 +365,7 @@ class HttpAdapter {
|
|
|
359
365
|
this.terminate("No data received");
|
|
360
366
|
return;
|
|
361
367
|
}
|
|
368
|
+
dataLength = this.contentLength || (typeof this.pipeTo === 'string' ? (0, util_1.getSize)(this.pipeTo) : 0);
|
|
362
369
|
result = encoding && !pipeline ? '' : null;
|
|
363
370
|
}
|
|
364
371
|
this.endResponse(result, dataLength, log);
|
|
@@ -371,12 +378,11 @@ class HttpAdapter {
|
|
|
371
378
|
}
|
|
372
379
|
endResponse(result, dataLength = 0, logging = this.state.log) {
|
|
373
380
|
if (logging) {
|
|
374
|
-
const messageUnit = this.dataTime && dataLength > 0 ? (0, util_1.getTransferRate)(dataLength, (0, types_1.convertTime)(process.hrtime.bigint() - this.dataTime, false) * 1000) : undefined;
|
|
375
381
|
this.instance.writeTimeProcess('HTTP' + this.httpVersion, this.opts.statusMessage || this.uri.toString(), this.startTime, {
|
|
376
382
|
type: 1024,
|
|
377
383
|
queue: !!this.instance.host,
|
|
378
384
|
titleBgColor: !result ? 'bgBlue' : undefined,
|
|
379
|
-
messageUnit,
|
|
385
|
+
messageUnit: this.formatMibs(dataLength),
|
|
380
386
|
messageUnitMinWidth: 9,
|
|
381
387
|
delayTime: this.delayTime,
|
|
382
388
|
bypassLog: module_1.hasLogType(32768)
|
|
@@ -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) {
|
|
@@ -488,6 +494,11 @@ class HttpAdapter {
|
|
|
488
494
|
formatStatus(value, hint) {
|
|
489
495
|
return value + ': ' + (hint || (0, util_1.fromStatusCode)(value)) + ` (${this.uri.toString()})`;
|
|
490
496
|
}
|
|
497
|
+
formatMibs(dataLength) {
|
|
498
|
+
if (dataLength > 0 && this.dataTime) {
|
|
499
|
+
return (0, util_1.getTransferRate)(dataLength, Math.max(1, (0, types_1.convertTime)(process.hrtime.bigint() - this.dataTime, false) * 1000));
|
|
500
|
+
}
|
|
501
|
+
}
|
|
491
502
|
close() {
|
|
492
503
|
this.closed = true;
|
|
493
504
|
this.instance.reset(this);
|
|
@@ -501,10 +512,10 @@ class HttpAdapter {
|
|
|
501
512
|
this.outStream = null;
|
|
502
513
|
}
|
|
503
514
|
}
|
|
504
|
-
formatNgFlags(value, statusCode, location) {
|
|
515
|
+
#formatNgFlags(value, statusCode, location) {
|
|
505
516
|
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
506
517
|
}
|
|
507
|
-
formatRetry(message) {
|
|
518
|
+
#formatRetry(message) {
|
|
508
519
|
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
509
520
|
}
|
|
510
521
|
set abortController(value) {
|
|
@@ -514,13 +525,13 @@ class HttpAdapter {
|
|
|
514
525
|
return this.opts.outAbort || null;
|
|
515
526
|
}
|
|
516
527
|
set outStream(value) {
|
|
517
|
-
this
|
|
528
|
+
this.#outStream = value;
|
|
518
529
|
if (value) {
|
|
519
530
|
this.opts.outStream = value;
|
|
520
531
|
}
|
|
521
532
|
}
|
|
522
533
|
get outStream() {
|
|
523
|
-
return this
|
|
534
|
+
return this.#outStream;
|
|
524
535
|
}
|
|
525
536
|
get destroyed() {
|
|
526
537
|
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
|
@@ -550,5 +561,4 @@ class HttpAdapter {
|
|
|
550
561
|
return this.state.config.redirectLimit;
|
|
551
562
|
}
|
|
552
563
|
}
|
|
553
|
-
_a = kOutStream;
|
|
554
564
|
module.exports = HttpAdapter;
|
package/index.js
CHANGED
|
@@ -676,7 +676,7 @@ class Fetch {
|
|
|
676
676
|
}
|
|
677
677
|
dataLength = Buffer.byteLength(buffer, encoding);
|
|
678
678
|
if (mibsTime) {
|
|
679
|
-
messageUnit =
|
|
679
|
+
messageUnit = this.formatMibs(dataLength, mibsTime);
|
|
680
680
|
}
|
|
681
681
|
if (contentLength > 0) {
|
|
682
682
|
parent.updateProgress("request", progressId, dataLength, dataLength);
|
|
@@ -730,6 +730,9 @@ class Fetch {
|
|
|
730
730
|
this.terminate("No data received");
|
|
731
731
|
return;
|
|
732
732
|
}
|
|
733
|
+
if (mibsTime && (contentLength > 0 || typeof pipeTo === 'string')) {
|
|
734
|
+
messageUnit = this.formatMibs(contentLength || (0, util_1.getSize)(pipeTo), mibsTime);
|
|
735
|
+
}
|
|
733
736
|
result = encoding && !pipeline ? '' : null;
|
|
734
737
|
titleBgColor = 'bgBlue';
|
|
735
738
|
}
|
|
@@ -843,6 +846,11 @@ class Fetch {
|
|
|
843
846
|
formatRetry(message) {
|
|
844
847
|
return message + ` (${this.retries} / ${this.retryLimit})`;
|
|
845
848
|
}
|
|
849
|
+
formatMibs(dataLength, mibsTime) {
|
|
850
|
+
if (dataLength > 0) {
|
|
851
|
+
return (0, util_1.getTransferRate)(dataLength, Math.max(1, (0, types_1.convertTime)(process.hrtime(mibsTime), false) * 1000));
|
|
852
|
+
}
|
|
853
|
+
}
|
|
846
854
|
close() {
|
|
847
855
|
if (this.timeout) {
|
|
848
856
|
clearTimeout(this.timeout);
|
|
@@ -2360,7 +2368,7 @@ class Request extends module_1 {
|
|
|
2360
2368
|
else {
|
|
2361
2369
|
options = {};
|
|
2362
2370
|
}
|
|
2363
|
-
const headers = (0, util_1.parseOutgoingHeaders)(options.headers
|
|
2371
|
+
const headers = (0, util_1.parseOutgoingHeaders)(options.headers) || {};
|
|
2364
2372
|
for (const attr in headers) {
|
|
2365
2373
|
const name = attr.toLowerCase();
|
|
2366
2374
|
if (name === 'content-type' || name === 'content-length') {
|
|
@@ -2470,6 +2478,7 @@ class Request extends module_1 {
|
|
|
2470
2478
|
options.httpVersion = 1;
|
|
2471
2479
|
options.postData = data;
|
|
2472
2480
|
headers['content-type'] = contentType || "text/plain";
|
|
2481
|
+
options.headers = headers;
|
|
2473
2482
|
return this.get(uri, options);
|
|
2474
2483
|
}
|
|
2475
2484
|
async get(uri, options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.10",
|
|
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.10.
|
|
24
|
-
"@e-mc/types": "0.10.
|
|
23
|
+
"@e-mc/module": "0.10.10",
|
|
24
|
+
"@e-mc/types": "0.10.10",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"picomatch": "^4.0.2",
|