rekwest 6.2.1 → 7.0.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/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} +11 -9
- 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.cjs +140 -0
- package/dist/{validation.js → validation.cjs} +1 -1
- package/package.json +13 -12
- 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} +100 -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 +152 -0
- package/src/{validation.mjs → validation.js} +33 -33
- package/dist/postflight.js +0 -117
- package/dist/transform.js +0 -79
- package/dist/utils.js +0 -188
- 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/src/utils.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Blob,
|
|
3
|
+
File,
|
|
4
|
+
} from 'node:buffer';
|
|
5
|
+
import http2 from 'node:http2';
|
|
6
|
+
import {
|
|
7
|
+
isReadable,
|
|
8
|
+
Readable,
|
|
9
|
+
} from 'node:stream';
|
|
10
|
+
import config from './config.js';
|
|
11
|
+
import { TimeoutError } from './errors.js';
|
|
12
|
+
|
|
13
|
+
const {
|
|
14
|
+
HTTP2_HEADER_STATUS,
|
|
15
|
+
} = http2.constants;
|
|
16
|
+
|
|
17
|
+
export const augment = (res, headers, options) => {
|
|
18
|
+
const { h2 } = options;
|
|
19
|
+
|
|
20
|
+
if (h2) {
|
|
21
|
+
Reflect.defineProperty(res, 'headers', {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
value: headers,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
Reflect.defineProperty(res, 'httpVersion', {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
value: `${ h2 + 1 }.0`,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
Reflect.defineProperty(res, 'statusCode', {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
value: headers[HTTP2_HEADER_STATUS],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Reflect.defineProperty(res, 'ok', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
value: /^2\d{2}$/.test(res.statusCode),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
Reflect.defineProperty(res, 'redirected', {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
value: !!options.redirected,
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const brandCheck = (value, ctor) => {
|
|
49
|
+
if (!(value instanceof ctor)) {
|
|
50
|
+
throw new TypeError('Illegal invocation.');
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const copyWithMerge = (target, ...rest) => {
|
|
55
|
+
target = structuredClone(target);
|
|
56
|
+
if (!rest.length) {
|
|
57
|
+
return target;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return merge(target, ...rest);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const dispatch = (req, { body }) => {
|
|
64
|
+
if (isReadable(body)) {
|
|
65
|
+
body.pipe(req);
|
|
66
|
+
} else {
|
|
67
|
+
req.end(body);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const isFileLike = (value) => {
|
|
72
|
+
return [
|
|
73
|
+
Blob,
|
|
74
|
+
File,
|
|
75
|
+
].some((it) => value instanceof it);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const isPipeStream = (value) => {
|
|
79
|
+
return value instanceof Readable;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const isReadableStream = (value) => {
|
|
83
|
+
return value instanceof ReadableStream;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const merge = (target, ...rest) => {
|
|
87
|
+
rest = rest.filter((it) => Object(it) === it);
|
|
88
|
+
for (const source of rest) {
|
|
89
|
+
for (const key of Object.getOwnPropertyNames(source)) {
|
|
90
|
+
const sv = source[key];
|
|
91
|
+
const tv = target[key];
|
|
92
|
+
|
|
93
|
+
if (Object(sv) === sv && Object(tv) === tv) {
|
|
94
|
+
target[key] = merge(tv, sv);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
target[key] = source[key];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return target;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const normalize = (url, options = {}) => {
|
|
106
|
+
if (!options.redirected) {
|
|
107
|
+
options = copyWithMerge(config.defaults, options);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (options.trimTrailingSlashes) {
|
|
111
|
+
url = `${ url }`.replace(/(?<!:)\/+/g, '/');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (options.stripTrailingSlash) {
|
|
115
|
+
url = `${ url }`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
url = new URL(url, options.baseURL);
|
|
119
|
+
|
|
120
|
+
return Object.assign(options, { url });
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
|
|
124
|
+
|
|
125
|
+
export const snoop = (client, req, options) => {
|
|
126
|
+
req.once('close', () => client?.close());
|
|
127
|
+
req.once('end', () => client?.close());
|
|
128
|
+
req.once('timeout', () => req.destroy(new TimeoutError(`Timed out after ${ options.timeout } ms.`)));
|
|
129
|
+
req.once('trailers', (trailers) => {
|
|
130
|
+
Reflect.defineProperty(req, 'trailers', {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
value: trailers,
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export async function* tap(value) {
|
|
138
|
+
if (Reflect.has(value, Symbol.asyncIterator)) {
|
|
139
|
+
yield* value;
|
|
140
|
+
} else if (value.stream) {
|
|
141
|
+
yield* value.stream();
|
|
142
|
+
} else {
|
|
143
|
+
yield await value.arrayBuffer();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const toCamelCase = (str) => str?.toLowerCase().replace(
|
|
148
|
+
/\p{Punctuation}.|\p{White_Space}./gu,
|
|
149
|
+
(val) => val.replace(/\p{Punctuation}+|\p{White_Space}+/gu, '').toUpperCase(),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
export const unwind = (encodings) => encodings.split(',').map((it) => it.toLowerCase().trim());
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import http2 from 'node:http2';
|
|
2
|
-
import {
|
|
3
|
-
requestCredentials,
|
|
4
|
-
requestRedirect,
|
|
5
|
-
} from './constants.
|
|
6
|
-
|
|
7
|
-
const {
|
|
8
|
-
HTTP2_METHOD_GET,
|
|
9
|
-
HTTP2_METHOD_HEAD,
|
|
10
|
-
} = http2.constants;
|
|
11
|
-
|
|
12
|
-
export const validation = (options = {}) => {
|
|
13
|
-
if (options.body && [
|
|
14
|
-
HTTP2_METHOD_GET,
|
|
15
|
-
HTTP2_METHOD_HEAD,
|
|
16
|
-
].includes(options.method)) {
|
|
17
|
-
throw new TypeError(`Request with ${ HTTP2_METHOD_GET }/${ HTTP2_METHOD_HEAD } method cannot have body.`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (!Object.values(requestCredentials).includes(options.credentials)) {
|
|
21
|
-
throw new TypeError(`Failed to read the 'credentials' property from 'options': The provided value '${
|
|
22
|
-
options.credentials
|
|
23
|
-
}' is not a valid enum value.`);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (!Reflect.has(requestRedirect, options.redirect)) {
|
|
27
|
-
throw new TypeError(`Failed to read the 'redirect' property from 'options': The provided value '${
|
|
28
|
-
options.redirect
|
|
29
|
-
}' is not a valid enum value.`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return options;
|
|
33
|
-
};
|
|
1
|
+
import http2 from 'node:http2';
|
|
2
|
+
import {
|
|
3
|
+
requestCredentials,
|
|
4
|
+
requestRedirect,
|
|
5
|
+
} from './constants.js';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
HTTP2_METHOD_GET,
|
|
9
|
+
HTTP2_METHOD_HEAD,
|
|
10
|
+
} = http2.constants;
|
|
11
|
+
|
|
12
|
+
export const validation = (options = {}) => {
|
|
13
|
+
if (options.body && [
|
|
14
|
+
HTTP2_METHOD_GET,
|
|
15
|
+
HTTP2_METHOD_HEAD,
|
|
16
|
+
].includes(options.method)) {
|
|
17
|
+
throw new TypeError(`Request with ${ HTTP2_METHOD_GET }/${ HTTP2_METHOD_HEAD } method cannot have body.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!Object.values(requestCredentials).includes(options.credentials)) {
|
|
21
|
+
throw new TypeError(`Failed to read the 'credentials' property from 'options': The provided value '${
|
|
22
|
+
options.credentials
|
|
23
|
+
}' is not a valid enum value.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!Reflect.has(requestRedirect, options.redirect)) {
|
|
27
|
+
throw new TypeError(`Failed to read the 'redirect' property from 'options': The provided value '${
|
|
28
|
+
options.redirect
|
|
29
|
+
}' is not a valid enum value.`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return options;
|
|
33
|
+
};
|
package/dist/postflight.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.postflight = void 0;
|
|
7
|
-
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
8
|
-
var _promises = require("node:timers/promises");
|
|
9
|
-
var _constants = require("./constants");
|
|
10
|
-
var _cookies = require("./cookies");
|
|
11
|
-
var _errors = require("./errors");
|
|
12
|
-
var _index = _interopRequireDefault(require("./index"));
|
|
13
|
-
var _mixin = require("./mixin");
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
|
-
const {
|
|
17
|
-
HTTP2_HEADER_LOCATION,
|
|
18
|
-
HTTP2_HEADER_RETRY_AFTER,
|
|
19
|
-
HTTP2_HEADER_SET_COOKIE,
|
|
20
|
-
HTTP2_METHOD_GET,
|
|
21
|
-
HTTP2_METHOD_HEAD,
|
|
22
|
-
HTTP2_METHOD_POST,
|
|
23
|
-
HTTP_STATUS_BAD_REQUEST,
|
|
24
|
-
HTTP_STATUS_FOUND,
|
|
25
|
-
HTTP_STATUS_MOVED_PERMANENTLY,
|
|
26
|
-
HTTP_STATUS_SEE_OTHER
|
|
27
|
-
} = _nodeHttp.default.constants;
|
|
28
|
-
const postflight = (req, res, options, {
|
|
29
|
-
reject,
|
|
30
|
-
resolve
|
|
31
|
-
}) => {
|
|
32
|
-
const {
|
|
33
|
-
cookies,
|
|
34
|
-
credentials,
|
|
35
|
-
follow,
|
|
36
|
-
h2,
|
|
37
|
-
redirect,
|
|
38
|
-
url
|
|
39
|
-
} = options;
|
|
40
|
-
let headers;
|
|
41
|
-
if (h2) {
|
|
42
|
-
headers = res;
|
|
43
|
-
res = req;
|
|
44
|
-
} else {
|
|
45
|
-
res.once('error', reject);
|
|
46
|
-
}
|
|
47
|
-
(0, _utils.admix)(res, headers, options);
|
|
48
|
-
if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
|
|
49
|
-
if (_cookies.Cookies.jar.has(url.origin)) {
|
|
50
|
-
const cookie = new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options);
|
|
51
|
-
_cookies.Cookies.jar.get(url.origin).forEach((val, key) => {
|
|
52
|
-
if (!cookie.has(key)) {
|
|
53
|
-
cookie.set(key, val);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
_cookies.Cookies.jar.set(url.origin, cookie);
|
|
57
|
-
} else {
|
|
58
|
-
_cookies.Cookies.jar.set(url.origin, new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
Reflect.defineProperty(res, 'cookies', {
|
|
62
|
-
enumerable: true,
|
|
63
|
-
value: cookies !== false && _cookies.Cookies.jar.has(url.origin) ? _cookies.Cookies.jar.get(url.origin) : void 0
|
|
64
|
-
});
|
|
65
|
-
const {
|
|
66
|
-
statusCode
|
|
67
|
-
} = res;
|
|
68
|
-
if (follow && /3\d{2}/.test(statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
|
|
69
|
-
if (!_constants.requestRedirectCodes.includes(statusCode)) {
|
|
70
|
-
return res.emit('error', new RangeError(`Invalid status code: ${statusCode}`));
|
|
71
|
-
}
|
|
72
|
-
if (redirect === _constants.requestRedirect.error) {
|
|
73
|
-
return res.emit('error', new _errors.RequestError(`Unexpected redirect, redirect mode is set to '${redirect}'.`));
|
|
74
|
-
}
|
|
75
|
-
if (redirect === _constants.requestRedirect.follow) {
|
|
76
|
-
const location = new URL(res.headers[HTTP2_HEADER_LOCATION], url);
|
|
77
|
-
if (!/^https?:/i.test(location.protocol)) {
|
|
78
|
-
return res.emit('error', new _errors.RequestError('URL scheme must be "http" or "https".'));
|
|
79
|
-
}
|
|
80
|
-
if (!(0, _utils.sameOrigin)(location, url)) {
|
|
81
|
-
if (credentials !== _constants.requestCredentials.include) {
|
|
82
|
-
options.credentials = _constants.requestCredentials.omit;
|
|
83
|
-
}
|
|
84
|
-
options.h2 = false;
|
|
85
|
-
}
|
|
86
|
-
if (statusCode !== HTTP_STATUS_SEE_OTHER && options.body?.pipe?.constructor === Function) {
|
|
87
|
-
return res.emit('error', new _errors.RequestError(`Unable to ${redirect} redirect with streamable body.`));
|
|
88
|
-
}
|
|
89
|
-
if ([HTTP_STATUS_MOVED_PERMANENTLY, HTTP_STATUS_FOUND].includes(statusCode) && options.method === HTTP2_METHOD_POST || statusCode === HTTP_STATUS_SEE_OTHER && ![HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
|
|
90
|
-
for (const it of Object.keys(options.headers).filter(val => /^content-/i.test(val))) {
|
|
91
|
-
Reflect.deleteProperty(options.headers, it);
|
|
92
|
-
}
|
|
93
|
-
options.body = null;
|
|
94
|
-
options.method = HTTP2_METHOD_GET;
|
|
95
|
-
}
|
|
96
|
-
options.follow--;
|
|
97
|
-
options.redirected = true;
|
|
98
|
-
options.url = location;
|
|
99
|
-
if (statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
|
|
100
|
-
let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
|
|
101
|
-
interval = Number(interval) * 1e3 || new Date(interval) - Date.now();
|
|
102
|
-
if (interval > options.maxRetryAfter) {
|
|
103
|
-
return res.emit('error', (0, _utils.maxRetryAfterError)(interval, {
|
|
104
|
-
cause: (0, _mixin.mixin)(res, options)
|
|
105
|
-
}));
|
|
106
|
-
}
|
|
107
|
-
return (0, _promises.setTimeout)(interval).then(() => (0, _index.default)(options.url, options).then(resolve, reject));
|
|
108
|
-
}
|
|
109
|
-
return (0, _index.default)(options.url, options).then(resolve, reject);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (statusCode >= HTTP_STATUS_BAD_REQUEST) {
|
|
113
|
-
return reject((0, _mixin.mixin)(res, options));
|
|
114
|
-
}
|
|
115
|
-
resolve((0, _mixin.mixin)(res, options));
|
|
116
|
-
};
|
|
117
|
-
exports.postflight = postflight;
|
package/dist/transform.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
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 _formdata = require("./formdata");
|
|
12
|
-
var _mediatypes = require("./mediatypes");
|
|
13
|
-
var _utils = require("./utils");
|
|
14
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
-
const {
|
|
16
|
-
HTTP2_HEADER_CONTENT_ENCODING,
|
|
17
|
-
HTTP2_HEADER_CONTENT_LENGTH,
|
|
18
|
-
HTTP2_HEADER_CONTENT_TYPE
|
|
19
|
-
} = _nodeHttp.default.constants;
|
|
20
|
-
const transform = async options => {
|
|
21
|
-
let {
|
|
22
|
-
body,
|
|
23
|
-
headers
|
|
24
|
-
} = options;
|
|
25
|
-
if (!body) {
|
|
26
|
-
return options;
|
|
27
|
-
}
|
|
28
|
-
if ((0, _utils.isFileLike)(body)) {
|
|
29
|
-
headers = {
|
|
30
|
-
[HTTP2_HEADER_CONTENT_LENGTH]: body.size,
|
|
31
|
-
[HTTP2_HEADER_CONTENT_TYPE]: body.type || _mediatypes.APPLICATION_OCTET_STREAM
|
|
32
|
-
};
|
|
33
|
-
body = body.stream();
|
|
34
|
-
} else if (_formdata.FormData.alike(body)) {
|
|
35
|
-
body = _formdata.FormData.actuate(body);
|
|
36
|
-
headers = {
|
|
37
|
-
[HTTP2_HEADER_CONTENT_TYPE]: body.contentType
|
|
38
|
-
};
|
|
39
|
-
} else if (!Buffer.isBuffer(body)) {
|
|
40
|
-
if (_nodeUtil.types.isAnyArrayBuffer(body)) {
|
|
41
|
-
body = Buffer.from(body);
|
|
42
|
-
} else if (_nodeUtil.types.isArrayBufferView(body)) {
|
|
43
|
-
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
44
|
-
} else if (body === Object(body) && !Reflect.has(body, Symbol.asyncIterator)) {
|
|
45
|
-
if (body.constructor === URLSearchParams) {
|
|
46
|
-
headers = {
|
|
47
|
-
[HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_FORM_URLENCODED
|
|
48
|
-
};
|
|
49
|
-
body = body.toString();
|
|
50
|
-
} else if (!(!Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
51
|
-
headers = {
|
|
52
|
-
[HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_JSON
|
|
53
|
-
};
|
|
54
|
-
body = JSON.stringify(body);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const encodings = options.headers[HTTP2_HEADER_CONTENT_ENCODING];
|
|
59
|
-
if (body === Object(body) && (Reflect.has(body, Symbol.asyncIterator) || !Array.isArray(body) && Reflect.has(body, Symbol.iterator))) {
|
|
60
|
-
body = (0, _nodeStream.isReadable)(body) ? (0, _utils.isReadableStream)(body) ? _nodeStream.Readable.fromWeb(body) : body : _nodeStream.Readable.from(body);
|
|
61
|
-
body = encodings ? (0, _utils.compress)(body, encodings, options) : body;
|
|
62
|
-
} else if (encodings) {
|
|
63
|
-
body = await (0, _consumers.buffer)((0, _utils.compress)(_nodeStream.Readable.from(body), encodings, options));
|
|
64
|
-
}
|
|
65
|
-
Object.assign(options.headers, {
|
|
66
|
-
...headers,
|
|
67
|
-
...(!body[Symbol.asyncIterator] && {
|
|
68
|
-
[HTTP2_HEADER_CONTENT_LENGTH]: Buffer.byteLength(body)
|
|
69
|
-
}),
|
|
70
|
-
...(options.headers[HTTP2_HEADER_CONTENT_TYPE] && {
|
|
71
|
-
[HTTP2_HEADER_CONTENT_TYPE]: options.headers[HTTP2_HEADER_CONTENT_TYPE]
|
|
72
|
-
})
|
|
73
|
-
});
|
|
74
|
-
return {
|
|
75
|
-
...options,
|
|
76
|
-
body
|
|
77
|
-
};
|
|
78
|
-
};
|
|
79
|
-
exports.transform = transform;
|
package/dist/utils.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.sameOrigin = exports.normalize = exports.merge = exports.maxRetryAfterError = exports.maxRetryAfter = exports.isReadableStream = exports.isFileLike = exports.dispatch = exports.decompress = exports.copyWithMerge = exports.compress = exports.brandCheck = exports.affix = exports.admix = void 0;
|
|
7
|
-
exports.tap = tap;
|
|
8
|
-
exports.unwind = exports.toCamelCase = void 0;
|
|
9
|
-
var _nodeBuffer = require("node:buffer");
|
|
10
|
-
var _nodeHttp = _interopRequireDefault(require("node:http2"));
|
|
11
|
-
var _nodeStream = require("node:stream");
|
|
12
|
-
var _nodeZlib = _interopRequireDefault(require("node:zlib"));
|
|
13
|
-
var _config = _interopRequireWildcard(require("./config"));
|
|
14
|
-
var _errors = require("./errors");
|
|
15
|
-
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
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
|
-
const {
|
|
18
|
-
HTTP2_HEADER_RETRY_AFTER,
|
|
19
|
-
HTTP2_HEADER_STATUS
|
|
20
|
-
} = _nodeHttp.default.constants;
|
|
21
|
-
const admix = (res, headers, options) => {
|
|
22
|
-
const {
|
|
23
|
-
h2
|
|
24
|
-
} = options;
|
|
25
|
-
if (h2) {
|
|
26
|
-
Reflect.defineProperty(res, 'headers', {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
value: headers
|
|
29
|
-
});
|
|
30
|
-
Reflect.defineProperty(res, 'httpVersion', {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
value: `${h2 + 1}.0`
|
|
33
|
-
});
|
|
34
|
-
Reflect.defineProperty(res, 'statusCode', {
|
|
35
|
-
enumerable: true,
|
|
36
|
-
value: headers[HTTP2_HEADER_STATUS]
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
Reflect.defineProperty(res, 'ok', {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
value: /^2\d{2}$/.test(res.statusCode)
|
|
42
|
-
});
|
|
43
|
-
Reflect.defineProperty(res, 'redirected', {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
value: !!options.redirected
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
exports.admix = admix;
|
|
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;
|
|
61
|
-
const brandCheck = (value, ctor) => {
|
|
62
|
-
if (!(value instanceof ctor)) {
|
|
63
|
-
throw new TypeError('Illegal invocation');
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
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
|
-
const copyWithMerge = (target, ...rest) => {
|
|
91
|
-
target = structuredClone(target);
|
|
92
|
-
if (!rest.length) {
|
|
93
|
-
return target;
|
|
94
|
-
}
|
|
95
|
-
return merge(target, ...rest);
|
|
96
|
-
};
|
|
97
|
-
exports.copyWithMerge = copyWithMerge;
|
|
98
|
-
const decompress = (readable, encodings = '', {
|
|
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 = ({
|
|
122
|
-
body
|
|
123
|
-
}, req) => {
|
|
124
|
-
if (body?.pipe?.constructor === Function) {
|
|
125
|
-
body.pipe(req);
|
|
126
|
-
} else {
|
|
127
|
-
req.end(body);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
exports.dispatch = dispatch;
|
|
131
|
-
const isFileLike = instance => {
|
|
132
|
-
return [_nodeBuffer.Blob.name, _nodeBuffer.File.name].includes(instance?.[Symbol.toStringTag]);
|
|
133
|
-
};
|
|
134
|
-
exports.isFileLike = isFileLike;
|
|
135
|
-
const isReadableStream = instance => {
|
|
136
|
-
return ReadableStream.name === instance?.[Symbol.toStringTag];
|
|
137
|
-
};
|
|
138
|
-
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
|
-
const merge = (target, ...rest) => {
|
|
143
|
-
rest = rest.filter(it => it === Object(it));
|
|
144
|
-
for (const source of rest) {
|
|
145
|
-
for (const key of Object.getOwnPropertyNames(source)) {
|
|
146
|
-
const sv = source[key];
|
|
147
|
-
const tv = target[key];
|
|
148
|
-
if (Object(sv) === sv && Object(tv) === tv) {
|
|
149
|
-
target[key] = merge(tv, sv);
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
target[key] = source[key];
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return target;
|
|
156
|
-
};
|
|
157
|
-
exports.merge = merge;
|
|
158
|
-
const normalize = (url, options = {}) => {
|
|
159
|
-
if (!options.redirected) {
|
|
160
|
-
options = copyWithMerge(_config.default.defaults, options);
|
|
161
|
-
}
|
|
162
|
-
if (options.trimTrailingSlashes) {
|
|
163
|
-
url = `${url}`.replace(/(?<!:)\/+/g, '/');
|
|
164
|
-
}
|
|
165
|
-
if (options.stripTrailingSlash) {
|
|
166
|
-
url = `${url}`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
|
|
167
|
-
}
|
|
168
|
-
url = new URL(url, options.baseURL);
|
|
169
|
-
return Object.assign(options, {
|
|
170
|
-
url
|
|
171
|
-
});
|
|
172
|
-
};
|
|
173
|
-
exports.normalize = normalize;
|
|
174
|
-
const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
|
|
175
|
-
exports.sameOrigin = sameOrigin;
|
|
176
|
-
async function* tap(value) {
|
|
177
|
-
if (Reflect.has(value, Symbol.asyncIterator)) {
|
|
178
|
-
yield* value;
|
|
179
|
-
} else if (value.stream) {
|
|
180
|
-
yield* value.stream();
|
|
181
|
-
} else {
|
|
182
|
-
yield await value.arrayBuffer();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const toCamelCase = str => str?.toLowerCase().replace(/\p{Punctuation}.|\p{White_Space}./gu, val => val.replace(/\p{Punctuation}+|\p{White_Space}+/gu, '').toUpperCase());
|
|
186
|
-
exports.toCamelCase = toCamelCase;
|
|
187
|
-
const unwind = encodings => encodings.split(',').map(it => it.trim());
|
|
188
|
-
exports.unwind = unwind;
|