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