rekwest 2.3.0 → 2.3.4
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 +6 -5
- package/dist/helpers.js +16 -21
- package/package.json +13 -9
- package/src/formdata.mjs +7 -6
- package/src/helpers.mjs +20 -26
package/dist/formdata.js
CHANGED
|
@@ -17,6 +17,7 @@ var _mediatypes = require("./mediatypes.mjs");
|
|
|
17
17
|
|
|
18
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
19
|
|
|
20
|
+
const CRLF = '\r\n';
|
|
20
21
|
const {
|
|
21
22
|
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
22
23
|
HTTP2_HEADER_CONTENT_TYPE
|
|
@@ -26,11 +27,11 @@ class FormData {
|
|
|
26
27
|
static actuate(fd) {
|
|
27
28
|
const boundary = (0, _crypto.randomBytes)(24).toString('hex');
|
|
28
29
|
const contentType = `${_mediatypes.MULTIPART_FORM_DATA}; boundary=${boundary}`;
|
|
29
|
-
const prefix = `--${boundary}
|
|
30
|
+
const prefix = `--${boundary}${CRLF}${HTTP2_HEADER_CONTENT_DISPOSITION}: form-data`;
|
|
30
31
|
|
|
31
32
|
const escape = str => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
32
33
|
|
|
33
|
-
const normalize = value => value.replace(/\r?\n|\r/g,
|
|
34
|
+
const normalize = value => value.replace(/\r?\n|\r/g, CRLF);
|
|
34
35
|
|
|
35
36
|
return {
|
|
36
37
|
contentType,
|
|
@@ -40,11 +41,11 @@ class FormData {
|
|
|
40
41
|
|
|
41
42
|
for (const [name, value] of fd) {
|
|
42
43
|
if (value.constructor === String) {
|
|
43
|
-
yield encoder.encode(`${prefix}; name="${escape(normalize(name))}"
|
|
44
|
+
yield encoder.encode(`${prefix}; name="${escape(normalize(name))}"${CRLF.repeat(2)}${normalize(value)}${CRLF}`);
|
|
44
45
|
} else {
|
|
45
|
-
yield encoder.encode(`${prefix}; name="${escape(normalize(name))}"${value.name ? `; filename="${escape(value.name)}"` : ''}
|
|
46
|
+
yield encoder.encode(`${prefix}; name="${escape(normalize(name))}"${value.name ? `; filename="${escape(value.name)}"` : ''}${CRLF}${HTTP2_HEADER_CONTENT_TYPE}: ${value.type || _mediatypes.APPLICATION_OCTET_STREAM}${CRLF.repeat(2)}`);
|
|
46
47
|
yield* (0, _helpers.tap)(value);
|
|
47
|
-
yield encoder.encode(
|
|
48
|
+
yield encoder.encode(CRLF);
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
|
package/dist/helpers.js
CHANGED
|
@@ -51,7 +51,7 @@ const inflate = (0, _util.promisify)(_zlib.default.inflate);
|
|
|
51
51
|
const compress = (buf, encoding, {
|
|
52
52
|
async = false
|
|
53
53
|
} = {}) => {
|
|
54
|
-
encoding &&= encoding.match(
|
|
54
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
55
55
|
const compressor = {
|
|
56
56
|
br: async ? brotliCompress : _zlib.default.brotliCompressSync,
|
|
57
57
|
deflate: async ? deflate : _zlib.default.deflateSync,
|
|
@@ -65,7 +65,7 @@ exports.compress = compress;
|
|
|
65
65
|
const decompress = (buf, encoding, {
|
|
66
66
|
async = false
|
|
67
67
|
} = {}) => {
|
|
68
|
-
encoding &&= encoding.match(
|
|
68
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
69
69
|
const decompressor = {
|
|
70
70
|
br: async ? brotliDecompress : _zlib.default.brotliDecompressSync,
|
|
71
71
|
deflate: async ? inflate : _zlib.default.inflateSync,
|
|
@@ -199,36 +199,31 @@ const premix = (res, {
|
|
|
199
199
|
Object.defineProperties(res, {
|
|
200
200
|
arrayBuffer: {
|
|
201
201
|
enumerable: true,
|
|
202
|
-
value:
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const {
|
|
202
|
+
value: function () {
|
|
203
|
+
parse &&= false;
|
|
204
|
+
return this.body().then(({
|
|
206
205
|
buffer,
|
|
207
206
|
byteLength,
|
|
208
207
|
byteOffset
|
|
209
|
-
}
|
|
210
|
-
return buffer.slice(byteOffset, byteOffset + byteLength);
|
|
208
|
+
}) => buffer.slice(byteOffset, byteOffset + byteLength));
|
|
211
209
|
}
|
|
212
210
|
},
|
|
213
211
|
blob: {
|
|
214
212
|
enumerable: true,
|
|
215
|
-
value:
|
|
216
|
-
|
|
217
|
-
return new _buffer.Blob([val]);
|
|
213
|
+
value: function () {
|
|
214
|
+
return this.arrayBuffer().then(res => new _buffer.Blob([res]));
|
|
218
215
|
}
|
|
219
216
|
},
|
|
220
217
|
json: {
|
|
221
218
|
enumerable: true,
|
|
222
|
-
value:
|
|
223
|
-
|
|
224
|
-
return JSON.parse(val);
|
|
219
|
+
value: function () {
|
|
220
|
+
return this.text().then(res => JSON.parse(res));
|
|
225
221
|
}
|
|
226
222
|
},
|
|
227
223
|
text: {
|
|
228
224
|
enumerable: true,
|
|
229
|
-
value:
|
|
230
|
-
|
|
231
|
-
return val.toString();
|
|
225
|
+
value: function () {
|
|
226
|
+
return this.blob().then(blob => blob.text());
|
|
232
227
|
}
|
|
233
228
|
}
|
|
234
229
|
});
|
|
@@ -258,12 +253,12 @@ const premix = (res, {
|
|
|
258
253
|
|
|
259
254
|
if (spool.length && parse) {
|
|
260
255
|
const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
|
|
261
|
-
const charset = contentType.split(';').find(it => /charset=/i.test(it))?.toLowerCase()
|
|
256
|
+
const charset = contentType.split(';').find(it => /charset=/i.test(it))?.toLowerCase().replace('charset=', '').replace('iso-8859-1', 'latin1').trim() || 'utf-8';
|
|
262
257
|
|
|
263
|
-
if (/
|
|
258
|
+
if (/\bjson\b/i.test(contentType)) {
|
|
264
259
|
spool = JSON.parse(spool.toString(charset));
|
|
265
|
-
} else if (
|
|
266
|
-
if (
|
|
260
|
+
} else if (/\b(text|xml)\b/i.test(contentType)) {
|
|
261
|
+
if (/\b(latin1|ucs-2|utf-(8|16le))\b/.test(charset)) {
|
|
267
262
|
spool = spool.toString(charset);
|
|
268
263
|
} else {
|
|
269
264
|
spool = new TextDecoder(charset).decode(spool);
|
package/package.json
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
"url": "https://github.com/bricss/rekwest/issues"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@babel/cli": "^7.16.
|
|
12
|
-
"@babel/core": "^7.16.
|
|
13
|
-
"@babel/eslint-parser": "^7.16.
|
|
14
|
-
"@babel/preset-env": "^7.16.
|
|
15
|
-
"c8": "^7.
|
|
16
|
-
"eslint": "^8.
|
|
17
|
-
"eslint-config-ultra-refined": "^2.
|
|
18
|
-
"mocha": "^9.
|
|
11
|
+
"@babel/cli": "^7.16.8",
|
|
12
|
+
"@babel/core": "^7.16.12",
|
|
13
|
+
"@babel/eslint-parser": "^7.16.5",
|
|
14
|
+
"@babel/preset-env": "^7.16.11",
|
|
15
|
+
"c8": "^7.11.0",
|
|
16
|
+
"eslint": "^8.7.0",
|
|
17
|
+
"eslint-config-ultra-refined": "^2.3.0",
|
|
18
|
+
"mocha": "^9.2.0"
|
|
19
19
|
},
|
|
20
20
|
"description": "The robust request library that humanity deserves 🌐",
|
|
21
21
|
"engines": {
|
|
@@ -33,9 +33,13 @@
|
|
|
33
33
|
"keywords": [
|
|
34
34
|
"fetch",
|
|
35
35
|
"fetch-alike",
|
|
36
|
+
"formdata",
|
|
36
37
|
"http",
|
|
37
38
|
"https",
|
|
39
|
+
"h2",
|
|
40
|
+
"h2c",
|
|
38
41
|
"http2",
|
|
42
|
+
"multipart",
|
|
39
43
|
"request"
|
|
40
44
|
],
|
|
41
45
|
"license": "MIT",
|
|
@@ -55,5 +59,5 @@
|
|
|
55
59
|
"test": "mocha --exit --recursive",
|
|
56
60
|
"test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
|
|
57
61
|
},
|
|
58
|
-
"version": "2.3.
|
|
62
|
+
"version": "2.3.4"
|
|
59
63
|
}
|
package/src/formdata.mjs
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
MULTIPART_FORM_DATA,
|
|
9
9
|
} from './mediatypes.mjs';
|
|
10
10
|
|
|
11
|
+
const CRLF = '\r\n';
|
|
11
12
|
const {
|
|
12
13
|
HTTP2_HEADER_CONTENT_DISPOSITION,
|
|
13
14
|
HTTP2_HEADER_CONTENT_TYPE,
|
|
@@ -18,10 +19,10 @@ export class FormData {
|
|
|
18
19
|
static actuate(fd) {
|
|
19
20
|
const boundary = randomBytes(24).toString('hex');
|
|
20
21
|
const contentType = `${ MULTIPART_FORM_DATA }; boundary=${ boundary }`;
|
|
21
|
-
const prefix = `--${ boundary }
|
|
22
|
+
const prefix = `--${ boundary }${ CRLF }${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
|
|
22
23
|
|
|
23
24
|
const escape = (str) => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
|
|
24
|
-
const normalize = (value) => value.replace(/\r?\n|\r/g,
|
|
25
|
+
const normalize = (value) => value.replace(/\r?\n|\r/g, CRLF);
|
|
25
26
|
|
|
26
27
|
return {
|
|
27
28
|
contentType,
|
|
@@ -32,17 +33,17 @@ export class FormData {
|
|
|
32
33
|
if (value.constructor === String) {
|
|
33
34
|
yield encoder.encode(`${ prefix }; name="${
|
|
34
35
|
escape(normalize(name))
|
|
35
|
-
}"
|
|
36
|
+
}"${ CRLF.repeat(2) }${ normalize(value) }${ CRLF }`);
|
|
36
37
|
} else {
|
|
37
38
|
yield encoder.encode(`${ prefix }; name="${
|
|
38
39
|
escape(normalize(name))
|
|
39
|
-
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }
|
|
40
|
+
}"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
|
|
40
41
|
HTTP2_HEADER_CONTENT_TYPE
|
|
41
42
|
}: ${
|
|
42
43
|
value.type || APPLICATION_OCTET_STREAM
|
|
43
|
-
}
|
|
44
|
+
}${ CRLF.repeat(2) }`);
|
|
44
45
|
yield* tap(value);
|
|
45
|
-
yield encoder.encode(
|
|
46
|
+
yield encoder.encode(CRLF);
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
|
package/src/helpers.mjs
CHANGED
|
@@ -44,7 +44,7 @@ const deflate = promisify(zlib.deflate);
|
|
|
44
44
|
const inflate = promisify(zlib.inflate);
|
|
45
45
|
|
|
46
46
|
export const compress = (buf, encoding, { async = false } = {}) => {
|
|
47
|
-
encoding &&= encoding.match(
|
|
47
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
48
48
|
const compressor = {
|
|
49
49
|
br: async ? brotliCompress : zlib.brotliCompressSync,
|
|
50
50
|
deflate: async ? deflate : zlib.deflateSync,
|
|
@@ -55,7 +55,7 @@ export const compress = (buf, encoding, { async = false } = {}) => {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
export const decompress = (buf, encoding, { async = false } = {}) => {
|
|
58
|
-
encoding &&= encoding.match(
|
|
58
|
+
encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
|
|
59
59
|
const decompressor = {
|
|
60
60
|
br: async ? brotliDecompress : zlib.brotliDecompressSync,
|
|
61
61
|
deflate: async ? inflate : zlib.inflateSync,
|
|
@@ -182,37 +182,31 @@ export const premix = (res, { digest = false, parse = false } = {}) => {
|
|
|
182
182
|
Object.defineProperties(res, {
|
|
183
183
|
arrayBuffer: {
|
|
184
184
|
enumerable: true,
|
|
185
|
-
value:
|
|
186
|
-
|
|
185
|
+
value: function () {
|
|
186
|
+
parse &&= false;
|
|
187
187
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
188
|
+
return this.body().then(({ buffer, byteLength, byteOffset }) => buffer.slice(
|
|
189
|
+
byteOffset,
|
|
190
|
+
byteOffset + byteLength,
|
|
191
|
+
));
|
|
192
192
|
},
|
|
193
193
|
},
|
|
194
194
|
blob: {
|
|
195
195
|
enumerable: true,
|
|
196
|
-
value:
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
return new Blob([val]);
|
|
196
|
+
value: function () {
|
|
197
|
+
return this.arrayBuffer().then((res) => new Blob([res]));
|
|
200
198
|
},
|
|
201
199
|
},
|
|
202
200
|
json: {
|
|
203
201
|
enumerable: true,
|
|
204
|
-
value:
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
return JSON.parse(val);
|
|
202
|
+
value: function () {
|
|
203
|
+
return this.text().then((res) => JSON.parse(res));
|
|
208
204
|
},
|
|
209
205
|
},
|
|
210
206
|
text: {
|
|
211
207
|
enumerable: true,
|
|
212
|
-
value:
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return val.toString();
|
|
208
|
+
value: function () {
|
|
209
|
+
return this.blob().then((blob) => blob.text());
|
|
216
210
|
},
|
|
217
211
|
},
|
|
218
212
|
});
|
|
@@ -243,14 +237,14 @@ export const premix = (res, { digest = false, parse = false } = {}) => {
|
|
|
243
237
|
const charset = contentType.split(';')
|
|
244
238
|
.find((it) => /charset=/i.test(it))
|
|
245
239
|
?.toLowerCase()
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
240
|
+
.replace('charset=', '')
|
|
241
|
+
.replace('iso-8859-1', 'latin1')
|
|
242
|
+
.trim() || 'utf-8';
|
|
249
243
|
|
|
250
|
-
if (/
|
|
244
|
+
if (/\bjson\b/i.test(contentType)) {
|
|
251
245
|
spool = JSON.parse(spool.toString(charset));
|
|
252
|
-
} else if (
|
|
253
|
-
if (
|
|
246
|
+
} else if (/\b(text|xml)\b/i.test(contentType)) {
|
|
247
|
+
if (/\b(latin1|ucs-2|utf-(8|16le))\b/.test(charset)) {
|
|
254
248
|
spool = spool.toString(charset);
|
|
255
249
|
} else {
|
|
256
250
|
spool = new TextDecoder(charset).decode(spool);
|