rekwest 3.1.1 → 3.3.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 +186 -184
- package/dist/ackn.js +2 -2
- package/dist/cookies.js +3 -0
- package/dist/file.js +4 -4
- package/dist/formdata.js +29 -15
- package/dist/index.js +57 -48
- package/dist/{helpers.js → utils.js} +77 -37
- package/package.json +14 -9
- package/src/ackn.mjs +33 -33
- package/src/cookies.mjs +4 -0
- package/src/file.mjs +2 -2
- package/src/formdata.mjs +20 -5
- package/src/index.mjs +20 -19
- package/src/{helpers.mjs → utils.mjs} +37 -13
package/src/formdata.mjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { randomBytes } from 'crypto';
|
|
2
|
-
import http2 from 'http2';
|
|
3
|
-
import { toUSVString } from 'util';
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import { toUSVString } from 'node:util';
|
|
4
4
|
import { File } from './file.mjs';
|
|
5
|
-
import { tap } from './helpers.mjs';
|
|
6
5
|
import {
|
|
7
6
|
APPLICATION_OCTET_STREAM,
|
|
8
7
|
MULTIPART_FORM_DATA,
|
|
9
8
|
} from './mediatypes.mjs';
|
|
9
|
+
import {
|
|
10
|
+
collate,
|
|
11
|
+
tap,
|
|
12
|
+
} from './utils.mjs';
|
|
10
13
|
|
|
11
14
|
const CRLF = '\r\n';
|
|
12
15
|
const {
|
|
@@ -140,11 +143,13 @@ export class FormData {
|
|
|
140
143
|
}
|
|
141
144
|
|
|
142
145
|
append(...args) {
|
|
146
|
+
collate(this, FormData);
|
|
143
147
|
this.#ensureArgs(args, 2, 'append');
|
|
144
148
|
this.#entries.push(this.constructor.#enfoldEntry(...args));
|
|
145
149
|
}
|
|
146
150
|
|
|
147
151
|
delete(...args) {
|
|
152
|
+
collate(this, FormData);
|
|
148
153
|
this.#ensureArgs(args, 1, 'delete');
|
|
149
154
|
const name = toUSVString(args[0]);
|
|
150
155
|
|
|
@@ -152,18 +157,20 @@ export class FormData {
|
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
forEach(...args) {
|
|
160
|
+
collate(this, FormData);
|
|
155
161
|
this.#ensureArgs(args, 1, 'forEach');
|
|
156
162
|
const [callback, thisArg] = args;
|
|
157
163
|
|
|
158
164
|
for (const entry of this) {
|
|
159
165
|
Reflect.apply(callback, thisArg, [
|
|
160
|
-
...
|
|
166
|
+
...entry.reverse(),
|
|
161
167
|
this,
|
|
162
168
|
]);
|
|
163
169
|
}
|
|
164
170
|
}
|
|
165
171
|
|
|
166
172
|
get(...args) {
|
|
173
|
+
collate(this, FormData);
|
|
167
174
|
this.#ensureArgs(args, 1, 'get');
|
|
168
175
|
const name = toUSVString(args[0]);
|
|
169
176
|
|
|
@@ -171,6 +178,7 @@ export class FormData {
|
|
|
171
178
|
}
|
|
172
179
|
|
|
173
180
|
getAll(...args) {
|
|
181
|
+
collate(this, FormData);
|
|
174
182
|
this.#ensureArgs(args, 1, 'getAll');
|
|
175
183
|
const name = toUSVString(args[0]);
|
|
176
184
|
|
|
@@ -178,6 +186,7 @@ export class FormData {
|
|
|
178
186
|
}
|
|
179
187
|
|
|
180
188
|
has(...args) {
|
|
189
|
+
collate(this, FormData);
|
|
181
190
|
this.#ensureArgs(args, 1, 'has');
|
|
182
191
|
const name = toUSVString(args[0]);
|
|
183
192
|
|
|
@@ -185,6 +194,7 @@ export class FormData {
|
|
|
185
194
|
}
|
|
186
195
|
|
|
187
196
|
set(...args) {
|
|
197
|
+
collate(this, FormData);
|
|
188
198
|
this.#ensureArgs(args, 2, 'set');
|
|
189
199
|
const entry = this.constructor.#enfoldEntry(...args);
|
|
190
200
|
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
@@ -197,6 +207,7 @@ export class FormData {
|
|
|
197
207
|
}
|
|
198
208
|
|
|
199
209
|
* entries() {
|
|
210
|
+
collate(this, FormData);
|
|
200
211
|
for (const { name, value } of this.#entries) {
|
|
201
212
|
yield [
|
|
202
213
|
name,
|
|
@@ -206,18 +217,22 @@ export class FormData {
|
|
|
206
217
|
}
|
|
207
218
|
|
|
208
219
|
* keys() {
|
|
220
|
+
collate(this, FormData);
|
|
209
221
|
for (const [name] of this) {
|
|
210
222
|
yield name;
|
|
211
223
|
}
|
|
212
224
|
}
|
|
213
225
|
|
|
214
226
|
* values() {
|
|
227
|
+
collate(this, FormData);
|
|
215
228
|
for (const [, value] of this) {
|
|
216
229
|
yield value;
|
|
217
230
|
}
|
|
218
231
|
}
|
|
219
232
|
|
|
220
233
|
[Symbol.iterator]() {
|
|
234
|
+
collate(this, FormData);
|
|
235
|
+
|
|
221
236
|
return this.entries();
|
|
222
237
|
}
|
|
223
238
|
|
package/src/index.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import http2 from 'http2';
|
|
3
|
-
import https from 'https';
|
|
4
|
-
import { setTimeout as setTimeoutPromise } from 'timers/promises';
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
|
|
5
5
|
import { ackn } from './ackn.mjs';
|
|
6
6
|
import { Cookies } from './cookies.mjs';
|
|
7
7
|
import { RequestError } from './errors.mjs';
|
|
8
|
+
import { APPLICATION_OCTET_STREAM } from './mediatypes.mjs';
|
|
8
9
|
import {
|
|
9
10
|
admix,
|
|
10
11
|
affix,
|
|
@@ -13,18 +14,18 @@ import {
|
|
|
13
14
|
mixin,
|
|
14
15
|
preflight,
|
|
15
16
|
redirects,
|
|
17
|
+
revise,
|
|
16
18
|
transform,
|
|
17
|
-
} from './
|
|
18
|
-
import { APPLICATION_OCTET_STREAM } from './mediatypes.mjs';
|
|
19
|
+
} from './utils.mjs';
|
|
19
20
|
|
|
20
|
-
export { constants } from 'http2';
|
|
21
|
+
export { constants } from 'node:http2';
|
|
21
22
|
|
|
22
23
|
export * from './ackn.mjs';
|
|
23
24
|
export * from './cookies.mjs';
|
|
24
25
|
export * from './errors.mjs';
|
|
25
26
|
export * from './file.mjs';
|
|
26
27
|
export * from './formdata.mjs';
|
|
27
|
-
export * from './
|
|
28
|
+
export * from './utils.mjs';
|
|
28
29
|
|
|
29
30
|
const {
|
|
30
31
|
HTTP2_HEADER_CONTENT_LENGTH,
|
|
@@ -69,7 +70,7 @@ let defaults = {
|
|
|
69
70
|
};
|
|
70
71
|
|
|
71
72
|
export default async function rekwest(url, options = {}) {
|
|
72
|
-
url = options
|
|
73
|
+
({ url } = revise({ options, url }));
|
|
73
74
|
if (!options.redirected) {
|
|
74
75
|
options = merge(rekwest.defaults, options);
|
|
75
76
|
}
|
|
@@ -98,8 +99,8 @@ export default async function rekwest(url, options = {}) {
|
|
|
98
99
|
|
|
99
100
|
options = preflight(options);
|
|
100
101
|
|
|
101
|
-
const { cookies, digest, follow, h2, redirect, redirected, thenable
|
|
102
|
-
const { request } = (protocol === 'http:' ? http : https);
|
|
102
|
+
const { cookies, digest, follow, h2, redirect, redirected, thenable } = options;
|
|
103
|
+
const { request } = (url.protocol === 'http:' ? http : https);
|
|
103
104
|
let { body } = options;
|
|
104
105
|
|
|
105
106
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -149,7 +150,7 @@ export default async function rekwest(url, options = {}) {
|
|
|
149
150
|
|
|
150
151
|
if (follow && /^3\d{2}$/.test(res.statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
|
|
151
152
|
if (redirect === redirects.error) {
|
|
152
|
-
res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'.`));
|
|
153
|
+
return res.emit('error', new RequestError(`Unexpected redirect, redirect mode is set to '${ redirect }'.`));
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
if (redirect === redirects.follow) {
|
|
@@ -157,7 +158,7 @@ export default async function rekwest(url, options = {}) {
|
|
|
157
158
|
|
|
158
159
|
if (res.statusCode !== HTTP_STATUS_SEE_OTHER
|
|
159
160
|
&& body === Object(body) && body.pipe?.constructor === Function) {
|
|
160
|
-
res.emit('error', new RequestError(`Unable to ${ redirect } redirect with body
|
|
161
|
+
return res.emit('error', new RequestError(`Unable to ${ redirect } redirect with streamable body.`));
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
options.follow--;
|
|
@@ -176,7 +177,7 @@ export default async function rekwest(url, options = {}) {
|
|
|
176
177
|
interval = Number(interval) * 1000 || new Date(interval) - Date.now();
|
|
177
178
|
|
|
178
179
|
if (interval > options.maxRetryAfter) {
|
|
179
|
-
res.emit('error', maxRetryAfterError(interval, { cause: mixin(res, options) }));
|
|
180
|
+
return res.emit('error', maxRetryAfterError(interval, { cause: mixin(res, options) }));
|
|
180
181
|
}
|
|
181
182
|
|
|
182
183
|
return setTimeoutPromise(interval).then(() => rekwest(options.url, options).then(resolve, reject));
|
|
@@ -240,24 +241,24 @@ export default async function rekwest(url, options = {}) {
|
|
|
240
241
|
|
|
241
242
|
Reflect.defineProperty(rekwest, 'stream', {
|
|
242
243
|
enumerable: true,
|
|
243
|
-
value
|
|
244
|
+
value(url, options = {}) {
|
|
245
|
+
({ url } = revise({ options, url }));
|
|
244
246
|
options = preflight({
|
|
245
|
-
url,
|
|
246
247
|
...merge(rekwest.defaults, {
|
|
247
248
|
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
248
249
|
}, options),
|
|
249
250
|
redirect: redirects.manual,
|
|
250
251
|
});
|
|
251
252
|
|
|
252
|
-
const { h2
|
|
253
|
-
const { request } = (protocol === 'http:' ? http : https);
|
|
253
|
+
const { h2 } = options;
|
|
254
|
+
const { request } = (url.protocol === 'http:' ? http : https);
|
|
254
255
|
let client, req;
|
|
255
256
|
|
|
256
257
|
if (h2) {
|
|
257
258
|
client = http2.connect(url.origin, options);
|
|
258
259
|
req = client.request(options.headers, options);
|
|
259
260
|
} else {
|
|
260
|
-
req = request(
|
|
261
|
+
req = request(url, options);
|
|
261
262
|
}
|
|
262
263
|
|
|
263
264
|
affix(client, req, options);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { Blob } from 'buffer';
|
|
2
|
-
import http2 from 'http2';
|
|
1
|
+
import { Blob } from 'node:buffer';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
3
|
import {
|
|
4
4
|
PassThrough,
|
|
5
5
|
Readable,
|
|
6
|
-
} from 'stream';
|
|
6
|
+
} from 'node:stream';
|
|
7
7
|
import {
|
|
8
8
|
promisify,
|
|
9
9
|
types,
|
|
10
|
-
} from 'util';
|
|
11
|
-
import zlib from 'zlib';
|
|
10
|
+
} from 'node:util';
|
|
11
|
+
import zlib from 'node:zlib';
|
|
12
12
|
import { Cookies } from './cookies.mjs';
|
|
13
13
|
import { TimeoutError } from './errors.mjs';
|
|
14
14
|
import { File } from './file.mjs';
|
|
@@ -86,6 +86,12 @@ export const affix = (client, req, options) => {
|
|
|
86
86
|
});
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
+
export const collate = (entity, primordial) => {
|
|
90
|
+
if (entity?.constructor !== primordial) {
|
|
91
|
+
throw new TypeError('Illegal invocation');
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
89
95
|
export const compress = (buf, encoding, { async = false } = {}) => {
|
|
90
96
|
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
91
97
|
const compressor = {
|
|
@@ -163,7 +169,8 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
|
163
169
|
Object.defineProperties(res, {
|
|
164
170
|
arrayBuffer: {
|
|
165
171
|
enumerable: true,
|
|
166
|
-
value
|
|
172
|
+
value() {
|
|
173
|
+
collate(this, res?.constructor);
|
|
167
174
|
parse &&= false;
|
|
168
175
|
|
|
169
176
|
return this.body().then(({ buffer, byteLength, byteOffset }) => buffer.slice(
|
|
@@ -174,19 +181,25 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
|
174
181
|
},
|
|
175
182
|
blob: {
|
|
176
183
|
enumerable: true,
|
|
177
|
-
value
|
|
184
|
+
value() {
|
|
185
|
+
collate(this, res?.constructor);
|
|
186
|
+
|
|
178
187
|
return this.arrayBuffer().then((res) => new Blob([res]));
|
|
179
188
|
},
|
|
180
189
|
},
|
|
181
190
|
json: {
|
|
182
191
|
enumerable: true,
|
|
183
|
-
value
|
|
192
|
+
value() {
|
|
193
|
+
collate(this, res?.constructor);
|
|
194
|
+
|
|
184
195
|
return this.text().then((res) => JSON.parse(res));
|
|
185
196
|
},
|
|
186
197
|
},
|
|
187
198
|
text: {
|
|
188
199
|
enumerable: true,
|
|
189
|
-
value
|
|
200
|
+
value() {
|
|
201
|
+
collate(this, res?.constructor);
|
|
202
|
+
|
|
190
203
|
return this.blob().then((blob) => blob.text());
|
|
191
204
|
},
|
|
192
205
|
},
|
|
@@ -197,8 +210,10 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
|
197
210
|
body: {
|
|
198
211
|
enumerable: true,
|
|
199
212
|
value: async function () {
|
|
213
|
+
collate(this, res?.constructor);
|
|
214
|
+
|
|
200
215
|
if (this.bodyUsed) {
|
|
201
|
-
throw new TypeError('Response stream already read
|
|
216
|
+
throw new TypeError('Response stream already read');
|
|
202
217
|
}
|
|
203
218
|
|
|
204
219
|
let spool = [];
|
|
@@ -239,7 +254,7 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
|
239
254
|
},
|
|
240
255
|
bodyUsed: {
|
|
241
256
|
enumerable: true,
|
|
242
|
-
get
|
|
257
|
+
get() {
|
|
243
258
|
return this.readableEnded;
|
|
244
259
|
},
|
|
245
260
|
},
|
|
@@ -247,8 +262,7 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
|
|
|
247
262
|
};
|
|
248
263
|
|
|
249
264
|
export const preflight = (options) => {
|
|
250
|
-
const
|
|
251
|
-
const { cookies, h2 = false, method = HTTP2_METHOD_GET, headers, redirected } = options;
|
|
265
|
+
const { cookies, h2 = false, headers, method = HTTP2_METHOD_GET, redirected, url } = options;
|
|
252
266
|
|
|
253
267
|
if (h2) {
|
|
254
268
|
options.endStream = [
|
|
@@ -316,6 +330,16 @@ export const redirects = {
|
|
|
316
330
|
manual: 'manual',
|
|
317
331
|
};
|
|
318
332
|
|
|
333
|
+
export const revise = ({ url, options }) => {
|
|
334
|
+
if (options.trimTrailingSlashes) {
|
|
335
|
+
url = `${ url }`.replace(/(?<!:)\/+/gi, '/');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
url = new URL(url);
|
|
339
|
+
|
|
340
|
+
return Object.assign(options, { url });
|
|
341
|
+
};
|
|
342
|
+
|
|
319
343
|
export async function* tap(value) {
|
|
320
344
|
if (Reflect.has(value, Symbol.asyncIterator)) {
|
|
321
345
|
yield* value;
|