rekwest 3.3.3 → 4.0.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/dist/index.js CHANGED
@@ -2,83 +2,64 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  var _exportNames = {
5
- constants: true
5
+ constants: true,
6
+ mediatypes: true
6
7
  };
7
8
  exports.constants = void 0;
8
9
  exports.default = rekwest;
9
-
10
+ exports.mediatypes = void 0;
10
11
  var _nodeHttp = _interopRequireDefault(require("node:http"));
11
-
12
12
  var _nodeHttp2 = _interopRequireWildcard(require("node:http2"));
13
-
14
13
  exports.constants = _nodeHttp2.constants;
15
-
16
14
  var _nodeHttps = _interopRequireDefault(require("node:https"));
17
-
18
15
  var _promises = require("node:timers/promises");
19
-
20
- var _ackn = require("./ackn.js");
21
-
16
+ var _ackn = require("./ackn");
22
17
  Object.keys(_ackn).forEach(function (key) {
23
18
  if (key === "default" || key === "__esModule") return;
24
19
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
25
20
  if (key in exports && exports[key] === _ackn[key]) return;
26
21
  exports[key] = _ackn[key];
27
22
  });
28
-
29
- var _cookies = require("./cookies.js");
30
-
23
+ var _cookies = require("./cookies");
31
24
  Object.keys(_cookies).forEach(function (key) {
32
25
  if (key === "default" || key === "__esModule") return;
33
26
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
34
27
  if (key in exports && exports[key] === _cookies[key]) return;
35
28
  exports[key] = _cookies[key];
36
29
  });
37
-
38
- var _errors = require("./errors.js");
39
-
30
+ var _errors = require("./errors");
40
31
  Object.keys(_errors).forEach(function (key) {
41
32
  if (key === "default" || key === "__esModule") return;
42
33
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
43
34
  if (key in exports && exports[key] === _errors[key]) return;
44
35
  exports[key] = _errors[key];
45
36
  });
46
-
47
- var _mediatypes = require("./mediatypes.js");
48
-
49
- var _utils = require("./utils.js");
50
-
37
+ var _mediatypes = _interopRequireWildcard(require("./mediatypes"));
38
+ exports.mediatypes = _mediatypes;
39
+ var _utils = require("./utils");
51
40
  Object.keys(_utils).forEach(function (key) {
52
41
  if (key === "default" || key === "__esModule") return;
53
42
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
54
43
  if (key in exports && exports[key] === _utils[key]) return;
55
44
  exports[key] = _utils[key];
56
45
  });
57
-
58
- var _file = require("./file.js");
59
-
46
+ var _file = require("./file");
60
47
  Object.keys(_file).forEach(function (key) {
61
48
  if (key === "default" || key === "__esModule") return;
62
49
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
63
50
  if (key in exports && exports[key] === _file[key]) return;
64
51
  exports[key] = _file[key];
65
52
  });
66
-
67
- var _formdata = require("./formdata.js");
68
-
53
+ var _formdata = require("./formdata");
69
54
  Object.keys(_formdata).forEach(function (key) {
70
55
  if (key === "default" || key === "__esModule") return;
71
56
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
72
57
  if (key in exports && exports[key] === _formdata[key]) return;
73
58
  exports[key] = _formdata[key];
74
59
  });
75
-
76
60
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
77
-
78
61
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
79
-
80
62
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
81
-
82
63
  const {
83
64
  HTTP2_HEADER_CONTENT_LENGTH,
84
65
  HTTP2_HEADER_CONTENT_TYPE,
@@ -94,20 +75,15 @@ const {
94
75
  HTTP_STATUS_TOO_MANY_REQUESTS
95
76
  } = _nodeHttp2.default.constants;
96
77
  const maxRetryAfter = Symbol('maxRetryAfter');
97
-
98
78
  const maxRetryAfterError = (interval, options) => new _errors.RequestError(`Maximum '${HTTP2_HEADER_RETRY_AFTER}' limit exceeded: ${interval} ms.`, options);
99
-
100
79
  let defaults = {
101
80
  follow: 20,
102
-
103
81
  get maxRetryAfter() {
104
82
  return this[maxRetryAfter] ?? this.timeout;
105
83
  },
106
-
107
84
  set maxRetryAfter(value) {
108
85
  this[maxRetryAfter] = value;
109
86
  },
110
-
111
87
  method: HTTP2_METHOD_GET,
112
88
  retry: {
113
89
  attempts: 0,
@@ -118,34 +94,26 @@ let defaults = {
118
94
  },
119
95
  timeout: 3e5
120
96
  };
121
-
122
- async function rekwest(url, options = {}) {
123
- ({
124
- url
125
- } = (0, _utils.revise)({
126
- options,
97
+ async function rekwest(...args) {
98
+ let options = (0, _utils.sanitize)(...args);
99
+ const {
127
100
  url
128
- }));
129
-
101
+ } = options;
130
102
  if (!options.redirected) {
131
103
  options = (0, _utils.merge)(rekwest.defaults, options);
132
104
  }
133
-
134
105
  if (options.body && [HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
135
106
  throw new TypeError(`Request with ${HTTP2_METHOD_GET}/${HTTP2_METHOD_HEAD} method cannot have body.`);
136
107
  }
137
-
138
108
  if (options.follow === 0) {
139
109
  throw new _errors.RequestError(`Maximum redirect reached at: ${url.href}`);
140
110
  }
141
-
142
111
  if (url.protocol === 'https:') {
143
112
  options = await (0, _ackn.ackn)(options);
144
113
  } else if (Reflect.has(options, 'alpnProtocol')) {
145
114
  ['alpnProtocol', 'createConnection', 'h2', 'protocol'].forEach(it => Reflect.deleteProperty(options, it));
146
115
  }
147
-
148
- options = (0, _utils.preflight)(options);
116
+ options = await (0, _utils.transform)((0, _utils.preflight)(options));
149
117
  const {
150
118
  cookies,
151
119
  digest,
@@ -158,36 +126,27 @@ async function rekwest(url, options = {}) {
158
126
  const {
159
127
  request
160
128
  } = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
161
- let {
162
- body
163
- } = options;
164
129
  const promise = new Promise((resolve, reject) => {
165
130
  let client, req;
166
- body &&= (0, _utils.transform)(body, options);
167
-
168
131
  if (h2) {
169
132
  client = _nodeHttp2.default.connect(url.origin, options);
170
133
  req = client.request(options.headers, options);
171
134
  } else {
172
135
  req = request(url, options);
173
136
  }
174
-
175
137
  (0, _utils.affix)(client, req, options);
176
138
  req.once('error', reject);
177
139
  req.once('frameError', reject);
178
140
  req.once('goaway', reject);
179
141
  req.once('response', res => {
180
142
  let headers;
181
-
182
143
  if (h2) {
183
144
  headers = res;
184
145
  res = req;
185
146
  } else {
186
147
  res.once('error', reject);
187
148
  }
188
-
189
149
  (0, _utils.admix)(res, headers, options);
190
-
191
150
  if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
192
151
  if (_cookies.Cookies.jar.has(url.origin)) {
193
152
  new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]).forEach(function (val, key) {
@@ -197,85 +156,64 @@ async function rekwest(url, options = {}) {
197
156
  _cookies.Cookies.jar.set(url.origin, new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]));
198
157
  }
199
158
  }
200
-
201
159
  Reflect.defineProperty(res, 'cookies', {
202
160
  enumerable: true,
203
161
  value: cookies !== false && _cookies.Cookies.jar.has(url.origin) ? _cookies.Cookies.jar.get(url.origin) : void 0
204
162
  });
205
-
206
163
  if (follow && /^3\d{2}$/.test(res.statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
207
164
  if (redirect === _utils.redirects.error) {
208
165
  return res.emit('error', new _errors.RequestError(`Unexpected redirect, redirect mode is set to '${redirect}'.`));
209
166
  }
210
-
211
167
  if (redirect === _utils.redirects.follow) {
212
168
  options.url = new URL(res.headers[HTTP2_HEADER_LOCATION], url).href;
213
-
214
- if (res.statusCode !== HTTP_STATUS_SEE_OTHER && body === Object(body) && body.pipe?.constructor === Function) {
169
+ if (res.statusCode !== HTTP_STATUS_SEE_OTHER && options?.body?.pipe?.constructor === Function) {
215
170
  return res.emit('error', new _errors.RequestError(`Unable to ${redirect} redirect with streamable body.`));
216
171
  }
217
-
218
172
  options.follow--;
219
-
220
173
  if (res.statusCode === HTTP_STATUS_SEE_OTHER) {
221
174
  Reflect.deleteProperty(options.headers, HTTP2_HEADER_CONTENT_LENGTH);
222
175
  options.method = HTTP2_METHOD_GET;
223
176
  options.body = null;
224
177
  }
225
-
226
178
  Reflect.set(options, 'redirected', true);
227
-
228
179
  if (res.statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
229
180
  let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
230
181
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
231
-
232
182
  if (interval > options.maxRetryAfter) {
233
183
  return res.emit('error', maxRetryAfterError(interval, {
234
184
  cause: (0, _utils.mixin)(res, options)
235
185
  }));
236
186
  }
237
-
238
187
  return (0, _promises.setTimeout)(interval).then(() => rekwest(options.url, options).then(resolve, reject));
239
188
  }
240
-
241
189
  return rekwest(options.url, options).then(resolve, reject);
242
190
  }
243
191
  }
244
-
245
192
  if (res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
246
193
  return reject((0, _utils.mixin)(res, options));
247
194
  }
248
-
249
195
  resolve((0, _utils.mixin)(res, options));
250
196
  });
251
- (0, _utils.dispatch)(req, { ...options,
252
- body
253
- });
197
+ (0, _utils.dispatch)(options, req);
254
198
  });
255
-
256
199
  try {
257
200
  const res = await promise;
258
-
259
201
  if (digest && !redirected) {
260
202
  res.body = await res.body();
261
203
  }
262
-
263
204
  return res;
264
205
  } catch (ex) {
265
206
  const {
266
207
  maxRetryAfter,
267
208
  retry
268
209
  } = options;
269
-
270
210
  if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
271
211
  let {
272
212
  interval
273
213
  } = retry;
274
-
275
214
  if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
276
215
  interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
277
216
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
278
-
279
217
  if (interval > maxRetryAfter) {
280
218
  throw maxRetryAfterError(interval, {
281
219
  cause: ex
@@ -284,16 +222,13 @@ async function rekwest(url, options = {}) {
284
222
  } else {
285
223
  interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
286
224
  }
287
-
288
225
  retry.attempts--;
289
226
  retry.interval = interval;
290
227
  return (0, _promises.setTimeout)(interval).then(() => rekwest(url, options));
291
228
  }
292
-
293
229
  if (digest && !redirected && ex.body) {
294
230
  ex.body = await ex.body();
295
231
  }
296
-
297
232
  if (!thenable) {
298
233
  throw ex;
299
234
  } else {
@@ -301,63 +236,49 @@ async function rekwest(url, options = {}) {
301
236
  }
302
237
  }
303
238
  }
304
-
305
239
  Reflect.defineProperty(rekwest, 'stream', {
306
240
  enumerable: true,
307
-
308
- value(url, options = {}) {
309
- ({
310
- url
311
- } = (0, _utils.revise)({
312
- options,
313
- url
314
- }));
315
- options = (0, _utils.preflight)({ ...(0, _utils.merge)(rekwest.defaults, {
241
+ value(...args) {
242
+ const options = (0, _utils.preflight)({
243
+ ...(0, _utils.merge)(rekwest.defaults, {
316
244
  headers: {
317
245
  [HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_OCTET_STREAM
318
246
  }
319
- }, options),
247
+ }, (0, _utils.sanitize)(...args)),
320
248
  redirect: _utils.redirects.manual
321
249
  });
322
250
  const {
323
- h2
251
+ h2,
252
+ url
324
253
  } = options;
325
254
  const {
326
255
  request
327
256
  } = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
328
257
  let client, req;
329
-
330
258
  if (h2) {
331
259
  client = _nodeHttp2.default.connect(url.origin, options);
332
260
  req = client.request(options.headers, options);
333
261
  } else {
334
262
  req = request(url, options);
335
263
  }
336
-
337
264
  (0, _utils.affix)(client, req, options);
338
265
  req.once('response', res => {
339
266
  let headers;
340
-
341
267
  if (h2) {
342
268
  headers = res;
343
269
  res = req;
344
270
  }
345
-
346
271
  (0, _utils.admix)(res, headers, options);
347
272
  });
348
273
  return req;
349
274
  }
350
-
351
275
  });
352
276
  Reflect.defineProperty(rekwest, 'defaults', {
353
277
  enumerable: true,
354
-
355
278
  get() {
356
279
  return defaults;
357
280
  },
358
-
359
281
  set(value) {
360
282
  defaults = (0, _utils.merge)(defaults, value);
361
283
  }
362
-
363
284
  });