@e-mc/request 0.10.6 → 0.11.1
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"license": "BSD-3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/module": "0.
|
|
24
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/module": "0.11.1",
|
|
24
|
+
"@e-mc/types": "0.11.1",
|
|
25
25
|
"combined-stream": "^1.0.8",
|
|
26
26
|
"js-yaml": "^4.1.0",
|
|
27
27
|
"picomatch": "^4.0.2",
|
|
28
|
-
"which": "^
|
|
28
|
+
"which": "^4.0.0"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/util.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { AuthValue } from '@e-mc/types/lib/http';
|
|
2
2
|
import type { HttpProxySettings } from '@e-mc/types/lib/settings';
|
|
3
3
|
|
|
4
|
-
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
|
|
5
|
-
import type { Readable, Writable } from 'stream';
|
|
4
|
+
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http';
|
|
5
|
+
import type { Readable, Writable } from 'node:stream';
|
|
6
6
|
|
|
7
7
|
declare namespace util {
|
|
8
8
|
function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
|
|
@@ -13,11 +13,13 @@ declare namespace util {
|
|
|
13
13
|
function hasBasicAuth(value: string): boolean;
|
|
14
14
|
function checkRetryable(err: unknown): boolean;
|
|
15
15
|
function isRetryable(value: number, timeout?: boolean): boolean;
|
|
16
|
-
function parseHttpProxy(value?: string
|
|
16
|
+
function parseHttpProxy(value?: string): HttpProxySettings | undefined;
|
|
17
17
|
function trimPath(value: string): string;
|
|
18
18
|
function asInt(value: unknown): number;
|
|
19
19
|
function asFloat(value: unknown): number;
|
|
20
20
|
function fromSeconds(value: unknown): number;
|
|
21
|
+
function fromURL(url: URL, value: string): string;
|
|
22
|
+
function fromStatusCode(value: number | string): string;
|
|
21
23
|
function getTransferRate(length: number, timeMs: number, unitSeparator?: string): string;
|
|
22
24
|
function hasSameStat(src: string, dest: string, keepEmpty?: boolean): boolean;
|
|
23
25
|
function hasSize(value: string, keepEmpty?: boolean): boolean;
|
package/util.js
CHANGED
|
@@ -11,18 +11,20 @@ exports.trimPath = trimPath;
|
|
|
11
11
|
exports.asInt = asInt;
|
|
12
12
|
exports.asFloat = asFloat;
|
|
13
13
|
exports.fromSeconds = fromSeconds;
|
|
14
|
+
exports.fromURL = fromURL;
|
|
15
|
+
exports.fromStatusCode = fromStatusCode;
|
|
14
16
|
exports.getTransferRate = getTransferRate;
|
|
15
17
|
exports.hasSize = hasSize;
|
|
16
18
|
exports.getSize = getSize;
|
|
17
19
|
exports.hasSameStat = hasSameStat;
|
|
18
20
|
exports.byteLength = byteLength;
|
|
19
21
|
exports.cleanupStream = cleanupStream;
|
|
20
|
-
const path = require("path");
|
|
21
|
-
const fs = require("fs");
|
|
22
|
-
const
|
|
22
|
+
const path = require("node:path");
|
|
23
|
+
const fs = require("node:fs");
|
|
24
|
+
const node_util_1 = require("node:util");
|
|
23
25
|
const types_1 = require("@e-mc/types");
|
|
24
26
|
const module_1 = require("@e-mc/module");
|
|
25
|
-
const
|
|
27
|
+
const host_1 = require("@e-mc/request/http/host");
|
|
26
28
|
const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
|
|
27
29
|
function parseHeader(headers, name) {
|
|
28
30
|
const value = headers[name];
|
|
@@ -86,7 +88,7 @@ function getBasicAuth(username, password) {
|
|
|
86
88
|
if ((0, types_1.isObject)(username)) {
|
|
87
89
|
({ username, password } = username);
|
|
88
90
|
}
|
|
89
|
-
return (0, types_1.isString)(username) ? encodeURIComponent(
|
|
91
|
+
return (0, types_1.isString)(username) ? encodeURIComponent((0, node_util_1.toUSVString)(username)) + ((0, types_1.isString)(password) ? ':' + encodeURIComponent((0, node_util_1.toUSVString)(password)) : '') + '@' : '';
|
|
90
92
|
}
|
|
91
93
|
function hasBasicAuth(value) {
|
|
92
94
|
try {
|
|
@@ -212,6 +214,155 @@ function fromSeconds(value) {
|
|
|
212
214
|
return NaN;
|
|
213
215
|
}
|
|
214
216
|
}
|
|
217
|
+
function fromURL(url, value) {
|
|
218
|
+
if (module_1.isURL(value)) {
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
221
|
+
const auth = host_1.formatBasicAuth(url);
|
|
222
|
+
return url.protocol + '//' + (auth && (auth + '@')) + url.hostname + (url.port ? ':' + url.port : '') + (value.startsWith('/') ? '' : '/') + value;
|
|
223
|
+
}
|
|
224
|
+
function fromStatusCode(value) {
|
|
225
|
+
value = +value;
|
|
226
|
+
if (value < 200) {
|
|
227
|
+
switch (value) {
|
|
228
|
+
case 100:
|
|
229
|
+
return 'Continue';
|
|
230
|
+
case 101:
|
|
231
|
+
return 'Switching Protocol';
|
|
232
|
+
case 102:
|
|
233
|
+
return 'Processing';
|
|
234
|
+
case 103:
|
|
235
|
+
return 'Early Hints';
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else if (value < 300) {
|
|
239
|
+
switch (value) {
|
|
240
|
+
case 200:
|
|
241
|
+
return 'OK';
|
|
242
|
+
case 201:
|
|
243
|
+
return 'Created';
|
|
244
|
+
case 202:
|
|
245
|
+
return 'Accepted';
|
|
246
|
+
case 203:
|
|
247
|
+
return 'Non-Authoritative Information';
|
|
248
|
+
case 204:
|
|
249
|
+
return 'No Content';
|
|
250
|
+
case 205:
|
|
251
|
+
return 'Reset Content';
|
|
252
|
+
case 206:
|
|
253
|
+
return 'Partial Content';
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else if (value < 400) {
|
|
257
|
+
switch (value) {
|
|
258
|
+
case 300:
|
|
259
|
+
return 'Multiple Choice';
|
|
260
|
+
case 301:
|
|
261
|
+
return 'Moved Permanently';
|
|
262
|
+
case 302:
|
|
263
|
+
return 'Found';
|
|
264
|
+
case 303:
|
|
265
|
+
return 'See Other';
|
|
266
|
+
case 304:
|
|
267
|
+
return 'Not Modified';
|
|
268
|
+
case 305:
|
|
269
|
+
return 'Use Proxy';
|
|
270
|
+
case 307:
|
|
271
|
+
return 'Temporary Redirect';
|
|
272
|
+
case 308:
|
|
273
|
+
return 'Permanent Redirect';
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
else if (value < 500) {
|
|
277
|
+
switch (value) {
|
|
278
|
+
case 400:
|
|
279
|
+
return 'Bad Request';
|
|
280
|
+
case 401:
|
|
281
|
+
return 'Upgrade Required';
|
|
282
|
+
case 402:
|
|
283
|
+
return 'Payment Required';
|
|
284
|
+
case 403:
|
|
285
|
+
return 'Forbidden';
|
|
286
|
+
case 404:
|
|
287
|
+
return 'Not Found';
|
|
288
|
+
case 405:
|
|
289
|
+
return 'Method Not Allowed';
|
|
290
|
+
case 406:
|
|
291
|
+
return 'Not Acceptable';
|
|
292
|
+
case 407:
|
|
293
|
+
return 'Proxy Authentication Required';
|
|
294
|
+
case 408:
|
|
295
|
+
return 'Request Timeout';
|
|
296
|
+
case 409:
|
|
297
|
+
return 'Conflict';
|
|
298
|
+
case 410:
|
|
299
|
+
return 'Gone';
|
|
300
|
+
case 411:
|
|
301
|
+
return 'Length Required';
|
|
302
|
+
case 412:
|
|
303
|
+
return 'Precondition Failed';
|
|
304
|
+
case 413:
|
|
305
|
+
return 'Payload Too Large';
|
|
306
|
+
case 414:
|
|
307
|
+
return 'URI Too Long';
|
|
308
|
+
case 415:
|
|
309
|
+
return 'Unsupported Media Type';
|
|
310
|
+
case 416:
|
|
311
|
+
return 'Range Not Satisfiable';
|
|
312
|
+
case 417:
|
|
313
|
+
return 'Expectation Failed';
|
|
314
|
+
case 421:
|
|
315
|
+
return 'Misdirected Request';
|
|
316
|
+
case 422:
|
|
317
|
+
return 'Unprocessable Entity';
|
|
318
|
+
case 423:
|
|
319
|
+
return 'Locked';
|
|
320
|
+
case 424:
|
|
321
|
+
return 'Failed Dependency';
|
|
322
|
+
case 425:
|
|
323
|
+
return 'Too Early';
|
|
324
|
+
case 426:
|
|
325
|
+
return 'Upgrade Required';
|
|
326
|
+
case 428:
|
|
327
|
+
return 'Precondition Required';
|
|
328
|
+
case 429:
|
|
329
|
+
return 'Too Many Requests';
|
|
330
|
+
case 431:
|
|
331
|
+
return 'Request Header Fields Too Large';
|
|
332
|
+
case 451:
|
|
333
|
+
return 'Unavailable For Legal Reasons';
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
switch (value) {
|
|
338
|
+
case 500:
|
|
339
|
+
return 'Internal Server Error';
|
|
340
|
+
case 501:
|
|
341
|
+
return 'Not Implemented';
|
|
342
|
+
case 502:
|
|
343
|
+
return 'Bad Gateway';
|
|
344
|
+
case 503:
|
|
345
|
+
return 'Service Unavailable';
|
|
346
|
+
case 504:
|
|
347
|
+
return 'Gateway Timeout';
|
|
348
|
+
case 505:
|
|
349
|
+
return 'HTTP Version Not Supported';
|
|
350
|
+
case 506:
|
|
351
|
+
return 'Variant Also Negotiates';
|
|
352
|
+
case 507:
|
|
353
|
+
return 'Insufficient Storage';
|
|
354
|
+
case 508:
|
|
355
|
+
return 'Loop Detected';
|
|
356
|
+
case 509:
|
|
357
|
+
return 'Bandwidth Limit Exceeded';
|
|
358
|
+
case 510:
|
|
359
|
+
return 'Not Extended';
|
|
360
|
+
case 511:
|
|
361
|
+
return 'Network Authentication Required';
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return "Unknown";
|
|
365
|
+
}
|
|
215
366
|
function getTransferRate(length, timeMs, unitSeparator = '') {
|
|
216
367
|
const unit = (length * 8) / timeMs;
|
|
217
368
|
if (unit < 1) {
|
|
@@ -273,8 +424,9 @@ function cleanupStream(stream, uri) {
|
|
|
273
424
|
}
|
|
274
425
|
}
|
|
275
426
|
catch (err) {
|
|
276
|
-
if (
|
|
277
|
-
|
|
427
|
+
if (module_1.isErrorCode(err, 'ENOENT')) {
|
|
428
|
+
return;
|
|
278
429
|
}
|
|
430
|
+
module_1.writeFail(["Unable to delete file", path.basename(uri)], err, 32);
|
|
279
431
|
}
|
|
280
432
|
}
|