rekwest 4.4.4 → 4.5.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/src/cookies.mjs CHANGED
@@ -1,22 +1,68 @@
1
- import { brandCheck } from './utils.mjs';
1
+ import {
2
+ brandCheck,
3
+ toCamelCase,
4
+ } from './utils.mjs';
2
5
 
3
6
  export class Cookies extends URLSearchParams {
4
7
 
5
8
  static jar = new Map();
6
9
 
10
+ #chronometry = new Map();
11
+
7
12
  get [Symbol.toStringTag]() {
8
13
  return this.constructor.name;
9
14
  }
10
15
 
11
- constructor(input) {
16
+ constructor(input, { cookiesTTL } = { cookiesTTL: false }) {
12
17
  if (Array.isArray(input) && input.every((it) => !Array.isArray(it))) {
13
- input = input.join(';').split(';')
14
- .filter((it) => !/\b(?:Domain|Expires|HttpOnly|Max-Age|Path|SameParty|SameSite|Secure)\b/i.test(it))
15
- .map((it) => it.trim())
16
- .join('&');
18
+ input = input.map((it) => {
19
+ if (!cookiesTTL) {
20
+ return [it.split(';').at(0).trim()];
21
+ }
22
+
23
+ const [cookie, ...attrs] = it.split(';').map((it) => it.trim());
24
+ const ttl = attrs.reduce((acc, val) => {
25
+ if (/(?:Expires|Max-Age)=/i.test(val)) {
26
+ const [key, value] = val.toLowerCase().split('=');
27
+
28
+ acc[toCamelCase(key)] = !Number.isNaN(Number(value)) ? value * 1e3 : Date.parse(value) - Date.now();
29
+ }
30
+
31
+ return acc;
32
+ }, {});
33
+
34
+ return [
35
+ cookie.replace(/\u0022/g, ''),
36
+ Object.keys(ttl).length ? ttl : null,
37
+ ];
38
+ });
17
39
  }
18
40
 
19
- super(input);
41
+ super(Array.isArray(input) ? input.map((it) => it.at(0)).join('&') : input);
42
+
43
+ if (Array.isArray(input) && cookiesTTL) {
44
+ input.filter((it) => it.at(1)).forEach(([cookie, ttl]) => {
45
+ cookie = cookie.split('=').at(0);
46
+ if (this.#chronometry.has(cookie)) {
47
+ clearTimeout(this.#chronometry.get(cookie));
48
+ this.#chronometry.delete(cookie);
49
+ }
50
+
51
+ const { expires, maxAge } = ttl;
52
+
53
+ [
54
+ maxAge,
55
+ expires,
56
+ ].filter((it) => Number.isInteger(it)).some((ms) => {
57
+ const tid = setTimeout(() => {
58
+ this.#chronometry.delete(cookie);
59
+ this.delete(cookie);
60
+ }, Math.max(ms, 0));
61
+
62
+ return this.#chronometry.set(cookie, tid);
63
+ });
64
+ });
65
+ }
20
66
  }
21
67
 
22
68
  toString() {
package/src/defaults.mjs CHANGED
@@ -12,6 +12,7 @@ const {
12
12
  } = http2.constants;
13
13
 
14
14
  const stash = {
15
+ cookiesTTL: false,
15
16
  credentials: requestCredentials.sameOrigin,
16
17
  digest: true,
17
18
  follow: 20,
@@ -36,6 +37,7 @@ const stash = {
36
37
  HTTP_STATUS_SERVICE_UNAVAILABLE,
37
38
  ],
38
39
  },
40
+ stripTrailingSlash: false,
39
41
  thenable: false,
40
42
  timeout: 3e5,
41
43
  trimTrailingSlashes: false,
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
+ }
package/src/index.mjs CHANGED
@@ -5,12 +5,12 @@ import { requestRedirect } from './constants.mjs';
5
5
  import defaults from './defaults.mjs';
6
6
  import { APPLICATION_OCTET_STREAM } from './mediatypes.mjs';
7
7
  import { preflight } from './preflight.mjs';
8
+ import { transfer } from './transfer.mjs';
8
9
  import {
9
10
  admix,
10
11
  affix,
11
12
  merge,
12
13
  normalize,
13
- transfer,
14
14
  } from './utils.mjs';
15
15
  import { validation } from './validation.mjs';
16
16
 
@@ -44,11 +44,16 @@ export const postflight = (req, res, options, { reject, resolve }) => {
44
44
 
45
45
  if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
46
46
  if (Cookies.jar.has(url.origin)) {
47
- new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]).forEach(function (val, key) {
48
- this.set(key, val);
49
- }, Cookies.jar.get(url.origin));
47
+ const cookie = new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options);
48
+
49
+ Cookies.jar.get(url.origin).forEach((val, key) => {
50
+ if (!cookie.has(key)) {
51
+ cookie.set(key, val);
52
+ }
53
+ });
54
+ Cookies.jar.set(url.origin, cookie);
50
55
  } else {
51
- Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]));
56
+ Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options));
52
57
  }
53
58
  }
54
59
 
package/src/preflight.mjs CHANGED
@@ -30,18 +30,23 @@ export const preflight = (options) => {
30
30
  }
31
31
 
32
32
  if (cookies !== false) {
33
- let cookie = Cookies.jar.get(url.origin);
33
+ let cookie = Cookies.jar.has(url.origin);
34
34
 
35
35
  if (cookies === Object(cookies) && [
36
36
  requestCredentials.include,
37
37
  requestCredentials.sameOrigin,
38
38
  ].includes(credentials)) {
39
39
  if (cookie) {
40
- new Cookies(cookies).forEach(function (val, key) {
41
- this.set(key, val);
42
- }, cookie);
40
+ cookie = new Cookies(cookies, options);
41
+
42
+ Cookies.jar.get(url.origin).forEach((val, key) => {
43
+ if (!cookie.has(key)) {
44
+ cookie.set(key, val);
45
+ }
46
+ });
47
+ Cookies.jar.set(url.origin, cookie);
43
48
  } else {
44
- cookie = new Cookies(cookies);
49
+ cookie = new Cookies(cookies, options);
45
50
  Cookies.jar.set(url.origin, cookie);
46
51
  }
47
52
  }