rekwest 2.1.0 → 2.3.2

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
@@ -84,7 +84,9 @@ const blob = new Blob(['bits']);
84
84
  const file = new File(['bits'], 'file.dab');
85
85
  const readable = Readable.from('bits');
86
86
 
87
- const fd = new FormData();
87
+ const fd = new FormData({
88
+ aux: Date.now(), // either [[key, value]], or kv sequenceable
89
+ });
88
90
 
89
91
  fd.append('celestial', 'payload');
90
92
  fd.append('blob', blob, 'blob.dab');
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}\r\n${HTTP2_HEADER_CONTENT_DISPOSITION}: form-data`;
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, '\r\n');
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))}"\r\n\r\n${normalize(value)}\r\n`);
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)}"` : ''}\r\n${HTTP2_HEADER_CONTENT_TYPE}: ${value.type || _mediatypes.APPLICATION_OCTET_STREAM}\r\n\r\n`);
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('\r\n');
48
+ yield encoder.encode(CRLF);
48
49
  }
49
50
  }
50
51
 
@@ -65,7 +66,7 @@ class FormData {
65
66
  if (_file.File.alike(value)) {
66
67
  value = new _file.File([value], filename, value);
67
68
  } else if (this.#ensureInstance(value)) {
68
- value.name = filename;
69
+ value.name = filename || value.name;
69
70
  } else {
70
71
  value = (0, _util.toUSVString)(value);
71
72
  }
@@ -86,6 +87,28 @@ class FormData {
86
87
  return this.constructor.name;
87
88
  }
88
89
 
90
+ constructor(input) {
91
+ if (input === Object(input) && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
92
+ if (input.constructor !== Object) {
93
+ input = Array.from(input);
94
+ }
95
+
96
+ if (Array.isArray(input)) {
97
+ if (!input.every(it => Array.isArray(it))) {
98
+ throw new TypeError(`Failed to construct '${this[Symbol.toStringTag]}': The provided value cannot be converted to a sequence.`);
99
+ } else if (!input.every(it => it.length === 2)) {
100
+ throw new TypeError(`Failed to construct '${this[Symbol.toStringTag]}': Sequence initializer must only contain pair elements.`);
101
+ }
102
+ }
103
+
104
+ if (input.constructor === Object) {
105
+ input = Object.entries(input);
106
+ }
107
+
108
+ input.forEach(([key, value]) => this.append(key, value));
109
+ }
110
+ }
111
+
89
112
  #ensureArgs(args, expected, method) {
90
113
  if (args.length < expected) {
91
114
  throw new TypeError(`Failed to execute '${method}' on '${this[Symbol.toStringTag]}': ${expected} arguments required, but only ${args.length} present.`);
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(/\bbr\b|\bdeflate\b|\bgzip\b/i)?.[0].toLowerCase();
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(/\bbr\b|\bdeflate\b|\bgzip\b/i)?.[0].toLowerCase();
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,
@@ -260,10 +260,10 @@ const premix = (res, {
260
260
  const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
261
261
  const charset = contentType.split(';').find(it => /charset=/i.test(it))?.toLowerCase()?.replace('charset=', '')?.replace('iso-8859-1', 'latin1')?.trim() || 'utf-8';
262
262
 
263
- if (/json/i.test(contentType)) {
263
+ if (/\bjson\b/i.test(contentType)) {
264
264
  spool = JSON.parse(spool.toString(charset));
265
- } else if (/text|xml/i.test(contentType)) {
266
- if (/latin1|utf-(8|16le)|ucs-2/.test(charset)) {
265
+ } else if (/\b(text|xml)\b/i.test(contentType)) {
266
+ if (/\b(latin1|ucs-2|utf-(8|16le))\b/.test(charset)) {
267
267
  spool = spool.toString(charset);
268
268
  } else {
269
269
  spool = new TextDecoder(charset).decode(spool);
package/package.json CHANGED
@@ -8,13 +8,13 @@
8
8
  "url": "https://github.com/bricss/rekwest/issues"
9
9
  },
10
10
  "devDependencies": {
11
- "@babel/cli": "^7.16.0",
12
- "@babel/core": "^7.16.0",
13
- "@babel/eslint-parser": "^7.16.3",
14
- "@babel/preset-env": "^7.16.4",
15
- "c8": "^7.10.0",
16
- "eslint": "^8.3.0",
17
- "eslint-config-ultra-refined": "^2.2.0",
11
+ "@babel/cli": "^7.16.8",
12
+ "@babel/core": "^7.16.7",
13
+ "@babel/eslint-parser": "^7.16.5",
14
+ "@babel/preset-env": "^7.16.8",
15
+ "c8": "^7.11.0",
16
+ "eslint": "^8.6.0",
17
+ "eslint-config-ultra-refined": "^2.3.0",
18
18
  "mocha": "^9.1.3"
19
19
  },
20
20
  "description": "The robust request library that humanity deserves 🌐",
@@ -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.1.0"
62
+ "version": "2.3.2"
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 }\r\n${ HTTP2_HEADER_CONTENT_DISPOSITION }: form-data`;
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, '\r\n');
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
- }"\r\n\r\n${ normalize(value) }\r\n`);
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) }"` : '' }\r\n${
40
+ }"${ value.name ? `; filename="${ escape(value.name) }"` : '' }${ CRLF }${
40
41
  HTTP2_HEADER_CONTENT_TYPE
41
42
  }: ${
42
43
  value.type || APPLICATION_OCTET_STREAM
43
- }\r\n\r\n`);
44
+ }${ CRLF.repeat(2) }`);
44
45
  yield* tap(value);
45
- yield encoder.encode('\r\n');
46
+ yield encoder.encode(CRLF);
46
47
  }
47
48
  }
48
49
 
@@ -62,7 +63,7 @@ export class FormData {
62
63
  if (File.alike(value)) {
63
64
  value = new File([value], filename, value);
64
65
  } else if (this.#ensureInstance(value)) {
65
- value.name = filename;
66
+ value.name = filename || value.name;
66
67
  } else {
67
68
  value = toUSVString(value);
68
69
  }
@@ -83,6 +84,34 @@ export class FormData {
83
84
  return this.constructor.name;
84
85
  }
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
+
86
115
  #ensureArgs(args, expected, method) {
87
116
  if (args.length < expected) {
88
117
  throw new TypeError(`Failed to execute '${ method }' on '${
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(/\bbr\b|\bdeflate\b|\bgzip\b/i)?.[0].toLowerCase();
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(/\bbr\b|\bdeflate\b|\bgzip\b/i)?.[0].toLowerCase();
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,
@@ -247,10 +247,10 @@ export const premix = (res, { digest = false, parse = false } = {}) => {
247
247
  ?.replace('iso-8859-1', 'latin1')
248
248
  ?.trim() || 'utf-8';
249
249
 
250
- if (/json/i.test(contentType)) {
250
+ if (/\bjson\b/i.test(contentType)) {
251
251
  spool = JSON.parse(spool.toString(charset));
252
- } else if (/text|xml/i.test(contentType)) {
253
- if (/latin1|utf-(8|16le)|ucs-2/.test(charset)) {
252
+ } else if (/\b(text|xml)\b/i.test(contentType)) {
253
+ if (/\b(latin1|ucs-2|utf-(8|16le))\b/.test(charset)) {
254
254
  spool = spool.toString(charset);
255
255
  } else {
256
256
  spool = new TextDecoder(charset).decode(spool);