rekwest 2.4.0 → 3.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 +30 -21
- package/dist/errors.js +8 -4
- package/dist/helpers.js +124 -74
- package/dist/index.js +120 -60
- package/package.json +7 -7
- package/src/ackn.mjs +33 -33
- package/src/cookies.mjs +24 -24
- package/src/errors.mjs +4 -2
- package/src/file.mjs +40 -40
- package/src/formdata.mjs +224 -224
- package/src/helpers.mjs +377 -331
- package/src/index.mjs +105 -62
package/src/ackn.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { connect } from 'tls';
|
|
2
|
-
|
|
3
|
-
export const ackn = (options) => new Promise((resolve, reject) => {
|
|
4
|
-
const { url } = options;
|
|
5
|
-
const socket = connect({
|
|
6
|
-
...options,
|
|
7
|
-
ALPNProtocols: [
|
|
8
|
-
'h2',
|
|
9
|
-
'http/1.1',
|
|
10
|
-
],
|
|
11
|
-
host: url.hostname,
|
|
12
|
-
port: parseInt(url.port) || 443,
|
|
13
|
-
servername: url.hostname,
|
|
14
|
-
}, () => {
|
|
15
|
-
socket.off('error', reject);
|
|
16
|
-
socket.off('timeout', reject);
|
|
17
|
-
|
|
18
|
-
const { alpnProtocol } = socket;
|
|
19
|
-
|
|
20
|
-
resolve({
|
|
21
|
-
...options,
|
|
22
|
-
alpnProtocol,
|
|
23
|
-
createConnection() {
|
|
24
|
-
return socket;
|
|
25
|
-
},
|
|
26
|
-
h2: /h2c?/.test(alpnProtocol),
|
|
27
|
-
protocol: url.protocol,
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
socket.on('error', reject);
|
|
32
|
-
socket.on('timeout', reject);
|
|
33
|
-
});
|
|
1
|
+
import { connect } from 'tls';
|
|
2
|
+
|
|
3
|
+
export const ackn = (options) => new Promise((resolve, reject) => {
|
|
4
|
+
const { url } = options;
|
|
5
|
+
const socket = connect({
|
|
6
|
+
...options,
|
|
7
|
+
ALPNProtocols: [
|
|
8
|
+
'h2',
|
|
9
|
+
'http/1.1',
|
|
10
|
+
],
|
|
11
|
+
host: url.hostname,
|
|
12
|
+
port: parseInt(url.port) || 443,
|
|
13
|
+
servername: url.hostname,
|
|
14
|
+
}, () => {
|
|
15
|
+
socket.off('error', reject);
|
|
16
|
+
socket.off('timeout', reject);
|
|
17
|
+
|
|
18
|
+
const { alpnProtocol } = socket;
|
|
19
|
+
|
|
20
|
+
resolve({
|
|
21
|
+
...options,
|
|
22
|
+
alpnProtocol,
|
|
23
|
+
createConnection() {
|
|
24
|
+
return socket;
|
|
25
|
+
},
|
|
26
|
+
h2: /h2c?/.test(alpnProtocol),
|
|
27
|
+
protocol: url.protocol,
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
socket.on('error', reject);
|
|
32
|
+
socket.on('timeout', reject);
|
|
33
|
+
});
|
package/src/cookies.mjs
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
export class Cookies extends URLSearchParams {
|
|
2
|
-
|
|
3
|
-
static jar = new Map();
|
|
4
|
-
|
|
5
|
-
get [Symbol.toStringTag]() {
|
|
6
|
-
return this.constructor.name;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
constructor(input) {
|
|
10
|
-
if (Array.isArray(input) && input.every((it) => !Array.isArray(it))) {
|
|
11
|
-
input = input.join(';').split(';')
|
|
12
|
-
.filter((it) => !/\b(Domain|Expires|HttpOnly|Max-Age|Path|SameParty|SameSite|Secure)\b/i.test(it))
|
|
13
|
-
.map((it) => it.trim())
|
|
14
|
-
.join('&');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
super(input);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
toString() {
|
|
21
|
-
return super.toString().split('&').join('; ').trim();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
}
|
|
1
|
+
export class Cookies extends URLSearchParams {
|
|
2
|
+
|
|
3
|
+
static jar = new Map();
|
|
4
|
+
|
|
5
|
+
get [Symbol.toStringTag]() {
|
|
6
|
+
return this.constructor.name;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(input) {
|
|
10
|
+
if (Array.isArray(input) && input.every((it) => !Array.isArray(it))) {
|
|
11
|
+
input = input.join(';').split(';')
|
|
12
|
+
.filter((it) => !/\b(Domain|Expires|HttpOnly|Max-Age|Path|SameParty|SameSite|Secure)\b/i.test(it))
|
|
13
|
+
.map((it) => it.trim())
|
|
14
|
+
.join('&');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
super(input);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
toString() {
|
|
21
|
+
return super.toString().split('&').join('; ').trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|
package/src/errors.mjs
CHANGED
|
@@ -8,9 +8,11 @@ export class RequestError extends Error {
|
|
|
8
8
|
return this[Symbol.toStringTag];
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
12
|
-
super(
|
|
11
|
+
constructor(...args) {
|
|
12
|
+
super(...args);
|
|
13
13
|
Error.captureStackTrace(this, this.constructor);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
export class TimeoutError extends RequestError {}
|
package/src/file.mjs
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { Blob } from 'buffer';
|
|
2
|
-
|
|
3
|
-
export { Blob } from 'buffer';
|
|
4
|
-
|
|
5
|
-
export class File extends Blob {
|
|
6
|
-
|
|
7
|
-
static alike(instance) {
|
|
8
|
-
return [
|
|
9
|
-
Blob.name,
|
|
10
|
-
File.name,
|
|
11
|
-
].includes(instance?.constructor.name);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
#lastModified;
|
|
15
|
-
#name;
|
|
16
|
-
|
|
17
|
-
get [Symbol.toStringTag]() {
|
|
18
|
-
return this.constructor.name;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
get lastModified() {
|
|
22
|
-
return this.#lastModified;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get name() {
|
|
26
|
-
return this.#name;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
constructor(bits, name = 'blob', options = {}) {
|
|
30
|
-
const {
|
|
31
|
-
name: filename,
|
|
32
|
-
lastModified = Date.now(),
|
|
33
|
-
} = options;
|
|
34
|
-
|
|
35
|
-
super(bits, options);
|
|
36
|
-
this.#lastModified = lastModified;
|
|
37
|
-
this.#name = filename || name;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}
|
|
1
|
+
import { Blob } from 'buffer';
|
|
2
|
+
|
|
3
|
+
export { Blob } from 'buffer';
|
|
4
|
+
|
|
5
|
+
export class File extends Blob {
|
|
6
|
+
|
|
7
|
+
static alike(instance) {
|
|
8
|
+
return [
|
|
9
|
+
Blob.name,
|
|
10
|
+
File.name,
|
|
11
|
+
].includes(instance?.constructor.name);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#lastModified;
|
|
15
|
+
#name;
|
|
16
|
+
|
|
17
|
+
get [Symbol.toStringTag]() {
|
|
18
|
+
return this.constructor.name;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get lastModified() {
|
|
22
|
+
return this.#lastModified;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get name() {
|
|
26
|
+
return this.#name;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
constructor(bits, name = 'blob', options = {}) {
|
|
30
|
+
const {
|
|
31
|
+
name: filename,
|
|
32
|
+
lastModified = Date.now(),
|
|
33
|
+
} = options;
|
|
34
|
+
|
|
35
|
+
super(bits, options);
|
|
36
|
+
this.#lastModified = lastModified;
|
|
37
|
+
this.#name = filename || name;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
package/src/formdata.mjs
CHANGED
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
import { randomBytes } from 'crypto';
|
|
2
|
-
import http2 from 'http2';
|
|
3
|
-
import { toUSVString } from 'util';
|
|
4
|
-
import { File } from './file.mjs';
|
|
5
|
-
import { tap } from './helpers.mjs';
|
|
6
|
-
import {
|
|
7
|
-
APPLICATION_OCTET_STREAM,
|
|
8
|
-
MULTIPART_FORM_DATA,
|
|
9
|
-
} from './mediatypes.mjs';
|
|
10
|
-
|
|
11
|
-
const CRLF = '\r\n';
|
|
12
|
-
const {
|
|
13
|
-
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
14
|
-
HTTP2_HEADER_CONTENT_TYPE,
|
|
15
|
-
} = http2.constants;
|
|
16
|
-
|
|
17
|
-
export class FormData {
|
|
18
|
-
|
|
19
|
-
static actuate(fd) {
|
|
20
|
-
const boundary = randomBytes(24).toString('hex');
|
|
21
|
-
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
22
|
-
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
23
|
-
|
|
24
|
-
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
25
|
-
const normalize = (value) => value.replace(/\r?\n|\r/g, CRLF);
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
contentType,
|
|
29
|
-
async* [Symbol.asyncIterator]() {
|
|
30
|
-
const encoder = new TextEncoder();
|
|
31
|
-
|
|
32
|
-
for (const [name, value] of fd) {
|
|
33
|
-
if (value.constructor === String) {
|
|
34
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
35
|
-
escape(normalize(name))
|
|
36
|
-
}"${ CRLF.repeat(2) }${ normalize(value) }${ CRLF }`);
|
|
37
|
-
} else {
|
|
38
|
-
yield encoder.encode(`${ prefix }; name="${
|
|
39
|
-
escape(normalize(name))
|
|
40
|
-
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
|
|
41
|
-
HTTP2_HEADER_CONTENT_TYPE
|
|
42
|
-
}: ${
|
|
43
|
-
value.type || APPLICATION_OCTET_STREAM
|
|
44
|
-
}${ CRLF.repeat(2) }`);
|
|
45
|
-
yield* tap(value);
|
|
46
|
-
yield encoder.encode(CRLF);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
yield encoder.encode(`--${ boundary }--`);
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static alike(instance) {
|
|
56
|
-
return instance?.constructor.name === FormData.name;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static #enfoldEntry(name, value, filename) {
|
|
60
|
-
name = toUSVString(name);
|
|
61
|
-
filename &&= toUSVString(filename);
|
|
62
|
-
|
|
63
|
-
if (File.alike(value)) {
|
|
64
|
-
value = new File([value], filename, value);
|
|
65
|
-
} else if (this.#ensureInstance(value)) {
|
|
66
|
-
value.name = filename || value.name;
|
|
67
|
-
} else {
|
|
68
|
-
value = toUSVString(value);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
name,
|
|
73
|
-
value,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
static #ensureInstance(value) {
|
|
78
|
-
return File.alike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
#entries = [];
|
|
82
|
-
|
|
83
|
-
get [Symbol.toStringTag]() {
|
|
84
|
-
return this.constructor.name;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
constructor(input) {
|
|
88
|
-
if (input === Object(input)
|
|
89
|
-
&& (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
|
|
90
|
-
|
|
91
|
-
if (input.constructor !== Object) {
|
|
92
|
-
input = Array.from(input);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (Array.isArray(input)) {
|
|
96
|
-
if (!input.every((it) => Array.isArray(it))) {
|
|
97
|
-
throw new TypeError(`Failed to construct '${
|
|
98
|
-
this[Symbol.toStringTag]
|
|
99
|
-
}': The provided value cannot be converted to a sequence.`);
|
|
100
|
-
} else if (!input.every((it) => it.length === 2)) {
|
|
101
|
-
throw new TypeError(`Failed to construct '${
|
|
102
|
-
this[Symbol.toStringTag]
|
|
103
|
-
}': Sequence initializer must only contain pair elements.`);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (input.constructor === Object) {
|
|
108
|
-
input = Object.entries(input);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
input.forEach(([key, value]) => this.append(key, value));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
#ensureArgs(args, expected, method) {
|
|
116
|
-
if (args.length < expected) {
|
|
117
|
-
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
118
|
-
this[Symbol.toStringTag]
|
|
119
|
-
}': ${ expected } arguments required, but only ${ args.length } present.`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if ([
|
|
123
|
-
'append',
|
|
124
|
-
'set',
|
|
125
|
-
].includes(method)) {
|
|
126
|
-
if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
|
|
127
|
-
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
128
|
-
this[Symbol.toStringTag]
|
|
129
|
-
}': parameter ${ expected } is not of type 'Blob', 'File' or async iterable.`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (method === 'forEach') {
|
|
134
|
-
if (args[0]?.constructor !== Function) {
|
|
135
|
-
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
136
|
-
this[Symbol.toStringTag]
|
|
137
|
-
}': parameter ${ expected } is not of type 'Function'.`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
append(...args) {
|
|
143
|
-
this.#ensureArgs(args, 2, 'append');
|
|
144
|
-
this.#entries.push(this.constructor.#enfoldEntry(...args));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
delete(...args) {
|
|
148
|
-
this.#ensureArgs(args, 1, 'delete');
|
|
149
|
-
const name = toUSVString(args[0]);
|
|
150
|
-
|
|
151
|
-
this.#entries = this.#entries.filter((it) => it.name !== name);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
forEach(...args) {
|
|
155
|
-
this.#ensureArgs(args, 1, 'forEach');
|
|
156
|
-
const [callback, thisArg] = args;
|
|
157
|
-
|
|
158
|
-
for (const entry of this) {
|
|
159
|
-
Reflect.apply(callback, thisArg, [
|
|
160
|
-
...(entry.reverse()),
|
|
161
|
-
this,
|
|
162
|
-
]);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
get(...args) {
|
|
167
|
-
this.#ensureArgs(args, 1, 'get');
|
|
168
|
-
const name = toUSVString(args[0]);
|
|
169
|
-
|
|
170
|
-
return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
getAll(...args) {
|
|
174
|
-
this.#ensureArgs(args, 1, 'getAll');
|
|
175
|
-
const name = toUSVString(args[0]);
|
|
176
|
-
|
|
177
|
-
return this.#entries.filter((it) => it.name === name).map((it) => it.value);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
has(...args) {
|
|
181
|
-
this.#ensureArgs(args, 1, 'has');
|
|
182
|
-
const name = toUSVString(args[0]);
|
|
183
|
-
|
|
184
|
-
return !!this.#entries.find((it) => it.name === name);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
set(...args) {
|
|
188
|
-
this.#ensureArgs(args, 2, 'set');
|
|
189
|
-
const entry = this.constructor.#enfoldEntry(...args);
|
|
190
|
-
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
191
|
-
|
|
192
|
-
if (idx !== -1) {
|
|
193
|
-
this.#entries.splice(idx, 1, entry);
|
|
194
|
-
} else {
|
|
195
|
-
this.#entries.push(entry);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
* entries() {
|
|
200
|
-
for (const { name, value } of this.#entries) {
|
|
201
|
-
yield [
|
|
202
|
-
name,
|
|
203
|
-
value,
|
|
204
|
-
];
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
* keys() {
|
|
209
|
-
for (const [name] of this) {
|
|
210
|
-
yield name;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
* values() {
|
|
215
|
-
for (const [, value] of this) {
|
|
216
|
-
yield value;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
[Symbol.iterator]() {
|
|
221
|
-
return this.entries();
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
}
|
|
1
|
+
import { randomBytes } from 'crypto';
|
|
2
|
+
import http2 from 'http2';
|
|
3
|
+
import { toUSVString } from 'util';
|
|
4
|
+
import { File } from './file.mjs';
|
|
5
|
+
import { tap } from './helpers.mjs';
|
|
6
|
+
import {
|
|
7
|
+
APPLICATION_OCTET_STREAM,
|
|
8
|
+
MULTIPART_FORM_DATA,
|
|
9
|
+
} from './mediatypes.mjs';
|
|
10
|
+
|
|
11
|
+
const CRLF = '\r\n';
|
|
12
|
+
const {
|
|
13
|
+
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
14
|
+
HTTP2_HEADER_CONTENT_TYPE,
|
|
15
|
+
} = http2.constants;
|
|
16
|
+
|
|
17
|
+
export class FormData {
|
|
18
|
+
|
|
19
|
+
static actuate(fd) {
|
|
20
|
+
const boundary = randomBytes(24).toString('hex');
|
|
21
|
+
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
22
|
+
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
23
|
+
|
|
24
|
+
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
25
|
+
const normalize = (value) => value.replace(/\r?\n|\r/g, CRLF);
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
contentType,
|
|
29
|
+
async* [Symbol.asyncIterator]() {
|
|
30
|
+
const encoder = new TextEncoder();
|
|
31
|
+
|
|
32
|
+
for (const [name, value] of fd) {
|
|
33
|
+
if (value.constructor === String) {
|
|
34
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
35
|
+
escape(normalize(name))
|
|
36
|
+
}"${ CRLF.repeat(2) }${ normalize(value) }${ CRLF }`);
|
|
37
|
+
} else {
|
|
38
|
+
yield encoder.encode(`${ prefix }; name="${
|
|
39
|
+
escape(normalize(name))
|
|
40
|
+
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
|
|
41
|
+
HTTP2_HEADER_CONTENT_TYPE
|
|
42
|
+
}: ${
|
|
43
|
+
value.type || APPLICATION_OCTET_STREAM
|
|
44
|
+
}${ CRLF.repeat(2) }`);
|
|
45
|
+
yield* tap(value);
|
|
46
|
+
yield encoder.encode(CRLF);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
yield encoder.encode(`--${ boundary }--`);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static alike(instance) {
|
|
56
|
+
return instance?.constructor.name === FormData.name;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static #enfoldEntry(name, value, filename) {
|
|
60
|
+
name = toUSVString(name);
|
|
61
|
+
filename &&= toUSVString(filename);
|
|
62
|
+
|
|
63
|
+
if (File.alike(value)) {
|
|
64
|
+
value = new File([value], filename, value);
|
|
65
|
+
} else if (this.#ensureInstance(value)) {
|
|
66
|
+
value.name = filename || value.name;
|
|
67
|
+
} else {
|
|
68
|
+
value = toUSVString(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
name,
|
|
73
|
+
value,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static #ensureInstance(value) {
|
|
78
|
+
return File.alike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
#entries = [];
|
|
82
|
+
|
|
83
|
+
get [Symbol.toStringTag]() {
|
|
84
|
+
return this.constructor.name;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
constructor(input) {
|
|
88
|
+
if (input === Object(input)
|
|
89
|
+
&& (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
|
|
90
|
+
|
|
91
|
+
if (input.constructor !== Object) {
|
|
92
|
+
input = Array.from(input);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (Array.isArray(input)) {
|
|
96
|
+
if (!input.every((it) => Array.isArray(it))) {
|
|
97
|
+
throw new TypeError(`Failed to construct '${
|
|
98
|
+
this[Symbol.toStringTag]
|
|
99
|
+
}': The provided value cannot be converted to a sequence.`);
|
|
100
|
+
} else if (!input.every((it) => it.length === 2)) {
|
|
101
|
+
throw new TypeError(`Failed to construct '${
|
|
102
|
+
this[Symbol.toStringTag]
|
|
103
|
+
}': Sequence initializer must only contain pair elements.`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (input.constructor === Object) {
|
|
108
|
+
input = Object.entries(input);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
input.forEach(([key, value]) => this.append(key, value));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#ensureArgs(args, expected, method) {
|
|
116
|
+
if (args.length < expected) {
|
|
117
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
118
|
+
this[Symbol.toStringTag]
|
|
119
|
+
}': ${ expected } arguments required, but only ${ args.length } present.`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if ([
|
|
123
|
+
'append',
|
|
124
|
+
'set',
|
|
125
|
+
].includes(method)) {
|
|
126
|
+
if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
|
|
127
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
128
|
+
this[Symbol.toStringTag]
|
|
129
|
+
}': parameter ${ expected } is not of type 'Blob', 'File' or async iterable.`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (method === 'forEach') {
|
|
134
|
+
if (args[0]?.constructor !== Function) {
|
|
135
|
+
throw new TypeError(`Failed to execute '${ method }' on '${
|
|
136
|
+
this[Symbol.toStringTag]
|
|
137
|
+
}': parameter ${ expected } is not of type 'Function'.`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
append(...args) {
|
|
143
|
+
this.#ensureArgs(args, 2, 'append');
|
|
144
|
+
this.#entries.push(this.constructor.#enfoldEntry(...args));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
delete(...args) {
|
|
148
|
+
this.#ensureArgs(args, 1, 'delete');
|
|
149
|
+
const name = toUSVString(args[0]);
|
|
150
|
+
|
|
151
|
+
this.#entries = this.#entries.filter((it) => it.name !== name);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
forEach(...args) {
|
|
155
|
+
this.#ensureArgs(args, 1, 'forEach');
|
|
156
|
+
const [callback, thisArg] = args;
|
|
157
|
+
|
|
158
|
+
for (const entry of this) {
|
|
159
|
+
Reflect.apply(callback, thisArg, [
|
|
160
|
+
...(entry.reverse()),
|
|
161
|
+
this,
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
get(...args) {
|
|
167
|
+
this.#ensureArgs(args, 1, 'get');
|
|
168
|
+
const name = toUSVString(args[0]);
|
|
169
|
+
|
|
170
|
+
return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getAll(...args) {
|
|
174
|
+
this.#ensureArgs(args, 1, 'getAll');
|
|
175
|
+
const name = toUSVString(args[0]);
|
|
176
|
+
|
|
177
|
+
return this.#entries.filter((it) => it.name === name).map((it) => it.value);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
has(...args) {
|
|
181
|
+
this.#ensureArgs(args, 1, 'has');
|
|
182
|
+
const name = toUSVString(args[0]);
|
|
183
|
+
|
|
184
|
+
return !!this.#entries.find((it) => it.name === name);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
set(...args) {
|
|
188
|
+
this.#ensureArgs(args, 2, 'set');
|
|
189
|
+
const entry = this.constructor.#enfoldEntry(...args);
|
|
190
|
+
const idx = this.#entries.findIndex((it) => it.name === entry.name);
|
|
191
|
+
|
|
192
|
+
if (idx !== -1) {
|
|
193
|
+
this.#entries.splice(idx, 1, entry);
|
|
194
|
+
} else {
|
|
195
|
+
this.#entries.push(entry);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
* entries() {
|
|
200
|
+
for (const { name, value } of this.#entries) {
|
|
201
|
+
yield [
|
|
202
|
+
name,
|
|
203
|
+
value,
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
* keys() {
|
|
209
|
+
for (const [name] of this) {
|
|
210
|
+
yield name;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
* values() {
|
|
215
|
+
for (const [, value] of this) {
|
|
216
|
+
yield value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
[Symbol.iterator]() {
|
|
221
|
+
return this.entries();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
}
|