rekwest 7.2.7 → 8.1.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 +264 -260
- package/dist/ackn.cjs +9 -0
- package/dist/config.cjs +2 -1
- package/dist/cookies.cjs +1 -1
- package/dist/transfer.cjs +1 -5
- package/dist/utils.cjs +4 -0
- package/package.json +81 -76
- package/src/ackn.js +46 -33
- package/src/config.js +91 -90
- package/src/constants.js +11 -11
- package/src/cookies.js +121 -121
- package/src/errors.js +18 -18
- package/src/formdata.js +229 -229
- package/src/index.js +83 -83
- package/src/mediatypes.js +6 -6
- package/src/mixin.js +118 -118
- package/src/postflight.js +64 -64
- package/src/preflight.js +84 -84
- package/src/redirects.js +86 -86
- package/src/transfer.js +104 -106
- package/src/transform.js +112 -112
- package/src/utils.js +198 -193
- package/src/validation.js +33 -33
package/src/utils.js
CHANGED
|
@@ -1,193 +1,198 @@
|
|
|
1
|
-
import http2 from 'node:http2';
|
|
2
|
-
import {
|
|
3
|
-
isReadable,
|
|
4
|
-
Readable,
|
|
5
|
-
} from 'node:stream';
|
|
6
|
-
import config, { isZstdSupported } from './config.js';
|
|
7
|
-
import { TimeoutError } from './errors.js';
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
HTTP2_HEADER_ACCEPT_ENCODING,
|
|
11
|
-
HTTP2_HEADER_STATUS,
|
|
12
|
-
} = http2.constants;
|
|
13
|
-
|
|
14
|
-
export const addSearchParams = (url, params = {}) => {
|
|
15
|
-
for (const [key, val] of Object.entries(params)) {
|
|
16
|
-
if (Array.isArray(val)) {
|
|
17
|
-
for (const v of val) {
|
|
18
|
-
url.searchParams.append(key, v);
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
url.searchParams.set(key, val);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return url;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const augment = (res, headers, options) => {
|
|
29
|
-
const h2 = /\bh2c?\b/i.test(res.session?.alpnProtocol);
|
|
30
|
-
|
|
31
|
-
if (h2) {
|
|
32
|
-
Reflect.defineProperty(res, 'headers', {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
value: headers,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
Reflect.defineProperty(res, 'httpVersion', {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
value: `${ h2 + 1 }.0`,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
Reflect.defineProperty(res, 'statusCode', {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
value: headers[HTTP2_HEADER_STATUS],
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
Reflect.defineProperty(res, 'ok', {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
value: /^2\d{2}$/.test(res.statusCode),
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
Reflect.defineProperty(res, 'redirected', {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
value: !!options.redirected,
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const brandCheck = (val, ctor) => {
|
|
60
|
-
if (!(val instanceof ctor)) {
|
|
61
|
-
throw new TypeError('Illegal invocation');
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const cloneWith = (target, ...rest) => {
|
|
66
|
-
target = structuredClone(target);
|
|
67
|
-
if (!rest.length) {
|
|
68
|
-
return target;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return deepMerge(target, ...rest);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export const deepMerge = (target, ...rest) => {
|
|
75
|
-
rest = rest.filter((it) => Object(it) === it);
|
|
76
|
-
for (const source of rest) {
|
|
77
|
-
for (const key of Object.getOwnPropertyNames(source)) {
|
|
78
|
-
const sv = source[key];
|
|
79
|
-
const tv = target[key];
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
target[key] =
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
req.once('
|
|
165
|
-
req.once('
|
|
166
|
-
req.once('
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
export const
|
|
1
|
+
import http2 from 'node:http2';
|
|
2
|
+
import {
|
|
3
|
+
isReadable,
|
|
4
|
+
Readable,
|
|
5
|
+
} from 'node:stream';
|
|
6
|
+
import config, { isZstdSupported } from './config.js';
|
|
7
|
+
import { TimeoutError } from './errors.js';
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
HTTP2_HEADER_ACCEPT_ENCODING,
|
|
11
|
+
HTTP2_HEADER_STATUS,
|
|
12
|
+
} = http2.constants;
|
|
13
|
+
|
|
14
|
+
export const addSearchParams = (url, params = {}) => {
|
|
15
|
+
for (const [key, val] of Object.entries(params)) {
|
|
16
|
+
if (Array.isArray(val)) {
|
|
17
|
+
for (const v of val) {
|
|
18
|
+
url.searchParams.append(key, v);
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
url.searchParams.set(key, val);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return url;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const augment = (res, headers, options) => {
|
|
29
|
+
const h2 = /\bh2c?\b/i.test(res.session?.alpnProtocol);
|
|
30
|
+
|
|
31
|
+
if (h2) {
|
|
32
|
+
Reflect.defineProperty(res, 'headers', {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
value: headers,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
Reflect.defineProperty(res, 'httpVersion', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
value: `${ h2 + 1 }.0`,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
Reflect.defineProperty(res, 'statusCode', {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
value: headers[HTTP2_HEADER_STATUS],
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Reflect.defineProperty(res, 'ok', {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
value: /^2\d{2}$/.test(res.statusCode),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
Reflect.defineProperty(res, 'redirected', {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
value: !!options.redirected,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const brandCheck = (val, ctor) => {
|
|
60
|
+
if (!(val instanceof ctor)) {
|
|
61
|
+
throw new TypeError('Illegal invocation');
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const cloneWith = (target, ...rest) => {
|
|
66
|
+
target = structuredClone(target);
|
|
67
|
+
if (!rest.length) {
|
|
68
|
+
return target;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return deepMerge(target, ...rest);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const deepMerge = (target, ...rest) => {
|
|
75
|
+
rest = rest.filter((it) => Object(it) === it);
|
|
76
|
+
for (const source of rest) {
|
|
77
|
+
for (const key of Object.getOwnPropertyNames(source)) {
|
|
78
|
+
const sv = source[key];
|
|
79
|
+
const tv = target[key];
|
|
80
|
+
|
|
81
|
+
if (sv instanceof Function) {
|
|
82
|
+
target[key] = source[key];
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (Object(sv) === sv && Object(tv) === tv) {
|
|
87
|
+
target[key] = deepMerge(tv, sv);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
target[key] = source[key];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return target;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const dispatch = (req, { body }) => {
|
|
99
|
+
if (isReadable(body)) {
|
|
100
|
+
body.once('error', (err) => (req.session && req.emit('error', err) && req.destroy()) || req.destroy(err));
|
|
101
|
+
body.pipe(req);
|
|
102
|
+
} else {
|
|
103
|
+
req.end(body);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const isBlobLike = (val) => val instanceof Blob;
|
|
108
|
+
|
|
109
|
+
export const isLikelyH2cPrefaceError = (err) => err.code === 'HPE_INVALID_CONSTANT';
|
|
110
|
+
|
|
111
|
+
export const isPipeStream = (val) => val instanceof Readable;
|
|
112
|
+
|
|
113
|
+
export const isReadableStream = (val) => val instanceof ReadableStream;
|
|
114
|
+
|
|
115
|
+
export const normalize = (url, options = {}) => {
|
|
116
|
+
if (!options.redirected) {
|
|
117
|
+
options = cloneWith(config.defaults, options);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return Object.assign(options, {
|
|
121
|
+
headers: normalizeHeaders(options.headers),
|
|
122
|
+
method: options.method?.toUpperCase(),
|
|
123
|
+
url: addSearchParams(normalizeUrl(new URL(url, options.baseURL), options), options.params),
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export const normalizeHeaders = (headers = {}) => {
|
|
128
|
+
const acc = {};
|
|
129
|
+
|
|
130
|
+
for (let [key, val] of Object.entries(headers)) {
|
|
131
|
+
key = key.toLowerCase();
|
|
132
|
+
|
|
133
|
+
acc[key] = val;
|
|
134
|
+
|
|
135
|
+
if (key === HTTP2_HEADER_ACCEPT_ENCODING && !isZstdSupported) {
|
|
136
|
+
val = val.replace(/\s?zstd,?/gi, '').trim();
|
|
137
|
+
|
|
138
|
+
if (val) {
|
|
139
|
+
acc[key] = val;
|
|
140
|
+
} else {
|
|
141
|
+
Reflect.deleteProperty(acc, key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return acc;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
function normalizeUrl(url, { trimTrailingSlashes, stripTrailingSlash } = {}) {
|
|
150
|
+
if (trimTrailingSlashes) {
|
|
151
|
+
url.pathname = url.pathname.replace(/\/{2,}/g, '/');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (stripTrailingSlash && url.pathname !== '/') {
|
|
155
|
+
url.pathname = url.pathname.replace(/\/$/, '');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return url;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export const sameOrigin = (a, b) => a.origin === b.origin;
|
|
162
|
+
|
|
163
|
+
export const snoop = (client, req, options, { reject } = { reject: () => void 0 }) => {
|
|
164
|
+
req.once('aborted', reject);
|
|
165
|
+
req.once('close', () => client?.close());
|
|
166
|
+
req.once('end', () => client?.close());
|
|
167
|
+
req.once('error', reject);
|
|
168
|
+
req.once('frameError', reject);
|
|
169
|
+
req.once('goaway', reject);
|
|
170
|
+
req.once('timeout', () => req.destroy(new TimeoutError(`Timed out after ${ options.timeout } ms`)));
|
|
171
|
+
req.once('trailers', (trailers) => {
|
|
172
|
+
Reflect.defineProperty(req, 'trailers', {
|
|
173
|
+
enumerable: true,
|
|
174
|
+
value: trailers,
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const stripHeaders = (headers = {}, keys = []) => {
|
|
180
|
+
keys = new Set(keys);
|
|
181
|
+
|
|
182
|
+
return Object.fromEntries(Object.entries(headers).filter(([key]) => !keys.has(key)));
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export async function* tap(val) {
|
|
186
|
+
if (Reflect.has(val, Symbol.asyncIterator)) {
|
|
187
|
+
yield* val;
|
|
188
|
+
} else if (val.stream) {
|
|
189
|
+
yield* val.stream();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const toCamelCase = (str) => str?.toLowerCase().replace(
|
|
194
|
+
/\p{Punctuation}.|\p{White_Space}./gu,
|
|
195
|
+
(val) => val.replace(/\p{Punctuation}+|\p{White_Space}+/gu, '').toUpperCase(),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
export const unwind = (encodings) => encodings.split(',').map((it) => it.toLowerCase().trim());
|
package/src/validation.js
CHANGED
|
@@ -1,33 +1,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
|
-
};
|
|
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
|
+
};
|