axios 1.20.0 → 1.21.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.
Files changed (39) hide show
  1. package/README.md +41 -106
  2. package/dist/axios.js +931 -278
  3. package/dist/axios.min.js +2 -2
  4. package/dist/axios.min.js.map +1 -1
  5. package/dist/browser/axios.cjs +948 -268
  6. package/dist/esm/axios.js +948 -268
  7. package/dist/esm/axios.min.js +2 -2
  8. package/dist/esm/axios.min.js.map +1 -1
  9. package/dist/node/axios.cjs +925 -242
  10. package/index.d.cts +20 -1
  11. package/index.d.ts +18 -1
  12. package/lib/adapters/fetch.js +73 -20
  13. package/lib/adapters/http.js +145 -26
  14. package/lib/adapters/xhr.js +120 -13
  15. package/lib/core/Axios.js +67 -12
  16. package/lib/core/AxiosError.js +59 -0
  17. package/lib/core/AxiosHeaders.js +17 -4
  18. package/lib/core/InterceptorManager.js +13 -2
  19. package/lib/core/assertParamsAcyclic.js +49 -0
  20. package/lib/core/createMultipartBody.js +37 -0
  21. package/lib/core/dispatchRequest.js +20 -0
  22. package/lib/core/mergeConfig.js +183 -120
  23. package/lib/defaults/transitional.js +1 -0
  24. package/lib/env/data.js +1 -1
  25. package/lib/helpers/AxiosTransformStream.js +11 -2
  26. package/lib/helpers/Http2Sessions.js +2 -1
  27. package/lib/helpers/composeSignals.js +6 -3
  28. package/lib/helpers/cookies.js +30 -6
  29. package/lib/helpers/estimateDataURLDecodedBytes.js +34 -7
  30. package/lib/helpers/formDataToBlob.js +113 -0
  31. package/lib/helpers/formDataToJSON.js +25 -1
  32. package/lib/helpers/fromDataURI.js +8 -2
  33. package/lib/helpers/isURLSameOrigin.js +4 -9
  34. package/lib/helpers/parseHeaders.js +2 -1
  35. package/lib/helpers/parseProtocol.js +1 -1
  36. package/lib/helpers/resolveConfig.js +16 -3
  37. package/lib/helpers/shouldBypassProxy.js +16 -1
  38. package/lib/utils.js +61 -10
  39. package/package.json +1 -2
@@ -65,6 +65,29 @@ function arrayToObject(arr) {
65
65
  return obj;
66
66
  }
67
67
 
68
+ function normalizeArray(value) {
69
+ if (!utils.isArray(value)) return value;
70
+
71
+ const keys = Object.keys(value);
72
+ return keys.length !== value.length || keys.some((key, index) => key !== String(index))
73
+ ? arrayToObject(value)
74
+ : value;
75
+ }
76
+
77
+ function normalizeArrays(obj) {
78
+ const pending = [obj];
79
+ while (pending.length) {
80
+ const target = pending.pop();
81
+ Object.keys(target).forEach((key) => {
82
+ const value = (target[key] = normalizeArray(target[key]));
83
+ if (utils.isArray(value) || utils.isPlainObject(value)) {
84
+ pending.push(value);
85
+ }
86
+ });
87
+ }
88
+ return obj;
89
+ }
90
+
68
91
  /**
69
92
  * It takes a FormData object and returns a JavaScript object
70
93
  *
@@ -86,6 +109,7 @@ function formDataToJSON(formData) {
86
109
 
87
110
  if (isLast) {
88
111
  if (utils.hasOwnProp(target, name)) {
112
+ target[name] = normalizeArray(target[name]);
89
113
  target[name] = utils.isArray(target[name])
90
114
  ? target[name].concat(value)
91
115
  : [target[name], value];
@@ -116,7 +140,7 @@ function formDataToJSON(formData) {
116
140
  buildPath(parsePropPath(name), value, obj, 0);
117
141
  });
118
142
 
119
- return obj;
143
+ return normalizeArrays(obj);
120
144
  }
121
145
 
122
146
  return null;
@@ -6,7 +6,10 @@ import platform from '../platform/index.js';
6
6
 
7
7
  // RFC 2397: data:[<mediatype>][;base64],<data>
8
8
  // mediatype = type/subtype followed by optional ;name=value parameters
9
- const DATA_URL_PATTERN = /^([^,;/]+\/[^,;/]+)?((?:;[^,;=]+=[^,;]+)*)(;base64)?,([\s\S]*)$/;
9
+ // The `i` flag makes the `;base64` token case-insensitive, matching the WHATWG
10
+ // URL/Fetch data URL processor (and therefore the fetch adapter and browsers).
11
+ // The remaining character classes do not depend on letter case.
12
+ const DATA_URL_PATTERN = /^([^,;/]+\/[^,;/]+)?((?:;[^,;=]+=[^,;]+)*)(;base64)?,([\s\S]*)$/i;
10
13
 
11
14
  /**
12
15
  * Parse data uri to a Buffer or Blob
@@ -51,7 +54,10 @@ export default function fromDataURI(uri, asBlob, options) {
51
54
 
52
55
  const buffer =
53
56
  encoding === 'base64'
54
- ? Buffer.from(body, 'base64')
57
+ ? Buffer.from(
58
+ body.replace(/%([\da-f]{2})/gi, (_, hex) => String.fromCharCode(parseInt(hex, 16))),
59
+ 'base64'
60
+ )
55
61
  : Buffer.from(decodeURIComponent(body), encoding);
56
62
 
57
63
  if (asBlob) {
@@ -1,16 +1,11 @@
1
1
  import platform from '../platform/index.js';
2
2
 
3
3
  export default platform.hasStandardBrowserEnv
4
- ? ((origin, isMSIE) => (url) => {
5
- url = new URL(url, platform.origin);
4
+ ? ((origin) => (url) => {
5
+ url = new URL(url, document.baseURI || platform.origin);
6
6
 
7
7
  return (
8
- origin.protocol === url.protocol &&
9
- origin.host === url.host &&
10
- (isMSIE || origin.port === url.port)
8
+ origin.protocol === url.protocol && origin.host === url.host && origin.port === url.port
11
9
  );
12
- })(
13
- new URL(platform.origin),
14
- platform.navigator && /(msie|trident)/i.test(platform.navigator.userAgent)
15
- )
10
+ })(new URL(platform.origin))
16
11
  : () => true;
@@ -39,7 +39,8 @@ const ignoreDuplicateOf = utils.toObjectSet([
39
39
  * @returns {Object} Headers parsed into an object
40
40
  */
41
41
  export default (rawHeaders) => {
42
- const parsed = {};
42
+ // Header names are data, including __proto__; avoid inherited setters.
43
+ const parsed = Object.create(null);
43
44
  let key;
44
45
  let val;
45
46
  let i;
@@ -2,5 +2,5 @@
2
2
 
3
3
  export default function parseProtocol(url) {
4
4
  const match = /^([-+\w]{1,25}):(?:\/\/)?/.exec(url);
5
- return (match && match[1]) || '';
5
+ return (match && match[1].toLowerCase()) || '';
6
6
  }
@@ -22,7 +22,9 @@ const encodeUTF8 = (str) =>
22
22
  String.fromCharCode(parseInt(hex, 16))
23
23
  );
24
24
 
25
- function resolveConfig(config) {
25
+ const readCookie = (name) => cookies.read(name);
26
+
27
+ function resolveConfig(config, readXsrfCookie = readCookie) {
26
28
  const newConfig = mergeConfig({}, config);
27
29
 
28
30
  // Read only own properties to prevent prototype pollution gadgets
@@ -51,11 +53,22 @@ function resolveConfig(config) {
51
53
  if (auth) {
52
54
  const username = utils.getSafeProp(auth, 'username') || '';
53
55
  const password = utils.getSafeProp(auth, 'password') || '';
56
+ const transitional = own('transitional');
57
+ const encodeUsername =
58
+ transitional &&
59
+ utils.hasOwnProp(transitional, 'encodeBasicAuthUsername') &&
60
+ transitional.encodeBasicAuthUsername === true;
54
61
 
55
62
  try {
56
63
  headers.set(
57
64
  'Authorization',
58
- 'Basic ' + btoa(username + ':' + (password ? encodeUTF8(password) : ''))
65
+ // Preserve legacy username bytes unless the caller opts in to UTF-8.
66
+ 'Basic ' +
67
+ btoa(
68
+ (encodeUsername ? encodeUTF8(username) : username) +
69
+ ':' +
70
+ (password ? encodeUTF8(password) : '')
71
+ )
59
72
  );
60
73
  } catch (e) {
61
74
  throw AxiosError.from(e, AxiosError.ERR_BAD_OPTION_VALUE, config);
@@ -93,7 +106,7 @@ function resolveConfig(config) {
93
106
  withXSRFToken === true || (withXSRFToken == null && isURLSameOrigin(newConfig.url));
94
107
 
95
108
  if (shouldSendXSRF) {
96
- const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
109
+ const xsrfValue = xsrfHeaderName && xsrfCookieName && readXsrfCookie(xsrfCookieName);
97
110
 
98
111
  if (xsrfValue) {
99
112
  headers.set(xsrfHeaderName, xsrfValue);
@@ -1,3 +1,5 @@
1
+ import { domainToASCII } from 'url';
2
+
1
3
  const LOOPBACK_HOSTNAMES = new Set(['localhost', '0.0.0.0']);
2
4
 
3
5
  const trimTrailingDots = (value) => {
@@ -319,7 +321,20 @@ const normalizeNoProxyHost = (hostname) => {
319
321
  hostname = hostname.slice(1, -1);
320
322
  }
321
323
 
322
- const trimmed = trimTrailingDots(hostname);
324
+ let trimmed = trimTrailingDots(hostname);
325
+ if (trimmed.indexOf(':') !== -1) {
326
+ try {
327
+ trimmed = new URL('http://[' + trimmed + ']/').hostname.slice(1, -1);
328
+ } catch (_err) {
329
+ return trimmed;
330
+ }
331
+ } else if (/[\u0080-\uffff]/.test(trimmed)) {
332
+ const prefix = /^[*.]+/.exec(trimmed);
333
+ const start = prefix ? prefix[0] : '';
334
+ const ascii = domainToASCII(trimmed.slice(start.length));
335
+ if (!ascii) return '';
336
+ trimmed = start + trimTrailingDots(ascii);
337
+ }
323
338
 
324
339
  // IPv4 shorthand/octal/hex → dotted-decimal; helper is a no-op for inputs
325
340
  // containing ':' (IPv6 and IPv4-mapped IPv6) so we fall through to unmap.
package/lib/utils.js CHANGED
@@ -313,11 +313,17 @@ const isPlainObject = (val) => {
313
313
  const prototype = getPrototypeOf(val);
314
314
  return (
315
315
  (prototype === null || prototype === Object.prototype || getPrototypeOf(prototype) === null) &&
316
- // Treat safe own/inherited Symbol.toStringTag or Symbol.iterator members as
317
- // evidence the value is tagged/iterable, while ignoring members reachable
318
- // only through shared or terminal prototype boundaries.
319
- !hasOwnInPrototypeChain(val, toStringTag) &&
320
- !hasOwnInPrototypeChain(val, iterator)
316
+ // Treat own Symbol.toStringTag or Symbol.iterator members as evidence the
317
+ // value is tagged/iterable, ignoring members reachable only through shared
318
+ // or terminal prototype boundaries. This is exactly what
319
+ // hasOwnInPrototypeChain(val, prop) computes here: every prototype admitted
320
+ // above (null, Object.prototype, or a terminal null-prototype template) is
321
+ // a boundary, so the walk never inspects anything but `val`'s own members,
322
+ // and stops before even those when `val` is itself the boundary
323
+ // Object.prototype. Checking that inline avoids allocating the walker's
324
+ // `seen` list twice per call on the mergeConfig / merge hot path.
325
+ (isPrototypeBoundary(val, prototype, true) ||
326
+ (!hasOwnProperty(val, toStringTag) && !hasOwnProperty(val, iterator)))
321
327
  );
322
328
  };
323
329
 
@@ -581,15 +587,30 @@ const isContextDefined = (context) => !isUndefined(context) && context !== _glob
581
587
  function merge(...objs) {
582
588
  const { caseless, skipUndefined } = (isContextDefined(this) && this) || {};
583
589
  const result = {};
590
+ // Caseless lookup only applies to string keys — symbol keys are
591
+ // identity-matched. Every string key that reaches `result` passes through
592
+ // this index first, so it can never hold two case variants of one key and a
593
+ // lowercase -> key map is exactly what findKey(result, key) would rescan
594
+ // Object.keys(result) for on each assignment.
595
+ const lowerCaseKeys = caseless ? Object.create(null) : null;
584
596
  const assignValue = (val, key) => {
585
597
  // Skip dangerous property names to prevent prototype pollution
586
598
  if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
587
599
  return;
588
600
  }
589
601
 
590
- // findKey lowercases the key, so caseless lookup only applies to strings —
591
- // symbol keys are identity-matched.
592
- const targetKey = (caseless && typeof key === 'string' && findKey(result, key)) || key;
602
+ let targetKey = key;
603
+ if (lowerCaseKeys && typeof key === 'string') {
604
+ const lowerKey = key.toLowerCase();
605
+ const knownKey = lowerCaseKeys[lowerKey];
606
+ // A key recorded but absent from `result` was skipped by skipUndefined;
607
+ // the later spelling wins then, as it does for findKey.
608
+ if (knownKey !== undefined && hasOwnProperty(result, knownKey)) {
609
+ targetKey = knownKey;
610
+ } else {
611
+ lowerCaseKeys[lowerKey] = key;
612
+ }
613
+ }
593
614
  // Read via own-prop only — a bare `result[targetKey]` walks the prototype
594
615
  // chain, so a polluted Object.prototype value could surface here and get
595
616
  // copied into the merged result.
@@ -613,7 +634,7 @@ function merge(...objs) {
613
634
 
614
635
  forEach(source, assignValue);
615
636
 
616
- if (typeof source !== 'object' || isArray(source)) {
637
+ if (typeof source !== 'object' || isArray(source) || !Object.getOwnPropertySymbols) {
617
638
  continue;
618
639
  }
619
640
 
@@ -772,7 +793,37 @@ const toArray = (thing) => {
772
793
  if (!thing) return null;
773
794
  if (isArray(thing)) return thing;
774
795
  let i = thing.length;
775
- if (!isNumber(i)) return null;
796
+ if (!Number.isSafeInteger(i) || i < 0 || i > 0xffffffff) return null;
797
+
798
+ // Bound the copy by actual indexed properties, including hidden and inherited
799
+ // members of array-like collections, rather than by the declared length alone.
800
+ const indexes = new Set();
801
+ const seen = new Set();
802
+ let source = thing;
803
+
804
+ while (source != null && indexes.size < i) {
805
+ if (seen.has(source)) return null;
806
+ seen.add(source);
807
+
808
+ const prototype = getPrototypeOf(source);
809
+ if (isPrototypeBoundary(source, prototype, source === thing)) break;
810
+
811
+ Object.getOwnPropertyNames(source).forEach((key) => {
812
+ const index = +key;
813
+ if (
814
+ Number.isInteger(index) &&
815
+ index >= 0 &&
816
+ index < i &&
817
+ String(index) === key &&
818
+ hasOwnProperty(source, key)
819
+ ) {
820
+ indexes.add(index);
821
+ }
822
+ });
823
+ source = prototype;
824
+ }
825
+
826
+ if (indexes.size !== i) return null;
776
827
  const arr = new Array(i);
777
828
  while (i-- > 0) {
778
829
  arr[i] = thing[i];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios",
3
- "version": "1.20.0",
3
+ "version": "1.21.0",
4
4
  "description": "Promise based HTTP client for the browser and node.js",
5
5
  "main": "./dist/node/axios.cjs",
6
6
  "module": "./index.js",
@@ -161,7 +161,6 @@
161
161
  "abortcontroller-polyfill": "^1.7.8",
162
162
  "acorn": "^8.17.0",
163
163
  "body-parser": "^2.3.0",
164
- "chalk": "^5.6.2",
165
164
  "cross-env": "^10.1.0",
166
165
  "dev-null": "^0.1.1",
167
166
  "eslint": "^10.6.0",