@e-mc/request 0.13.10 → 0.14.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/README.md +5 -5
- package/http/adapter/index.js +34 -37
- package/http/host/altsvc/index.d.ts +5 -0
- package/http/host/altsvc/index.js +2 -0
- package/http/host/index.js +8 -21
- package/index.js +235 -209
- package/package.json +3 -6
- package/util.d.ts +10 -3
- package/util.js +68 -40
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
7
|
"repository": {
|
|
11
8
|
"type": "git",
|
|
12
9
|
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
@@ -19,8 +16,8 @@
|
|
|
19
16
|
"license": "BSD-3-Clause",
|
|
20
17
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
21
18
|
"dependencies": {
|
|
22
|
-
"@e-mc/module": "0.
|
|
23
|
-
"@e-mc/types": "0.
|
|
19
|
+
"@e-mc/module": "0.14.1",
|
|
20
|
+
"@e-mc/types": "0.14.1",
|
|
24
21
|
"combined-stream": "^1.0.8",
|
|
25
22
|
"js-yaml": "^4.1.1",
|
|
26
23
|
"picomatch": "^4.0.4",
|
package/util.d.ts
CHANGED
|
@@ -4,10 +4,17 @@ import type { HttpProxySettings } from '@e-mc/types/lib/settings';
|
|
|
4
4
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http';
|
|
5
5
|
import type { Readable, Writable } from 'node:stream';
|
|
6
6
|
|
|
7
|
+
type OutgoingHeaders = OutgoingHttpHeaders | Headers;
|
|
8
|
+
|
|
7
9
|
declare namespace util {
|
|
8
|
-
function parseHeader
|
|
9
|
-
function
|
|
10
|
-
function
|
|
10
|
+
function parseHeader(headers: IncomingHttpHeaders | OutgoingHttpHeaders, name: "cache-control"): string[] | undefined;
|
|
11
|
+
function parseHeader(headers: IncomingHttpHeaders, name: "expires" | "date" | "age"): number | undefined;
|
|
12
|
+
function parseHeader(headers: IncomingHttpHeaders, name: "content-disposition"): string | undefined;
|
|
13
|
+
function parseHeader<T extends string | string[] | number>(headers: IncomingHttpHeaders, name: string): T | undefined;
|
|
14
|
+
/** @deprecated copyHeaders (rename) */
|
|
15
|
+
function parseOutgoingHeaders(headers: OutgoingHeaders | undefined, ...ignore: string[]): OutgoingHttpHeaders | undefined;
|
|
16
|
+
function copyHeaders(headers: OutgoingHeaders | undefined, ...ignore: string[]): OutgoingHttpHeaders | undefined;
|
|
17
|
+
function normalizeHeaders(headers: OutgoingHeaders): OutgoingHttpHeaders;
|
|
11
18
|
function getBasicAuth(auth: AuthValue): string;
|
|
12
19
|
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
13
20
|
function hasBasicAuth(value: string): boolean;
|
package/util.js
CHANGED
|
@@ -1,30 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
exports.parseHttpProxy = parseHttpProxy;
|
|
10
|
-
exports.trimPath = trimPath;
|
|
11
|
-
exports.asInt = asInt;
|
|
12
|
-
exports.asFloat = asFloat;
|
|
13
|
-
exports.fromSeconds = fromSeconds;
|
|
14
|
-
exports.fromURL = fromURL;
|
|
15
|
-
exports.fromStatusCode = fromStatusCode;
|
|
16
|
-
exports.getTransferRate = getTransferRate;
|
|
17
|
-
exports.hasSize = hasSize;
|
|
18
|
-
exports.getSize = getSize;
|
|
19
|
-
exports.hasSameStat = hasSameStat;
|
|
20
|
-
exports.byteLength = byteLength;
|
|
21
|
-
exports.cleanupStream = cleanupStream;
|
|
22
|
-
const path = require("node:path");
|
|
23
|
-
const fs = require("node:fs");
|
|
24
|
-
const node_util_1 = require("node:util");
|
|
25
|
-
const types_1 = require("@e-mc/types");
|
|
26
|
-
const module_1 = require("@e-mc/module");
|
|
27
|
-
const host_1 = require("@e-mc/request/http/host");
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const { toUSVString } = require('node:util');
|
|
6
|
+
const { getEncoding, isArray, isError, isErrorCode, isObject, isString } = require('@e-mc/types');
|
|
7
|
+
const Module = require('@e-mc/module');
|
|
8
|
+
const HttpHost = require('@e-mc/request/http/host');
|
|
28
9
|
function setHeader(result, key, value) {
|
|
29
10
|
if (key === 'set-cookie') {
|
|
30
11
|
(result[key] ||= []).push(value);
|
|
@@ -36,9 +17,32 @@ function setHeader(result, key, value) {
|
|
|
36
17
|
const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
|
|
37
18
|
function parseHeader(headers, name) {
|
|
38
19
|
const value = headers[name];
|
|
20
|
+
if (!value) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
39
23
|
switch (name.toLowerCase()) {
|
|
24
|
+
case 'date':
|
|
25
|
+
case 'expires': {
|
|
26
|
+
const result = Date.parse(value);
|
|
27
|
+
if (!isNaN(result)) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'age': {
|
|
33
|
+
const result = parseInt(value);
|
|
34
|
+
if (!isNaN(result)) {
|
|
35
|
+
return result * 1000;
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 'cache-control':
|
|
40
|
+
if (isString(value)) {
|
|
41
|
+
return value.toLowerCase().split(/\s*,\s*/);
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
40
44
|
case 'content-disposition':
|
|
41
|
-
if (
|
|
45
|
+
if (isString(value)) {
|
|
42
46
|
let result;
|
|
43
47
|
for (const match of value.matchAll(/\bfilename(?:\*\s*=\s*UTF-8''([^\s;]+)|\s*=\s*(?:"([^"]+)"|([^\s;]+)))/gi)) {
|
|
44
48
|
if (match[1]) {
|
|
@@ -49,9 +53,11 @@ function parseHeader(headers, name) {
|
|
|
49
53
|
return result;
|
|
50
54
|
}
|
|
51
55
|
break;
|
|
56
|
+
default:
|
|
57
|
+
return value;
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
|
-
function
|
|
60
|
+
function copyHeaders(headers, ...ignore) {
|
|
55
61
|
if (!headers) {
|
|
56
62
|
return;
|
|
57
63
|
}
|
|
@@ -92,10 +98,10 @@ function normalizeHeaders(headers) {
|
|
|
92
98
|
value = value.trim();
|
|
93
99
|
break;
|
|
94
100
|
default:
|
|
95
|
-
if (!
|
|
101
|
+
if (!isArray(value)) {
|
|
96
102
|
continue;
|
|
97
103
|
}
|
|
98
|
-
value = value.map(out =>
|
|
104
|
+
value = value.map(out => Module.asString(out).trim());
|
|
99
105
|
break;
|
|
100
106
|
}
|
|
101
107
|
if (value) {
|
|
@@ -105,10 +111,10 @@ function normalizeHeaders(headers) {
|
|
|
105
111
|
return result;
|
|
106
112
|
}
|
|
107
113
|
function getBasicAuth(username, password) {
|
|
108
|
-
if (
|
|
114
|
+
if (isObject(username)) {
|
|
109
115
|
({ username, password } = username);
|
|
110
116
|
}
|
|
111
|
-
return
|
|
117
|
+
return isString(username) ? encodeURIComponent(toUSVString(username)) + (isString(password) ? ':' + encodeURIComponent(toUSVString(password)) : '') + '@' : '';
|
|
112
118
|
}
|
|
113
119
|
function hasBasicAuth(value) {
|
|
114
120
|
try {
|
|
@@ -119,7 +125,7 @@ function hasBasicAuth(value) {
|
|
|
119
125
|
return false;
|
|
120
126
|
}
|
|
121
127
|
function checkRetryable(err) {
|
|
122
|
-
if (err
|
|
128
|
+
if (isError(err)) {
|
|
123
129
|
const { code, errno } = err;
|
|
124
130
|
switch (code) {
|
|
125
131
|
case 'ETIMEDOUT':
|
|
@@ -174,11 +180,11 @@ function parseHttpProxy(value, ignoreEnv) {
|
|
|
174
180
|
exclude = ['**'];
|
|
175
181
|
break;
|
|
176
182
|
}
|
|
177
|
-
if (
|
|
183
|
+
if (Module.isURL(item)) {
|
|
178
184
|
exclude.push(item);
|
|
179
185
|
}
|
|
180
186
|
else {
|
|
181
|
-
const negate = item.startsWith('!') ? (item = item.
|
|
187
|
+
const negate = item.startsWith('!') ? (item = item.slice(1), '!') : '';
|
|
182
188
|
if (!/^(?:[^.]+|(?:\d+\.){3}\d+)$/.test(item) && !item.includes('*')) {
|
|
183
189
|
item = '*' + item;
|
|
184
190
|
}
|
|
@@ -237,10 +243,10 @@ function fromSeconds(value) {
|
|
|
237
243
|
}
|
|
238
244
|
}
|
|
239
245
|
function fromURL(url, value) {
|
|
240
|
-
if (
|
|
246
|
+
if (Module.isURL(value)) {
|
|
241
247
|
return value;
|
|
242
248
|
}
|
|
243
|
-
const auth =
|
|
249
|
+
const auth = HttpHost.formatBasicAuth(url);
|
|
244
250
|
return url.protocol + '//' + (auth && (auth + '@')) + url.hostname + (url.port ? ':' + url.port : '') + (value.startsWith('/') ? '' : '/') + value;
|
|
245
251
|
}
|
|
246
252
|
function fromStatusCode(value) {
|
|
@@ -435,7 +441,7 @@ function hasSameStat(src, dest, keepEmpty) {
|
|
|
435
441
|
return false;
|
|
436
442
|
}
|
|
437
443
|
function byteLength(value, encoding) {
|
|
438
|
-
return typeof value === 'string' && (path.isAbsolute(value) ||
|
|
444
|
+
return typeof value === 'string' && (path.isAbsolute(value) || Module.isPath(value)) ? getSize(value) : Buffer.byteLength(value, encoding && getEncoding(encoding));
|
|
439
445
|
}
|
|
440
446
|
function cleanupStream(stream, uri) {
|
|
441
447
|
try {
|
|
@@ -446,8 +452,30 @@ function cleanupStream(stream, uri) {
|
|
|
446
452
|
}
|
|
447
453
|
}
|
|
448
454
|
catch (err) {
|
|
449
|
-
if (!
|
|
450
|
-
|
|
455
|
+
if (!isErrorCode(err, 'ENOENT')) {
|
|
456
|
+
Module.writeFail(["Unable to delete file", path.basename(uri)], err, 32);
|
|
451
457
|
}
|
|
452
458
|
}
|
|
453
459
|
}
|
|
460
|
+
|
|
461
|
+
exports.asFloat = asFloat;
|
|
462
|
+
exports.asInt = asInt;
|
|
463
|
+
exports.byteLength = byteLength;
|
|
464
|
+
exports.checkRetryable = checkRetryable;
|
|
465
|
+
exports.cleanupStream = cleanupStream;
|
|
466
|
+
exports.copyHeaders = copyHeaders;
|
|
467
|
+
exports.fromSeconds = fromSeconds;
|
|
468
|
+
exports.fromStatusCode = fromStatusCode;
|
|
469
|
+
exports.fromURL = fromURL;
|
|
470
|
+
exports.getBasicAuth = getBasicAuth;
|
|
471
|
+
exports.getSize = getSize;
|
|
472
|
+
exports.getTransferRate = getTransferRate;
|
|
473
|
+
exports.hasBasicAuth = hasBasicAuth;
|
|
474
|
+
exports.hasSameStat = hasSameStat;
|
|
475
|
+
exports.hasSize = hasSize;
|
|
476
|
+
exports.isRetryable = isRetryable;
|
|
477
|
+
exports.normalizeHeaders = normalizeHeaders;
|
|
478
|
+
exports.parseHeader = parseHeader;
|
|
479
|
+
exports.parseHttpProxy = parseHttpProxy;
|
|
480
|
+
exports.parseOutgoingHeaders = copyHeaders;
|
|
481
|
+
exports.trimPath = trimPath;
|