@e-mc/request 0.10.5 → 0.11.0
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/README.md +16 -12
- package/http/adapter/index.d.ts +5 -0
- package/http/adapter/index.js +554 -0
- package/http/host/index.js +2 -2
- package/index.js +160 -727
- package/package.json +4 -4
- package/util.d.ts +5 -3
- package/util.js +159 -7
package/LICENSE
CHANGED
|
@@ -8,4 +8,4 @@ Redistribution and use in source and binary forms, with or without modification,
|
|
|
8
8
|
|
|
9
9
|
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
10
|
|
|
11
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @e-mc/request
|
|
2
2
|
|
|
3
|
-
* NodeJS 16
|
|
4
|
-
*
|
|
3
|
+
* NodeJS 16 LTS
|
|
4
|
+
* ES2021
|
|
5
5
|
|
|
6
6
|
## General Usage
|
|
7
7
|
|
|
@@ -9,17 +9,17 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.11.0/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
16
16
|
import type { HttpAgentSettings, HttpProtocolVersion, HttpRequestClient, InternetProtocolVersion } from "./http";
|
|
17
|
-
import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, OpenOptions, PostOptions, ProxySettings, PutOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
|
|
17
|
+
import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, IHttpAdapter, OpenOptions, PostOptions, ProxySettings, PutOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
|
|
18
18
|
import type { DnsLookupSettings, RequestModule, RequestSettings } from "./settings";
|
|
19
19
|
|
|
20
|
-
import type { ClientRequest, OutgoingHttpHeaders } from "http";
|
|
21
|
-
import type { LookupFunction } from "net";
|
|
22
|
-
import type { Writable } from "stream";
|
|
20
|
+
import type { ClientRequest, OutgoingHttpHeaders } from "node:http";
|
|
21
|
+
import type { LookupFunction } from "node:net";
|
|
22
|
+
import type { Writable } from "node:stream";
|
|
23
23
|
|
|
24
24
|
interface IRequest extends IModule {
|
|
25
25
|
module: RequestModule;
|
|
@@ -55,8 +55,9 @@ interface IRequest extends IModule {
|
|
|
55
55
|
post(uri: string | URL, data: unknown, contentType?: string, options?: PostOptions): Promise<Buffer | string | null>;
|
|
56
56
|
get(uri: string | URL, options?: OpenOptions): Promise<Buffer | object | string | null>;
|
|
57
57
|
detach(singleton?: boolean): void;
|
|
58
|
-
reset(): void;
|
|
58
|
+
reset(adapter?: IHttpAdapter): void;
|
|
59
59
|
close(): void;
|
|
60
|
+
set adapter(value: unknown);
|
|
60
61
|
set agentTimeout(value);
|
|
61
62
|
get agentTimeout(): number;
|
|
62
63
|
set httpVersion(value);
|
|
@@ -71,10 +72,13 @@ interface RequestConstructor extends ModuleConstructor {
|
|
|
71
72
|
readTLSKey(value: string, cache?: boolean): string;
|
|
72
73
|
readTLSCert(value: string, cache?: boolean): string;
|
|
73
74
|
isCert(value: string): boolean;
|
|
75
|
+
/** @deprecated */
|
|
74
76
|
fromURL(url: URL, value: string): string;
|
|
77
|
+
/** @deprecated */
|
|
75
78
|
fromStatusCode(value: number | string): string;
|
|
76
79
|
defineHttpAgent(options: HttpAgentSettings): void;
|
|
77
80
|
defineDnsLookup(options: DnsLookupSettings, clear?: boolean): void;
|
|
81
|
+
defineHttpAdapter(module: unknown): void;
|
|
78
82
|
getAria2Path(): string;
|
|
79
83
|
readonly prototype: IRequest;
|
|
80
84
|
new(module?: RequestModule): IRequest;
|
|
@@ -205,12 +209,12 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
|
|
|
205
209
|
|
|
206
210
|
## References
|
|
207
211
|
|
|
208
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
209
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
210
|
-
- https://www.unpkg.com/@e-mc/types@0.
|
|
212
|
+
- https://www.unpkg.com/@e-mc/types@0.11.0/lib/http.d.ts
|
|
213
|
+
- https://www.unpkg.com/@e-mc/types@0.11.0/lib/request.d.ts
|
|
214
|
+
- https://www.unpkg.com/@e-mc/types@0.11.0/lib/settings.d.ts
|
|
211
215
|
|
|
212
216
|
* https://www.npmjs.com/package/@types/node
|
|
213
217
|
|
|
214
218
|
## LICENSE
|
|
215
219
|
|
|
216
|
-
BSD 3-Clause
|
|
220
|
+
BSD 3-Clause
|
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const http2 = require("node:http2");
|
|
5
|
+
const yaml = require("js-yaml");
|
|
6
|
+
const types_1 = require("@e-mc/types");
|
|
7
|
+
const module_1 = require("@e-mc/module");
|
|
8
|
+
const util_1 = require("@e-mc/request/util");
|
|
9
|
+
const kOutStream = Symbol('outStream');
|
|
10
|
+
const kOptions = Symbol('options');
|
|
11
|
+
let LOG_TIMEFORMAT = 'readable';
|
|
12
|
+
class HttpAdapter {
|
|
13
|
+
static isUnsupported(value) {
|
|
14
|
+
return value === 421 || value === 505;
|
|
15
|
+
}
|
|
16
|
+
static isDowngrade(err) {
|
|
17
|
+
return err instanceof Error && (err.code === 'ERR_HTTP2_ERROR' || this.isUnsupported(Math.abs(err.errno)));
|
|
18
|
+
}
|
|
19
|
+
static wasAborted(err) {
|
|
20
|
+
return err instanceof Error && err.message.startsWith("Aborted");
|
|
21
|
+
}
|
|
22
|
+
static isConnectionError(err) {
|
|
23
|
+
switch (err instanceof Error && err.code) {
|
|
24
|
+
case 'ETIMEDOUT':
|
|
25
|
+
case 'ECONNRESET':
|
|
26
|
+
return true;
|
|
27
|
+
default:
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
static defineHostConfig({ settings }) {
|
|
32
|
+
const time_format = settings?.time_format;
|
|
33
|
+
switch (time_format) {
|
|
34
|
+
case 'readable':
|
|
35
|
+
case 'relative':
|
|
36
|
+
case 'none':
|
|
37
|
+
LOG_TIMEFORMAT = time_format;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
constructor(instance, state, uri, options) {
|
|
42
|
+
this.instance = instance;
|
|
43
|
+
this.state = state;
|
|
44
|
+
this.uri = uri;
|
|
45
|
+
this.contentLength = 0;
|
|
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;
|
|
55
|
+
this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
|
|
56
|
+
this.setOpts();
|
|
57
|
+
}
|
|
58
|
+
async start() {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
this.resolve = resolve;
|
|
61
|
+
this.reject = reject;
|
|
62
|
+
this.init();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
init() {
|
|
66
|
+
this.aborted = false;
|
|
67
|
+
this.setWriteStream();
|
|
68
|
+
this.client = this.instance.open(this.uri, this.opts);
|
|
69
|
+
if (this.opts.httpVersion === 2) {
|
|
70
|
+
this.client
|
|
71
|
+
.on('response', (headers, flags) => {
|
|
72
|
+
if (this.destroyed) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const statusCode = headers[':status'];
|
|
76
|
+
if (statusCode < 300) {
|
|
77
|
+
this.acceptResponse(headers);
|
|
78
|
+
}
|
|
79
|
+
else if (statusCode < 400) {
|
|
80
|
+
this.redirectResponse(statusCode, headers.location);
|
|
81
|
+
}
|
|
82
|
+
else if (statusCode === 401 ||
|
|
83
|
+
statusCode === 402 ||
|
|
84
|
+
statusCode === 403 ||
|
|
85
|
+
statusCode === 404 ||
|
|
86
|
+
statusCode === 407 ||
|
|
87
|
+
statusCode === 410) {
|
|
88
|
+
this.terminate(this.formatStatus(statusCode));
|
|
89
|
+
}
|
|
90
|
+
else if (this.isRetry(statusCode)) {
|
|
91
|
+
this.retryResponse(statusCode, headers['retry-after']);
|
|
92
|
+
}
|
|
93
|
+
else if (HttpAdapter.isUnsupported(statusCode)) {
|
|
94
|
+
this.retryDownload(true, this.formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
switch (flags) {
|
|
98
|
+
case http2.constants.NGHTTP2_PROTOCOL_ERROR:
|
|
99
|
+
case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
|
|
100
|
+
case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
|
|
101
|
+
this.retryDownload(true, this.formatNgFlags(flags, statusCode, headers.location));
|
|
102
|
+
break;
|
|
103
|
+
default:
|
|
104
|
+
this.retryDownload(false, this.formatStatus(statusCode));
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
.on('unknownProtocol', () => {
|
|
110
|
+
if (this.aborted) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.retryDownload(true, 'Unknown protocol (HTTP/2)');
|
|
114
|
+
})
|
|
115
|
+
.on('aborted', () => {
|
|
116
|
+
this.aborted = true;
|
|
117
|
+
this.terminate((0, types_1.createAbortError)());
|
|
118
|
+
})
|
|
119
|
+
.on('error', async (err) => {
|
|
120
|
+
if (this.aborted) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (HttpAdapter.wasAborted(err)) {
|
|
124
|
+
this.errorResponse(err);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
switch (!HttpAdapter.isDowngrade(err) && await this.host.hasProtocol(2)) {
|
|
128
|
+
case 1:
|
|
129
|
+
this.errorResponse(err);
|
|
130
|
+
break;
|
|
131
|
+
case 2:
|
|
132
|
+
this.retryDownload(false, err);
|
|
133
|
+
break;
|
|
134
|
+
default:
|
|
135
|
+
this.retryDownload(true, err);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
this.client
|
|
143
|
+
.on('response', res => {
|
|
144
|
+
if (this.destroyed) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const statusCode = res.statusCode;
|
|
148
|
+
if (statusCode < 300) {
|
|
149
|
+
this.acceptResponse(res.headers);
|
|
150
|
+
}
|
|
151
|
+
else if (statusCode < 400) {
|
|
152
|
+
this.redirectResponse(statusCode, res.headers.location);
|
|
153
|
+
}
|
|
154
|
+
else if (this.isRetry(statusCode)) {
|
|
155
|
+
this.retryResponse(statusCode, res.headers['retry-after']);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
this.terminate(this.formatStatus(statusCode));
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
.on('abort', () => {
|
|
162
|
+
this.aborted = true;
|
|
163
|
+
this.terminate((0, types_1.createAbortError)());
|
|
164
|
+
})
|
|
165
|
+
.on('error', err => {
|
|
166
|
+
if (this.aborted) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
this.errorResponse(err);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
this.client.on('timeout', () => {
|
|
173
|
+
if (this.aborted) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (++this.retries <= this.retryLimit) {
|
|
177
|
+
this.abortResponse();
|
|
178
|
+
this.retryTimeout();
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
this.terminate(this.formatStatus(408));
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
setOpts(uri) {
|
|
186
|
+
if (uri) {
|
|
187
|
+
this.uri = uri;
|
|
188
|
+
}
|
|
189
|
+
this.opts = this.instance.opts(this.uri, this[kOptions]);
|
|
190
|
+
}
|
|
191
|
+
setWriteStream() {
|
|
192
|
+
const pipeTo = this.pipeTo;
|
|
193
|
+
if (typeof pipeTo === 'string') {
|
|
194
|
+
try {
|
|
195
|
+
this.outStream = fs.createWriteStream(pipeTo, { emitClose: false, highWaterMark: this.host.streamSize });
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
this.terminate(err);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (pipeTo) {
|
|
202
|
+
this.outStream = pipeTo;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
retryDownload(downgrade, message) {
|
|
206
|
+
if (this.aborted) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
this.abortResponse();
|
|
210
|
+
if (downgrade) {
|
|
211
|
+
const host = this.host;
|
|
212
|
+
host.failed(2);
|
|
213
|
+
if (host.version > 1) {
|
|
214
|
+
if (this.state.verbose) {
|
|
215
|
+
this.instance.formatMessage(1024, 'HTTP2', ["Unsupported protocol", host.origin], message, { failed: true });
|
|
216
|
+
}
|
|
217
|
+
host.version = 1;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
this.opts.httpVersion = 1;
|
|
221
|
+
this.init();
|
|
222
|
+
}
|
|
223
|
+
acceptResponse(headers) {
|
|
224
|
+
const opts = this.opts;
|
|
225
|
+
if ('outHeaders' in opts) {
|
|
226
|
+
opts.outHeaders = headers;
|
|
227
|
+
}
|
|
228
|
+
if ('outFilename' in opts) {
|
|
229
|
+
opts.outFilename = (0, util_1.parseHeader)(headers, 'content-disposition');
|
|
230
|
+
}
|
|
231
|
+
const pipeline = this.pipeTo ? !(0, types_1.isString)(this.pipeTo) : false;
|
|
232
|
+
const enabled = opts.connected?.call(this.client, headers) !== false && !pipeline;
|
|
233
|
+
const maxBufferSize = opts.maxBufferSize ? (0, types_1.alignSize)(opts.maxBufferSize) : 0;
|
|
234
|
+
this.contentLength = parseInt(headers['content-length'] || '0');
|
|
235
|
+
const updating = opts.progressId !== undefined && !!this.instance.host && this.contentLength > 0;
|
|
236
|
+
const readTimeout = this.instance.readTimeout;
|
|
237
|
+
let log = this.state.log, buffer = null, dataLength = 0;
|
|
238
|
+
this.client.once('readable', () => {
|
|
239
|
+
if (readTimeout > 0) {
|
|
240
|
+
this.timeout = setTimeout(() => {
|
|
241
|
+
this.terminate((0, types_1.errorValue)("Timeout was exceeded", this.uri.toString()));
|
|
242
|
+
}, readTimeout);
|
|
243
|
+
}
|
|
244
|
+
if (log) {
|
|
245
|
+
switch (this.instance.settings?.time_format || LOG_TIMEFORMAT) {
|
|
246
|
+
case 'readable':
|
|
247
|
+
this.delayTime = process.hrtime.bigint() - this.startTime;
|
|
248
|
+
break;
|
|
249
|
+
case 'relative':
|
|
250
|
+
this.delayTime = Date.now() - this.instance.startTime;
|
|
251
|
+
break;
|
|
252
|
+
case 'none':
|
|
253
|
+
if (!this.silent) {
|
|
254
|
+
log = false;
|
|
255
|
+
}
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
this.dataTime = process.hrtime.bigint();
|
|
260
|
+
if (updating) {
|
|
261
|
+
this.updateProgress(0, 0);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
if (enabled) {
|
|
265
|
+
const encoding = opts.encoding;
|
|
266
|
+
this.client.on('data', (chunk) => {
|
|
267
|
+
if (buffer) {
|
|
268
|
+
if (typeof buffer === 'string') {
|
|
269
|
+
buffer += typeof chunk === 'string' ? chunk : chunk.toString(encoding);
|
|
270
|
+
}
|
|
271
|
+
else if (Array.isArray(buffer)) {
|
|
272
|
+
buffer.push(typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
buffer = Buffer.concat([buffer, typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
buffer = typeof chunk === 'string' ? chunk : [chunk];
|
|
280
|
+
}
|
|
281
|
+
if (updating) {
|
|
282
|
+
dataLength += Buffer.byteLength(chunk, encoding);
|
|
283
|
+
this.updateProgress(dataLength, this.contentLength);
|
|
284
|
+
}
|
|
285
|
+
if (maxBufferSize > 0) {
|
|
286
|
+
if (!updating) {
|
|
287
|
+
dataLength += Buffer.byteLength(chunk, encoding);
|
|
288
|
+
}
|
|
289
|
+
if (dataLength > maxBufferSize) {
|
|
290
|
+
this.terminate((0, types_1.errorValue)("Size limit was exceeded", this.uri.toString()));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
this.client.once('end', () => {
|
|
296
|
+
if (this.closed || this.aborted) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
this.close();
|
|
300
|
+
const encoding = opts.encoding;
|
|
301
|
+
let result;
|
|
302
|
+
if (buffer) {
|
|
303
|
+
if (Array.isArray(buffer)) {
|
|
304
|
+
buffer = Buffer.concat(buffer);
|
|
305
|
+
if (encoding) {
|
|
306
|
+
buffer = buffer.toString(encoding);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
dataLength = Buffer.byteLength(buffer, encoding);
|
|
310
|
+
if (updating) {
|
|
311
|
+
this.updateProgress(dataLength, dataLength);
|
|
312
|
+
}
|
|
313
|
+
if (typeof buffer === 'string') {
|
|
314
|
+
if (buffer.startsWith('\uFEFF') && encoding !== 'utf16le' && encoding !== 'utf-16le') {
|
|
315
|
+
buffer = buffer.substring(1);
|
|
316
|
+
}
|
|
317
|
+
if (opts.outFormat) {
|
|
318
|
+
const { out: format, parser } = opts.outFormat;
|
|
319
|
+
let packageName;
|
|
320
|
+
try {
|
|
321
|
+
switch (format) {
|
|
322
|
+
case 'yaml':
|
|
323
|
+
result = yaml.load(buffer, parser);
|
|
324
|
+
break;
|
|
325
|
+
case 'json5':
|
|
326
|
+
result = require(packageName = 'json5').parse(buffer);
|
|
327
|
+
break;
|
|
328
|
+
case 'xml':
|
|
329
|
+
result = new (require(packageName = 'fast-xml-parser').XMLParser)(parser).parse(buffer);
|
|
330
|
+
break;
|
|
331
|
+
case 'toml':
|
|
332
|
+
result = require(packageName = 'toml').parse(buffer);
|
|
333
|
+
break;
|
|
334
|
+
default:
|
|
335
|
+
result = JSON.parse(buffer);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
if (!(0, types_1.isObject)(result)) {
|
|
339
|
+
result = null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
catch (err) {
|
|
343
|
+
if (this.state.verbose && !(packageName && this.instance.checkPackage(err, packageName))) {
|
|
344
|
+
this.instance.writeFail(["Unable to parse URI response", format], err, 1024);
|
|
345
|
+
}
|
|
346
|
+
result = null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (result === undefined) {
|
|
351
|
+
result = buffer;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
if (updating) {
|
|
356
|
+
this.updateProgress(0, this.contentLength);
|
|
357
|
+
}
|
|
358
|
+
if (enabled && this.instance.readExpect === 'always') {
|
|
359
|
+
this.terminate("No data received");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
result = encoding && !pipeline ? '' : null;
|
|
363
|
+
}
|
|
364
|
+
this.endResponse(result, dataLength, log);
|
|
365
|
+
});
|
|
366
|
+
this.host.success(this.httpVersion);
|
|
367
|
+
}
|
|
368
|
+
updateProgress(dataLength, contentLength) {
|
|
369
|
+
const { host, moduleName } = this.instance;
|
|
370
|
+
host.updateProgress(moduleName, this.opts.progressId, dataLength, contentLength, this.dataTime);
|
|
371
|
+
}
|
|
372
|
+
endResponse(result, dataLength = 0, logging = this.state.log) {
|
|
373
|
+
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
|
+
this.instance.writeTimeProcess('HTTP' + this.httpVersion, this.opts.statusMessage || this.uri.toString(), this.startTime, {
|
|
376
|
+
type: 1024,
|
|
377
|
+
queue: !!this.instance.host,
|
|
378
|
+
titleBgColor: !result ? 'bgBlue' : undefined,
|
|
379
|
+
messageUnit,
|
|
380
|
+
messageUnitMinWidth: 9,
|
|
381
|
+
delayTime: this.delayTime,
|
|
382
|
+
bypassLog: module_1.hasLogType(32768)
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
this.resolve(result);
|
|
386
|
+
}
|
|
387
|
+
redirectResponse(statusCode, location) {
|
|
388
|
+
if (location) {
|
|
389
|
+
if (this.opts.followRedirect === false) {
|
|
390
|
+
this.terminate(this.formatStatus(statusCode, "Follow redirect was disabled"));
|
|
391
|
+
}
|
|
392
|
+
else if (++this.redirects <= this.redirectLimit) {
|
|
393
|
+
this.abortResponse();
|
|
394
|
+
this.setOpts((0, util_1.fromURL)(this.opts.url, location));
|
|
395
|
+
this.init();
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
this.terminate(this.formatStatus(statusCode, "Exceeded redirect limit"));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
this.terminate(this.formatStatus(statusCode, "Missing redirect location"));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
abortResponse() {
|
|
406
|
+
if (this.closed) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
this.aborted = true;
|
|
410
|
+
this.client.destroy();
|
|
411
|
+
this.instance.reset(this);
|
|
412
|
+
this.cleanup();
|
|
413
|
+
}
|
|
414
|
+
errorResponse(err) {
|
|
415
|
+
if (HttpAdapter.wasAborted(err)) {
|
|
416
|
+
this.terminate(err);
|
|
417
|
+
}
|
|
418
|
+
else if ((0, util_1.checkRetryable)(err) && ++this.retries <= this.retryLimit) {
|
|
419
|
+
this.abortResponse();
|
|
420
|
+
if (HttpAdapter.isConnectionError(err)) {
|
|
421
|
+
this.retryTimeout();
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
setTimeout(() => {
|
|
425
|
+
this.init();
|
|
426
|
+
}, this.retryWait);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
this.host.error(this.httpVersion);
|
|
431
|
+
this.terminate(err);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
retryResponse(statusCode, retryAfter) {
|
|
435
|
+
this.abortResponse();
|
|
436
|
+
if (retryAfter && this.retryAfter > 0) {
|
|
437
|
+
let offset = +retryAfter || new Date(retryAfter);
|
|
438
|
+
if (offset instanceof Date) {
|
|
439
|
+
offset = Math.max(0, offset.getTime() - Date.now());
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
offset *= 1000;
|
|
443
|
+
}
|
|
444
|
+
if (offset > 0) {
|
|
445
|
+
if (offset <= this.retryAfter) {
|
|
446
|
+
this.sendWarning(`Retry After (${retryAfter})`);
|
|
447
|
+
setTimeout(() => {
|
|
448
|
+
this.init();
|
|
449
|
+
}, offset);
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
this.terminate(this.formatStatus(statusCode));
|
|
453
|
+
}
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
this.sendWarning(this.formatRetry((0, util_1.fromStatusCode)(statusCode)));
|
|
458
|
+
if ((0, util_1.isRetryable)(statusCode, true)) {
|
|
459
|
+
setImmediate(this.init.bind(this));
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
setTimeout(() => {
|
|
463
|
+
this.init();
|
|
464
|
+
}, this.retryWait);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
isRetry(value) {
|
|
468
|
+
return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
|
|
469
|
+
}
|
|
470
|
+
retryTimeout() {
|
|
471
|
+
this.sendWarning(this.formatRetry("HTTP connection timeout"));
|
|
472
|
+
this.init();
|
|
473
|
+
}
|
|
474
|
+
terminate(err) {
|
|
475
|
+
if (this.closed) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
this.cleanup();
|
|
479
|
+
this.close();
|
|
480
|
+
this.reject(typeof err === 'string' ? new Error(err) : err);
|
|
481
|
+
}
|
|
482
|
+
sendWarning(message) {
|
|
483
|
+
if (this.state.verbose) {
|
|
484
|
+
const { host, url } = this.opts;
|
|
485
|
+
this.instance.formatMessage(1024, 'HTTP' + this.httpVersion, [message, host.origin], url.toString(), { ...module_1.LOG_STYLE_WARN });
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
formatStatus(value, hint) {
|
|
489
|
+
return value + ': ' + (hint || (0, util_1.fromStatusCode)(value)) + ` (${this.uri.toString()})`;
|
|
490
|
+
}
|
|
491
|
+
close() {
|
|
492
|
+
this.closed = true;
|
|
493
|
+
this.instance.reset(this);
|
|
494
|
+
if (this.aborted && !this.client.aborted) {
|
|
495
|
+
this.abortController?.abort();
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
cleanup() {
|
|
499
|
+
if ((0, types_1.isString)(this.pipeTo) && this.outStream) {
|
|
500
|
+
(0, util_1.cleanupStream)(this.outStream, this.pipeTo);
|
|
501
|
+
this.outStream = null;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
formatNgFlags(value, statusCode, location) {
|
|
505
|
+
return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
|
|
506
|
+
}
|
|
507
|
+
formatRetry(message) {
|
|
508
|
+
return `${message} (${this.retries} / ${this.retryLimit})`;
|
|
509
|
+
}
|
|
510
|
+
set abortController(value) {
|
|
511
|
+
this.opts.outAbort = value;
|
|
512
|
+
}
|
|
513
|
+
get abortController() {
|
|
514
|
+
return this.opts.outAbort || null;
|
|
515
|
+
}
|
|
516
|
+
set outStream(value) {
|
|
517
|
+
this[kOutStream] = value;
|
|
518
|
+
if (value) {
|
|
519
|
+
this.opts.outStream = value;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
get outStream() {
|
|
523
|
+
return this[kOutStream];
|
|
524
|
+
}
|
|
525
|
+
get destroyed() {
|
|
526
|
+
return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
|
|
527
|
+
}
|
|
528
|
+
get host() {
|
|
529
|
+
return this.opts.host;
|
|
530
|
+
}
|
|
531
|
+
get httpVersion() {
|
|
532
|
+
return this.opts.httpVersion;
|
|
533
|
+
}
|
|
534
|
+
get pipeTo() {
|
|
535
|
+
return this.opts.pipeTo;
|
|
536
|
+
}
|
|
537
|
+
get silent() {
|
|
538
|
+
return this.opts.silent === false;
|
|
539
|
+
}
|
|
540
|
+
get retryLimit() {
|
|
541
|
+
return this.state.config.retryLimit;
|
|
542
|
+
}
|
|
543
|
+
get retryWait() {
|
|
544
|
+
return this.state.config.retryWait;
|
|
545
|
+
}
|
|
546
|
+
get retryAfter() {
|
|
547
|
+
return this.state.config.retryAfter;
|
|
548
|
+
}
|
|
549
|
+
get redirectLimit() {
|
|
550
|
+
return this.state.config.redirectLimit;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
_a = kOutStream;
|
|
554
|
+
module.exports = HttpAdapter;
|
package/http/host/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var _a, _b, _c, _d;
|
|
3
|
-
const tls = require("tls");
|
|
3
|
+
const tls = require("node:tls");
|
|
4
4
|
const types_1 = require("@e-mc/types");
|
|
5
5
|
const kProtocol = Symbol('protocol');
|
|
6
6
|
const kSecure = Symbol('secure');
|
|
@@ -19,7 +19,7 @@ const HOST_HTTP_1_1 = [];
|
|
|
19
19
|
const HOST_ALPN_H2C = [];
|
|
20
20
|
const HOST_ALPN_H2 = [];
|
|
21
21
|
(function () {
|
|
22
|
-
const nic = require('os').networkInterfaces();
|
|
22
|
+
const nic = require('node:os').networkInterfaces();
|
|
23
23
|
for (const name in nic) {
|
|
24
24
|
for (const { address } of nic[name].filter(item => item.internal)) {
|
|
25
25
|
HOST_LOCAL.add(address);
|