rekwest 8.0.0 → 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 +1 -0
- package/dist/cookies.cjs +1 -1
- package/dist/transfer.cjs +1 -5
- 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 -198
- package/src/validation.js +33 -33
package/src/formdata.js
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
|
-
import http2 from 'node:http2';
|
|
3
|
-
import {
|
|
4
|
-
APPLICATION_OCTET_STREAM,
|
|
5
|
-
MULTIPART_FORM_DATA,
|
|
6
|
-
} from './mediatypes.js';
|
|
7
|
-
import {
|
|
8
|
-
brandCheck,
|
|
9
|
-
isBlobLike,
|
|
10
|
-
isPipeStream,
|
|
11
|
-
isReadableStream,
|
|
12
|
-
tap,
|
|
13
|
-
} from './utils.js';
|
|
14
|
-
|
|
15
|
-
const CRLF = '\r\n';
|
|
16
|
-
const {
|
|
17
|
-
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
18
|
-
HTTP2_HEADER_CONTENT_TYPE,
|
|
19
|
-
} = http2.constants;
|
|
20
|
-
|
|
21
|
-
export class FormData {
|
|
22
|
-
|
|
23
|
-
static #ensureArgs(args, expect, method) {
|
|
24
|
-
if (args.length < expect) {
|
|
25
|
-
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
26
|
-
this[Symbol.toStringTag]
|
|
27
|
-
}': ${ expect } arguments required, but only ${ args.length } present`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (method === 'forEach') {
|
|
31
|
-
if (args[0]?.constructor !== Function) {
|
|
32
|
-
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
33
|
-
this[Symbol.toStringTag]
|
|
34
|
-
}': parameter ${ expect } is not of type 'Function'`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
static #formEntry(name, value, filename) {
|
|
40
|
-
name = String(name).toWellFormed();
|
|
41
|
-
filename &&= String(filename).toWellFormed();
|
|
42
|
-
|
|
43
|
-
if (isBlobLike(value)) {
|
|
44
|
-
filename ??= String(value.name ?? 'blob').toWellFormed();
|
|
45
|
-
value = new File([value], filename, value);
|
|
46
|
-
} else if (isPipeStream(value) || isReadableStream(value)) {
|
|
47
|
-
value.name = filename ?? 'blob';
|
|
48
|
-
} else {
|
|
49
|
-
value = String(value).toWellFormed();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
name,
|
|
54
|
-
value,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
#entries = [];
|
|
59
|
-
|
|
60
|
-
get [Symbol.toStringTag]() {
|
|
61
|
-
return this.constructor.name;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
constructor(input) {
|
|
65
|
-
if (Object(input) === input) {
|
|
66
|
-
if (Array.isArray(input)) {
|
|
67
|
-
if (!input.every((it) => Array.isArray(it))) {
|
|
68
|
-
throw new TypeError(`Failed to construct '${
|
|
69
|
-
this[Symbol.toStringTag]
|
|
70
|
-
}': The provided value cannot be converted to a sequence`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (!input.every((it) => it.length === 2)) {
|
|
74
|
-
throw new TypeError(`Failed to construct '${
|
|
75
|
-
this[Symbol.toStringTag]
|
|
76
|
-
}': Sequence initializer must only contain pair elements`);
|
|
77
|
-
}
|
|
78
|
-
} else if (!Reflect.has(input, Symbol.iterator)) {
|
|
79
|
-
input = Object.entries(input);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
for (const [key, val] of input) {
|
|
83
|
-
this.append(key, val);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
append(...args) {
|
|
89
|
-
brandCheck(this, FormData);
|
|
90
|
-
this.constructor.#ensureArgs(args, 2, this.append.name);
|
|
91
|
-
this.#entries.push(this.constructor.#formEntry(...args));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
delete(...args) {
|
|
95
|
-
brandCheck(this, FormData);
|
|
96
|
-
this.constructor.#ensureArgs(args, 1, this.delete.name);
|
|
97
|
-
const name = String(args[0]).toWellFormed();
|
|
98
|
-
|
|
99
|
-
this.#entries = this.#entries.filter((it) => it.name !== name);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
forEach(...args) {
|
|
103
|
-
brandCheck(this, FormData);
|
|
104
|
-
this.constructor.#ensureArgs(args, 1, this.forEach.name);
|
|
105
|
-
const [callback, thisArg] = args;
|
|
106
|
-
|
|
107
|
-
for (const entry of this) {
|
|
108
|
-
Reflect.apply(callback, thisArg, [
|
|
109
|
-
...entry.reverse(),
|
|
110
|
-
this,
|
|
111
|
-
]);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
get(...args) {
|
|
116
|
-
brandCheck(this, FormData);
|
|
117
|
-
this.constructor.#ensureArgs(args, 1, this.get.name);
|
|
118
|
-
const name = String(args[0]).toWellFormed();
|
|
119
|
-
|
|
120
|
-
return this.#entries.find((it) => it.name === name)?.value ?? null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
getAll(...args) {
|
|
124
|
-
brandCheck(this, FormData);
|
|
125
|
-
this.constructor.#ensureArgs(args, 1, this.getAll.name);
|
|
126
|
-
const name = String(args[0]).toWellFormed();
|
|
127
|
-
|
|
128
|
-
return this.#entries.filter((it) => it.name === name).map((it) => it.value);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
has(...args) {
|
|
132
|
-
brandCheck(this, FormData);
|
|
133
|
-
this.constructor.#ensureArgs(args, 1, this.has.name);
|
|
134
|
-
const name = String(args[0]).toWellFormed();
|
|
135
|
-
|
|
136
|
-
return !!this.#entries.find((it) => it.name === name);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
set(...args) {
|
|
140
|
-
brandCheck(this, FormData);
|
|
141
|
-
this.constructor.#ensureArgs(args, 2, this.set.name);
|
|
142
|
-
const entry = this.constructor.#formEntry(...args);
|
|
143
|
-
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
144
|
-
|
|
145
|
-
if (idx !== -1) {
|
|
146
|
-
this.#entries.splice(idx, 1, entry);
|
|
147
|
-
} else {
|
|
148
|
-
this.#entries.push(entry);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
* entries() {
|
|
153
|
-
brandCheck(this, FormData);
|
|
154
|
-
for (const { name, value } of this.#entries) {
|
|
155
|
-
yield [
|
|
156
|
-
name,
|
|
157
|
-
value,
|
|
158
|
-
];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
* keys() {
|
|
163
|
-
brandCheck(this, FormData);
|
|
164
|
-
for (const [name] of this) {
|
|
165
|
-
yield name;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
* values() {
|
|
170
|
-
brandCheck(this, FormData);
|
|
171
|
-
for (const [, val] of this) {
|
|
172
|
-
yield val;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
[Symbol.iterator]() {
|
|
177
|
-
brandCheck(this, FormData);
|
|
178
|
-
|
|
179
|
-
return this.entries();
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export const fdToAsyncIterable = (fd) => {
|
|
185
|
-
const boundary = randomBytes(32).toString('hex');
|
|
186
|
-
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
187
|
-
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
188
|
-
|
|
189
|
-
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
190
|
-
const normalize = (str) => str.replace(/\r?\n|\r/g, CRLF);
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
contentType,
|
|
194
|
-
async* [Symbol.asyncIterator]() {
|
|
195
|
-
const encoder = new TextEncoder();
|
|
196
|
-
|
|
197
|
-
for (const [name, val] of fd) {
|
|
198
|
-
if (val.constructor === String) {
|
|
199
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
200
|
-
escape(normalize(name))
|
|
201
|
-
}"${ CRLF.repeat(2) }${ normalize(val) }${ CRLF }`);
|
|
202
|
-
} else {
|
|
203
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
204
|
-
escape(normalize(name))
|
|
205
|
-
}"${ val.name ? `; filename="${ escape(val.name) }"` : '' }${ CRLF }${
|
|
206
|
-
HTTP2_HEADER_CONTENT_TYPE
|
|
207
|
-
}: ${
|
|
208
|
-
val.type || APPLICATION_OCTET_STREAM
|
|
209
|
-
}${ CRLF.repeat(2) }`);
|
|
210
|
-
yield* tap(val);
|
|
211
|
-
yield new Uint8Array([
|
|
212
|
-
13,
|
|
213
|
-
10,
|
|
214
|
-
]);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
yield encoder.encode(`--${ boundary }--${ CRLF }`);
|
|
219
|
-
},
|
|
220
|
-
};
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
export const isFormData = (val) => FormData.name === val?.[Symbol.toStringTag];
|
|
224
|
-
|
|
225
|
-
export const parseFormData = (str) => {
|
|
226
|
-
const rex = /^-+[^\r\n]+\r?\ncontent-disposition:\s*form-data;\s*name="(?<name>[^"]+)"(?:;\s*filename="(?<filename>[^"]+)")?(?:\r?\n[^\r\n:]+:[^\r\n]*)*\r?\n\r?\n(?<content>.*?)(?=\r?\n-+[^\r\n]+)/gims;
|
|
227
|
-
|
|
228
|
-
return [...str.matchAll(rex)].map(({ groups }) => structuredClone(groups));
|
|
229
|
-
};
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import {
|
|
4
|
+
APPLICATION_OCTET_STREAM,
|
|
5
|
+
MULTIPART_FORM_DATA,
|
|
6
|
+
} from './mediatypes.js';
|
|
7
|
+
import {
|
|
8
|
+
brandCheck,
|
|
9
|
+
isBlobLike,
|
|
10
|
+
isPipeStream,
|
|
11
|
+
isReadableStream,
|
|
12
|
+
tap,
|
|
13
|
+
} from './utils.js';
|
|
14
|
+
|
|
15
|
+
const CRLF = '\r\n';
|
|
16
|
+
const {
|
|
17
|
+
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
18
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
19
|
+
} = http2.constants;
|
|
20
|
+
|
|
21
|
+
export class FormData {
|
|
22
|
+
|
|
23
|
+
static #ensureArgs(args, expect, method) {
|
|
24
|
+
if (args.length < expect) {
|
|
25
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
26
|
+
this[Symbol.toStringTag]
|
|
27
|
+
}': ${ expect } arguments required, but only ${ args.length } present`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (method === 'forEach') {
|
|
31
|
+
if (args[0]?.constructor !== Function) {
|
|
32
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
33
|
+
this[Symbol.toStringTag]
|
|
34
|
+
}': parameter ${ expect } is not of type 'Function'`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static #formEntry(name, value, filename) {
|
|
40
|
+
name = String(name).toWellFormed();
|
|
41
|
+
filename &&= String(filename).toWellFormed();
|
|
42
|
+
|
|
43
|
+
if (isBlobLike(value)) {
|
|
44
|
+
filename ??= String(value.name ?? 'blob').toWellFormed();
|
|
45
|
+
value = new File([value], filename, value);
|
|
46
|
+
} else if (isPipeStream(value) || isReadableStream(value)) {
|
|
47
|
+
value.name = filename ?? 'blob';
|
|
48
|
+
} else {
|
|
49
|
+
value = String(value).toWellFormed();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
name,
|
|
54
|
+
value,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#entries = [];
|
|
59
|
+
|
|
60
|
+
get [Symbol.toStringTag]() {
|
|
61
|
+
return this.constructor.name;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
constructor(input) {
|
|
65
|
+
if (Object(input) === input) {
|
|
66
|
+
if (Array.isArray(input)) {
|
|
67
|
+
if (!input.every((it) => Array.isArray(it))) {
|
|
68
|
+
throw new TypeError(`Failed to construct '${
|
|
69
|
+
this[Symbol.toStringTag]
|
|
70
|
+
}': The provided value cannot be converted to a sequence`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!input.every((it) => it.length === 2)) {
|
|
74
|
+
throw new TypeError(`Failed to construct '${
|
|
75
|
+
this[Symbol.toStringTag]
|
|
76
|
+
}': Sequence initializer must only contain pair elements`);
|
|
77
|
+
}
|
|
78
|
+
} else if (!Reflect.has(input, Symbol.iterator)) {
|
|
79
|
+
input = Object.entries(input);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (const [key, val] of input) {
|
|
83
|
+
this.append(key, val);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
append(...args) {
|
|
89
|
+
brandCheck(this, FormData);
|
|
90
|
+
this.constructor.#ensureArgs(args, 2, this.append.name);
|
|
91
|
+
this.#entries.push(this.constructor.#formEntry(...args));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
delete(...args) {
|
|
95
|
+
brandCheck(this, FormData);
|
|
96
|
+
this.constructor.#ensureArgs(args, 1, this.delete.name);
|
|
97
|
+
const name = String(args[0]).toWellFormed();
|
|
98
|
+
|
|
99
|
+
this.#entries = this.#entries.filter((it) => it.name !== name);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
forEach(...args) {
|
|
103
|
+
brandCheck(this, FormData);
|
|
104
|
+
this.constructor.#ensureArgs(args, 1, this.forEach.name);
|
|
105
|
+
const [callback, thisArg] = args;
|
|
106
|
+
|
|
107
|
+
for (const entry of this) {
|
|
108
|
+
Reflect.apply(callback, thisArg, [
|
|
109
|
+
...entry.reverse(),
|
|
110
|
+
this,
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get(...args) {
|
|
116
|
+
brandCheck(this, FormData);
|
|
117
|
+
this.constructor.#ensureArgs(args, 1, this.get.name);
|
|
118
|
+
const name = String(args[0]).toWellFormed();
|
|
119
|
+
|
|
120
|
+
return this.#entries.find((it) => it.name === name)?.value ?? null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
getAll(...args) {
|
|
124
|
+
brandCheck(this, FormData);
|
|
125
|
+
this.constructor.#ensureArgs(args, 1, this.getAll.name);
|
|
126
|
+
const name = String(args[0]).toWellFormed();
|
|
127
|
+
|
|
128
|
+
return this.#entries.filter((it) => it.name === name).map((it) => it.value);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
has(...args) {
|
|
132
|
+
brandCheck(this, FormData);
|
|
133
|
+
this.constructor.#ensureArgs(args, 1, this.has.name);
|
|
134
|
+
const name = String(args[0]).toWellFormed();
|
|
135
|
+
|
|
136
|
+
return !!this.#entries.find((it) => it.name === name);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
set(...args) {
|
|
140
|
+
brandCheck(this, FormData);
|
|
141
|
+
this.constructor.#ensureArgs(args, 2, this.set.name);
|
|
142
|
+
const entry = this.constructor.#formEntry(...args);
|
|
143
|
+
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
144
|
+
|
|
145
|
+
if (idx !== -1) {
|
|
146
|
+
this.#entries.splice(idx, 1, entry);
|
|
147
|
+
} else {
|
|
148
|
+
this.#entries.push(entry);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
* entries() {
|
|
153
|
+
brandCheck(this, FormData);
|
|
154
|
+
for (const { name, value } of this.#entries) {
|
|
155
|
+
yield [
|
|
156
|
+
name,
|
|
157
|
+
value,
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
* keys() {
|
|
163
|
+
brandCheck(this, FormData);
|
|
164
|
+
for (const [name] of this) {
|
|
165
|
+
yield name;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
* values() {
|
|
170
|
+
brandCheck(this, FormData);
|
|
171
|
+
for (const [, val] of this) {
|
|
172
|
+
yield val;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
[Symbol.iterator]() {
|
|
177
|
+
brandCheck(this, FormData);
|
|
178
|
+
|
|
179
|
+
return this.entries();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const fdToAsyncIterable = (fd) => {
|
|
185
|
+
const boundary = randomBytes(32).toString('hex');
|
|
186
|
+
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
187
|
+
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
188
|
+
|
|
189
|
+
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
190
|
+
const normalize = (str) => str.replace(/\r?\n|\r/g, CRLF);
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
contentType,
|
|
194
|
+
async* [Symbol.asyncIterator]() {
|
|
195
|
+
const encoder = new TextEncoder();
|
|
196
|
+
|
|
197
|
+
for (const [name, val] of fd) {
|
|
198
|
+
if (val.constructor === String) {
|
|
199
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
200
|
+
escape(normalize(name))
|
|
201
|
+
}"${ CRLF.repeat(2) }${ normalize(val) }${ CRLF }`);
|
|
202
|
+
} else {
|
|
203
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
204
|
+
escape(normalize(name))
|
|
205
|
+
}"${ val.name ? `; filename="${ escape(val.name) }"` : '' }${ CRLF }${
|
|
206
|
+
HTTP2_HEADER_CONTENT_TYPE
|
|
207
|
+
}: ${
|
|
208
|
+
val.type || APPLICATION_OCTET_STREAM
|
|
209
|
+
}${ CRLF.repeat(2) }`);
|
|
210
|
+
yield* tap(val);
|
|
211
|
+
yield new Uint8Array([
|
|
212
|
+
13,
|
|
213
|
+
10,
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
yield encoder.encode(`--${ boundary }--${ CRLF }`);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const isFormData = (val) => FormData.name === val?.[Symbol.toStringTag];
|
|
224
|
+
|
|
225
|
+
export const parseFormData = (str) => {
|
|
226
|
+
const rex = /^-+[^\r\n]+\r?\ncontent-disposition:\s*form-data;\s*name="(?<name>[^"]+)"(?:;\s*filename="(?<filename>[^"]+)")?(?:\r?\n[^\r\n:]+:[^\r\n]*)*\r?\n\r?\n(?<content>.*?)(?=\r?\n-+[^\r\n]+)/gims;
|
|
227
|
+
|
|
228
|
+
return [...str.matchAll(rex)].map(({ groups }) => structuredClone(groups));
|
|
229
|
+
};
|
package/src/index.js
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
import http from 'node:http';
|
|
2
|
-
import http2 from 'node:http2';
|
|
3
|
-
import https from 'node:https';
|
|
4
|
-
import config from './config.js';
|
|
5
|
-
import { requestRedirect } from './constants.js';
|
|
6
|
-
import { APPLICATION_OCTET_STREAM } from './mediatypes.js';
|
|
7
|
-
import { preflight } from './preflight.js';
|
|
8
|
-
import { transfer } from './transfer.js';
|
|
9
|
-
import {
|
|
10
|
-
augment,
|
|
11
|
-
cloneWith,
|
|
12
|
-
normalize,
|
|
13
|
-
snoop,
|
|
14
|
-
} from './utils.js';
|
|
15
|
-
import { validation } from './validation.js';
|
|
16
|
-
|
|
17
|
-
export { constants } from 'node:http2';
|
|
18
|
-
export * from './ackn.js';
|
|
19
|
-
export * from './codecs.js';
|
|
20
|
-
export * from './constants.js';
|
|
21
|
-
export * from './cookies.js';
|
|
22
|
-
export * from './errors.js';
|
|
23
|
-
export * from './formdata.js';
|
|
24
|
-
export * as mediatypes from './mediatypes.js';
|
|
25
|
-
export * from './mixin.js';
|
|
26
|
-
export * from './utils.js';
|
|
27
|
-
export * from './validation.js';
|
|
28
|
-
|
|
29
|
-
const {
|
|
30
|
-
HTTP2_HEADER_CONTENT_TYPE,
|
|
31
|
-
} = http2.constants;
|
|
32
|
-
|
|
33
|
-
export default function rekwest(url, options) {
|
|
34
|
-
return transfer(validation(normalize(url, options)));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
Reflect.defineProperty(rekwest, 'defaults', {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
get() { return config.defaults; },
|
|
40
|
-
set(val) { config.defaults = cloneWith(config.defaults, val); },
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
Reflect.defineProperty(rekwest, 'extend', {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
value(options) {
|
|
46
|
-
return (url, opts) => rekwest(url, cloneWith(options, opts));
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
Reflect.defineProperty(rekwest, 'stream', {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
value(url, options) {
|
|
53
|
-
options = preflight(validation(normalize(url, cloneWith({}, options, {
|
|
54
|
-
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
55
|
-
redirect: requestRedirect.manual,
|
|
56
|
-
}))));
|
|
57
|
-
let client, req;
|
|
58
|
-
|
|
59
|
-
if (options.h2) {
|
|
60
|
-
client = http2.connect(options.url.origin, options);
|
|
61
|
-
req = client.request(options.headers, options);
|
|
62
|
-
} else {
|
|
63
|
-
const { request } = options.url.protocol === 'http:' ? http : https;
|
|
64
|
-
|
|
65
|
-
req = request(options.url, options);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
snoop(client, req, options);
|
|
69
|
-
|
|
70
|
-
req.once('response', (res) => {
|
|
71
|
-
let headers;
|
|
72
|
-
|
|
73
|
-
if (options.h2) {
|
|
74
|
-
headers = res;
|
|
75
|
-
res = req;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
augment(res, headers, options);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
return req;
|
|
82
|
-
},
|
|
83
|
-
});
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import http2 from 'node:http2';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import config from './config.js';
|
|
5
|
+
import { requestRedirect } from './constants.js';
|
|
6
|
+
import { APPLICATION_OCTET_STREAM } from './mediatypes.js';
|
|
7
|
+
import { preflight } from './preflight.js';
|
|
8
|
+
import { transfer } from './transfer.js';
|
|
9
|
+
import {
|
|
10
|
+
augment,
|
|
11
|
+
cloneWith,
|
|
12
|
+
normalize,
|
|
13
|
+
snoop,
|
|
14
|
+
} from './utils.js';
|
|
15
|
+
import { validation } from './validation.js';
|
|
16
|
+
|
|
17
|
+
export { constants } from 'node:http2';
|
|
18
|
+
export * from './ackn.js';
|
|
19
|
+
export * from './codecs.js';
|
|
20
|
+
export * from './constants.js';
|
|
21
|
+
export * from './cookies.js';
|
|
22
|
+
export * from './errors.js';
|
|
23
|
+
export * from './formdata.js';
|
|
24
|
+
export * as mediatypes from './mediatypes.js';
|
|
25
|
+
export * from './mixin.js';
|
|
26
|
+
export * from './utils.js';
|
|
27
|
+
export * from './validation.js';
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
31
|
+
} = http2.constants;
|
|
32
|
+
|
|
33
|
+
export default function rekwest(url, options) {
|
|
34
|
+
return transfer(validation(normalize(url, options)));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Reflect.defineProperty(rekwest, 'defaults', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get() { return config.defaults; },
|
|
40
|
+
set(val) { config.defaults = cloneWith(config.defaults, val); },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
Reflect.defineProperty(rekwest, 'extend', {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
value(options) {
|
|
46
|
+
return (url, opts) => rekwest(url, cloneWith(options, opts));
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
Reflect.defineProperty(rekwest, 'stream', {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
value(url, options) {
|
|
53
|
+
options = preflight(validation(normalize(url, cloneWith({}, options, {
|
|
54
|
+
headers: { [HTTP2_HEADER_CONTENT_TYPE]: APPLICATION_OCTET_STREAM },
|
|
55
|
+
redirect: requestRedirect.manual,
|
|
56
|
+
}))));
|
|
57
|
+
let client, req;
|
|
58
|
+
|
|
59
|
+
if (options.h2) {
|
|
60
|
+
client = http2.connect(options.url.origin, options);
|
|
61
|
+
req = client.request(options.headers, options);
|
|
62
|
+
} else {
|
|
63
|
+
const { request } = options.url.protocol === 'http:' ? http : https;
|
|
64
|
+
|
|
65
|
+
req = request(options.url, options);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
snoop(client, req, options);
|
|
69
|
+
|
|
70
|
+
req.once('response', (res) => {
|
|
71
|
+
let headers;
|
|
72
|
+
|
|
73
|
+
if (options.h2) {
|
|
74
|
+
headers = res;
|
|
75
|
+
res = req;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
augment(res, headers, options);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return req;
|
|
82
|
+
},
|
|
83
|
+
});
|
package/src/mediatypes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export const APPLICATION_FORM_URLENCODED = 'application/x-www-form-urlencoded';
|
|
2
|
-
export const APPLICATION_JSON = 'application/json';
|
|
3
|
-
export const APPLICATION_OCTET_STREAM = 'application/octet-stream';
|
|
4
|
-
export const MULTIPART_FORM_DATA = 'multipart/form-data';
|
|
5
|
-
export const TEXT_PLAIN = 'text/plain';
|
|
6
|
-
export const WILDCARD = '*/*';
|
|
1
|
+
export const APPLICATION_FORM_URLENCODED = 'application/x-www-form-urlencoded';
|
|
2
|
+
export const APPLICATION_JSON = 'application/json';
|
|
3
|
+
export const APPLICATION_OCTET_STREAM = 'application/octet-stream';
|
|
4
|
+
export const MULTIPART_FORM_DATA = 'multipart/form-data';
|
|
5
|
+
export const TEXT_PLAIN = 'text/plain';
|
|
6
|
+
export const WILDCARD = '*/*';
|