rekwest 4.4.4 → 4.4.5

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/dist/formdata.js CHANGED
@@ -167,6 +167,7 @@ class FormData {
167
167
  }
168
168
  }
169
169
  [Symbol.iterator]() {
170
+ (0, _utils.brandCheck)(this, FormData);
170
171
  return this.entries();
171
172
  }
172
173
  }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "@babel/preset-env": "^7.22.2",
15
15
  "c8": "^7.13.0",
16
16
  "eslint": "^8.41.0",
17
- "eslint-config-ultra-refined": "^2.14.0",
17
+ "eslint-config-ultra-refined": "^2.15.0",
18
18
  "mocha": "^10.2.0"
19
19
  },
20
20
  "description": "The robust request library that humanity deserves 🌐",
@@ -67,5 +67,5 @@
67
67
  "test:bail": "mocha --bail",
68
68
  "test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
69
69
  },
70
- "version": "4.4.4"
70
+ "version": "4.4.5"
71
71
  }
package/src/formdata.mjs CHANGED
@@ -1,241 +1,243 @@
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
- brandCheck,
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 redress = (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(redress(name))
39
- }"${ CRLF.repeat(2) }${ redress(value) }${ CRLF }`);
40
- } else {
41
- yield encoder.encode(`${ prefix }; name="${
42
- escape(redress(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 new Uint8Array([
50
- 13,
51
- 10,
52
- ]);
53
- }
54
- }
55
-
56
- yield encoder.encode(`--${ boundary }--`);
57
- },
58
- };
59
- }
60
-
61
- static alike(instance) {
62
- return instance?.constructor.name === FormData.name;
63
- }
64
-
65
- static #enfoldEntry(name, value, filename) {
66
- name = toUSVString(name);
67
- filename &&= toUSVString(filename);
68
-
69
- if (File.alike(value)) {
70
- filename ??= value.name || 'blob';
71
- value = new File([value], filename, value);
72
- } else if (this.#ensureInstance(value)) {
73
- value.name = filename;
74
- } else {
75
- value = toUSVString(value);
76
- }
77
-
78
- return {
79
- name,
80
- value,
81
- };
82
- }
83
-
84
- static #ensureInstance(value) {
85
- return File.alike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
86
- }
87
-
88
- #entries = [];
89
-
90
- get [Symbol.toStringTag]() {
91
- return this.constructor.name;
92
- }
93
-
94
- constructor(input) {
95
- if (input === Object(input)
96
- && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
97
-
98
- if (input.constructor !== Object) {
99
- input = Array.from(input);
100
- }
101
-
102
- if (Array.isArray(input)) {
103
- if (!input.every((it) => Array.isArray(it))) {
104
- throw new TypeError(`Failed to construct '${
105
- this[Symbol.toStringTag]
106
- }': The provided value cannot be converted to a sequence.`);
107
- } else if (!input.every((it) => it.length === 2)) {
108
- throw new TypeError(`Failed to construct '${
109
- this[Symbol.toStringTag]
110
- }': Sequence initializer must only contain pair elements.`);
111
- }
112
- }
113
-
114
- if (input.constructor === Object) {
115
- input = Object.entries(input);
116
- }
117
-
118
- input.forEach(([key, value]) => this.append(key, value));
119
- }
120
- }
121
-
122
- #ensureArgs(args, expected, method) {
123
- if (args.length < expected) {
124
- throw new TypeError(`Failed to execute '${ method }' on '${
125
- this[Symbol.toStringTag]
126
- }': ${ expected } arguments required, but only ${ args.length } present.`);
127
- }
128
-
129
- if ([
130
- 'append',
131
- 'set',
132
- ].includes(method)) {
133
- if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
134
- throw new TypeError(`Failed to execute '${ method }' on '${
135
- this[Symbol.toStringTag]
136
- }': parameter ${ expected } is not of type 'Blob'.`);
137
- }
138
- }
139
-
140
- if (method === 'forEach') {
141
- if (args[0]?.constructor !== Function) {
142
- throw new TypeError(`Failed to execute '${ method }' on '${
143
- this[Symbol.toStringTag]
144
- }': parameter ${ expected } is not of type 'Function'.`);
145
- }
146
- }
147
- }
148
-
149
- append(...args) {
150
- brandCheck(this, FormData);
151
- this.#ensureArgs(args, 2, 'append');
152
- this.#entries.push(this.constructor.#enfoldEntry(...args));
153
- }
154
-
155
- delete(...args) {
156
- brandCheck(this, FormData);
157
- this.#ensureArgs(args, 1, 'delete');
158
- const name = toUSVString(args[0]);
159
-
160
- this.#entries = this.#entries.filter((it) => it.name !== name);
161
- }
162
-
163
- forEach(...args) {
164
- brandCheck(this, FormData);
165
- this.#ensureArgs(args, 1, 'forEach');
166
- const [callback, thisArg] = args;
167
-
168
- for (const entry of this) {
169
- Reflect.apply(callback, thisArg, [
170
- ...entry.reverse(),
171
- this,
172
- ]);
173
- }
174
- }
175
-
176
- get(...args) {
177
- brandCheck(this, FormData);
178
- this.#ensureArgs(args, 1, 'get');
179
- const name = toUSVString(args[0]);
180
-
181
- return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
182
- }
183
-
184
- getAll(...args) {
185
- brandCheck(this, FormData);
186
- this.#ensureArgs(args, 1, 'getAll');
187
- const name = toUSVString(args[0]);
188
-
189
- return this.#entries.filter((it) => it.name === name).map((it) => it.value);
190
- }
191
-
192
- has(...args) {
193
- brandCheck(this, FormData);
194
- this.#ensureArgs(args, 1, 'has');
195
- const name = toUSVString(args[0]);
196
-
197
- return !!this.#entries.find((it) => it.name === name);
198
- }
199
-
200
- set(...args) {
201
- brandCheck(this, FormData);
202
- this.#ensureArgs(args, 2, 'set');
203
- const entry = this.constructor.#enfoldEntry(...args);
204
- const idx = this.#entries.findIndex((it) => it.name === entry.name);
205
-
206
- if (idx !== -1) {
207
- this.#entries.splice(idx, 1, entry);
208
- } else {
209
- this.#entries.push(entry);
210
- }
211
- }
212
-
213
- * entries() {
214
- brandCheck(this, FormData);
215
- for (const { name, value } of this.#entries) {
216
- yield [
217
- name,
218
- value,
219
- ];
220
- }
221
- }
222
-
223
- * keys() {
224
- brandCheck(this, FormData);
225
- for (const [name] of this) {
226
- yield name;
227
- }
228
- }
229
-
230
- * values() {
231
- brandCheck(this, FormData);
232
- for (const [, value] of this) {
233
- yield value;
234
- }
235
- }
236
-
237
- [Symbol.iterator]() {
238
- return this.entries();
239
- }
240
-
241
- }
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
+ brandCheck,
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 redress = (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(redress(name))
39
+ }"${ CRLF.repeat(2) }${ redress(value) }${ CRLF }`);
40
+ } else {
41
+ yield encoder.encode(`${ prefix }; name="${
42
+ escape(redress(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 new Uint8Array([
50
+ 13,
51
+ 10,
52
+ ]);
53
+ }
54
+ }
55
+
56
+ yield encoder.encode(`--${ boundary }--`);
57
+ },
58
+ };
59
+ }
60
+
61
+ static alike(instance) {
62
+ return instance?.constructor.name === FormData.name;
63
+ }
64
+
65
+ static #enfoldEntry(name, value, filename) {
66
+ name = toUSVString(name);
67
+ filename &&= toUSVString(filename);
68
+
69
+ if (File.alike(value)) {
70
+ filename ??= value.name || 'blob';
71
+ value = new File([value], filename, value);
72
+ } else if (this.#ensureInstance(value)) {
73
+ value.name = filename;
74
+ } else {
75
+ value = toUSVString(value);
76
+ }
77
+
78
+ return {
79
+ name,
80
+ value,
81
+ };
82
+ }
83
+
84
+ static #ensureInstance(value) {
85
+ return File.alike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
86
+ }
87
+
88
+ #entries = [];
89
+
90
+ get [Symbol.toStringTag]() {
91
+ return this.constructor.name;
92
+ }
93
+
94
+ constructor(input) {
95
+ if (input === Object(input)
96
+ && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
97
+
98
+ if (input.constructor !== Object) {
99
+ input = Array.from(input);
100
+ }
101
+
102
+ if (Array.isArray(input)) {
103
+ if (!input.every((it) => Array.isArray(it))) {
104
+ throw new TypeError(`Failed to construct '${
105
+ this[Symbol.toStringTag]
106
+ }': The provided value cannot be converted to a sequence.`);
107
+ } else if (!input.every((it) => it.length === 2)) {
108
+ throw new TypeError(`Failed to construct '${
109
+ this[Symbol.toStringTag]
110
+ }': Sequence initializer must only contain pair elements.`);
111
+ }
112
+ }
113
+
114
+ if (input.constructor === Object) {
115
+ input = Object.entries(input);
116
+ }
117
+
118
+ input.forEach(([key, value]) => this.append(key, value));
119
+ }
120
+ }
121
+
122
+ #ensureArgs(args, expected, method) {
123
+ if (args.length < expected) {
124
+ throw new TypeError(`Failed to execute '${ method }' on '${
125
+ this[Symbol.toStringTag]
126
+ }': ${ expected } arguments required, but only ${ args.length } present.`);
127
+ }
128
+
129
+ if ([
130
+ 'append',
131
+ 'set',
132
+ ].includes(method)) {
133
+ if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
134
+ throw new TypeError(`Failed to execute '${ method }' on '${
135
+ this[Symbol.toStringTag]
136
+ }': parameter ${ expected } is not of type 'Blob'.`);
137
+ }
138
+ }
139
+
140
+ if (method === 'forEach') {
141
+ if (args[0]?.constructor !== Function) {
142
+ throw new TypeError(`Failed to execute '${ method }' on '${
143
+ this[Symbol.toStringTag]
144
+ }': parameter ${ expected } is not of type 'Function'.`);
145
+ }
146
+ }
147
+ }
148
+
149
+ append(...args) {
150
+ brandCheck(this, FormData);
151
+ this.#ensureArgs(args, 2, 'append');
152
+ this.#entries.push(this.constructor.#enfoldEntry(...args));
153
+ }
154
+
155
+ delete(...args) {
156
+ brandCheck(this, FormData);
157
+ this.#ensureArgs(args, 1, 'delete');
158
+ const name = toUSVString(args[0]);
159
+
160
+ this.#entries = this.#entries.filter((it) => it.name !== name);
161
+ }
162
+
163
+ forEach(...args) {
164
+ brandCheck(this, FormData);
165
+ this.#ensureArgs(args, 1, 'forEach');
166
+ const [callback, thisArg] = args;
167
+
168
+ for (const entry of this) {
169
+ Reflect.apply(callback, thisArg, [
170
+ ...entry.reverse(),
171
+ this,
172
+ ]);
173
+ }
174
+ }
175
+
176
+ get(...args) {
177
+ brandCheck(this, FormData);
178
+ this.#ensureArgs(args, 1, 'get');
179
+ const name = toUSVString(args[0]);
180
+
181
+ return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
182
+ }
183
+
184
+ getAll(...args) {
185
+ brandCheck(this, FormData);
186
+ this.#ensureArgs(args, 1, 'getAll');
187
+ const name = toUSVString(args[0]);
188
+
189
+ return this.#entries.filter((it) => it.name === name).map((it) => it.value);
190
+ }
191
+
192
+ has(...args) {
193
+ brandCheck(this, FormData);
194
+ this.#ensureArgs(args, 1, 'has');
195
+ const name = toUSVString(args[0]);
196
+
197
+ return !!this.#entries.find((it) => it.name === name);
198
+ }
199
+
200
+ set(...args) {
201
+ brandCheck(this, FormData);
202
+ this.#ensureArgs(args, 2, 'set');
203
+ const entry = this.constructor.#enfoldEntry(...args);
204
+ const idx = this.#entries.findIndex((it) => it.name === entry.name);
205
+
206
+ if (idx !== -1) {
207
+ this.#entries.splice(idx, 1, entry);
208
+ } else {
209
+ this.#entries.push(entry);
210
+ }
211
+ }
212
+
213
+ * entries() {
214
+ brandCheck(this, FormData);
215
+ for (const { name, value } of this.#entries) {
216
+ yield [
217
+ name,
218
+ value,
219
+ ];
220
+ }
221
+ }
222
+
223
+ * keys() {
224
+ brandCheck(this, FormData);
225
+ for (const [name] of this) {
226
+ yield name;
227
+ }
228
+ }
229
+
230
+ * values() {
231
+ brandCheck(this, FormData);
232
+ for (const [, value] of this) {
233
+ yield value;
234
+ }
235
+ }
236
+
237
+ [Symbol.iterator]() {
238
+ brandCheck(this, FormData);
239
+
240
+ return this.entries();
241
+ }
242
+
243
+ }