rekwest 3.3.4 → 4.1.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/formdata.js CHANGED
@@ -2,97 +2,73 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.FormData = void 0;
5
-
6
5
  var _nodeCrypto = require("node:crypto");
7
-
8
6
  var _nodeHttp = _interopRequireDefault(require("node:http2"));
9
-
10
7
  var _nodeUtil = require("node:util");
11
-
12
- var _file = require("./file.js");
13
-
14
- var _mediatypes = require("./mediatypes.js");
15
-
16
- var _utils = require("./utils.js");
17
-
8
+ var _file = require("./file");
9
+ var _mediatypes = require("./mediatypes");
10
+ var _utils = require("./utils");
18
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
12
  const CRLF = '\r\n';
21
13
  const {
22
14
  HTTP2_HEADER_CONTENT_DISPOSITION,
23
15
  HTTP2_HEADER_CONTENT_TYPE
24
16
  } = _nodeHttp.default.constants;
25
-
26
17
  class FormData {
27
18
  static actuate(fd) {
28
19
  const boundary = (0, _nodeCrypto.randomBytes)(24).toString('hex');
29
20
  const contentType = `${_mediatypes.MULTIPART_FORM_DATA}; boundary=${boundary}`;
30
21
  const prefix = `--${boundary}${CRLF}${HTTP2_HEADER_CONTENT_DISPOSITION}: form-data`;
31
-
32
22
  const escape = str => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
33
-
34
23
  const normalize = value => value.replace(/\r?\n|\r/g, CRLF);
35
-
36
24
  return {
37
25
  contentType,
38
-
39
26
  async *[Symbol.asyncIterator]() {
40
27
  const encoder = new TextEncoder();
41
-
42
28
  for (const [name, value] of fd) {
43
29
  if (value.constructor === String) {
44
30
  yield encoder.encode(`${prefix}; name="${escape(normalize(name))}"${CRLF.repeat(2)}${normalize(value)}${CRLF}`);
45
31
  } else {
46
32
  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)}`);
47
33
  yield* (0, _utils.tap)(value);
48
- yield encoder.encode(CRLF);
34
+ yield new Uint8Array([13, 10]);
49
35
  }
50
36
  }
51
-
52
37
  yield encoder.encode(`--${boundary}--`);
53
38
  }
54
-
55
39
  };
56
40
  }
57
-
58
41
  static alike(instance) {
59
42
  return instance?.constructor.name === FormData.name;
60
43
  }
61
-
62
44
  static #enfoldEntry(name, value, filename) {
63
45
  name = (0, _nodeUtil.toUSVString)(name);
64
46
  filename &&= (0, _nodeUtil.toUSVString)(filename);
65
-
66
47
  if (_file.File.alike(value)) {
48
+ filename ??= value.name || 'blob';
67
49
  value = new _file.File([value], filename, value);
68
50
  } else if (this.#ensureInstance(value)) {
69
- value.name = filename || value.name;
51
+ value.name = filename;
70
52
  } else {
71
53
  value = (0, _nodeUtil.toUSVString)(value);
72
54
  }
73
-
74
55
  return {
75
56
  name,
76
57
  value
77
58
  };
78
59
  }
79
-
80
60
  static #ensureInstance(value) {
81
61
  return _file.File.alike(value) || value === Object(value) && Reflect.has(value, Symbol.asyncIterator);
82
62
  }
83
-
84
63
  #entries = [];
85
-
86
64
  get [Symbol.toStringTag]() {
87
65
  return this.constructor.name;
88
66
  }
89
-
90
67
  constructor(input) {
91
68
  if (input === Object(input) && (input?.constructor === Object || Reflect.has(input, Symbol.iterator))) {
92
69
  if (input.constructor !== Object) {
93
70
  input = Array.from(input);
94
71
  }
95
-
96
72
  if (Array.isArray(input)) {
97
73
  if (!input.every(it => Array.isArray(it))) {
98
74
  throw new TypeError(`Failed to construct '${this[Symbol.toStringTag]}': The provided value cannot be converted to a sequence.`);
@@ -100,93 +76,77 @@ class FormData {
100
76
  throw new TypeError(`Failed to construct '${this[Symbol.toStringTag]}': Sequence initializer must only contain pair elements.`);
101
77
  }
102
78
  }
103
-
104
79
  if (input.constructor === Object) {
105
80
  input = Object.entries(input);
106
81
  }
107
-
108
82
  input.forEach(([key, value]) => this.append(key, value));
109
83
  }
110
84
  }
111
-
112
85
  #ensureArgs(args, expected, method) {
113
86
  if (args.length < expected) {
114
87
  throw new TypeError(`Failed to execute '${method}' on '${this[Symbol.toStringTag]}': ${expected} arguments required, but only ${args.length} present.`);
115
88
  }
116
-
117
89
  if (['append', 'set'].includes(method)) {
118
90
  if (args.length === 3 && !this.constructor.#ensureInstance(args[1])) {
119
- throw new TypeError(`Failed to execute '${method}' on '${this[Symbol.toStringTag]}': parameter ${expected} is not of type 'Blob', 'File' or async iterable.`);
91
+ throw new TypeError(`Failed to execute '${method}' on '${this[Symbol.toStringTag]}': parameter ${expected} is not of type 'Blob'.`);
120
92
  }
121
93
  }
122
-
123
94
  if (method === 'forEach') {
124
95
  if (args[0]?.constructor !== Function) {
125
96
  throw new TypeError(`Failed to execute '${method}' on '${this[Symbol.toStringTag]}': parameter ${expected} is not of type 'Function'.`);
126
97
  }
127
98
  }
128
99
  }
129
-
130
100
  append(...args) {
131
- (0, _utils.collate)(this, FormData);
101
+ (0, _utils.brandCheck)(this, FormData);
132
102
  this.#ensureArgs(args, 2, 'append');
133
103
  this.#entries.push(this.constructor.#enfoldEntry(...args));
134
104
  }
135
-
136
105
  delete(...args) {
137
- (0, _utils.collate)(this, FormData);
106
+ (0, _utils.brandCheck)(this, FormData);
138
107
  this.#ensureArgs(args, 1, 'delete');
139
108
  const name = (0, _nodeUtil.toUSVString)(args[0]);
140
109
  this.#entries = this.#entries.filter(it => it.name !== name);
141
110
  }
142
-
143
111
  forEach(...args) {
144
- (0, _utils.collate)(this, FormData);
112
+ (0, _utils.brandCheck)(this, FormData);
145
113
  this.#ensureArgs(args, 1, 'forEach');
146
114
  const [callback, thisArg] = args;
147
-
148
115
  for (const entry of this) {
149
116
  Reflect.apply(callback, thisArg, [...entry.reverse(), this]);
150
117
  }
151
118
  }
152
-
153
119
  get(...args) {
154
- (0, _utils.collate)(this, FormData);
120
+ (0, _utils.brandCheck)(this, FormData);
155
121
  this.#ensureArgs(args, 1, 'get');
156
122
  const name = (0, _nodeUtil.toUSVString)(args[0]);
157
123
  return (this.#entries.find(it => it.name === name) ?? {}).value ?? null;
158
124
  }
159
-
160
125
  getAll(...args) {
161
- (0, _utils.collate)(this, FormData);
126
+ (0, _utils.brandCheck)(this, FormData);
162
127
  this.#ensureArgs(args, 1, 'getAll');
163
128
  const name = (0, _nodeUtil.toUSVString)(args[0]);
164
129
  return this.#entries.filter(it => it.name === name).map(it => it.value);
165
130
  }
166
-
167
131
  has(...args) {
168
- (0, _utils.collate)(this, FormData);
132
+ (0, _utils.brandCheck)(this, FormData);
169
133
  this.#ensureArgs(args, 1, 'has');
170
134
  const name = (0, _nodeUtil.toUSVString)(args[0]);
171
135
  return !!this.#entries.find(it => it.name === name);
172
136
  }
173
-
174
137
  set(...args) {
175
- (0, _utils.collate)(this, FormData);
138
+ (0, _utils.brandCheck)(this, FormData);
176
139
  this.#ensureArgs(args, 2, 'set');
177
140
  const entry = this.constructor.#enfoldEntry(...args);
178
141
  const idx = this.#entries.findIndex(it => it.name === entry.name);
179
-
180
142
  if (idx !== -1) {
181
143
  this.#entries.splice(idx, 1, entry);
182
144
  } else {
183
145
  this.#entries.push(entry);
184
146
  }
185
147
  }
186
-
187
148
  *entries() {
188
- (0, _utils.collate)(this, FormData);
189
-
149
+ (0, _utils.brandCheck)(this, FormData);
190
150
  for (const {
191
151
  name,
192
152
  value
@@ -194,28 +154,21 @@ class FormData {
194
154
  yield [name, value];
195
155
  }
196
156
  }
197
-
198
157
  *keys() {
199
- (0, _utils.collate)(this, FormData);
200
-
158
+ (0, _utils.brandCheck)(this, FormData);
201
159
  for (const [name] of this) {
202
160
  yield name;
203
161
  }
204
162
  }
205
-
206
163
  *values() {
207
- (0, _utils.collate)(this, FormData);
208
-
164
+ (0, _utils.brandCheck)(this, FormData);
209
165
  for (const [, value] of this) {
210
166
  yield value;
211
167
  }
212
168
  }
213
-
214
169
  [Symbol.iterator]() {
215
- (0, _utils.collate)(this, FormData);
170
+ (0, _utils.brandCheck)(this, FormData);
216
171
  return this.entries();
217
172
  }
218
-
219
173
  }
220
-
221
174
  exports.FormData = FormData;
package/dist/index.js CHANGED
@@ -8,110 +8,91 @@ 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 _constants = require("./constants");
24
+ Object.keys(_constants).forEach(function (key) {
25
+ if (key === "default" || key === "__esModule") return;
26
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
27
+ if (key in exports && exports[key] === _constants[key]) return;
28
+ exports[key] = _constants[key];
29
+ });
30
+ var _cookies = require("./cookies");
33
31
  Object.keys(_cookies).forEach(function (key) {
34
32
  if (key === "default" || key === "__esModule") return;
35
33
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
36
34
  if (key in exports && exports[key] === _cookies[key]) return;
37
35
  exports[key] = _cookies[key];
38
36
  });
39
-
40
- var _errors = require("./errors.js");
41
-
37
+ var _errors = require("./errors");
42
38
  Object.keys(_errors).forEach(function (key) {
43
39
  if (key === "default" || key === "__esModule") return;
44
40
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
45
41
  if (key in exports && exports[key] === _errors[key]) return;
46
42
  exports[key] = _errors[key];
47
43
  });
48
-
49
- var _mediatypes = _interopRequireWildcard(require("./mediatypes.js"));
50
-
44
+ var _mediatypes = _interopRequireWildcard(require("./mediatypes"));
51
45
  exports.mediatypes = _mediatypes;
52
-
53
- var _utils = require("./utils.js");
54
-
46
+ var _utils = require("./utils");
55
47
  Object.keys(_utils).forEach(function (key) {
56
48
  if (key === "default" || key === "__esModule") return;
57
49
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
58
50
  if (key in exports && exports[key] === _utils[key]) return;
59
51
  exports[key] = _utils[key];
60
52
  });
61
-
62
- var _file = require("./file.js");
63
-
53
+ var _file = require("./file");
64
54
  Object.keys(_file).forEach(function (key) {
65
55
  if (key === "default" || key === "__esModule") return;
66
56
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
67
57
  if (key in exports && exports[key] === _file[key]) return;
68
58
  exports[key] = _file[key];
69
59
  });
70
-
71
- var _formdata = require("./formdata.js");
72
-
60
+ var _formdata = require("./formdata");
73
61
  Object.keys(_formdata).forEach(function (key) {
74
62
  if (key === "default" || key === "__esModule") return;
75
63
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
76
64
  if (key in exports && exports[key] === _formdata[key]) return;
77
65
  exports[key] = _formdata[key];
78
66
  });
79
-
80
67
  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
68
  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
69
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
85
-
86
70
  const {
87
- HTTP2_HEADER_CONTENT_LENGTH,
71
+ HTTP2_HEADER_AUTHORIZATION,
88
72
  HTTP2_HEADER_CONTENT_TYPE,
89
73
  HTTP2_HEADER_LOCATION,
90
74
  HTTP2_HEADER_RETRY_AFTER,
91
75
  HTTP2_HEADER_SET_COOKIE,
92
76
  HTTP2_METHOD_GET,
93
77
  HTTP2_METHOD_HEAD,
78
+ HTTP2_METHOD_POST,
94
79
  HTTP_STATUS_BAD_REQUEST,
80
+ HTTP_STATUS_FOUND,
95
81
  HTTP_STATUS_MOVED_PERMANENTLY,
96
82
  HTTP_STATUS_SEE_OTHER,
97
83
  HTTP_STATUS_SERVICE_UNAVAILABLE,
98
84
  HTTP_STATUS_TOO_MANY_REQUESTS
99
85
  } = _nodeHttp2.default.constants;
100
86
  const maxRetryAfter = Symbol('maxRetryAfter');
101
-
102
87
  const maxRetryAfterError = (interval, options) => new _errors.RequestError(`Maximum '${HTTP2_HEADER_RETRY_AFTER}' limit exceeded: ${interval} ms.`, options);
103
-
104
88
  let defaults = {
105
89
  follow: 20,
106
-
107
90
  get maxRetryAfter() {
108
91
  return this[maxRetryAfter] ?? this.timeout;
109
92
  },
110
-
111
93
  set maxRetryAfter(value) {
112
94
  this[maxRetryAfter] = value;
113
95
  },
114
-
115
96
  method: HTTP2_METHOD_GET,
116
97
  retry: {
117
98
  attempts: 0,
@@ -122,34 +103,26 @@ let defaults = {
122
103
  },
123
104
  timeout: 3e5
124
105
  };
125
-
126
- async function rekwest(url, options = {}) {
127
- ({
128
- url
129
- } = (0, _utils.revise)({
130
- options,
106
+ async function rekwest(...args) {
107
+ let options = (0, _utils.sanitize)(...args);
108
+ const {
131
109
  url
132
- }));
133
-
110
+ } = options;
134
111
  if (!options.redirected) {
135
112
  options = (0, _utils.merge)(rekwest.defaults, options);
136
113
  }
137
-
138
114
  if (options.body && [HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
139
115
  throw new TypeError(`Request with ${HTTP2_METHOD_GET}/${HTTP2_METHOD_HEAD} method cannot have body.`);
140
116
  }
141
-
142
117
  if (options.follow === 0) {
143
118
  throw new _errors.RequestError(`Maximum redirect reached at: ${url.href}`);
144
119
  }
145
-
146
120
  if (url.protocol === 'https:') {
147
121
  options = await (0, _ackn.ackn)(options);
148
122
  } else if (Reflect.has(options, 'alpnProtocol')) {
149
123
  ['alpnProtocol', 'createConnection', 'h2', 'protocol'].forEach(it => Reflect.deleteProperty(options, it));
150
124
  }
151
-
152
- options = (0, _utils.preflight)(options);
125
+ options = await (0, _utils.transform)((0, _utils.preflight)(options));
153
126
  const {
154
127
  cookies,
155
128
  digest,
@@ -162,36 +135,27 @@ async function rekwest(url, options = {}) {
162
135
  const {
163
136
  request
164
137
  } = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
165
- let {
166
- body
167
- } = options;
168
138
  const promise = new Promise((resolve, reject) => {
169
139
  let client, req;
170
- body &&= (0, _utils.transform)(body, options);
171
-
172
140
  if (h2) {
173
141
  client = _nodeHttp2.default.connect(url.origin, options);
174
142
  req = client.request(options.headers, options);
175
143
  } else {
176
144
  req = request(url, options);
177
145
  }
178
-
179
146
  (0, _utils.affix)(client, req, options);
180
147
  req.once('error', reject);
181
148
  req.once('frameError', reject);
182
149
  req.once('goaway', reject);
183
150
  req.once('response', res => {
184
151
  let headers;
185
-
186
152
  if (h2) {
187
153
  headers = res;
188
154
  res = req;
189
155
  } else {
190
156
  res.once('error', reject);
191
157
  }
192
-
193
158
  (0, _utils.admix)(res, headers, options);
194
-
195
159
  if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
196
160
  if (_cookies.Cookies.jar.has(url.origin)) {
197
161
  new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]).forEach(function (val, key) {
@@ -201,85 +165,78 @@ async function rekwest(url, options = {}) {
201
165
  _cookies.Cookies.jar.set(url.origin, new _cookies.Cookies(res.headers[HTTP2_HEADER_SET_COOKIE]));
202
166
  }
203
167
  }
204
-
205
168
  Reflect.defineProperty(res, 'cookies', {
206
169
  enumerable: true,
207
170
  value: cookies !== false && _cookies.Cookies.jar.has(url.origin) ? _cookies.Cookies.jar.get(url.origin) : void 0
208
171
  });
209
-
210
- if (follow && /^3\d{2}$/.test(res.statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
211
- if (redirect === _utils.redirects.error) {
172
+ const {
173
+ statusCode
174
+ } = res;
175
+ if (follow && /3\d{2}/.test(statusCode) && res.headers[HTTP2_HEADER_LOCATION]) {
176
+ if (!_constants.redirectStatusCodes.includes(statusCode)) {
177
+ return res.emit('error', new RangeError(`Invalid status code: ${statusCode}`));
178
+ }
179
+ if (redirect === _constants.redirectModes.error) {
212
180
  return res.emit('error', new _errors.RequestError(`Unexpected redirect, redirect mode is set to '${redirect}'.`));
213
181
  }
214
-
215
- if (redirect === _utils.redirects.follow) {
216
- 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) {
182
+ if (redirect === _constants.redirectModes.follow) {
183
+ const location = new URL(res.headers[HTTP2_HEADER_LOCATION], url);
184
+ if (!/^https?:/.test(location.protocol)) {
185
+ return res.emit('error', new _errors.RequestError('URL scheme must be "http" or "https".'));
186
+ }
187
+ if (!(0, _utils.sameOrigin)(location, url)) {
188
+ Reflect.deleteProperty(options.headers, HTTP2_HEADER_AUTHORIZATION);
189
+ location.password = location.username = '';
190
+ }
191
+ options.url = location;
192
+ if (statusCode !== HTTP_STATUS_SEE_OTHER && options?.body?.pipe?.constructor === Function) {
219
193
  return res.emit('error', new _errors.RequestError(`Unable to ${redirect} redirect with streamable body.`));
220
194
  }
221
-
222
195
  options.follow--;
223
-
224
- if (res.statusCode === HTTP_STATUS_SEE_OTHER) {
225
- Reflect.deleteProperty(options.headers, HTTP2_HEADER_CONTENT_LENGTH);
226
- options.method = HTTP2_METHOD_GET;
196
+ if ([HTTP_STATUS_MOVED_PERMANENTLY, HTTP_STATUS_FOUND].includes(statusCode) && request.method === HTTP2_METHOD_POST || statusCode === HTTP_STATUS_SEE_OTHER && ![HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
197
+ Object.keys(options.headers).filter(it => /^content-/i.test(it)).forEach(it => Reflect.deleteProperty(options.headers, it));
227
198
  options.body = null;
199
+ options.method = HTTP2_METHOD_GET;
228
200
  }
229
-
230
201
  Reflect.set(options, 'redirected', true);
231
-
232
- if (res.statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
202
+ if (statusCode === HTTP_STATUS_MOVED_PERMANENTLY && res.headers[HTTP2_HEADER_RETRY_AFTER]) {
233
203
  let interval = res.headers[HTTP2_HEADER_RETRY_AFTER];
234
204
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
235
-
236
205
  if (interval > options.maxRetryAfter) {
237
206
  return res.emit('error', maxRetryAfterError(interval, {
238
207
  cause: (0, _utils.mixin)(res, options)
239
208
  }));
240
209
  }
241
-
242
210
  return (0, _promises.setTimeout)(interval).then(() => rekwest(options.url, options).then(resolve, reject));
243
211
  }
244
-
245
212
  return rekwest(options.url, options).then(resolve, reject);
246
213
  }
247
214
  }
248
-
249
- if (res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
215
+ if (statusCode >= HTTP_STATUS_BAD_REQUEST) {
250
216
  return reject((0, _utils.mixin)(res, options));
251
217
  }
252
-
253
218
  resolve((0, _utils.mixin)(res, options));
254
219
  });
255
- (0, _utils.dispatch)(req, { ...options,
256
- body
257
- });
220
+ (0, _utils.dispatch)(options, req);
258
221
  });
259
-
260
222
  try {
261
223
  const res = await promise;
262
-
263
224
  if (digest && !redirected) {
264
225
  res.body = await res.body();
265
226
  }
266
-
267
227
  return res;
268
228
  } catch (ex) {
269
229
  const {
270
230
  maxRetryAfter,
271
231
  retry
272
232
  } = options;
273
-
274
233
  if (retry?.attempts && retry?.statusCodes.includes(ex.statusCode)) {
275
234
  let {
276
235
  interval
277
236
  } = retry;
278
-
279
237
  if (retry.retryAfter && ex.headers[HTTP2_HEADER_RETRY_AFTER]) {
280
238
  interval = ex.headers[HTTP2_HEADER_RETRY_AFTER];
281
239
  interval = Number(interval) * 1000 || new Date(interval) - Date.now();
282
-
283
240
  if (interval > maxRetryAfter) {
284
241
  throw maxRetryAfterError(interval, {
285
242
  cause: ex
@@ -288,16 +245,13 @@ async function rekwest(url, options = {}) {
288
245
  } else {
289
246
  interval = new Function('interval', `return Math.ceil(${retry.backoffStrategy});`)(interval);
290
247
  }
291
-
292
248
  retry.attempts--;
293
249
  retry.interval = interval;
294
250
  return (0, _promises.setTimeout)(interval).then(() => rekwest(url, options));
295
251
  }
296
-
297
252
  if (digest && !redirected && ex.body) {
298
253
  ex.body = await ex.body();
299
254
  }
300
-
301
255
  if (!thenable) {
302
256
  throw ex;
303
257
  } else {
@@ -305,63 +259,49 @@ async function rekwest(url, options = {}) {
305
259
  }
306
260
  }
307
261
  }
308
-
309
262
  Reflect.defineProperty(rekwest, 'stream', {
310
263
  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, {
264
+ value(...args) {
265
+ const options = (0, _utils.preflight)({
266
+ ...(0, _utils.merge)(rekwest.defaults, {
320
267
  headers: {
321
268
  [HTTP2_HEADER_CONTENT_TYPE]: _mediatypes.APPLICATION_OCTET_STREAM
322
269
  }
323
- }, options),
324
- redirect: _utils.redirects.manual
270
+ }, (0, _utils.sanitize)(...args)),
271
+ redirect: _constants.redirectModes.manual
325
272
  });
326
273
  const {
327
- h2
274
+ h2,
275
+ url
328
276
  } = options;
329
277
  const {
330
278
  request
331
279
  } = url.protocol === 'http:' ? _nodeHttp.default : _nodeHttps.default;
332
280
  let client, req;
333
-
334
281
  if (h2) {
335
282
  client = _nodeHttp2.default.connect(url.origin, options);
336
283
  req = client.request(options.headers, options);
337
284
  } else {
338
285
  req = request(url, options);
339
286
  }
340
-
341
287
  (0, _utils.affix)(client, req, options);
342
288
  req.once('response', res => {
343
289
  let headers;
344
-
345
290
  if (h2) {
346
291
  headers = res;
347
292
  res = req;
348
293
  }
349
-
350
294
  (0, _utils.admix)(res, headers, options);
351
295
  });
352
296
  return req;
353
297
  }
354
-
355
298
  });
356
299
  Reflect.defineProperty(rekwest, 'defaults', {
357
300
  enumerable: true,
358
-
359
301
  get() {
360
302
  return defaults;
361
303
  },
362
-
363
304
  set(value) {
364
305
  defaults = (0, _utils.merge)(defaults, value);
365
306
  }
366
-
367
307
  });