axios 1.2.6 → 1.3.1

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.

Potentially problematic release.


This version of axios might be problematic. Click here for more details.

@@ -1,4 +1,4 @@
1
- // Axios v1.2.6 Copyright (c) 2023 Matt Zabriskie and contributors
1
+ // Axios v1.3.1 Copyright (c) 2023 Matt Zabriskie and contributors
2
2
  'use strict';
3
3
 
4
4
  const FormData$1 = require('form-data');
@@ -6,6 +6,7 @@ const url = require('url');
6
6
  const proxyFromEnv = require('proxy-from-env');
7
7
  const http = require('http');
8
8
  const https = require('https');
9
+ const util = require('util');
9
10
  const followRedirects = require('follow-redirects');
10
11
  const zlib = require('zlib');
11
12
  const stream = require('stream');
@@ -17,6 +18,7 @@ const FormData__default = /*#__PURE__*/_interopDefaultLegacy(FormData$1);
17
18
  const url__default = /*#__PURE__*/_interopDefaultLegacy(url);
18
19
  const http__default = /*#__PURE__*/_interopDefaultLegacy(http);
19
20
  const https__default = /*#__PURE__*/_interopDefaultLegacy(https);
21
+ const util__default = /*#__PURE__*/_interopDefaultLegacy(util);
20
22
  const followRedirects__default = /*#__PURE__*/_interopDefaultLegacy(followRedirects);
21
23
  const zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
22
24
  const stream__default = /*#__PURE__*/_interopDefaultLegacy(stream);
@@ -538,7 +540,7 @@ const matchAll = (regExp, str) => {
538
540
  const isHTMLForm = kindOfTest('HTMLFormElement');
539
541
 
540
542
  const toCamelCase = str => {
541
- return str.toLowerCase().replace(/[_-\s]([a-z\d])(\w*)/g,
543
+ return str.toLowerCase().replace(/[-_\s]([a-z\d])(\w*)/g,
542
544
  function replacer(m, p1, p2) {
543
545
  return p1.toUpperCase() + p2;
544
546
  }
@@ -622,6 +624,37 @@ const toFiniteNumber = (value, defaultValue) => {
622
624
  return Number.isFinite(value) ? value : defaultValue;
623
625
  };
624
626
 
627
+ const ALPHA = 'abcdefghijklmnopqrstuvwxyz';
628
+
629
+ const DIGIT = '0123456789';
630
+
631
+ const ALPHABET = {
632
+ DIGIT,
633
+ ALPHA,
634
+ ALPHA_DIGIT: ALPHA + ALPHA.toUpperCase() + DIGIT
635
+ };
636
+
637
+ const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
638
+ let str = '';
639
+ const {length} = alphabet;
640
+ while (size--) {
641
+ str += alphabet[Math.random() * length|0];
642
+ }
643
+
644
+ return str;
645
+ };
646
+
647
+ /**
648
+ * If the thing is a FormData object, return true, otherwise return false.
649
+ *
650
+ * @param {unknown} thing - The thing to check.
651
+ *
652
+ * @returns {boolean}
653
+ */
654
+ function isSpecCompliantForm(thing) {
655
+ return !!(thing && isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator]);
656
+ }
657
+
625
658
  const toJSONObject = (obj) => {
626
659
  const stack = new Array(10);
627
660
 
@@ -699,6 +732,9 @@ const utils = {
699
732
  findKey,
700
733
  global: _global,
701
734
  isContextDefined,
735
+ ALPHABET,
736
+ generateString,
737
+ isSpecCompliantForm,
702
738
  toJSONObject
703
739
  };
704
740
 
@@ -852,17 +888,6 @@ const predicates = utils.toFlatObject(utils, {}, null, function filter(prop) {
852
888
  return /^is[A-Z]/.test(prop);
853
889
  });
854
890
 
855
- /**
856
- * If the thing is a FormData object, return true, otherwise return false.
857
- *
858
- * @param {unknown} thing - The thing to check.
859
- *
860
- * @returns {boolean}
861
- */
862
- function isSpecCompliant(thing) {
863
- return thing && utils.isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator];
864
- }
865
-
866
891
  /**
867
892
  * Convert a data object to FormData
868
893
  *
@@ -910,7 +935,7 @@ function toFormData(obj, formData, options) {
910
935
  const dots = options.dots;
911
936
  const indexes = options.indexes;
912
937
  const _Blob = options.Blob || typeof Blob !== 'undefined' && Blob;
913
- const useBlob = _Blob && isSpecCompliant(formData);
938
+ const useBlob = _Blob && utils.isSpecCompliantForm(formData);
914
939
 
915
940
  if (!utils.isFunction(visitor)) {
916
941
  throw new TypeError('visitor must be a function');
@@ -955,7 +980,7 @@ function toFormData(obj, formData, options) {
955
980
  value = JSON.stringify(value);
956
981
  } else if (
957
982
  (utils.isArray(value) && isFlatArray(value)) ||
958
- (utils.isFileList(value) || utils.endsWith(key, '[]') && (arr = utils.toArray(value))
983
+ ((utils.isFileList(value) || utils.endsWith(key, '[]')) && (arr = utils.toArray(value))
959
984
  )) {
960
985
  // eslint-disable-next-line no-param-reassign
961
986
  key = removeBrackets(key);
@@ -1664,7 +1689,7 @@ class AxiosHeaders {
1664
1689
  if (header) {
1665
1690
  const key = utils.findKey(this, header);
1666
1691
 
1667
- return !!(key && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
1692
+ return !!(key && this[key] !== undefined && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
1668
1693
  }
1669
1694
 
1670
1695
  return false;
@@ -1697,8 +1722,20 @@ class AxiosHeaders {
1697
1722
  return deleted;
1698
1723
  }
1699
1724
 
1700
- clear() {
1701
- return Object.keys(this).forEach(this.delete.bind(this));
1725
+ clear(matcher) {
1726
+ const keys = Object.keys(this);
1727
+ let i = keys.length;
1728
+ let deleted = false;
1729
+
1730
+ while (i--) {
1731
+ const key = keys[i];
1732
+ if(!matcher || matchHeaderValue(this, this[key], key, matcher)) {
1733
+ delete this[key];
1734
+ deleted = true;
1735
+ }
1736
+ }
1737
+
1738
+ return deleted;
1702
1739
  }
1703
1740
 
1704
1741
  normalize(format) {
@@ -1911,7 +1948,7 @@ function buildFullPath(baseURL, requestedURL) {
1911
1948
  return requestedURL;
1912
1949
  }
1913
1950
 
1914
- const VERSION = "1.2.6";
1951
+ const VERSION = "1.3.1";
1915
1952
 
1916
1953
  function parseProtocol(url) {
1917
1954
  const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
@@ -2233,6 +2270,154 @@ class AxiosTransformStream extends stream__default["default"].Transform{
2233
2270
 
2234
2271
  const AxiosTransformStream$1 = AxiosTransformStream;
2235
2272
 
2273
+ const {asyncIterator} = Symbol;
2274
+
2275
+ const readBlob = async function* (blob) {
2276
+ if (blob.stream) {
2277
+ yield* blob.stream();
2278
+ } else if (blob.arrayBuffer) {
2279
+ yield await blob.arrayBuffer();
2280
+ } else if (blob[asyncIterator]) {
2281
+ yield* blob[asyncIterator]();
2282
+ } else {
2283
+ yield blob;
2284
+ }
2285
+ };
2286
+
2287
+ const readBlob$1 = readBlob;
2288
+
2289
+ const BOUNDARY_ALPHABET = utils.ALPHABET.ALPHA_DIGIT + '-_';
2290
+
2291
+ const textEncoder = new TextEncoder();
2292
+
2293
+ const CRLF = '\r\n';
2294
+ const CRLF_BYTES = textEncoder.encode(CRLF);
2295
+ const CRLF_BYTES_COUNT = 2;
2296
+
2297
+ class FormDataPart {
2298
+ constructor(name, value) {
2299
+ const {escapeName} = this.constructor;
2300
+ const isStringValue = utils.isString(value);
2301
+
2302
+ let headers = `Content-Disposition: form-data; name="${escapeName(name)}"${
2303
+ !isStringValue && value.name ? `; filename="${escapeName(value.name)}"` : ''
2304
+ }${CRLF}`;
2305
+
2306
+ if (isStringValue) {
2307
+ value = textEncoder.encode(String(value).replace(/\r?\n|\r\n?/g, CRLF));
2308
+ } else {
2309
+ headers += `Content-Type: ${value.type || "application/octet-stream"}${CRLF}`;
2310
+ }
2311
+
2312
+ this.headers = textEncoder.encode(headers + CRLF);
2313
+
2314
+ this.contentLength = isStringValue ? value.byteLength : value.size;
2315
+
2316
+ this.size = this.headers.byteLength + this.contentLength + CRLF_BYTES_COUNT;
2317
+
2318
+ this.name = name;
2319
+ this.value = value;
2320
+ }
2321
+
2322
+ async *encode(){
2323
+ yield this.headers;
2324
+
2325
+ const {value} = this;
2326
+
2327
+ if(utils.isTypedArray(value)) {
2328
+ yield value;
2329
+ } else {
2330
+ yield* readBlob$1(value);
2331
+ }
2332
+
2333
+ yield CRLF_BYTES;
2334
+ }
2335
+
2336
+ static escapeName(name) {
2337
+ return String(name).replace(/[\r\n"]/g, (match) => ({
2338
+ '\r' : '%0D',
2339
+ '\n' : '%0A',
2340
+ '"' : '%22',
2341
+ }[match]));
2342
+ }
2343
+ }
2344
+
2345
+ const formDataToStream = (form, headersHandler, options) => {
2346
+ const {
2347
+ tag = 'form-data-boundary',
2348
+ size = 25,
2349
+ boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET)
2350
+ } = options || {};
2351
+
2352
+ if(!utils.isFormData(form)) {
2353
+ throw TypeError('FormData instance required');
2354
+ }
2355
+
2356
+ if (boundary.length < 1 || boundary.length > 70) {
2357
+ throw Error('boundary must be 10-70 characters long')
2358
+ }
2359
+
2360
+ const boundaryBytes = textEncoder.encode('--' + boundary + CRLF);
2361
+ const footerBytes = textEncoder.encode('--' + boundary + '--' + CRLF + CRLF);
2362
+ let contentLength = footerBytes.byteLength;
2363
+
2364
+ const parts = Array.from(form.entries()).map(([name, value]) => {
2365
+ const part = new FormDataPart(name, value);
2366
+ contentLength += part.size;
2367
+ return part;
2368
+ });
2369
+
2370
+ contentLength += boundaryBytes.byteLength * parts.length;
2371
+
2372
+ contentLength = utils.toFiniteNumber(contentLength);
2373
+
2374
+ const computedHeaders = {
2375
+ 'Content-Type': `multipart/form-data; boundary=${boundary}`
2376
+ };
2377
+
2378
+ if (Number.isFinite(contentLength)) {
2379
+ computedHeaders['Content-Length'] = contentLength;
2380
+ }
2381
+
2382
+ headersHandler && headersHandler(computedHeaders);
2383
+
2384
+ return stream.Readable.from((async function *() {
2385
+ for(const part of parts) {
2386
+ yield boundaryBytes;
2387
+ yield* part.encode();
2388
+ }
2389
+
2390
+ yield footerBytes;
2391
+ })());
2392
+ };
2393
+
2394
+ const formDataToStream$1 = formDataToStream;
2395
+
2396
+ class ZlibHeaderTransformStream extends stream__default["default"].Transform {
2397
+ __transform(chunk, encoding, callback) {
2398
+ this.push(chunk);
2399
+ callback();
2400
+ }
2401
+
2402
+ _transform(chunk, encoding, callback) {
2403
+ if (chunk.length !== 0) {
2404
+ this._transform = this.__transform;
2405
+
2406
+ // Add Default Compression headers if no zlib headers are present
2407
+ if (chunk[0] !== 120) { // Hex: 78
2408
+ const header = Buffer.alloc(2);
2409
+ header[0] = 120; // Hex: 78
2410
+ header[1] = 156; // Hex: 9C
2411
+ this.push(header, encoding);
2412
+ }
2413
+ }
2414
+
2415
+ this.__transform(chunk, encoding, callback);
2416
+ }
2417
+ }
2418
+
2419
+ const ZlibHeaderTransformStream$1 = ZlibHeaderTransformStream;
2420
+
2236
2421
  const zlibOptions = {
2237
2422
  flush: zlib__default["default"].constants.Z_SYNC_FLUSH,
2238
2423
  finishFlush: zlib__default["default"].constants.Z_SYNC_FLUSH
@@ -2327,7 +2512,8 @@ const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(pr
2327
2512
 
2328
2513
  /*eslint consistent-return:0*/
2329
2514
  const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2330
- return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
2515
+ /*eslint no-async-promise-executor:0*/
2516
+ return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) {
2331
2517
  let data = config.data;
2332
2518
  const responseType = config.responseType;
2333
2519
  const responseEncoding = config.responseEncoding;
@@ -2418,7 +2604,7 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2418
2604
  convertedData = convertedData.toString(responseEncoding);
2419
2605
 
2420
2606
  if (!responseEncoding || responseEncoding === 'utf8') {
2421
- data = utils.stripBOM(convertedData);
2607
+ convertedData = utils.stripBOM(convertedData);
2422
2608
  }
2423
2609
  } else if (responseType === 'stream') {
2424
2610
  convertedData = stream__default["default"].Readable.from(convertedData);
@@ -2455,9 +2641,32 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2455
2641
  let maxUploadRate = undefined;
2456
2642
  let maxDownloadRate = undefined;
2457
2643
 
2458
- // support for https://www.npmjs.com/package/form-data api
2459
- if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
2644
+ // support for spec compliant FormData objects
2645
+ if (utils.isSpecCompliantForm(data)) {
2646
+ const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
2647
+
2648
+ data = formDataToStream$1(data, (formHeaders) => {
2649
+ headers.set(formHeaders);
2650
+ }, {
2651
+ tag: `axios-${VERSION}-boundary`,
2652
+ boundary: userBoundary && userBoundary[1] || undefined
2653
+ });
2654
+ // support for https://www.npmjs.com/package/form-data api
2655
+ } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
2460
2656
  headers.set(data.getHeaders());
2657
+
2658
+ if (!headers.hasContentLength()) {
2659
+ try {
2660
+ const knownLength = await util__default["default"].promisify(data.getLength).call(data);
2661
+ headers.setContentLength(knownLength);
2662
+ /*eslint no-empty:0*/
2663
+ } catch (e) {
2664
+ }
2665
+ }
2666
+ } else if (utils.isBlob(data)) {
2667
+ data.size && headers.setContentType(data.type || 'application/octet-stream');
2668
+ headers.setContentLength(data.size || 0);
2669
+ data = stream__default["default"].Readable.from(readBlob$1(data));
2461
2670
  } else if (data && !utils.isStream(data)) {
2462
2671
  if (Buffer.isBuffer(data)) ; else if (utils.isArrayBuffer(data)) {
2463
2672
  data = Buffer.from(new Uint8Array(data));
@@ -2472,7 +2681,7 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2472
2681
  }
2473
2682
 
2474
2683
  // Add Content-Length header if data exists
2475
- headers.set('Content-Length', data.length, false);
2684
+ headers.setContentLength(data.length, false);
2476
2685
 
2477
2686
  if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
2478
2687
  return reject(new AxiosError(
@@ -2636,7 +2845,15 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2636
2845
  case 'x-gzip':
2637
2846
  case 'compress':
2638
2847
  case 'x-compress':
2848
+ // add the unzipper to the body stream processing pipeline
2849
+ streams.push(zlib__default["default"].createUnzip(zlibOptions));
2850
+
2851
+ // remove the content-encoding in order to not confuse downstream operations
2852
+ delete res.headers['content-encoding'];
2853
+ break;
2639
2854
  case 'deflate':
2855
+ streams.push(new ZlibHeaderTransformStream$1());
2856
+
2640
2857
  // add the unzipper to the body stream processing pipeline
2641
2858
  streams.push(zlib__default["default"].createUnzip(zlibOptions));
2642
2859