rekwest 6.2.1 → 7.0.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 +11 -10
- package/dist/codecs.cjs +55 -0
- package/dist/{config.js → config.cjs} +15 -19
- package/dist/{cookies.js → cookies.cjs} +2 -2
- package/dist/{formdata.js → formdata.cjs} +8 -11
- package/dist/{index.js → index.cjs} +27 -15
- package/dist/{mixin.js → mixin.cjs} +22 -17
- package/dist/postflight.cjs +60 -0
- package/dist/{preflight.js → preflight.cjs} +13 -23
- package/dist/redirects.cjs +63 -0
- package/dist/retries.cjs +57 -0
- package/dist/{transfer.js → transfer.cjs} +13 -40
- package/dist/transform.cjs +105 -0
- package/dist/{utils.js → utils.cjs} +57 -80
- package/dist/{validation.js → validation.cjs} +1 -1
- package/package.json +14 -13
- package/src/{ackn.mjs → ackn.js} +33 -33
- package/src/codecs.js +55 -0
- package/src/{config.mjs → config.js} +88 -93
- package/src/{constants.mjs → constants.js} +29 -29
- package/src/{cookies.mjs → cookies.js} +100 -100
- package/src/{formdata.mjs → formdata.js} +8 -14
- package/src/{index.mjs → index.js} +22 -22
- package/src/{mediatypes.mjs → mediatypes.js} +6 -6
- package/src/{mixin.mjs → mixin.js} +25 -26
- package/src/postflight.js +56 -0
- package/src/{preflight.mjs → preflight.js} +84 -91
- package/src/redirects.js +79 -0
- package/src/retries.js +51 -0
- package/src/transfer.js +92 -0
- package/src/transform.js +109 -0
- package/src/utils.js +187 -0
- package/src/{validation.mjs → validation.js} +33 -33
- package/dist/postflight.js +0 -117
- package/dist/transform.js +0 -79
- package/src/postflight.mjs +0 -136
- package/src/transfer.mjs +0 -121
- package/src/transform.mjs +0 -82
- package/src/utils.mjs +0 -205
- /package/dist/{ackn.js → ackn.cjs} +0 -0
- /package/dist/{constants.js → constants.cjs} +0 -0
- /package/dist/{errors.js → errors.cjs} +0 -0
- /package/dist/{mediatypes.js → mediatypes.cjs} +0 -0
- /package/src/{errors.mjs → errors.js} +0 -0
package/dist/retries.cjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.retries = void 0;
|
|
7
|
+
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
8
|
+
var _nodeStream = require("node:stream");
|
|
9
|
+
var _promises = require("node:timers/promises");
|
|
10
|
+
var _errors = require("./errors.cjs");
|
|
11
|
+
var _index = _interopRequireDefault(require("./index.cjs"));
|
|
12
|
+
var _utils = require("./utils.cjs");
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
const {
|
|
15
|
+
HTTP2_HEADER_RETRY_AFTER,
|
|
16
|
+
HTTP2_METHOD_GET,
|
|
17
|
+
HTTP2_METHOD_HEAD
|
|
18
|
+
} = _nodeHttp.default.constants;
|
|
19
|
+
const retries = (ex, options) => {
|
|
20
|
+
const {
|
|
21
|
+
body,
|
|
22
|
+
maxRetryAfter,
|
|
23
|
+
method,
|
|
24
|
+
retry,
|
|
25
|
+
url
|
|
26
|
+
} = options;
|
|
27
|
+
if (retry?.attempts > 0) {
|
|
28
|
+
if (![HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(method) && (0, _utils.isPipeStream)(body) && !(0, _nodeStream.isReadable)(body)) {
|
|
29
|
+
throw new _errors.RequestError('Request stream already read.', {
|
|
30
|
+
cause: ex
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (retry.errorCodes?.includes(ex.code) || retry.statusCodes?.includes(ex.statusCode)) {
|
|
34
|
+
let {
|
|
35
|
+
interval
|
|
36
|
+
} = retry;
|
|
37
|
+
if (retry.retryAfter && ex.headers?.[HTTP2_HEADER_RETRY_AFTER]) {
|
|
38
|
+
interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
39
|
+
interval = Math.abs(Number(interval) * 1e3 || new Date(interval) - Date.now()) || 0;
|
|
40
|
+
if (interval > maxRetryAfter) {
|
|
41
|
+
throw new _errors.RequestError(`Maximum '${HTTP2_HEADER_RETRY_AFTER}' limit exceeded: ${interval} ms.`, {
|
|
42
|
+
cause: ex
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
|
|
47
|
+
}
|
|
48
|
+
if (interval < 0) {
|
|
49
|
+
interval = 0;
|
|
50
|
+
}
|
|
51
|
+
retry.attempts--;
|
|
52
|
+
retry.interval = interval;
|
|
53
|
+
return (0, _promises.setTimeout)(interval).then(() => (0, _index.default)(url, options));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
exports.retries = retries;
|
|
@@ -7,18 +7,15 @@ exports.transfer = void 0;
|
|
|
7
7
|
var _nodeHttp = _interopRequireDefault(require("node:http"));
|
|
8
8
|
var _nodeHttp2 = _interopRequireDefault(require("node:http2"));
|
|
9
9
|
var _nodeHttps = _interopRequireDefault(require("node:https"));
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
var
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var _transform = require("./transform");
|
|
16
|
-
var _utils = require("./utils");
|
|
10
|
+
var _ackn = require("./ackn.cjs");
|
|
11
|
+
var _errors = require("./errors.cjs");
|
|
12
|
+
var _postflight = require("./postflight.cjs");
|
|
13
|
+
var _preflight = require("./preflight.cjs");
|
|
14
|
+
var _retries = require("./retries.cjs");
|
|
15
|
+
var _transform = require("./transform.cjs");
|
|
16
|
+
var _utils = require("./utils.cjs");
|
|
17
17
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
|
-
const {
|
|
19
|
-
HTTP2_HEADER_RETRY_AFTER
|
|
20
|
-
} = _nodeHttp2.default.constants;
|
|
21
|
-
const transfer = async (options, overact) => {
|
|
18
|
+
const transfer = async options => {
|
|
22
19
|
const {
|
|
23
20
|
digest,
|
|
24
21
|
redirected,
|
|
@@ -56,7 +53,7 @@ const transfer = async (options, overact) => {
|
|
|
56
53
|
} = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
|
|
57
54
|
req = request(url, options);
|
|
58
55
|
}
|
|
59
|
-
(0, _utils.
|
|
56
|
+
(0, _utils.snoop)(client, req, options);
|
|
60
57
|
req.once('aborted', reject);
|
|
61
58
|
req.once('error', reject);
|
|
62
59
|
req.once('frameError', reject);
|
|
@@ -65,7 +62,7 @@ const transfer = async (options, overact) => {
|
|
|
65
62
|
reject,
|
|
66
63
|
resolve
|
|
67
64
|
}));
|
|
68
|
-
(0, _utils.dispatch)(
|
|
65
|
+
(0, _utils.dispatch)(req, options);
|
|
69
66
|
});
|
|
70
67
|
try {
|
|
71
68
|
const res = await promise;
|
|
@@ -74,33 +71,9 @@ const transfer = async (options, overact) => {
|
|
|
74
71
|
}
|
|
75
72
|
return res;
|
|
76
73
|
} catch (ex) {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
} = options;
|
|
81
|
-
if (retry?.attempts > 0) {
|
|
82
|
-
if (retry.errorCodes?.includes(ex.code) || retry.statusCodes?.includes(ex.statusCode)) {
|
|
83
|
-
let {
|
|
84
|
-
interval
|
|
85
|
-
} = retry;
|
|
86
|
-
if (retry.retryAfter && ex.headers?.[HTTP2_HEADER_RETRY_AFTER]) {
|
|
87
|
-
interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
88
|
-
interval = Number(interval) * 1e3 || new Date(interval) - Date.now();
|
|
89
|
-
if (interval > maxRetryAfter) {
|
|
90
|
-
throw (0, _utils.maxRetryAfterError)(interval, {
|
|
91
|
-
cause: ex
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
|
|
96
|
-
}
|
|
97
|
-
if (interval < 0) {
|
|
98
|
-
interval = 0;
|
|
99
|
-
}
|
|
100
|
-
retry.attempts--;
|
|
101
|
-
retry.interval = interval;
|
|
102
|
-
return (0, _promises.setTimeout)(interval).then(() => overact(url, options));
|
|
103
|
-
}
|
|
74
|
+
const willRetry = (0, _retries.retries)(ex, options);
|
|
75
|
+
if (willRetry) {
|
|
76
|
+
return willRetry;
|
|
104
77
|
}
|
|
105
78
|
if (digest && !redirected && ex.body) {
|
|
106
79
|
ex.body = await ex.body();
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transform = void 0;
|
|
7
|
+
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
8
|
+
var _nodeStream = require("node:stream");
|
|
9
|
+
var _consumers = require("node:stream/consumers");
|
|
10
|
+
var _nodeUtil = require("node:util");
|
|
11
|
+
var _codecs = require("./codecs.cjs");
|
|
12
|
+
var _formdata = require("./formdata.cjs");
|
|
13
|
+
var _mediatypes = require("./mediatypes.cjs");
|
|
14
|
+
var _utils = require("./utils.cjs");
|
|
15
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
+
const {
|
|
17
|
+
HTTP2_HEADER_CONTENT_ENCODING,
|
|
18
|
+
HTTP2_HEADER_CONTENT_LENGTH,
|
|
19
|
+
HTTP2_HEADER_CONTENT_TYPE
|
|
20
|
+
} = _nodeHttp.default.constants;
|
|
21
|
+
const transform = async options => {
|
|
22
|
+
let {
|
|
23
|
+
body,
|
|
24
|
+
headers
|
|
25
|
+
} = options;
|
|
26
|
+
if (!body) {
|
|
27
|
+
return options;
|
|
28
|
+
}
|
|
29
|
+
if (!Buffer.isBuffer(body)) {
|
|
30
|
+
switch (true) {
|
|
31
|
+
case (0, _utils.isFileLike)(body):
|
|
32
|
+
{
|
|
33
|
+
headers = {
|
|
34
|
+
[HTTP2_HEADER_CONTENT_LENGTH]: body.size,
|
|
35
|
+
[HTTP2_HEADER_CONTENT_TYPE]: body.type || _mediatypes.APPLICATION_OCTET_STREAM
|
|
36
|
+
};
|
|
37
|
+
body = body.stream();
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case _formdata.FormData.alike(body):
|
|
41
|
+
{
|
|
42
|
+
body = _formdata.FormData.actuate(body);
|
|
43
|
+
headers = {
|
|
44
|
+
[HTTP2_HEADER_CONTENT_TYPE]: body.contentType
|
|
45
|
+
};
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case _nodeUtil.types.isAnyArrayBuffer(body):
|
|
49
|
+
{
|
|
50
|
+
body = Buffer.from(body);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case _nodeUtil.types.isArrayBufferView(body):
|
|
54
|
+
{
|
|
55
|
+
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
case Object(body) === body && !Reflect.has(body, Symbol.asyncIterator):
|
|
59
|
+
{
|
|
60
|
+
if (body.constructor === URLSearchParams) {
|
|
61
|
+
headers = {
|
|
62
|
+
[HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_FORM_URLENCODED
|
|
63
|
+
};
|
|
64
|
+
body = body.toString();
|
|
65
|
+
} else if (!(!Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
66
|
+
headers = {
|
|
67
|
+
[HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_JSON
|
|
68
|
+
};
|
|
69
|
+
body = JSON.stringify(body);
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const encodings = options.headers[HTTP2_HEADER_CONTENT_ENCODING];
|
|
78
|
+
if (Object(body) === body && (Reflect.has(body, Symbol.asyncIterator) || !Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
79
|
+
body = (0, _nodeStream.isReadable)(body) ? (0, _utils.isReadableStream)(body) ? _nodeStream.Readable.fromWeb(body) : body : _nodeStream.Readable.from(body);
|
|
80
|
+
body = encodings ? (0, _codecs.encode)(body, encodings, options) : body;
|
|
81
|
+
} else if (encodings) {
|
|
82
|
+
body = await (0, _consumers.buffer)((0, _codecs.encode)(_nodeStream.Readable.from(body), encodings, options));
|
|
83
|
+
}
|
|
84
|
+
if (options.bufferBody && Object(body) === body) {
|
|
85
|
+
if ((0, _nodeStream.isReadable)(body)) {
|
|
86
|
+
body = await (0, _consumers.buffer)(body);
|
|
87
|
+
} else if (Reflect.has(body, Symbol.asyncIterator)) {
|
|
88
|
+
body = await (0, _consumers.buffer)(body);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
Object.assign(options.headers, {
|
|
92
|
+
...headers,
|
|
93
|
+
...(!body[Symbol.asyncIterator] && {
|
|
94
|
+
[HTTP2_HEADER_CONTENT_LENGTH]: Buffer.byteLength(body)
|
|
95
|
+
}),
|
|
96
|
+
...(options.headers[HTTP2_HEADER_CONTENT_TYPE] && {
|
|
97
|
+
[HTTP2_HEADER_CONTENT_TYPE]: options.headers[HTTP2_HEADER_CONTENT_TYPE]
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
...options,
|
|
102
|
+
body
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
exports.transform = transform;
|
|
@@ -3,22 +3,21 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.stripHeaders = exports.snoop = exports.sameOrigin = exports.normalizeHeaders = exports.normalize = exports.merge = exports.isReadableStream = exports.isPipeStream = exports.isFileLike = exports.dispatch = exports.copyWithMerge = exports.brandCheck = exports.augment = void 0;
|
|
7
7
|
exports.tap = tap;
|
|
8
8
|
exports.unwind = exports.toCamelCase = void 0;
|
|
9
9
|
var _nodeBuffer = require("node:buffer");
|
|
10
10
|
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
11
11
|
var _nodeStream = require("node:stream");
|
|
12
|
-
var
|
|
13
|
-
var
|
|
14
|
-
var _errors = require("./errors");
|
|
12
|
+
var _config = _interopRequireWildcard(require("./config.cjs"));
|
|
13
|
+
var _errors = require("./errors.cjs");
|
|
15
14
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
16
15
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
16
|
const {
|
|
18
|
-
|
|
17
|
+
HTTP2_HEADER_ACCEPT_ENCODING,
|
|
19
18
|
HTTP2_HEADER_STATUS
|
|
20
19
|
} = _nodeHttp.default.constants;
|
|
21
|
-
const
|
|
20
|
+
const augment = (res, headers, options) => {
|
|
22
21
|
const {
|
|
23
22
|
h2
|
|
24
23
|
} = options;
|
|
@@ -45,48 +44,13 @@ const admix = (res, headers, options) => {
|
|
|
45
44
|
value: !!options.redirected
|
|
46
45
|
});
|
|
47
46
|
};
|
|
48
|
-
exports.
|
|
49
|
-
const affix = (client, req, options) => {
|
|
50
|
-
req.once('close', () => client?.close());
|
|
51
|
-
req.once('end', () => client?.close());
|
|
52
|
-
req.once('timeout', () => req.destroy(new _errors.TimeoutError(`Timed out after ${options.timeout} ms.`)));
|
|
53
|
-
req.once('trailers', trailers => {
|
|
54
|
-
Reflect.defineProperty(req, 'trailers', {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
value: trailers
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
exports.affix = affix;
|
|
47
|
+
exports.augment = augment;
|
|
61
48
|
const brandCheck = (value, ctor) => {
|
|
62
49
|
if (!(value instanceof ctor)) {
|
|
63
|
-
throw new TypeError('Illegal invocation');
|
|
50
|
+
throw new TypeError('Illegal invocation.');
|
|
64
51
|
}
|
|
65
52
|
};
|
|
66
53
|
exports.brandCheck = brandCheck;
|
|
67
|
-
const compress = (readable, encodings = '', {
|
|
68
|
-
compression
|
|
69
|
-
} = {}) => {
|
|
70
|
-
const encoders = [];
|
|
71
|
-
encodings = unwind(encodings);
|
|
72
|
-
for (const encoding of encodings) {
|
|
73
|
-
if (/\bbr\b/i.test(encoding)) {
|
|
74
|
-
encoders.push(_nodeZlib.default.createBrotliCompress(compression?.brotliOptions));
|
|
75
|
-
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
76
|
-
encoders.push(_nodeZlib.default.createDeflate(compression?.zlibOptions));
|
|
77
|
-
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
78
|
-
encoders.push(_nodeZlib.default.createDeflateRaw(compression?.zlibOptions));
|
|
79
|
-
} else if (/\bgzip\b/i.test(encoding)) {
|
|
80
|
-
encoders.push(_nodeZlib.default.createGzip(compression?.zlibOptions));
|
|
81
|
-
} else if (_config.isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
82
|
-
encoders.push(_nodeZlib.default.createZstdCompress(compression?.zstdOptions));
|
|
83
|
-
} else {
|
|
84
|
-
return readable;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return (0, _nodeStream.pipeline)(readable, ...encoders, () => void 0);
|
|
88
|
-
};
|
|
89
|
-
exports.compress = compress;
|
|
90
54
|
const copyWithMerge = (target, ...rest) => {
|
|
91
55
|
target = structuredClone(target);
|
|
92
56
|
if (!rest.length) {
|
|
@@ -95,52 +59,30 @@ const copyWithMerge = (target, ...rest) => {
|
|
|
95
59
|
return merge(target, ...rest);
|
|
96
60
|
};
|
|
97
61
|
exports.copyWithMerge = copyWithMerge;
|
|
98
|
-
const
|
|
99
|
-
decompression
|
|
100
|
-
} = {}) => {
|
|
101
|
-
const decoders = [];
|
|
102
|
-
encodings = unwind(encodings);
|
|
103
|
-
for (const encoding of encodings) {
|
|
104
|
-
if (/\bbr\b/i.test(encoding)) {
|
|
105
|
-
decoders.push(_nodeZlib.default.createBrotliDecompress(decompression?.brotliOptions));
|
|
106
|
-
} else if (/\bdeflate(?!-(?:\w+)?)\b/i.test(encoding)) {
|
|
107
|
-
decoders.push(_nodeZlib.default.createInflate(decompression?.zlibOptions));
|
|
108
|
-
} else if (/\bdeflate-raw\b/i.test(encoding)) {
|
|
109
|
-
decoders.push(_nodeZlib.default.createInflateRaw(decompression?.zlibOptions));
|
|
110
|
-
} else if (/\bgzip\b/i.test(encoding)) {
|
|
111
|
-
decoders.push(_nodeZlib.default.createGunzip(decompression?.zlibOptions));
|
|
112
|
-
} else if (_config.isZstdSupported && /\bzstd\b/i.test(encoding)) {
|
|
113
|
-
decoders.push(_nodeZlib.default.createZstdDecompress(decompression?.zstdOptions));
|
|
114
|
-
} else {
|
|
115
|
-
return readable;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return (0, _nodeStream.pipeline)(readable, ...decoders, () => void 0);
|
|
119
|
-
};
|
|
120
|
-
exports.decompress = decompress;
|
|
121
|
-
const dispatch = ({
|
|
62
|
+
const dispatch = (req, {
|
|
122
63
|
body
|
|
123
|
-
}
|
|
124
|
-
if (
|
|
64
|
+
}) => {
|
|
65
|
+
if ((0, _nodeStream.isReadable)(body)) {
|
|
125
66
|
body.pipe(req);
|
|
126
67
|
} else {
|
|
127
68
|
req.end(body);
|
|
128
69
|
}
|
|
129
70
|
};
|
|
130
71
|
exports.dispatch = dispatch;
|
|
131
|
-
const isFileLike =
|
|
132
|
-
return [_nodeBuffer.Blob
|
|
72
|
+
const isFileLike = value => {
|
|
73
|
+
return [_nodeBuffer.Blob, _nodeBuffer.File].some(it => value instanceof it);
|
|
133
74
|
};
|
|
134
75
|
exports.isFileLike = isFileLike;
|
|
135
|
-
const
|
|
136
|
-
return
|
|
76
|
+
const isPipeStream = value => {
|
|
77
|
+
return value instanceof _nodeStream.Readable;
|
|
78
|
+
};
|
|
79
|
+
exports.isPipeStream = isPipeStream;
|
|
80
|
+
const isReadableStream = value => {
|
|
81
|
+
return value instanceof ReadableStream;
|
|
137
82
|
};
|
|
138
83
|
exports.isReadableStream = isReadableStream;
|
|
139
|
-
const maxRetryAfter = exports.maxRetryAfter = Symbol('maxRetryAfter');
|
|
140
|
-
const maxRetryAfterError = (interval, options) => new _errors.RequestError(`Maximum '${HTTP2_HEADER_RETRY_AFTER}' limit exceeded: ${interval} ms.`, options);
|
|
141
|
-
exports.maxRetryAfterError = maxRetryAfterError;
|
|
142
84
|
const merge = (target, ...rest) => {
|
|
143
|
-
rest = rest.filter(it => it ===
|
|
85
|
+
rest = rest.filter(it => Object(it) === it);
|
|
144
86
|
for (const source of rest) {
|
|
145
87
|
for (const key of Object.getOwnPropertyNames(source)) {
|
|
146
88
|
const sv = source[key];
|
|
@@ -165,14 +107,49 @@ const normalize = (url, options = {}) => {
|
|
|
165
107
|
if (options.stripTrailingSlash) {
|
|
166
108
|
url = `${url}`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
|
|
167
109
|
}
|
|
168
|
-
url = new URL(url, options.baseURL);
|
|
169
110
|
return Object.assign(options, {
|
|
170
|
-
|
|
111
|
+
headers: normalizeHeaders(options.headers),
|
|
112
|
+
method: options.method.toUpperCase(),
|
|
113
|
+
url: new URL(url, options.baseURL)
|
|
171
114
|
});
|
|
172
115
|
};
|
|
173
116
|
exports.normalize = normalize;
|
|
117
|
+
const normalizeHeaders = headers => {
|
|
118
|
+
const collector = {};
|
|
119
|
+
for (const [key, value] of Object.entries(headers ?? {})) {
|
|
120
|
+
const name = key.toLowerCase();
|
|
121
|
+
collector[key] = value;
|
|
122
|
+
if (key === HTTP2_HEADER_ACCEPT_ENCODING && !_config.isZstdSupported) {
|
|
123
|
+
const stripped = value.replace(/\s?zstd,?/gi, '').trim();
|
|
124
|
+
if (stripped) {
|
|
125
|
+
collector[key] = stripped;
|
|
126
|
+
} else {
|
|
127
|
+
Reflect.deleteProperty(collector, name);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return collector;
|
|
132
|
+
};
|
|
133
|
+
exports.normalizeHeaders = normalizeHeaders;
|
|
174
134
|
const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
|
|
175
135
|
exports.sameOrigin = sameOrigin;
|
|
136
|
+
const snoop = (client, req, options) => {
|
|
137
|
+
req.once('close', () => client?.close());
|
|
138
|
+
req.once('end', () => client?.close());
|
|
139
|
+
req.once('timeout', () => req.destroy(new _errors.TimeoutError(`Timed out after ${options.timeout} ms.`)));
|
|
140
|
+
req.once('trailers', trailers => {
|
|
141
|
+
Reflect.defineProperty(req, 'trailers', {
|
|
142
|
+
enumerable: true,
|
|
143
|
+
value: trailers
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
exports.snoop = snoop;
|
|
148
|
+
const stripHeaders = (headers = {}, names = []) => {
|
|
149
|
+
names = new Set(names);
|
|
150
|
+
return Object.fromEntries(Object.entries(headers).filter(([key]) => !names.has(key.toLowerCase())));
|
|
151
|
+
};
|
|
152
|
+
exports.stripHeaders = stripHeaders;
|
|
176
153
|
async function* tap(value) {
|
|
177
154
|
if (Reflect.has(value, Symbol.asyncIterator)) {
|
|
178
155
|
yield* value;
|
|
@@ -184,5 +161,5 @@ async function* tap(value) {
|
|
|
184
161
|
}
|
|
185
162
|
const toCamelCase = str => str?.toLowerCase().replace(/\p{Punctuation}.|\p{White_Space}./gu, val => val.replace(/\p{Punctuation}+|\p{White_Space}+/gu, '').toUpperCase());
|
|
186
163
|
exports.toCamelCase = toCamelCase;
|
|
187
|
-
const unwind = encodings => encodings.split(',').map(it => it.trim());
|
|
164
|
+
const unwind = encodings => encodings.split(',').map(it => it.toLowerCase().trim());
|
|
188
165
|
exports.unwind = unwind;
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.validation = void 0;
|
|
7
7
|
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
8
|
-
var _constants = require("./constants");
|
|
8
|
+
var _constants = require("./constants.cjs");
|
|
9
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
const {
|
|
11
11
|
HTTP2_METHOD_GET,
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": {
|
|
3
|
-
"name": "Yehor Sergeenko",
|
|
4
3
|
"email": "yehor.sergeenko@gmail.com",
|
|
4
|
+
"name": "Yehor Sergeenko",
|
|
5
5
|
"url": "https://github.com/bricss"
|
|
6
6
|
},
|
|
7
7
|
"bugs": {
|
|
8
8
|
"url": "https://github.com/bricss/rekwest/issues"
|
|
9
9
|
},
|
|
10
|
+
"description": "The robust request library that humanity deserves 🌐",
|
|
10
11
|
"devDependencies": {
|
|
11
|
-
"@babel/cli": "^7.28.
|
|
12
|
-
"@babel/core": "^7.
|
|
13
|
-
"@babel/eslint-parser": "^7.28.
|
|
14
|
-
"@babel/preset-env": "^7.
|
|
12
|
+
"@babel/cli": "^7.28.6",
|
|
13
|
+
"@babel/core": "^7.29.0",
|
|
14
|
+
"@babel/eslint-parser": "^7.28.6",
|
|
15
|
+
"@babel/preset-env": "^7.29.0",
|
|
15
16
|
"c8": "^10.1.3",
|
|
16
17
|
"eslint": "^9.39.2",
|
|
17
|
-
"eslint-config-ultra-refined": "^3.8.
|
|
18
|
+
"eslint-config-ultra-refined": "^3.8.5",
|
|
18
19
|
"mocha": "^11.7.5"
|
|
19
20
|
},
|
|
20
|
-
"description": "The robust request library that humanity deserves 🌐",
|
|
21
21
|
"engines": {
|
|
22
22
|
"node": ">=20.0.0"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
|
-
"import": "./src/index.
|
|
26
|
-
"require": "./dist/index.
|
|
25
|
+
"import": "./src/index.js",
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"dist",
|
|
@@ -61,15 +61,16 @@
|
|
|
61
61
|
"url": "git+https://github.com/bricss/rekwest.git"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
|
-
"build": "rm -rf dist && npx babel src -
|
|
64
|
+
"build": "rm -rf dist && npx babel src --out-dir dist --out-file-extension .cjs",
|
|
65
65
|
"cert:gen": "openssl req -days 365 -keyout localhost.key -newkey ec -nodes -pkeyopt ec_paramgen_curve:prime256v1 -subj //SKIP=1/CN=localhost -out localhost.cert -x509",
|
|
66
66
|
"cert:ken": "openssl x509 -in localhost.cert -noout -text",
|
|
67
|
-
"lint": "eslint",
|
|
68
|
-
"prepack": "npm run build && sh
|
|
67
|
+
"lint": "eslint --concurrency=auto",
|
|
68
|
+
"prepack": "npm run build && sh misc.sh && npm run lint",
|
|
69
69
|
"pretest": "rm -rf coverage && npm run cert:gen",
|
|
70
70
|
"test": "mocha",
|
|
71
71
|
"test:bail": "mocha --bail",
|
|
72
72
|
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
|
|
73
73
|
},
|
|
74
|
-
"
|
|
74
|
+
"type": "module",
|
|
75
|
+
"version": "7.0.1"
|
|
75
76
|
}
|
package/src/{ackn.mjs → ackn.js}
RENAMED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { connect } from 'node:tls';
|
|
2
|
-
|
|
3
|
-
export const ackn = (options) => new Promise((resolve, reject) => {
|
|
4
|
-
const { url } = options;
|
|
5
|
-
const socket = connect({
|
|
6
|
-
...options,
|
|
7
|
-
ALPNProtocols: [
|
|
8
|
-
'h2',
|
|
9
|
-
'http/1.1',
|
|
10
|
-
],
|
|
11
|
-
host: url.hostname,
|
|
12
|
-
port: parseInt(url.port) || 443,
|
|
13
|
-
servername: url.hostname,
|
|
14
|
-
}, () => {
|
|
15
|
-
socket.off('error', reject);
|
|
16
|
-
socket.off('timeout', reject);
|
|
17
|
-
|
|
18
|
-
const { alpnProtocol } = socket;
|
|
19
|
-
|
|
20
|
-
resolve({
|
|
21
|
-
...options,
|
|
22
|
-
alpnProtocol,
|
|
23
|
-
createConnection() {
|
|
24
|
-
return socket;
|
|
25
|
-
},
|
|
26
|
-
h2: /h2c?/i.test(alpnProtocol),
|
|
27
|
-
protocol: url.protocol,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
socket.on('error', reject);
|
|
32
|
-
socket.on('timeout', reject);
|
|
33
|
-
});
|
|
1
|
+
import { connect } from 'node:tls';
|
|
2
|
+
|
|
3
|
+
export const ackn = (options) => new Promise((resolve, reject) => {
|
|
4
|
+
const { url } = options;
|
|
5
|
+
const socket = connect({
|
|
6
|
+
...options,
|
|
7
|
+
ALPNProtocols: [
|
|
8
|
+
'h2',
|
|
9
|
+
'http/1.1',
|
|
10
|
+
],
|
|
11
|
+
host: url.hostname,
|
|
12
|
+
port: parseInt(url.port) || 443,
|
|
13
|
+
servername: url.hostname,
|
|
14
|
+
}, () => {
|
|
15
|
+
socket.off('error', reject);
|
|
16
|
+
socket.off('timeout', reject);
|
|
17
|
+
|
|
18
|
+
const { alpnProtocol } = socket;
|
|
19
|
+
|
|
20
|
+
resolve({
|
|
21
|
+
...options,
|
|
22
|
+
alpnProtocol,
|
|
23
|
+
createConnection() {
|
|
24
|
+
return socket;
|
|
25
|
+
},
|
|
26
|
+
h2: /h2c?/i.test(alpnProtocol),
|
|
27
|
+
protocol: url.protocol,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
socket.on('error', reject);
|
|
32
|
+
socket.on('timeout', reject);
|
|
33
|
+
});
|
package/src/codecs.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { pipeline } from 'node:stream';
|
|
2
|
+
import zlib from 'node:zlib';
|
|
3
|
+
import { isZstdSupported } from './config.js';
|
|
4
|
+
import { unwind } from './utils.js';
|
|
5
|
+
|
|
6
|
+
export const decodeCodecs = {
|
|
7
|
+
br: (opts) => zlib.createBrotliDecompress(opts?.brotli),
|
|
8
|
+
deflate: (opts) => zlib.createInflate(opts?.zlib),
|
|
9
|
+
'deflate-raw': (opts) => zlib.createInflateRaw(opts?.zlib),
|
|
10
|
+
gzip: (opts) => zlib.createGunzip(opts?.zlib),
|
|
11
|
+
zstd: (opts) => isZstdSupported && zlib.createZstdDecompress(opts?.zstd),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const decode = (readable, encodings = '', { decodersOptions } = {}) => {
|
|
15
|
+
const decoders = [];
|
|
16
|
+
|
|
17
|
+
encodings = unwind(encodings).reverse();
|
|
18
|
+
|
|
19
|
+
for (const encoding of encodings) {
|
|
20
|
+
const decoder = decodeCodecs[encoding]?.(decodersOptions);
|
|
21
|
+
|
|
22
|
+
if (!decoder) {
|
|
23
|
+
return readable;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
decoders.push(decoder);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return pipeline(readable, ...decoders, () => void 0);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const encodeCodecs = {
|
|
33
|
+
br: (opts) => zlib.createBrotliCompress(opts?.brotli),
|
|
34
|
+
deflate: (opts) => zlib.createDeflate(opts?.zlib),
|
|
35
|
+
'deflate-raw': (opts) => zlib.createDeflateRaw(opts?.zlib),
|
|
36
|
+
gzip: (opts) => zlib.createGzip(opts?.zlib),
|
|
37
|
+
zstd: (opts) => isZstdSupported && zlib.createZstdCompress(opts?.zstd),
|
|
38
|
+
};
|
|
39
|
+
export const encode = (readable, encodings = '', { encodersOptions } = {}) => {
|
|
40
|
+
const encoders = [];
|
|
41
|
+
|
|
42
|
+
encodings = unwind(encodings);
|
|
43
|
+
|
|
44
|
+
for (const encoding of encodings) {
|
|
45
|
+
const encoder = encodeCodecs[encoding]?.(encodersOptions);
|
|
46
|
+
|
|
47
|
+
if (!encoder) {
|
|
48
|
+
return readable;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
encoders.push(encoder);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return pipeline(readable, ...encoders, () => void 0);
|
|
55
|
+
};
|