rekwest 3.3.4 → 4.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 +4 -3
- package/dist/ackn.js +4 -7
- package/dist/cookies.js +2 -10
- package/dist/errors.js +0 -7
- package/dist/file.js +9 -14
- package/dist/formdata.js +6 -53
- package/dist/index.js +20 -103
- package/dist/utils.js +105 -161
- package/package.json +13 -12
- package/src/cookies.mjs +28 -28
- package/src/file.mjs +49 -40
- package/src/formdata.mjs +240 -239
- package/src/index.mjs +12 -15
- package/src/utils.mjs +92 -80
package/src/formdata.mjs
CHANGED
|
@@ -1,239 +1,240 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import http2 from 'node:http2';
|
|
3
|
-
import { toUSVString } from 'node:util';
|
|
4
|
-
import { File } from './file.mjs';
|
|
5
|
-
import {
|
|
6
|
-
APPLICATION_OCTET_STREAM,
|
|
7
|
-
MULTIPART_FORM_DATA,
|
|
8
|
-
} from './mediatypes.mjs';
|
|
9
|
-
import {
|
|
10
|
-
collate,
|
|
11
|
-
tap,
|
|
12
|
-
} from './utils.mjs';
|
|
13
|
-
|
|
14
|
-
const CRLF = '\r\n';
|
|
15
|
-
const {
|
|
16
|
-
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
17
|
-
HTTP2_HEADER_CONTENT_TYPE,
|
|
18
|
-
} = http2.constants;
|
|
19
|
-
|
|
20
|
-
export class FormData {
|
|
21
|
-
|
|
22
|
-
static actuate(fd) {
|
|
23
|
-
const boundary = randomBytes(24).toString('hex');
|
|
24
|
-
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
25
|
-
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
26
|
-
|
|
27
|
-
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
28
|
-
const normalize = (value) => value.replace(/\r?\n|\r/g, CRLF);
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
contentType,
|
|
32
|
-
async* [Symbol.asyncIterator]() {
|
|
33
|
-
const encoder = new TextEncoder();
|
|
34
|
-
|
|
35
|
-
for (const [name, value] of fd) {
|
|
36
|
-
if (value.constructor === String) {
|
|
37
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
38
|
-
escape(normalize(name))
|
|
39
|
-
}"${ CRLF.repeat(2) }${ normalize(value) }${ CRLF }`);
|
|
40
|
-
} else {
|
|
41
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
42
|
-
escape(normalize(name))
|
|
43
|
-
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
|
|
44
|
-
HTTP2_HEADER_CONTENT_TYPE
|
|
45
|
-
}: ${
|
|
46
|
-
value.type || APPLICATION_OCTET_STREAM
|
|
47
|
-
}${ CRLF.repeat(2) }`);
|
|
48
|
-
yield* tap(value);
|
|
49
|
-
yield encoder.encode(CRLF);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
yield encoder.encode(`--${ boundary }--`);
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
static alike(instance) {
|
|
59
|
-
return instance?.constructor.name === FormData.name;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
static #enfoldEntry(name, value, filename) {
|
|
63
|
-
name = toUSVString(name);
|
|
64
|
-
filename &&= toUSVString(filename);
|
|
65
|
-
|
|
66
|
-
if (File.alike(value)) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
this
|
|
148
|
-
this.#
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
this
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
this
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
this
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
this
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import { toUSVString } from 'node:util';
|
|
4
|
+
import { File } from './file.mjs';
|
|
5
|
+
import {
|
|
6
|
+
APPLICATION_OCTET_STREAM,
|
|
7
|
+
MULTIPART_FORM_DATA,
|
|
8
|
+
} from './mediatypes.mjs';
|
|
9
|
+
import {
|
|
10
|
+
collate,
|
|
11
|
+
tap,
|
|
12
|
+
} from './utils.mjs';
|
|
13
|
+
|
|
14
|
+
const CRLF = '\r\n';
|
|
15
|
+
const {
|
|
16
|
+
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
17
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
18
|
+
} = http2.constants;
|
|
19
|
+
|
|
20
|
+
export class FormData {
|
|
21
|
+
|
|
22
|
+
static actuate(fd) {
|
|
23
|
+
const boundary = randomBytes(24).toString('hex');
|
|
24
|
+
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
25
|
+
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
26
|
+
|
|
27
|
+
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
28
|
+
const normalize = (value) => value.replace(/\r?\n|\r/g, CRLF);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
contentType,
|
|
32
|
+
async* [Symbol.asyncIterator]() {
|
|
33
|
+
const encoder = new TextEncoder();
|
|
34
|
+
|
|
35
|
+
for (const [name, value] of fd) {
|
|
36
|
+
if (value.constructor === String) {
|
|
37
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
38
|
+
escape(normalize(name))
|
|
39
|
+
}"${ CRLF.repeat(2) }${ normalize(value) }${ CRLF }`);
|
|
40
|
+
} else {
|
|
41
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
42
|
+
escape(normalize(name))
|
|
43
|
+
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
|
|
44
|
+
HTTP2_HEADER_CONTENT_TYPE
|
|
45
|
+
}: ${
|
|
46
|
+
value.type || APPLICATION_OCTET_STREAM
|
|
47
|
+
}${ CRLF.repeat(2) }`);
|
|
48
|
+
yield* tap(value);
|
|
49
|
+
yield encoder.encode(CRLF);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
yield encoder.encode(`--${ boundary }--`);
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static alike(instance) {
|
|
59
|
+
return instance?.constructor.name === FormData.name;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static #enfoldEntry(name, value, filename) {
|
|
63
|
+
name = toUSVString(name);
|
|
64
|
+
filename &&= toUSVString(filename);
|
|
65
|
+
|
|
66
|
+
if (File.alike(value)) {
|
|
67
|
+
filename ??= value.name || 'blob';
|
|
68
|
+
value = new File([value], filename, value);
|
|
69
|
+
} else if (this.#ensureInstance(value)) {
|
|
70
|
+
value.name = filename;
|
|
71
|
+
} else {
|
|
72
|
+
value = toUSVString(value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
name,
|
|
77
|
+
value,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static #ensureInstance(value) {
|
|
82
|
+
return File.alike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
#entries = [];
|
|
86
|
+
|
|
87
|
+
get [Symbol.toStringTag]() {
|
|
88
|
+
return this.constructor.name;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
constructor(input) {
|
|
92
|
+
if (input === Object(input)
|
|
93
|
+
&& (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
|
|
94
|
+
|
|
95
|
+
if (input.constructor !== Object) {
|
|
96
|
+
input = Array.from(input);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (Array.isArray(input)) {
|
|
100
|
+
if (!input.every((it) => Array.isArray(it))) {
|
|
101
|
+
throw new TypeError(`Failed to construct '${
|
|
102
|
+
this[Symbol.toStringTag]
|
|
103
|
+
}': The provided value cannot be converted to a sequence.`);
|
|
104
|
+
} else if (!input.every((it) => it.length === 2)) {
|
|
105
|
+
throw new TypeError(`Failed to construct '${
|
|
106
|
+
this[Symbol.toStringTag]
|
|
107
|
+
}': Sequence initializer must only contain pair elements.`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (input.constructor === Object) {
|
|
112
|
+
input = Object.entries(input);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
input.forEach(([key, value]) => this.append(key, value));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#ensureArgs(args, expected, method) {
|
|
120
|
+
if (args.length < expected) {
|
|
121
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
122
|
+
this[Symbol.toStringTag]
|
|
123
|
+
}': ${ expected } arguments required, but only ${ args.length } present.`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if ([
|
|
127
|
+
'append',
|
|
128
|
+
'set',
|
|
129
|
+
].includes(method)) {
|
|
130
|
+
if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
|
|
131
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
132
|
+
this[Symbol.toStringTag]
|
|
133
|
+
}': parameter ${ expected } is not of type 'Blob'.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (method === 'forEach') {
|
|
138
|
+
if (args[0]?.constructor !== Function) {
|
|
139
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
140
|
+
this[Symbol.toStringTag]
|
|
141
|
+
}': parameter ${ expected } is not of type 'Function'.`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
append(...args) {
|
|
147
|
+
collate(this, FormData);
|
|
148
|
+
this.#ensureArgs(args, 2, 'append');
|
|
149
|
+
this.#entries.push(this.constructor.#enfoldEntry(...args));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
delete(...args) {
|
|
153
|
+
collate(this, FormData);
|
|
154
|
+
this.#ensureArgs(args, 1, 'delete');
|
|
155
|
+
const name = toUSVString(args[0]);
|
|
156
|
+
|
|
157
|
+
this.#entries = this.#entries.filter((it) => it.name !== name);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
forEach(...args) {
|
|
161
|
+
collate(this, FormData);
|
|
162
|
+
this.#ensureArgs(args, 1, 'forEach');
|
|
163
|
+
const [callback, thisArg] = args;
|
|
164
|
+
|
|
165
|
+
for (const entry of this) {
|
|
166
|
+
Reflect.apply(callback, thisArg, [
|
|
167
|
+
...entry.reverse(),
|
|
168
|
+
this,
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
get(...args) {
|
|
174
|
+
collate(this, FormData);
|
|
175
|
+
this.#ensureArgs(args, 1, 'get');
|
|
176
|
+
const name = toUSVString(args[0]);
|
|
177
|
+
|
|
178
|
+
return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getAll(...args) {
|
|
182
|
+
collate(this, FormData);
|
|
183
|
+
this.#ensureArgs(args, 1, 'getAll');
|
|
184
|
+
const name = toUSVString(args[0]);
|
|
185
|
+
|
|
186
|
+
return this.#entries.filter((it) => it.name === name).map((it) => it.value);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
has(...args) {
|
|
190
|
+
collate(this, FormData);
|
|
191
|
+
this.#ensureArgs(args, 1, 'has');
|
|
192
|
+
const name = toUSVString(args[0]);
|
|
193
|
+
|
|
194
|
+
return !!this.#entries.find((it) => it.name === name);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
set(...args) {
|
|
198
|
+
collate(this, FormData);
|
|
199
|
+
this.#ensureArgs(args, 2, 'set');
|
|
200
|
+
const entry = this.constructor.#enfoldEntry(...args);
|
|
201
|
+
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
202
|
+
|
|
203
|
+
if (idx !== -1) {
|
|
204
|
+
this.#entries.splice(idx, 1, entry);
|
|
205
|
+
} else {
|
|
206
|
+
this.#entries.push(entry);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
* entries() {
|
|
211
|
+
collate(this, FormData);
|
|
212
|
+
for (const { name, value } of this.#entries) {
|
|
213
|
+
yield [
|
|
214
|
+
name,
|
|
215
|
+
value,
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
* keys() {
|
|
221
|
+
collate(this, FormData);
|
|
222
|
+
for (const [name] of this) {
|
|
223
|
+
yield name;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
* values() {
|
|
228
|
+
collate(this, FormData);
|
|
229
|
+
for (const [, value] of this) {
|
|
230
|
+
yield value;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
[Symbol.iterator]() {
|
|
235
|
+
collate(this, FormData);
|
|
236
|
+
|
|
237
|
+
return this.entries();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
}
|
package/src/index.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
mixin,
|
|
15
15
|
preflight,
|
|
16
16
|
redirects,
|
|
17
|
-
|
|
17
|
+
sanitize,
|
|
18
18
|
transform,
|
|
19
19
|
} from './utils.mjs';
|
|
20
20
|
|
|
@@ -70,8 +70,10 @@ let defaults = {
|
|
|
70
70
|
timeout: 3e5,
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
-
export default async function rekwest(
|
|
74
|
-
|
|
73
|
+
export default async function rekwest(...args) {
|
|
74
|
+
let options = sanitize(...args);
|
|
75
|
+
const { url } = options;
|
|
76
|
+
|
|
75
77
|
if (!options.redirected) {
|
|
76
78
|
options = merge(rekwest.defaults, options);
|
|
77
79
|
}
|
|
@@ -98,17 +100,14 @@ export default async function rekwest(url, options = {}) {
|
|
|
98
100
|
].forEach((it) => Reflect.deleteProperty(options, it));
|
|
99
101
|
}
|
|
100
102
|
|
|
101
|
-
options = preflight(options);
|
|
103
|
+
options = await transform(preflight(options));
|
|
102
104
|
|
|
103
105
|
const { cookies, digest, follow, h2, redirect, redirected, thenable } = options;
|
|
104
106
|
const { request } = (url.protocol === 'http:' ? http : https);
|
|
105
|
-
let { body } = options;
|
|
106
107
|
|
|
107
108
|
const promise = new Promise((resolve, reject) => {
|
|
108
109
|
let client, req;
|
|
109
110
|
|
|
110
|
-
body &&= transform(body, options);
|
|
111
|
-
|
|
112
111
|
if (h2) {
|
|
113
112
|
client = http2.connect(url.origin, options);
|
|
114
113
|
req = client.request(options.headers, options);
|
|
@@ -157,8 +156,7 @@ export default async function rekwest(url, options = {}) {
|
|
|
157
156
|
if (redirect === redirects.follow) {
|
|
158
157
|
options.url = new URL(res.headers[HTTP2_HEADER_LOCATION], url).href;
|
|
159
158
|
|
|
160
|
-
if (res.statusCode !== HTTP_STATUS_SEE_OTHER
|
|
161
|
-
&& body === Object(body) && body.pipe?.constructor === Function) {
|
|
159
|
+
if (res.statusCode !== HTTP_STATUS_SEE_OTHER && options?.body?.pipe?.constructor === Function) {
|
|
162
160
|
return res.emit('error', new RequestError(`Unable to ${ redirect } redirect with streamable body.`));
|
|
163
161
|
}
|
|
164
162
|
|
|
@@ -195,7 +193,7 @@ export default async function rekwest(url, options = {}) {
|
|
|
195
193
|
resolve(mixin(res, options));
|
|
196
194
|
});
|
|
197
195
|
|
|
198
|
-
dispatch(
|
|
196
|
+
dispatch(options, req);
|
|
199
197
|
});
|
|
200
198
|
|
|
201
199
|
try {
|
|
@@ -242,16 +240,15 @@ export default async function rekwest(url, options = {}) {
|
|
|
242
240
|
|
|
243
241
|
Reflect.defineProperty(rekwest, 'stream', {
|
|
244
242
|
enumerable: true,
|
|
245
|
-
value(
|
|
246
|
-
|
|
247
|
-
options = preflight({
|
|
243
|
+
value(...args) {
|
|
244
|
+
const options = preflight({
|
|
248
245
|
...merge(rekwest.defaults, {
|
|
249
246
|
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
250
|
-
},
|
|
247
|
+
}, sanitize(...args)),
|
|
251
248
|
redirect: redirects.manual,
|
|
252
249
|
});
|
|
253
250
|
|
|
254
|
-
const { h2 } = options;
|
|
251
|
+
const { h2, url } = options;
|
|
255
252
|
const { request } = (url.protocol === 'http:' ? http : https);
|
|
256
253
|
let client, req;
|
|
257
254
|
|