rekwest 5.2.8 → 5.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 CHANGED
@@ -161,6 +161,7 @@ console.log(res.body);
161
161
  * `arrayBuffer` **{AsyncFunction}** Reads the response and returns **ArrayBuffer**
162
162
  * `blob` **{AsyncFunction}** Reads the response and returns **Blob**
163
163
  * `body` **{AsyncFunction}** Reads the response and returns **Buffer** if `parse: false`
164
+ * `bytes` **{AsyncFunction}** Reads the response and returns **Uint8Array**
164
165
  * `json` **{AsyncFunction}** Reads the response and returns **Object**
165
166
  * `text` **{AsyncFunction}** Reads the response and returns **String**
166
167
  * `bodyUsed` **{boolean}** Indicates whether the response were read or not
package/dist/mixin.js CHANGED
@@ -39,6 +39,13 @@ const mixin = (res, {
39
39
  return new _nodeBuffer.Blob([val]);
40
40
  }
41
41
  },
42
+ bytes: {
43
+ enumerable: true,
44
+ value: async function () {
45
+ (0, _utils.brandCheck)(this, res?.constructor);
46
+ return new Uint8Array(await this.arrayBuffer());
47
+ }
48
+ },
42
49
  json: {
43
50
  enumerable: true,
44
51
  value: async function () {
package/package.json CHANGED
@@ -8,15 +8,15 @@
8
8
  "url": "https://github.com/bricss/rekwest/issues"
9
9
  },
10
10
  "devDependencies": {
11
- "@babel/cli": "^7.25.9",
12
- "@babel/core": "^7.25.9",
11
+ "@babel/cli": "^7.26.4",
12
+ "@babel/core": "^7.26.0",
13
13
  "@babel/eslint-parser": "^7.25.9",
14
- "@babel/preset-env": "^7.25.9",
15
- "@stylistic/eslint-plugin-js": "^2.9.0",
16
- "c8": "^10.1.2",
17
- "eslint": "^9.13.0",
18
- "eslint-config-ultra-refined": "^3.2.7",
19
- "mocha": "^10.7.3"
14
+ "@babel/preset-env": "^7.26.0",
15
+ "@stylistic/eslint-plugin-js": "^2.12.1",
16
+ "c8": "^10.1.3",
17
+ "eslint": "^9.17.0",
18
+ "eslint-config-ultra-refined": "^3.3.1",
19
+ "mocha": "^11.0.1"
20
20
  },
21
21
  "description": "The robust request library that humanity deserves 🌐",
22
22
  "engines": {
@@ -70,5 +70,5 @@
70
70
  "test:bail": "mocha --bail",
71
71
  "test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
72
72
  },
73
- "version": "5.2.8"
73
+ "version": "5.3.1"
74
74
  }
package/src/formdata.mjs CHANGED
@@ -1,244 +1,246 @@
1
- import { File } from 'node:buffer';
2
- import { randomBytes } from 'node:crypto';
3
- import http2 from 'node:http2';
4
- import { toUSVString } from 'node:util';
5
- import {
6
- APPLICATION_OCTET_STREAM,
7
- MULTIPART_FORM_DATA,
8
- } from './mediatypes.mjs';
9
- import {
10
- brandCheck,
11
- isFileLike,
12
- tap,
13
- } from './utils.mjs';
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 actuate(fd) {
24
- const boundary = randomBytes(24).toString('hex');
25
- const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
26
- const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
27
-
28
- const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
29
- const redress = (str) => str.replace(/\r?\n|\r/g, CRLF);
30
-
31
- return {
32
- contentType,
33
- async* [Symbol.asyncIterator]() {
34
- const encoder = new TextEncoder();
35
-
36
- for (const [name, value] of fd) {
37
- if (value.constructor === String) {
38
- yield encoder.encode(`${ prefix }; name="${
39
- escape(redress(name))
40
- }"${ CRLF.repeat(2) }${ redress(value) }${ CRLF }`);
41
- } else {
42
- yield encoder.encode(`${ prefix }; name="${
43
- escape(redress(name))
44
- }"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
45
- HTTP2_HEADER_CONTENT_TYPE
46
- }: ${
47
- value.type || APPLICATION_OCTET_STREAM
48
- }${ CRLF.repeat(2) }`);
49
- yield* tap(value);
50
- yield new Uint8Array([
51
- 13,
52
- 10,
53
- ]);
54
- }
55
- }
56
-
57
- yield encoder.encode(`--${ boundary }--`);
58
- },
59
- };
60
- }
61
-
62
- static alike(instance) {
63
- return FormData.name === instance?.[Symbol.toStringTag];
64
- }
65
-
66
- static #enfoldEntry(name, value, filename) {
67
- name = toUSVString(name);
68
- filename &&= toUSVString(filename);
69
-
70
- if (isFileLike(value)) {
71
- filename ??= value.name || 'blob';
72
- value = new File([value], filename, value);
73
- } else if (this.#ensureInstance(value)) {
74
- value.name = filename;
75
- } else {
76
- value = toUSVString(value);
77
- }
78
-
79
- return {
80
- name,
81
- value,
82
- };
83
- }
84
-
85
- static #ensureInstance(value) {
86
- return isFileLike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
87
- }
88
-
89
- #entries = [];
90
-
91
- get [Symbol.toStringTag]() {
92
- return this.constructor.name;
93
- }
94
-
95
- constructor(input) {
96
- if (input === Object(input)
97
- && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
98
-
99
- if (input.constructor !== Object) {
100
- input = Array.from(input);
101
- }
102
-
103
- if (Array.isArray(input)) {
104
- if (!input.every((it) => Array.isArray(it))) {
105
- throw new TypeError(`Failed to construct '${
106
- this[Symbol.toStringTag]
107
- }': The provided value cannot be converted to a sequence.`);
108
- } else if (!input.every((it) => it.length === 2)) {
109
- throw new TypeError(`Failed to construct '${
110
- this[Symbol.toStringTag]
111
- }': Sequence initializer must only contain pair elements.`);
112
- }
113
- }
114
-
115
- if (input.constructor === Object) {
116
- input = Object.entries(input);
117
- }
118
-
119
- for (const [key, value] of input) { this.append(key, value); }
120
- }
121
- }
122
-
123
- #ensureArgs(args, expected, method) {
124
- if (args.length < expected) {
125
- throw new TypeError(`Failed to execute '${ method }' on '${
126
- this[Symbol.toStringTag]
127
- }': ${ expected } arguments required, but only ${ args.length } present.`);
128
- }
129
-
130
- if ([
131
- 'append',
132
- 'set',
133
- ].includes(method)) {
134
- if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
135
- throw new TypeError(`Failed to execute '${ method }' on '${
136
- this[Symbol.toStringTag]
137
- }': parameter ${ expected } is not of type 'Blob'.`);
138
- }
139
- }
140
-
141
- if (method === 'forEach') {
142
- if (args[0]?.constructor !== Function) {
143
- throw new TypeError(`Failed to execute '${ method }' on '${
144
- this[Symbol.toStringTag]
145
- }': parameter ${ expected } is not of type 'Function'.`);
146
- }
147
- }
148
- }
149
-
150
- append(...args) {
151
- brandCheck(this, FormData);
152
- this.#ensureArgs(args, 2, 'append');
153
- this.#entries.push(this.constructor.#enfoldEntry(...args));
154
- }
155
-
156
- delete(...args) {
157
- brandCheck(this, FormData);
158
- this.#ensureArgs(args, 1, 'delete');
159
- const name = toUSVString(args[0]);
160
-
161
- this.#entries = this.#entries.filter((it) => it.name !== name);
162
- }
163
-
164
- forEach(...args) {
165
- brandCheck(this, FormData);
166
- this.#ensureArgs(args, 1, 'forEach');
167
- const [callback, thisArg] = args;
168
-
169
- for (const entry of this) {
170
- Reflect.apply(callback, thisArg, [
171
- ...entry.reverse(),
172
- this,
173
- ]);
174
- }
175
- }
176
-
177
- get(...args) {
178
- brandCheck(this, FormData);
179
- this.#ensureArgs(args, 1, 'get');
180
- const name = toUSVString(args[0]);
181
-
182
- return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
183
- }
184
-
185
- getAll(...args) {
186
- brandCheck(this, FormData);
187
- this.#ensureArgs(args, 1, 'getAll');
188
- const name = toUSVString(args[0]);
189
-
190
- return this.#entries.filter((it) => it.name === name).map((it) => it.value);
191
- }
192
-
193
- has(...args) {
194
- brandCheck(this, FormData);
195
- this.#ensureArgs(args, 1, 'has');
196
- const name = toUSVString(args[0]);
197
-
198
- return !!this.#entries.find((it) => it.name === name);
199
- }
200
-
201
- set(...args) {
202
- brandCheck(this, FormData);
203
- this.#ensureArgs(args, 2, 'set');
204
- const entry = this.constructor.#enfoldEntry(...args);
205
- const idx = this.#entries.findIndex((it) => it.name === entry.name);
206
-
207
- if (idx !== -1) {
208
- this.#entries.splice(idx, 1, entry);
209
- } else {
210
- this.#entries.push(entry);
211
- }
212
- }
213
-
214
- * entries() {
215
- brandCheck(this, FormData);
216
- for (const { name, value } of this.#entries) {
217
- yield [
218
- name,
219
- value,
220
- ];
221
- }
222
- }
223
-
224
- * keys() {
225
- brandCheck(this, FormData);
226
- for (const [name] of this) {
227
- yield name;
228
- }
229
- }
230
-
231
- * values() {
232
- brandCheck(this, FormData);
233
- for (const [, value] of this) {
234
- yield value;
235
- }
236
- }
237
-
238
- [Symbol.iterator]() {
239
- brandCheck(this, FormData);
240
-
241
- return this.entries();
242
- }
243
-
244
- }
1
+ import { File } from 'node:buffer';
2
+ import { randomBytes } from 'node:crypto';
3
+ import http2 from 'node:http2';
4
+ import { toUSVString } from 'node:util';
5
+ import {
6
+ APPLICATION_OCTET_STREAM,
7
+ MULTIPART_FORM_DATA,
8
+ } from './mediatypes.mjs';
9
+ import {
10
+ brandCheck,
11
+ isFileLike,
12
+ tap,
13
+ } from './utils.mjs';
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 actuate(fd) {
24
+ const boundary = randomBytes(24).toString('hex');
25
+ const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
26
+ const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
27
+
28
+ const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
29
+ const redress = (str) => str.replace(/\r?\n|\r/g, CRLF);
30
+
31
+ return {
32
+ contentType,
33
+ async* [Symbol.asyncIterator]() {
34
+ const encoder = new TextEncoder();
35
+
36
+ for (const [name, value] of fd) {
37
+ if (value.constructor === String) {
38
+ yield encoder.encode(`${ prefix }; name="${
39
+ escape(redress(name))
40
+ }"${ CRLF.repeat(2) }${ redress(value) }${ CRLF }`);
41
+ } else {
42
+ yield encoder.encode(`${ prefix }; name="${
43
+ escape(redress(name))
44
+ }"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
45
+ HTTP2_HEADER_CONTENT_TYPE
46
+ }: ${
47
+ value.type || APPLICATION_OCTET_STREAM
48
+ }${ CRLF.repeat(2) }`);
49
+ yield* tap(value);
50
+ yield new Uint8Array([
51
+ 13,
52
+ 10,
53
+ ]);
54
+ }
55
+ }
56
+
57
+ yield encoder.encode(`--${ boundary }--`);
58
+ },
59
+ };
60
+ }
61
+
62
+ static alike(instance) {
63
+ return FormData.name === instance?.[Symbol.toStringTag];
64
+ }
65
+
66
+ static #enfoldEntry(name, value, filename) {
67
+ name = toUSVString(name);
68
+ filename &&= toUSVString(filename);
69
+
70
+ if (isFileLike(value)) {
71
+ filename ??= value.name || 'blob';
72
+ value = new File([value], filename, value);
73
+ } else if (this.#ensureInstance(value)) {
74
+ value.name = filename;
75
+ } else {
76
+ value = toUSVString(value);
77
+ }
78
+
79
+ return {
80
+ name,
81
+ value,
82
+ };
83
+ }
84
+
85
+ static #ensureInstance(value) {
86
+ return isFileLike(value) || (value === Object(value) && Reflect.has(value, Symbol.asyncIterator));
87
+ }
88
+
89
+ #entries = [];
90
+
91
+ get [Symbol.toStringTag]() {
92
+ return this.constructor.name;
93
+ }
94
+
95
+ constructor(input) {
96
+ if (input === Object(input)
97
+ && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
98
+
99
+ if (input.constructor !== Object) {
100
+ input = Array.from(input);
101
+ }
102
+
103
+ if (Array.isArray(input)) {
104
+ if (!input.every((it) => Array.isArray(it))) {
105
+ throw new TypeError(`Failed to construct '${
106
+ this[Symbol.toStringTag]
107
+ }': The provided value cannot be converted to a sequence.`);
108
+ } else if (!input.every((it) => it.length === 2)) {
109
+ throw new TypeError(`Failed to construct '${
110
+ this[Symbol.toStringTag]
111
+ }': Sequence initializer must only contain pair elements.`);
112
+ }
113
+ }
114
+
115
+ if (input.constructor === Object) {
116
+ input = Object.entries(input);
117
+ }
118
+
119
+ for (const [key, value] of input) {
120
+ this.append(key, value);
121
+ }
122
+ }
123
+ }
124
+
125
+ #ensureArgs(args, expected, method) {
126
+ if (args.length < expected) {
127
+ throw new TypeError(`Failed to execute '${ method }' on '${
128
+ this[Symbol.toStringTag]
129
+ }': ${ expected } arguments required, but only ${ args.length } present.`);
130
+ }
131
+
132
+ if ([
133
+ 'append',
134
+ 'set',
135
+ ].includes(method)) {
136
+ if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
137
+ throw new TypeError(`Failed to execute '${ method }' on '${
138
+ this[Symbol.toStringTag]
139
+ }': parameter ${ expected } is not of type 'Blob'.`);
140
+ }
141
+ }
142
+
143
+ if (method === 'forEach') {
144
+ if (args[0]?.constructor !== Function) {
145
+ throw new TypeError(`Failed to execute '${ method }' on '${
146
+ this[Symbol.toStringTag]
147
+ }': parameter ${ expected } is not of type 'Function'.`);
148
+ }
149
+ }
150
+ }
151
+
152
+ append(...args) {
153
+ brandCheck(this, FormData);
154
+ this.#ensureArgs(args, 2, 'append');
155
+ this.#entries.push(this.constructor.#enfoldEntry(...args));
156
+ }
157
+
158
+ delete(...args) {
159
+ brandCheck(this, FormData);
160
+ this.#ensureArgs(args, 1, 'delete');
161
+ const name = toUSVString(args[0]);
162
+
163
+ this.#entries = this.#entries.filter((it) => it.name !== name);
164
+ }
165
+
166
+ forEach(...args) {
167
+ brandCheck(this, FormData);
168
+ this.#ensureArgs(args, 1, 'forEach');
169
+ const [callback, thisArg] = args;
170
+
171
+ for (const entry of this) {
172
+ Reflect.apply(callback, thisArg, [
173
+ ...entry.reverse(),
174
+ this,
175
+ ]);
176
+ }
177
+ }
178
+
179
+ get(...args) {
180
+ brandCheck(this, FormData);
181
+ this.#ensureArgs(args, 1, 'get');
182
+ const name = toUSVString(args[0]);
183
+
184
+ return (this.#entries.find((it) => it.name === name) ?? {}).value ?? null;
185
+ }
186
+
187
+ getAll(...args) {
188
+ brandCheck(this, FormData);
189
+ this.#ensureArgs(args, 1, 'getAll');
190
+ const name = toUSVString(args[0]);
191
+
192
+ return this.#entries.filter((it) => it.name === name).map((it) => it.value);
193
+ }
194
+
195
+ has(...args) {
196
+ brandCheck(this, FormData);
197
+ this.#ensureArgs(args, 1, 'has');
198
+ const name = toUSVString(args[0]);
199
+
200
+ return !!this.#entries.find((it) => it.name === name);
201
+ }
202
+
203
+ set(...args) {
204
+ brandCheck(this, FormData);
205
+ this.#ensureArgs(args, 2, 'set');
206
+ const entry = this.constructor.#enfoldEntry(...args);
207
+ const idx = this.#entries.findIndex((it) => it.name === entry.name);
208
+
209
+ if (idx !== -1) {
210
+ this.#entries.splice(idx, 1, entry);
211
+ } else {
212
+ this.#entries.push(entry);
213
+ }
214
+ }
215
+
216
+ * entries() {
217
+ brandCheck(this, FormData);
218
+ for (const { name, value } of this.#entries) {
219
+ yield [
220
+ name,
221
+ value,
222
+ ];
223
+ }
224
+ }
225
+
226
+ * keys() {
227
+ brandCheck(this, FormData);
228
+ for (const [name] of this) {
229
+ yield name;
230
+ }
231
+ }
232
+
233
+ * values() {
234
+ brandCheck(this, FormData);
235
+ for (const [, value] of this) {
236
+ yield value;
237
+ }
238
+ }
239
+
240
+ [Symbol.iterator]() {
241
+ brandCheck(this, FormData);
242
+
243
+ return this.entries();
244
+ }
245
+
246
+ }
package/src/mixin.mjs CHANGED
@@ -32,6 +32,14 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
32
32
  return new Blob([val]);
33
33
  },
34
34
  },
35
+ bytes: {
36
+ enumerable: true,
37
+ value: async function () {
38
+ brandCheck(this, res?.constructor);
39
+
40
+ return new Uint8Array(await this.arrayBuffer());
41
+ },
42
+ },
35
43
  json: {
36
44
  enumerable: true,
37
45
  value: async function () {