@lcap/axios-fixed 1.13.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.
Files changed (87) hide show
  1. package/.husky/commit-msg +4 -0
  2. package/CHANGELOG.md +1359 -0
  3. package/LICENSE +7 -0
  4. package/MIGRATION_GUIDE.md +3 -0
  5. package/README.md +1784 -0
  6. package/dist/axios.js +4471 -0
  7. package/dist/axios.js.map +1 -0
  8. package/dist/axios.min.js +3 -0
  9. package/dist/axios.min.js.map +1 -0
  10. package/dist/browser/axios.cjs +3909 -0
  11. package/dist/browser/axios.cjs.map +1 -0
  12. package/dist/esm/axios.js +3932 -0
  13. package/dist/esm/axios.js.map +1 -0
  14. package/dist/esm/axios.min.js +3 -0
  15. package/dist/esm/axios.min.js.map +1 -0
  16. package/dist/node/axios.cjs +5242 -0
  17. package/dist/node/axios.cjs.map +1 -0
  18. package/index.d.cts +572 -0
  19. package/index.d.ts +585 -0
  20. package/index.js +43 -0
  21. package/lib/adapters/README.md +37 -0
  22. package/lib/adapters/adapters.js +126 -0
  23. package/lib/adapters/fetch.js +288 -0
  24. package/lib/adapters/http.js +895 -0
  25. package/lib/adapters/xhr.js +200 -0
  26. package/lib/axios.js +89 -0
  27. package/lib/cancel/CancelToken.js +135 -0
  28. package/lib/cancel/CanceledError.js +25 -0
  29. package/lib/cancel/isCancel.js +5 -0
  30. package/lib/core/Axios.js +240 -0
  31. package/lib/core/AxiosError.js +110 -0
  32. package/lib/core/AxiosHeaders.js +314 -0
  33. package/lib/core/InterceptorManager.js +71 -0
  34. package/lib/core/README.md +8 -0
  35. package/lib/core/buildFullPath.js +22 -0
  36. package/lib/core/dispatchRequest.js +81 -0
  37. package/lib/core/mergeConfig.js +106 -0
  38. package/lib/core/settle.js +27 -0
  39. package/lib/core/transformData.js +28 -0
  40. package/lib/defaults/index.js +161 -0
  41. package/lib/defaults/transitional.js +7 -0
  42. package/lib/env/README.md +3 -0
  43. package/lib/env/classes/FormData.js +2 -0
  44. package/lib/env/data.js +1 -0
  45. package/lib/helpers/AxiosTransformStream.js +143 -0
  46. package/lib/helpers/AxiosURLSearchParams.js +58 -0
  47. package/lib/helpers/HttpStatusCode.js +77 -0
  48. package/lib/helpers/README.md +7 -0
  49. package/lib/helpers/ZlibHeaderTransformStream.js +28 -0
  50. package/lib/helpers/bind.js +14 -0
  51. package/lib/helpers/buildURL.js +67 -0
  52. package/lib/helpers/callbackify.js +16 -0
  53. package/lib/helpers/combineURLs.js +15 -0
  54. package/lib/helpers/composeSignals.js +48 -0
  55. package/lib/helpers/cookies.js +53 -0
  56. package/lib/helpers/deprecatedMethod.js +26 -0
  57. package/lib/helpers/estimateDataURLDecodedBytes.js +73 -0
  58. package/lib/helpers/formDataToJSON.js +95 -0
  59. package/lib/helpers/formDataToStream.js +112 -0
  60. package/lib/helpers/fromDataURI.js +53 -0
  61. package/lib/helpers/isAbsoluteURL.js +15 -0
  62. package/lib/helpers/isAxiosError.js +14 -0
  63. package/lib/helpers/isURLSameOrigin.js +14 -0
  64. package/lib/helpers/null.js +2 -0
  65. package/lib/helpers/parseHeaders.js +55 -0
  66. package/lib/helpers/parseProtocol.js +6 -0
  67. package/lib/helpers/progressEventReducer.js +44 -0
  68. package/lib/helpers/readBlob.js +15 -0
  69. package/lib/helpers/resolveConfig.js +61 -0
  70. package/lib/helpers/speedometer.js +55 -0
  71. package/lib/helpers/spread.js +28 -0
  72. package/lib/helpers/throttle.js +44 -0
  73. package/lib/helpers/toFormData.js +223 -0
  74. package/lib/helpers/toURLEncodedForm.js +19 -0
  75. package/lib/helpers/trackStream.js +87 -0
  76. package/lib/helpers/validator.js +99 -0
  77. package/lib/platform/browser/classes/Blob.js +3 -0
  78. package/lib/platform/browser/classes/FormData.js +3 -0
  79. package/lib/platform/browser/classes/URLSearchParams.js +4 -0
  80. package/lib/platform/browser/index.js +13 -0
  81. package/lib/platform/common/utils.js +51 -0
  82. package/lib/platform/index.js +7 -0
  83. package/lib/platform/node/classes/FormData.js +3 -0
  84. package/lib/platform/node/classes/URLSearchParams.js +4 -0
  85. package/lib/platform/node/index.js +38 -0
  86. package/lib/utils.js +782 -0
  87. package/package.json +237 -0
@@ -0,0 +1,895 @@
1
+ import utils from './../utils.js';
2
+ import settle from './../core/settle.js';
3
+ import buildFullPath from '../core/buildFullPath.js';
4
+ import buildURL from './../helpers/buildURL.js';
5
+ import proxyFromEnv from 'proxy-from-env';
6
+ import http from 'http';
7
+ import https from 'https';
8
+ import http2 from 'http2';
9
+ import util from 'util';
10
+ import followRedirects from 'follow-redirects';
11
+ import zlib from 'zlib';
12
+ import {VERSION} from '../env/data.js';
13
+ import transitionalDefaults from '../defaults/transitional.js';
14
+ import AxiosError from '../core/AxiosError.js';
15
+ import CanceledError from '../cancel/CanceledError.js';
16
+ import platform from '../platform/index.js';
17
+ import fromDataURI from '../helpers/fromDataURI.js';
18
+ import stream from 'stream';
19
+ import AxiosHeaders from '../core/AxiosHeaders.js';
20
+ import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
21
+ import {EventEmitter} from 'events';
22
+ import formDataToStream from "../helpers/formDataToStream.js";
23
+ import readBlob from "../helpers/readBlob.js";
24
+ import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
25
+ import callbackify from "../helpers/callbackify.js";
26
+ import {progressEventReducer, progressEventDecorator, asyncDecorator} from "../helpers/progressEventReducer.js";
27
+ import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
28
+
29
+ const zlibOptions = {
30
+ flush: zlib.constants.Z_SYNC_FLUSH,
31
+ finishFlush: zlib.constants.Z_SYNC_FLUSH
32
+ };
33
+
34
+ const brotliOptions = {
35
+ flush: zlib.constants.BROTLI_OPERATION_FLUSH,
36
+ finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
37
+ }
38
+
39
+ const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
40
+
41
+ const {http: httpFollow, https: httpsFollow} = followRedirects;
42
+
43
+ const isHttps = /https:?/;
44
+
45
+ const supportedProtocols = platform.protocols.map(protocol => {
46
+ return protocol + ':';
47
+ });
48
+
49
+
50
+ const flushOnFinish = (stream, [throttled, flush]) => {
51
+ stream
52
+ .on('end', flush)
53
+ .on('error', flush);
54
+
55
+ return throttled;
56
+ }
57
+
58
+ class Http2Sessions {
59
+ constructor() {
60
+ this.sessions = Object.create(null);
61
+ }
62
+
63
+ getSession(authority, options) {
64
+ options = Object.assign({
65
+ sessionTimeout: 1000
66
+ }, options);
67
+
68
+ let authoritySessions = this.sessions[authority];
69
+
70
+ if (authoritySessions) {
71
+ let len = authoritySessions.length;
72
+
73
+ for (let i = 0; i < len; i++) {
74
+ const [sessionHandle, sessionOptions] = authoritySessions[i];
75
+ if (!sessionHandle.destroyed && !sessionHandle.closed && util.isDeepStrictEqual(sessionOptions, options)) {
76
+ return sessionHandle;
77
+ }
78
+ }
79
+ }
80
+
81
+ const session = http2.connect(authority, options);
82
+
83
+ let removed;
84
+
85
+ const removeSession = () => {
86
+ if (removed) {
87
+ return;
88
+ }
89
+
90
+ removed = true;
91
+
92
+ let entries = authoritySessions, len = entries.length, i = len;
93
+
94
+ while (i--) {
95
+ if (entries[i][0] === session) {
96
+ if (len === 1) {
97
+ delete this.sessions[authority];
98
+ } else {
99
+ entries.splice(i, 1);
100
+ }
101
+ return;
102
+ }
103
+ }
104
+ };
105
+
106
+ const originalRequestFn = session.request;
107
+
108
+ const {sessionTimeout} = options;
109
+
110
+ if(sessionTimeout != null) {
111
+
112
+ let timer;
113
+ let streamsCount = 0;
114
+
115
+ session.request = function () {
116
+ const stream = originalRequestFn.apply(this, arguments);
117
+
118
+ streamsCount++;
119
+
120
+ if (timer) {
121
+ clearTimeout(timer);
122
+ timer = null;
123
+ }
124
+
125
+ stream.once('close', () => {
126
+ if (!--streamsCount) {
127
+ timer = setTimeout(() => {
128
+ timer = null;
129
+ removeSession();
130
+ }, sessionTimeout);
131
+ }
132
+ });
133
+
134
+ return stream;
135
+ }
136
+ }
137
+
138
+ session.once('close', removeSession);
139
+
140
+ let entry = [
141
+ session,
142
+ options
143
+ ];
144
+
145
+ authoritySessions ? authoritySessions.push(entry) : authoritySessions = this.sessions[authority] = [entry];
146
+
147
+ return session;
148
+ }
149
+ }
150
+
151
+ const http2Sessions = new Http2Sessions();
152
+
153
+
154
+ /**
155
+ * If the proxy or config beforeRedirects functions are defined, call them with the options
156
+ * object.
157
+ *
158
+ * @param {Object<string, any>} options - The options object that was passed to the request.
159
+ *
160
+ * @returns {Object<string, any>}
161
+ */
162
+ function dispatchBeforeRedirect(options, responseDetails) {
163
+ if (options.beforeRedirects.proxy) {
164
+ options.beforeRedirects.proxy(options);
165
+ }
166
+ if (options.beforeRedirects.config) {
167
+ options.beforeRedirects.config(options, responseDetails);
168
+ }
169
+ }
170
+
171
+ /**
172
+ * If the proxy or config afterRedirects functions are defined, call them with the options
173
+ *
174
+ * @param {http.ClientRequestArgs} options
175
+ * @param {AxiosProxyConfig} configProxy configuration from Axios options object
176
+ * @param {string} location
177
+ *
178
+ * @returns {http.ClientRequestArgs}
179
+ */
180
+ function setProxy(options, configProxy, location) {
181
+ let proxy = configProxy;
182
+ if (!proxy && proxy !== false) {
183
+ const proxyUrl = proxyFromEnv.getProxyForUrl(location);
184
+ if (proxyUrl) {
185
+ proxy = new URL(proxyUrl);
186
+ }
187
+ }
188
+ if (proxy) {
189
+ // Basic proxy authorization
190
+ if (proxy.username) {
191
+ proxy.auth = (proxy.username || '') + ':' + (proxy.password || '');
192
+ }
193
+
194
+ if (proxy.auth) {
195
+ // Support proxy auth object form
196
+ if (proxy.auth.username || proxy.auth.password) {
197
+ proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');
198
+ }
199
+ const base64 = Buffer
200
+ .from(proxy.auth, 'utf8')
201
+ .toString('base64');
202
+ options.headers['Proxy-Authorization'] = 'Basic ' + base64;
203
+ }
204
+
205
+ options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
206
+ const proxyHost = proxy.hostname || proxy.host;
207
+ options.hostname = proxyHost;
208
+ // Replace 'host' since options is not a URL object
209
+ options.host = proxyHost;
210
+ options.port = proxy.port;
211
+ options.path = location;
212
+ if (proxy.protocol) {
213
+ options.protocol = proxy.protocol.includes(':') ? proxy.protocol : `${proxy.protocol}:`;
214
+ }
215
+ }
216
+
217
+ options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
218
+ // Configure proxy for redirected request, passing the original config proxy to apply
219
+ // the exact same logic as if the redirected request was performed by axios directly.
220
+ setProxy(redirectOptions, configProxy, redirectOptions.href);
221
+ };
222
+ }
223
+
224
+ const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
225
+
226
+ // temporary hotfix
227
+
228
+ const wrapAsync = (asyncExecutor) => {
229
+ return new Promise((resolve, reject) => {
230
+ let onDone;
231
+ let isDone;
232
+
233
+ const done = (value, isRejected) => {
234
+ if (isDone) return;
235
+ isDone = true;
236
+ onDone && onDone(value, isRejected);
237
+ }
238
+
239
+ const _resolve = (value) => {
240
+ done(value);
241
+ resolve(value);
242
+ };
243
+
244
+ const _reject = (reason) => {
245
+ done(reason, true);
246
+ reject(reason);
247
+ }
248
+
249
+ asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
250
+ })
251
+ };
252
+
253
+ const resolveFamily = ({address, family}) => {
254
+ if (!utils.isString(address)) {
255
+ throw TypeError('address must be a string');
256
+ }
257
+ return ({
258
+ address,
259
+ family: family || (address.indexOf('.') < 0 ? 6 : 4)
260
+ });
261
+ }
262
+
263
+ const buildAddressEntry = (address, family) => resolveFamily(utils.isObject(address) ? address : {address, family});
264
+
265
+ const http2Transport = {
266
+ request(options, cb) {
267
+ const authority = options.protocol + '//' + options.hostname + ':' + (options.port || 80);
268
+
269
+ const {http2Options, headers} = options;
270
+
271
+ const session = http2Sessions.getSession(authority, http2Options);
272
+
273
+ const {
274
+ HTTP2_HEADER_SCHEME,
275
+ HTTP2_HEADER_METHOD,
276
+ HTTP2_HEADER_PATH,
277
+ HTTP2_HEADER_STATUS
278
+ } = http2.constants;
279
+
280
+ const http2Headers = {
281
+ [HTTP2_HEADER_SCHEME]: options.protocol.replace(':', ''),
282
+ [HTTP2_HEADER_METHOD]: options.method,
283
+ [HTTP2_HEADER_PATH]: options.path,
284
+ }
285
+
286
+ utils.forEach(headers, (header, name) => {
287
+ name.charAt(0) !== ':' && (http2Headers[name] = header);
288
+ });
289
+
290
+ const req = session.request(http2Headers);
291
+
292
+ req.once('response', (responseHeaders) => {
293
+ const response = req; //duplex
294
+
295
+ responseHeaders = Object.assign({}, responseHeaders);
296
+
297
+ const status = responseHeaders[HTTP2_HEADER_STATUS];
298
+
299
+ delete responseHeaders[HTTP2_HEADER_STATUS];
300
+
301
+ response.headers = responseHeaders;
302
+
303
+ response.statusCode = +status;
304
+
305
+ cb(response);
306
+ })
307
+
308
+ return req;
309
+ }
310
+ }
311
+
312
+ /*eslint consistent-return:0*/
313
+ export default isHttpAdapterSupported && function httpAdapter(config) {
314
+ return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
315
+ let {data, lookup, family, httpVersion = 1, http2Options} = config;
316
+ const {responseType, responseEncoding} = config;
317
+ const method = config.method.toUpperCase();
318
+ let isDone;
319
+ let rejected = false;
320
+ let req;
321
+
322
+ httpVersion = +httpVersion;
323
+
324
+ if (Number.isNaN(httpVersion)) {
325
+ throw TypeError(`Invalid protocol version: '${config.httpVersion}' is not a number`);
326
+ }
327
+
328
+ if (httpVersion !== 1 && httpVersion !== 2) {
329
+ throw TypeError(`Unsupported protocol version '${httpVersion}'`);
330
+ }
331
+
332
+ const isHttp2 = httpVersion === 2;
333
+
334
+ if (lookup) {
335
+ const _lookup = callbackify(lookup, (value) => utils.isArray(value) ? value : [value]);
336
+ // hotfix to support opt.all option which is required for node 20.x
337
+ lookup = (hostname, opt, cb) => {
338
+ _lookup(hostname, opt, (err, arg0, arg1) => {
339
+ if (err) {
340
+ return cb(err);
341
+ }
342
+
343
+ const addresses = utils.isArray(arg0) ? arg0.map(addr => buildAddressEntry(addr)) : [buildAddressEntry(arg0, arg1)];
344
+
345
+ opt.all ? cb(err, addresses) : cb(err, addresses[0].address, addresses[0].family);
346
+ });
347
+ }
348
+ }
349
+
350
+ const abortEmitter = new EventEmitter();
351
+
352
+ function abort(reason) {
353
+ try {
354
+ abortEmitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason);
355
+ } catch(err) {
356
+ console.warn('emit error', err);
357
+ }
358
+ }
359
+
360
+ abortEmitter.once('abort', reject);
361
+
362
+ const onFinished = () => {
363
+ if (config.cancelToken) {
364
+ config.cancelToken.unsubscribe(abort);
365
+ }
366
+
367
+ if (config.signal) {
368
+ config.signal.removeEventListener('abort', abort);
369
+ }
370
+
371
+ abortEmitter.removeAllListeners();
372
+ }
373
+
374
+ if (config.cancelToken || config.signal) {
375
+ config.cancelToken && config.cancelToken.subscribe(abort);
376
+ if (config.signal) {
377
+ config.signal.aborted ? abort() : config.signal.addEventListener('abort', abort);
378
+ }
379
+ }
380
+
381
+ onDone((response, isRejected) => {
382
+ isDone = true;
383
+
384
+ if (isRejected) {
385
+ rejected = true;
386
+ onFinished();
387
+ return;
388
+ }
389
+
390
+ const {data} = response;
391
+
392
+ if (data instanceof stream.Readable || data instanceof stream.Duplex) {
393
+ const offListeners = stream.finished(data, () => {
394
+ offListeners();
395
+ onFinished();
396
+ });
397
+ } else {
398
+ onFinished();
399
+ }
400
+ });
401
+
402
+
403
+
404
+
405
+
406
+ // Parse url
407
+ const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
408
+ const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
409
+ const protocol = parsed.protocol || supportedProtocols[0];
410
+
411
+ if (protocol === 'data:') {
412
+ // Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
413
+ if (config.maxContentLength > -1) {
414
+ // Use the exact string passed to fromDataURI (config.url); fall back to fullPath if needed.
415
+ const dataUrl = String(config.url || fullPath || '');
416
+ const estimated = estimateDataURLDecodedBytes(dataUrl);
417
+
418
+ if (estimated > config.maxContentLength) {
419
+ return reject(new AxiosError(
420
+ 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
421
+ AxiosError.ERR_BAD_RESPONSE,
422
+ config
423
+ ));
424
+ }
425
+ }
426
+
427
+ let convertedData;
428
+
429
+ if (method !== 'GET') {
430
+ return settle(resolve, reject, {
431
+ status: 405,
432
+ statusText: 'method not allowed',
433
+ headers: {},
434
+ config
435
+ });
436
+ }
437
+
438
+ try {
439
+ convertedData = fromDataURI(config.url, responseType === 'blob', {
440
+ Blob: config.env && config.env.Blob
441
+ });
442
+ } catch (err) {
443
+ throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
444
+ }
445
+
446
+ if (responseType === 'text') {
447
+ convertedData = convertedData.toString(responseEncoding);
448
+
449
+ if (!responseEncoding || responseEncoding === 'utf8') {
450
+ convertedData = utils.stripBOM(convertedData);
451
+ }
452
+ } else if (responseType === 'stream') {
453
+ convertedData = stream.Readable.from(convertedData);
454
+ }
455
+
456
+ return settle(resolve, reject, {
457
+ data: convertedData,
458
+ status: 200,
459
+ statusText: 'OK',
460
+ headers: new AxiosHeaders(),
461
+ config
462
+ });
463
+ }
464
+
465
+ if (supportedProtocols.indexOf(protocol) === -1) {
466
+ return reject(new AxiosError(
467
+ 'Unsupported protocol ' + protocol,
468
+ AxiosError.ERR_BAD_REQUEST,
469
+ config
470
+ ));
471
+ }
472
+
473
+ const headers = AxiosHeaders.from(config.headers).normalize();
474
+
475
+ // Set User-Agent (required by some servers)
476
+ // See https://github.com/axios/axios/issues/69
477
+ // User-Agent is specified; handle case where no UA header is desired
478
+ // Only set header if it hasn't been set in config
479
+ headers.set('User-Agent', 'axios/' + VERSION, false);
480
+
481
+ const {onUploadProgress, onDownloadProgress} = config;
482
+ const maxRate = config.maxRate;
483
+ let maxUploadRate = undefined;
484
+ let maxDownloadRate = undefined;
485
+
486
+ // support for spec compliant FormData objects
487
+ if (utils.isSpecCompliantForm(data)) {
488
+ const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
489
+
490
+ data = formDataToStream(data, (formHeaders) => {
491
+ headers.set(formHeaders);
492
+ }, {
493
+ tag: `axios-${VERSION}-boundary`,
494
+ boundary: userBoundary && userBoundary[1] || undefined
495
+ });
496
+ // support for https://www.npmjs.com/package/form-data api
497
+ } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
498
+ headers.set(data.getHeaders());
499
+
500
+ if (!headers.hasContentLength()) {
501
+ try {
502
+ const knownLength = await util.promisify(data.getLength).call(data);
503
+ Number.isFinite(knownLength) && knownLength >= 0 && headers.setContentLength(knownLength);
504
+ /*eslint no-empty:0*/
505
+ } catch (e) {
506
+ }
507
+ }
508
+ } else if (utils.isBlob(data) || utils.isFile(data)) {
509
+ data.size && headers.setContentType(data.type || 'application/octet-stream');
510
+ headers.setContentLength(data.size || 0);
511
+ data = stream.Readable.from(readBlob(data));
512
+ } else if (data && !utils.isStream(data)) {
513
+ if (Buffer.isBuffer(data)) {
514
+ // Nothing to do...
515
+ } else if (utils.isArrayBuffer(data)) {
516
+ data = Buffer.from(new Uint8Array(data));
517
+ } else if (utils.isString(data)) {
518
+ data = Buffer.from(data, 'utf-8');
519
+ } else {
520
+ return reject(new AxiosError(
521
+ 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
522
+ AxiosError.ERR_BAD_REQUEST,
523
+ config
524
+ ));
525
+ }
526
+
527
+ // Add Content-Length header if data exists
528
+ headers.setContentLength(data.length, false);
529
+
530
+ if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
531
+ return reject(new AxiosError(
532
+ 'Request body larger than maxBodyLength limit',
533
+ AxiosError.ERR_BAD_REQUEST,
534
+ config
535
+ ));
536
+ }
537
+ }
538
+
539
+ const contentLength = utils.toFiniteNumber(headers.getContentLength());
540
+
541
+ if (utils.isArray(maxRate)) {
542
+ maxUploadRate = maxRate[0];
543
+ maxDownloadRate = maxRate[1];
544
+ } else {
545
+ maxUploadRate = maxDownloadRate = maxRate;
546
+ }
547
+
548
+ if (data && (onUploadProgress || maxUploadRate)) {
549
+ if (!utils.isStream(data)) {
550
+ data = stream.Readable.from(data, {objectMode: false});
551
+ }
552
+
553
+ data = stream.pipeline([data, new AxiosTransformStream({
554
+ maxRate: utils.toFiniteNumber(maxUploadRate)
555
+ })], utils.noop);
556
+
557
+ onUploadProgress && data.on('progress', flushOnFinish(
558
+ data,
559
+ progressEventDecorator(
560
+ contentLength,
561
+ progressEventReducer(asyncDecorator(onUploadProgress), false, 3)
562
+ )
563
+ ));
564
+ }
565
+
566
+ // HTTP basic authentication
567
+ let auth = undefined;
568
+ if (config.auth) {
569
+ const username = config.auth.username || '';
570
+ const password = config.auth.password || '';
571
+ auth = username + ':' + password;
572
+ }
573
+
574
+ if (!auth && parsed.username) {
575
+ const urlUsername = parsed.username;
576
+ const urlPassword = parsed.password;
577
+ auth = urlUsername + ':' + urlPassword;
578
+ }
579
+
580
+ auth && headers.delete('authorization');
581
+
582
+ let path;
583
+
584
+ try {
585
+ path = buildURL(
586
+ parsed.pathname + parsed.search,
587
+ config.params,
588
+ config.paramsSerializer
589
+ ).replace(/^\?/, '');
590
+ } catch (err) {
591
+ const customErr = new Error(err.message);
592
+ customErr.config = config;
593
+ customErr.url = config.url;
594
+ customErr.exists = true;
595
+ return reject(customErr);
596
+ }
597
+
598
+ headers.set(
599
+ 'Accept-Encoding',
600
+ 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''), false
601
+ );
602
+
603
+ const options = {
604
+ path,
605
+ method: method,
606
+ headers: headers.toJSON(),
607
+ agents: { http: config.httpAgent, https: config.httpsAgent },
608
+ auth,
609
+ protocol,
610
+ family,
611
+ beforeRedirect: dispatchBeforeRedirect,
612
+ beforeRedirects: {},
613
+ http2Options
614
+ };
615
+
616
+ // cacheable-lookup integration hotfix
617
+ !utils.isUndefined(lookup) && (options.lookup = lookup);
618
+
619
+ if (config.socketPath) {
620
+ options.socketPath = config.socketPath;
621
+ } else {
622
+ options.hostname = parsed.hostname.startsWith("[") ? parsed.hostname.slice(1, -1) : parsed.hostname;
623
+ options.port = parsed.port;
624
+ setProxy(options, config.proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : parsed.protocol === 'https:' ? '443' : '') + options.path);
625
+ }
626
+
627
+ let transport;
628
+ const isHttpsRequest = isHttps.test(options.protocol);
629
+ options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
630
+
631
+ if (isHttp2) {
632
+ transport = http2Transport;
633
+ } else {
634
+ if (config.transport) {
635
+ transport = config.transport;
636
+ } else if (config.maxRedirects === 0) {
637
+ transport = isHttpsRequest ? https : http;
638
+ } else {
639
+ if (config.maxRedirects) {
640
+ options.maxRedirects = config.maxRedirects;
641
+ }
642
+ if (config.beforeRedirect) {
643
+ options.beforeRedirects.config = config.beforeRedirect;
644
+ }
645
+ transport = isHttpsRequest ? httpsFollow : httpFollow;
646
+ }
647
+ }
648
+
649
+ if (config.maxBodyLength > -1) {
650
+ options.maxBodyLength = config.maxBodyLength;
651
+ } else {
652
+ // follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
653
+ options.maxBodyLength = Infinity;
654
+ }
655
+
656
+ if (config.insecureHTTPParser) {
657
+ options.insecureHTTPParser = config.insecureHTTPParser;
658
+ }
659
+
660
+ // Create the request
661
+ req = transport.request(options, function handleResponse(res) {
662
+ if (req.destroyed) return;
663
+
664
+ const streams = [res];
665
+
666
+ const responseLength = utils.toFiniteNumber(res.headers['content-length']);
667
+
668
+ if (onDownloadProgress || maxDownloadRate) {
669
+ const transformStream = new AxiosTransformStream({
670
+ maxRate: utils.toFiniteNumber(maxDownloadRate)
671
+ });
672
+
673
+ onDownloadProgress && transformStream.on('progress', flushOnFinish(
674
+ transformStream,
675
+ progressEventDecorator(
676
+ responseLength,
677
+ progressEventReducer(asyncDecorator(onDownloadProgress), true, 3)
678
+ )
679
+ ));
680
+
681
+ streams.push(transformStream);
682
+ }
683
+
684
+ // decompress the response body transparently if required
685
+ let responseStream = res;
686
+
687
+ // return the last request in case of redirects
688
+ const lastRequest = res.req || req;
689
+
690
+ // if decompress disabled we should not decompress
691
+ if (config.decompress !== false && res.headers['content-encoding']) {
692
+ // if no content, but headers still say that it is encoded,
693
+ // remove the header not confuse downstream operations
694
+ if (method === 'HEAD' || res.statusCode === 204) {
695
+ delete res.headers['content-encoding'];
696
+ }
697
+
698
+ switch ((res.headers['content-encoding'] || '').toLowerCase()) {
699
+ /*eslint default-case:0*/
700
+ case 'gzip':
701
+ case 'x-gzip':
702
+ case 'compress':
703
+ case 'x-compress':
704
+ // add the unzipper to the body stream processing pipeline
705
+ streams.push(zlib.createUnzip(zlibOptions));
706
+
707
+ // remove the content-encoding in order to not confuse downstream operations
708
+ delete res.headers['content-encoding'];
709
+ break;
710
+ case 'deflate':
711
+ streams.push(new ZlibHeaderTransformStream());
712
+
713
+ // add the unzipper to the body stream processing pipeline
714
+ streams.push(zlib.createUnzip(zlibOptions));
715
+
716
+ // remove the content-encoding in order to not confuse downstream operations
717
+ delete res.headers['content-encoding'];
718
+ break;
719
+ case 'br':
720
+ if (isBrotliSupported) {
721
+ streams.push(zlib.createBrotliDecompress(brotliOptions));
722
+ delete res.headers['content-encoding'];
723
+ }
724
+ }
725
+ }
726
+
727
+ responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
728
+
729
+
730
+
731
+ const response = {
732
+ status: res.statusCode,
733
+ statusText: res.statusMessage,
734
+ headers: new AxiosHeaders(res.headers),
735
+ config,
736
+ request: lastRequest
737
+ };
738
+
739
+ if (responseType === 'stream') {
740
+ response.data = responseStream;
741
+ settle(resolve, reject, response);
742
+ } else {
743
+ const responseBuffer = [];
744
+ let totalResponseBytes = 0;
745
+
746
+ responseStream.on('data', function handleStreamData(chunk) {
747
+ responseBuffer.push(chunk);
748
+ totalResponseBytes += chunk.length;
749
+
750
+ // make sure the content length is not over the maxContentLength if specified
751
+ if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
752
+ // stream.destroy() emit aborted event before calling reject() on Node.js v16
753
+ rejected = true;
754
+ responseStream.destroy();
755
+ abort(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
756
+ AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
757
+ }
758
+ });
759
+
760
+ responseStream.on('aborted', function handlerStreamAborted() {
761
+ if (rejected) {
762
+ return;
763
+ }
764
+
765
+ const err = new AxiosError(
766
+ 'stream has been aborted',
767
+ AxiosError.ERR_BAD_RESPONSE,
768
+ config,
769
+ lastRequest
770
+ );
771
+ responseStream.destroy(err);
772
+ reject(err);
773
+ });
774
+
775
+ responseStream.on('error', function handleStreamError(err) {
776
+ if (req.destroyed) return;
777
+ reject(AxiosError.from(err, null, config, lastRequest));
778
+ });
779
+
780
+ responseStream.on('end', function handleStreamEnd() {
781
+ try {
782
+ let responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
783
+ if (responseType !== 'arraybuffer') {
784
+ responseData = responseData.toString(responseEncoding);
785
+ if (!responseEncoding || responseEncoding === 'utf8') {
786
+ responseData = utils.stripBOM(responseData);
787
+ }
788
+ }
789
+ response.data = responseData;
790
+ } catch (err) {
791
+ return reject(AxiosError.from(err, null, config, response.request, response));
792
+ }
793
+ settle(resolve, reject, response);
794
+ });
795
+ }
796
+
797
+ abortEmitter.once('abort', err => {
798
+ if (!responseStream.destroyed) {
799
+ responseStream.emit('error', err);
800
+ responseStream.destroy();
801
+ }
802
+ });
803
+ });
804
+
805
+ abortEmitter.once('abort', err => {
806
+ if (req.close) {
807
+ req.close();
808
+ } else {
809
+ req.destroy(err);
810
+ }
811
+ });
812
+
813
+ // Handle errors
814
+ req.on('error', function handleRequestError(err) {
815
+ // @todo remove
816
+ // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
817
+ reject(AxiosError.from(err, null, config, req));
818
+ });
819
+
820
+ // set tcp keep alive to prevent drop connection by peer
821
+ req.on('socket', function handleRequestSocket(socket) {
822
+ // default interval of sending ack packet is 1 minute
823
+ socket.setKeepAlive(true, 1000 * 60);
824
+ });
825
+
826
+ // Handle request timeout
827
+ if (config.timeout) {
828
+ // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
829
+ const timeout = parseInt(config.timeout, 10);
830
+
831
+ if (Number.isNaN(timeout)) {
832
+ abort(new AxiosError(
833
+ 'error trying to parse `config.timeout` to int',
834
+ AxiosError.ERR_BAD_OPTION_VALUE,
835
+ config,
836
+ req
837
+ ));
838
+
839
+ return;
840
+ }
841
+
842
+ // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
843
+ // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
844
+ // At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
845
+ // And then these socket which be hang up will devouring CPU little by little.
846
+ // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
847
+ req.setTimeout(timeout, function handleRequestTimeout() {
848
+ if (isDone) return;
849
+ let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
850
+ const transitional = config.transitional || transitionalDefaults;
851
+ if (config.timeoutErrorMessage) {
852
+ timeoutErrorMessage = config.timeoutErrorMessage;
853
+ }
854
+ abort(new AxiosError(
855
+ timeoutErrorMessage,
856
+ transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
857
+ config,
858
+ req
859
+ ));
860
+ });
861
+ } else {
862
+ // explicitly reset the socket timeout value for a possible `keep-alive` request
863
+ req.setTimeout(0);
864
+ }
865
+
866
+
867
+ // Send the request
868
+ if (utils.isStream(data)) {
869
+ let ended = false;
870
+ let errored = false;
871
+
872
+ data.on('end', () => {
873
+ ended = true;
874
+ });
875
+
876
+ data.once('error', err => {
877
+ errored = true;
878
+ req.destroy(err);
879
+ });
880
+
881
+ data.on('close', () => {
882
+ if (!ended && !errored) {
883
+ abort(new CanceledError('Request stream has been aborted', config, req));
884
+ }
885
+ });
886
+
887
+ data.pipe(req);
888
+ } else {
889
+ data && req.write(data);
890
+ req.end();
891
+ }
892
+ });
893
+ }
894
+
895
+ export const __setProxy = setProxy;