axios 1.2.5 → 1.3.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.

Potentially problematic release.


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

@@ -1,4 +1,4 @@
1
- // Axios v1.2.5 Copyright (c) 2023 Matt Zabriskie and contributors
1
+ // Axios v1.3.0 Copyright (c) 2023 Matt Zabriskie and contributors
2
2
  'use strict';
3
3
 
4
4
  const FormData$1 = require('form-data');
@@ -538,7 +538,7 @@ const matchAll = (regExp, str) => {
538
538
  const isHTMLForm = kindOfTest('HTMLFormElement');
539
539
 
540
540
  const toCamelCase = str => {
541
- return str.toLowerCase().replace(/[_-\s]([a-z\d])(\w*)/g,
541
+ return str.toLowerCase().replace(/[-_\s]([a-z\d])(\w*)/g,
542
542
  function replacer(m, p1, p2) {
543
543
  return p1.toUpperCase() + p2;
544
544
  }
@@ -622,6 +622,37 @@ const toFiniteNumber = (value, defaultValue) => {
622
622
  return Number.isFinite(value) ? value : defaultValue;
623
623
  };
624
624
 
625
+ const ALPHA = 'abcdefghijklmnopqrstuvwxyz';
626
+
627
+ const DIGIT = '0123456789';
628
+
629
+ const ALPHABET = {
630
+ DIGIT,
631
+ ALPHA,
632
+ ALPHA_DIGIT: ALPHA + ALPHA.toUpperCase() + DIGIT
633
+ };
634
+
635
+ const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
636
+ let str = '';
637
+ const {length} = alphabet;
638
+ while (size--) {
639
+ str += alphabet[Math.random() * length|0];
640
+ }
641
+
642
+ return str;
643
+ };
644
+
645
+ /**
646
+ * If the thing is a FormData object, return true, otherwise return false.
647
+ *
648
+ * @param {unknown} thing - The thing to check.
649
+ *
650
+ * @returns {boolean}
651
+ */
652
+ function isSpecCompliantForm(thing) {
653
+ return !!(thing && isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator]);
654
+ }
655
+
625
656
  const toJSONObject = (obj) => {
626
657
  const stack = new Array(10);
627
658
 
@@ -699,6 +730,9 @@ const utils = {
699
730
  findKey,
700
731
  global: _global,
701
732
  isContextDefined,
733
+ ALPHABET,
734
+ generateString,
735
+ isSpecCompliantForm,
702
736
  toJSONObject
703
737
  };
704
738
 
@@ -852,17 +886,6 @@ const predicates = utils.toFlatObject(utils, {}, null, function filter(prop) {
852
886
  return /^is[A-Z]/.test(prop);
853
887
  });
854
888
 
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
889
  /**
867
890
  * Convert a data object to FormData
868
891
  *
@@ -910,7 +933,7 @@ function toFormData(obj, formData, options) {
910
933
  const dots = options.dots;
911
934
  const indexes = options.indexes;
912
935
  const _Blob = options.Blob || typeof Blob !== 'undefined' && Blob;
913
- const useBlob = _Blob && isSpecCompliant(formData);
936
+ const useBlob = _Blob && utils.isSpecCompliantForm(formData);
914
937
 
915
938
  if (!utils.isFunction(visitor)) {
916
939
  throw new TypeError('visitor must be a function');
@@ -1697,8 +1720,20 @@ class AxiosHeaders {
1697
1720
  return deleted;
1698
1721
  }
1699
1722
 
1700
- clear() {
1701
- return Object.keys(this).forEach(this.delete.bind(this));
1723
+ clear(matcher) {
1724
+ const keys = Object.keys(this);
1725
+ let i = keys.length;
1726
+ let deleted = false;
1727
+
1728
+ while (i--) {
1729
+ const key = keys[i];
1730
+ if(!matcher || matchHeaderValue(this, this[key], key, matcher)) {
1731
+ delete this[key];
1732
+ deleted = true;
1733
+ }
1734
+ }
1735
+
1736
+ return deleted;
1702
1737
  }
1703
1738
 
1704
1739
  normalize(format) {
@@ -1789,7 +1824,7 @@ class AxiosHeaders {
1789
1824
  }
1790
1825
  }
1791
1826
 
1792
- AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent']);
1827
+ AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent', 'Authorization']);
1793
1828
 
1794
1829
  utils.freezeMethods(AxiosHeaders.prototype);
1795
1830
  utils.freezeMethods(AxiosHeaders);
@@ -1911,7 +1946,7 @@ function buildFullPath(baseURL, requestedURL) {
1911
1946
  return requestedURL;
1912
1947
  }
1913
1948
 
1914
- const VERSION = "1.2.5";
1949
+ const VERSION = "1.3.0";
1915
1950
 
1916
1951
  function parseProtocol(url) {
1917
1952
  const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
@@ -2233,6 +2268,154 @@ class AxiosTransformStream extends stream__default["default"].Transform{
2233
2268
 
2234
2269
  const AxiosTransformStream$1 = AxiosTransformStream;
2235
2270
 
2271
+ const {asyncIterator} = Symbol;
2272
+
2273
+ const readBlob = async function* (blob) {
2274
+ if (blob.stream) {
2275
+ yield* blob.stream();
2276
+ } else if (blob.arrayBuffer) {
2277
+ yield await blob.arrayBuffer();
2278
+ } else if (blob[asyncIterator]) {
2279
+ yield* blob[asyncIterator]();
2280
+ } else {
2281
+ yield blob;
2282
+ }
2283
+ };
2284
+
2285
+ const readBlob$1 = readBlob;
2286
+
2287
+ const BOUNDARY_ALPHABET = utils.ALPHABET.ALPHA_DIGIT + '-_';
2288
+
2289
+ const textEncoder = new TextEncoder();
2290
+
2291
+ const CRLF = '\r\n';
2292
+ const CRLF_BYTES = textEncoder.encode(CRLF);
2293
+ const CRLF_BYTES_COUNT = 2;
2294
+
2295
+ class FormDataPart {
2296
+ constructor(name, value) {
2297
+ const {escapeName} = this.constructor;
2298
+ const isStringValue = utils.isString(value);
2299
+
2300
+ let headers = `Content-Disposition: form-data; name="${escapeName(name)}"${
2301
+ !isStringValue && value.name ? `; filename="${escapeName(value.name)}"` : ''
2302
+ }${CRLF}`;
2303
+
2304
+ if (isStringValue) {
2305
+ value = textEncoder.encode(String(value).replace(/\r?\n|\r\n?/g, CRLF));
2306
+ } else {
2307
+ headers += `Content-Type: ${value.type || "application/octet-stream"}${CRLF}`;
2308
+ }
2309
+
2310
+ this.headers = textEncoder.encode(headers + CRLF);
2311
+
2312
+ this.contentLength = isStringValue ? value.byteLength : value.size;
2313
+
2314
+ this.size = this.headers.byteLength + this.contentLength + CRLF_BYTES_COUNT;
2315
+
2316
+ this.name = name;
2317
+ this.value = value;
2318
+ }
2319
+
2320
+ async *encode(){
2321
+ yield this.headers;
2322
+
2323
+ const {value} = this;
2324
+
2325
+ if(utils.isTypedArray(value)) {
2326
+ yield value;
2327
+ } else {
2328
+ yield* readBlob$1(value);
2329
+ }
2330
+
2331
+ yield CRLF_BYTES;
2332
+ }
2333
+
2334
+ static escapeName(name) {
2335
+ return String(name).replace(/[\r\n"]/g, (match) => ({
2336
+ '\r' : '%0D',
2337
+ '\n' : '%0A',
2338
+ '"' : '%22',
2339
+ }[match]));
2340
+ }
2341
+ }
2342
+
2343
+ const formDataToStream = (form, headersHandler, options) => {
2344
+ const {
2345
+ tag = 'form-data-boundary',
2346
+ size = 25,
2347
+ boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET)
2348
+ } = options || {};
2349
+
2350
+ if(!utils.isFormData(form)) {
2351
+ throw TypeError('FormData instance required');
2352
+ }
2353
+
2354
+ if (boundary.length < 1 || boundary.length > 70) {
2355
+ throw Error('boundary must be 10-70 characters long')
2356
+ }
2357
+
2358
+ const boundaryBytes = textEncoder.encode('--' + boundary + CRLF);
2359
+ const footerBytes = textEncoder.encode('--' + boundary + '--' + CRLF + CRLF);
2360
+ let contentLength = footerBytes.byteLength;
2361
+
2362
+ const parts = Array.from(form.entries()).map(([name, value]) => {
2363
+ const part = new FormDataPart(name, value);
2364
+ contentLength += part.size;
2365
+ return part;
2366
+ });
2367
+
2368
+ contentLength += boundaryBytes.byteLength * parts.length;
2369
+
2370
+ contentLength = utils.toFiniteNumber(contentLength);
2371
+
2372
+ const computedHeaders = {
2373
+ 'Content-Type': `multipart/form-data; boundary=${boundary}`
2374
+ };
2375
+
2376
+ if (Number.isFinite(contentLength)) {
2377
+ computedHeaders['Content-Length'] = contentLength;
2378
+ }
2379
+
2380
+ headersHandler && headersHandler(computedHeaders);
2381
+
2382
+ return stream.Readable.from((async function *() {
2383
+ for(const part of parts) {
2384
+ yield boundaryBytes;
2385
+ yield* part.encode();
2386
+ }
2387
+
2388
+ yield footerBytes;
2389
+ })());
2390
+ };
2391
+
2392
+ const formDataToStream$1 = formDataToStream;
2393
+
2394
+ class ZlibHeaderTransformStream extends stream__default["default"].Transform {
2395
+ __transform(chunk, encoding, callback) {
2396
+ this.push(chunk);
2397
+ callback();
2398
+ }
2399
+
2400
+ _transform(chunk, encoding, callback) {
2401
+ if (chunk.length !== 0) {
2402
+ this._transform = this.__transform;
2403
+
2404
+ // Add Default Compression headers if no zlib headers are present
2405
+ if (chunk[0] !== 120) { // Hex: 78
2406
+ const header = Buffer.alloc(2);
2407
+ header[0] = 120; // Hex: 78
2408
+ header[1] = 156; // Hex: 9C
2409
+ this.push(header, encoding);
2410
+ }
2411
+ }
2412
+
2413
+ this.__transform(chunk, encoding, callback);
2414
+ }
2415
+ }
2416
+
2417
+ const ZlibHeaderTransformStream$1 = ZlibHeaderTransformStream;
2418
+
2236
2419
  const zlibOptions = {
2237
2420
  flush: zlib__default["default"].constants.Z_SYNC_FLUSH,
2238
2421
  finishFlush: zlib__default["default"].constants.Z_SYNC_FLUSH
@@ -2455,9 +2638,27 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2455
2638
  let maxUploadRate = undefined;
2456
2639
  let maxDownloadRate = undefined;
2457
2640
 
2458
- // support for https://www.npmjs.com/package/form-data api
2459
- if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
2641
+ // support for spec compliant FormData objects
2642
+ if (utils.isSpecCompliantForm(data)) {
2643
+ const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
2644
+
2645
+ data = formDataToStream$1(data, (formHeaders) => {
2646
+ headers.set(formHeaders);
2647
+ }, {
2648
+ tag: `axios-${VERSION}-boundary`,
2649
+ boundary: userBoundary && userBoundary[1] || undefined
2650
+ });
2651
+ // support for https://www.npmjs.com/package/form-data api
2652
+ } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
2460
2653
  headers.set(data.getHeaders());
2654
+ if (utils.isFunction(data.getLengthSync)) { // check if the undocumented API exists
2655
+ const knownLength = data.getLengthSync();
2656
+ !utils.isUndefined(knownLength) && headers.setContentLength(knownLength, false);
2657
+ }
2658
+ } else if (utils.isBlob(data)) {
2659
+ data.size && headers.setContentType(data.type || 'application/octet-stream');
2660
+ headers.setContentLength(data.size || 0);
2661
+ data = stream__default["default"].Readable.from(readBlob$1(data));
2461
2662
  } else if (data && !utils.isStream(data)) {
2462
2663
  if (Buffer.isBuffer(data)) ; else if (utils.isArrayBuffer(data)) {
2463
2664
  data = Buffer.from(new Uint8Array(data));
@@ -2472,7 +2673,7 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2472
2673
  }
2473
2674
 
2474
2675
  // Add Content-Length header if data exists
2475
- headers.set('Content-Length', data.length, false);
2676
+ headers.setContentLength(data.length, false);
2476
2677
 
2477
2678
  if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
2478
2679
  return reject(new AxiosError(
@@ -2636,7 +2837,15 @@ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2636
2837
  case 'x-gzip':
2637
2838
  case 'compress':
2638
2839
  case 'x-compress':
2840
+ // add the unzipper to the body stream processing pipeline
2841
+ streams.push(zlib__default["default"].createUnzip(zlibOptions));
2842
+
2843
+ // remove the content-encoding in order to not confuse downstream operations
2844
+ delete res.headers['content-encoding'];
2845
+ break;
2639
2846
  case 'deflate':
2847
+ streams.push(new ZlibHeaderTransformStream$1());
2848
+
2640
2849
  // add the unzipper to the body stream processing pipeline
2641
2850
  streams.push(zlib__default["default"].createUnzip(zlibOptions));
2642
2851