axios 0.34.0 → 0.36.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.
@@ -4,7 +4,7 @@ var utils = require('./../utils');
4
4
  var settle = require('./../core/settle');
5
5
  var buildFullPath = require('../core/buildFullPath');
6
6
  var buildURL = require('./../helpers/buildURL');
7
- var getProxyForUrl = require('proxy-from-env').getProxyForUrl;
7
+ var getProxyForUrl = require('../helpers/proxyFromEnv').getProxyForUrl;
8
8
  var http = require('http');
9
9
  var https = require('https');
10
10
  var httpFollow = require('follow-redirects/http');
@@ -21,6 +21,7 @@ var fromDataURI = require('../helpers/fromDataURI');
21
21
  var stream = require('stream');
22
22
  var estimateDataURLDecodedBytes = require('../helpers/estimateDataURLDecodedBytes.js');
23
23
  var shouldBypassProxy = require('../helpers/shouldBypassProxy');
24
+ var getDirectAgent = require('../helpers/getDirectAgent');
24
25
 
25
26
  var isHttps = /https:?/;
26
27
 
@@ -28,13 +29,89 @@ var supportedProtocols = platform.protocols.map(function(protocol) {
28
29
  return protocol + ':';
29
30
  });
30
31
 
31
- function dispatchBeforeRedirect(options) {
32
+ // Options that follow-redirects reads but axios does not always set.
33
+ // follow-redirects copies the request options into a plain object, so any of
34
+ // these left unset would be read from Object.prototype. hostname and port are
35
+ // only unset for socketPath requests. Audited against follow-redirects 1.16.3.
36
+ var REDIRECT_OPTION_KEYS = [
37
+ 'host',
38
+ 'hostname',
39
+ 'port',
40
+ 'pathname',
41
+ 'search',
42
+ 'hash',
43
+ 'query',
44
+ 'slashes',
45
+ 'followRedirects',
46
+ 'trackRedirects',
47
+ 'sensitiveHeaders'
48
+ ];
49
+
50
+ // follow-redirects reads these when a response arrives rather than when the
51
+ // request starts, so they are pinned even when nothing is inherited yet.
52
+ var RESPONSE_OPTION_KEYS = ['followRedirects', 'trackRedirects'];
53
+
54
+ /**
55
+ * Give the options follow-redirects reads an own value whenever one would
56
+ * otherwise be inherited. dispatchBeforeRedirect also resets host, auth and
57
+ * query on each redirect hop. Other keys that are not inherited are left
58
+ * unset, so the options object keeps its usual shape.
59
+ *
60
+ * @param {Object} options
61
+ */
62
+ function setRedirectDefaults(options) {
63
+ REDIRECT_OPTION_KEYS.forEach(function setDefault(key) {
64
+ if (utils.hasOwnProperty(options, key)) {
65
+ return;
66
+ }
67
+ if (key in options || RESPONSE_OPTION_KEYS.indexOf(key) !== -1) {
68
+ options[key] = undefined;
69
+ }
70
+ });
71
+ }
72
+
73
+ function dispatchBeforeRedirect(options, responseDetails, requestDetails) {
74
+ // follow-redirects copies auth and query from a WHATWG URL, which has
75
+ // neither, so both would otherwise be read from Object.prototype.
76
+ options.auth = undefined;
77
+ options.query = undefined;
78
+ var sameOrigin = false;
79
+ try {
80
+ sameOrigin = new url.URL(requestDetails.url).origin === new url.URL(options.href).origin;
81
+ } catch (err) {
82
+ // Leave the comparison false when either URL is unavailable.
83
+ }
84
+ if (!sameOrigin) {
85
+ Object.keys(options.headers).forEach(function(header) {
86
+ if (/^(authorization|cookie)$/i.test(header)) {
87
+ delete options.headers[header];
88
+ }
89
+ });
90
+ }
32
91
  if (options.beforeRedirects.proxy) {
33
92
  options.beforeRedirects.proxy(options);
34
93
  }
94
+ if (options.beforeRedirects.direct) {
95
+ options.beforeRedirects.direct(options);
96
+ }
35
97
  if (options.beforeRedirects.config) {
36
98
  options.beforeRedirects.config(options);
37
99
  }
100
+ // follow-redirects deletes a truthy host after this hook returns, which
101
+ // would let the next hop read host from Object.prototype. Fold it into
102
+ // hostname the same way follow-redirects does, but keep an own value.
103
+ if (options.host && !options.hostname) {
104
+ options.hostname = options.host;
105
+ }
106
+ options.host = undefined;
107
+ setRedirectDefaults(options);
108
+ }
109
+
110
+ function setDirectAgent(options, httpAgent, httpsAgent) {
111
+ var secure = isHttps.test(options.protocol);
112
+ var agent = getDirectAgent(secure ? httpsAgent : httpAgent, secure ? https : http);
113
+ options.agent = agent;
114
+ options.agents[secure ? 'https' : 'http'] = agent;
38
115
  }
39
116
 
40
117
  function removeProxyAuthorization(headers) {
@@ -45,17 +122,56 @@ function removeProxyAuthorization(headers) {
45
122
  });
46
123
  }
47
124
 
125
+ // Windows named pipes and Linux abstract sockets (leading NUL byte) are not
126
+ // filesystem paths, so they are compared and connected to as given.
127
+ function isNonFileSocketPath(socketPath) {
128
+ return /^\\\\[.?]\\pipe\\/i.test(socketPath) || socketPath.charAt(0) === '\0';
129
+ }
130
+
48
131
  function normalizeSocketPath(socketPath) {
49
- if (/^\\\\[.?]\\pipe\\/i.test(socketPath)) {
132
+ if (isNonFileSocketPath(socketPath)) {
50
133
  return socketPath;
51
134
  }
52
135
 
53
136
  return path.resolve(socketPath);
54
137
  }
55
138
 
56
- function getAllowedSocketPaths(config) {
57
- var allowedSocketPaths = config.allowedSocketPaths;
139
+ // The longest socket path every supported platform accepts: sun_path is 104
140
+ // bytes on macOS and the BSDs and 108 on Linux, including the trailing NUL.
141
+ var MAX_SOCKET_PATH_BYTES = 103;
142
+
143
+ // The path to connect to when an allowlist is in use. `..` is collapsed
144
+ // lexically, as the allowlist check does, so a symlinked directory cannot send
145
+ // the connection somewhere the check did not allow.
146
+ //
147
+ // The absolute path is used whenever it fits, so the target does not depend on
148
+ // the working directory when the connection opens (an Agent can open it later,
149
+ // after a process.chdir()). Only when the absolute path is too long is the
150
+ // shorter path relative to the working directory used. Either way the result
151
+ // is never longer than the absolute path, because some Node versions silently
152
+ // truncate a socket path that is too long and would connect to a prefix of it.
153
+ function getConnectSocketPath(socketPath) {
154
+ if (isNonFileSocketPath(socketPath)) {
155
+ return socketPath;
156
+ }
157
+
158
+ var resolved = path.resolve(socketPath);
159
+
160
+ if (
161
+ path.isAbsolute(socketPath) ||
162
+ Buffer.byteLength(resolved) <= MAX_SOCKET_PATH_BYTES
163
+ ) {
164
+ return resolved;
165
+ }
166
+
167
+ var relative = path.relative(process.cwd(), resolved) || '.';
58
168
 
169
+ return Buffer.byteLength(relative) < Buffer.byteLength(resolved) ?
170
+ relative :
171
+ resolved;
172
+ }
173
+
174
+ function getAllowedSocketPaths(allowedSocketPaths) {
59
175
  if (
60
176
  allowedSocketPaths === null ||
61
177
  typeof allowedSocketPaths === 'undefined'
@@ -80,9 +196,12 @@ function getAllowedSocketPaths(config) {
80
196
  return false;
81
197
  }
82
198
 
83
- function checkSocketPath(config) {
84
- var socketPath = config.socketPath;
85
-
199
+ // Validates socketPath against allowedSocketPaths and returns the path to
200
+ // connect to, an AxiosError, or null when there is no socketPath. Each config
201
+ // value is read once by the caller, so the path that is connected is the one
202
+ // that was checked. It returns plain values rather than an object so no result
203
+ // field can be inherited from a polluted Object.prototype.
204
+ function resolveSocketPath(socketPath, allowedSocketPaths, config) {
86
205
  if (!socketPath) {
87
206
  return null;
88
207
  }
@@ -95,7 +214,7 @@ function checkSocketPath(config) {
95
214
  );
96
215
  }
97
216
 
98
- var allowedSocketPaths = getAllowedSocketPaths(config);
217
+ allowedSocketPaths = getAllowedSocketPaths(allowedSocketPaths);
99
218
 
100
219
  if (allowedSocketPaths === false) {
101
220
  return new AxiosError(
@@ -105,24 +224,29 @@ function checkSocketPath(config) {
105
224
  );
106
225
  }
107
226
 
108
- if (allowedSocketPaths) {
109
- var normalizedSocketPath = normalizeSocketPath(socketPath);
110
- var allowed = allowedSocketPaths.some(
111
- function isAllowed(allowedSocketPath) {
112
- return normalizeSocketPath(allowedSocketPath) === normalizedSocketPath;
113
- }
114
- );
227
+ if (!allowedSocketPaths) {
228
+ return socketPath;
229
+ }
115
230
 
116
- if (!allowed) {
117
- return new AxiosError(
118
- 'config.socketPath is not allowed by config.allowedSocketPaths',
119
- AxiosError.ERR_BAD_OPTION_VALUE,
120
- config
121
- );
231
+ // The checked value is derived from the path that will be connected, so the
232
+ // two cannot disagree.
233
+ var connectSocketPath = getConnectSocketPath(socketPath);
234
+ var normalizedSocketPath = normalizeSocketPath(connectSocketPath);
235
+ var allowed = allowedSocketPaths.some(
236
+ function isAllowed(allowedSocketPath) {
237
+ return normalizeSocketPath(allowedSocketPath) === normalizedSocketPath;
122
238
  }
239
+ );
240
+
241
+ if (!allowed) {
242
+ return new AxiosError(
243
+ 'config.socketPath is not allowed by config.allowedSocketPaths',
244
+ AxiosError.ERR_BAD_OPTION_VALUE,
245
+ config
246
+ );
123
247
  }
124
248
 
125
- return null;
249
+ return connectSocketPath;
126
250
  }
127
251
 
128
252
  /**
@@ -317,6 +441,18 @@ module.exports = function httpAdapter(config) {
317
441
  throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
318
442
  }
319
443
 
444
+ // Check bytes before converting the result to text or a stream.
445
+ if (config.maxContentLength > -1 &&
446
+ (responseType === 'blob' ? convertedData.size : convertedData.length) > config.maxContentLength) {
447
+ return reject(
448
+ new AxiosError(
449
+ 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
450
+ AxiosError.ERR_BAD_RESPONSE,
451
+ config
452
+ )
453
+ );
454
+ }
455
+
320
456
  if (responseType === 'text') {
321
457
  convertedData = convertedData.toString(responseEncoding);
322
458
 
@@ -451,6 +587,9 @@ module.exports = function httpAdapter(config) {
451
587
  reject(customErr);
452
588
  }
453
589
 
590
+ var configProxy = utils.hasOwnProperty(config, 'proxy') ? config.proxy : undefined;
591
+ var httpAgent = config.httpAgent;
592
+ var httpsAgent = config.httpsAgent;
454
593
  var options = {
455
594
  path: buildURL(
456
595
  parsed.path,
@@ -459,7 +598,7 @@ module.exports = function httpAdapter(config) {
459
598
  ).replace(/^\?/, ''),
460
599
  method: method,
461
600
  headers: headers,
462
- agents: { http: config.httpAgent, https: config.httpsAgent },
601
+ agents: { http: httpAgent, https: httpsAgent },
463
602
  auth: auth,
464
603
  protocol: protocol,
465
604
  beforeRedirect: dispatchBeforeRedirect,
@@ -470,17 +609,27 @@ module.exports = function httpAdapter(config) {
470
609
  beforeRedirects: Object.create(null)
471
610
  };
472
611
 
473
- var socketPathError = checkSocketPath(config);
474
- if (socketPathError) {
475
- return reject(socketPathError);
612
+ var socketTarget = resolveSocketPath(
613
+ config.socketPath,
614
+ config.allowedSocketPaths,
615
+ config
616
+ );
617
+ if (socketTarget instanceof AxiosError) {
618
+ return reject(socketTarget);
476
619
  }
477
620
 
478
- if (config.socketPath) {
479
- options.socketPath = config.socketPath;
621
+ if (socketTarget) {
622
+ options.socketPath = socketTarget;
480
623
  } else {
624
+ // Node 14 and older copy the request options into a plain object and
625
+ // read socketPath from it, so an inherited value would turn this into a
626
+ // socket request. Only pinned when one is inherited, to keep the usual
627
+ // shape of the options object.
628
+ if ('socketPath' in options) {
629
+ options.socketPath = undefined;
630
+ }
481
631
  options.hostname = parsed.hostname;
482
632
  options.port = parsed.port;
483
- var configProxy = utils.hasOwnProperty(config, 'proxy') ? config.proxy : undefined;
484
633
  setProxy(
485
634
  options,
486
635
  configProxy,
@@ -490,7 +639,14 @@ module.exports = function httpAdapter(config) {
490
639
 
491
640
  var transport;
492
641
  var isHttpsRequest = isHttps.test(options.protocol);
493
- options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
642
+ if (configProxy === false) {
643
+ setDirectAgent(options, httpAgent, httpsAgent);
644
+ options.beforeRedirects.direct = function(redirectOptions) {
645
+ setDirectAgent(redirectOptions, httpAgent, httpsAgent);
646
+ };
647
+ } else {
648
+ options.agent = isHttpsRequest ? httpsAgent : httpAgent;
649
+ }
494
650
  if (utils.hasOwnProperty(config, 'transport') && config.transport) {
495
651
  transport = config.transport;
496
652
  } else if (config.maxRedirects === 0) {
@@ -502,6 +658,7 @@ module.exports = function httpAdapter(config) {
502
658
  if (config.beforeRedirect) {
503
659
  options.beforeRedirects.config = config.beforeRedirect;
504
660
  }
661
+ setRedirectDefaults(options);
505
662
  transport = isHttpsRequest ? httpsFollow : httpFollow;
506
663
  }
507
664
 
@@ -769,11 +926,9 @@ module.exports = function httpAdapter(config) {
769
926
  reject(AxiosError.from(err, config, null, req));
770
927
  });
771
928
 
772
- // follow-redirects enforces options.maxBodyLength for stream uploads, but the
773
- // native http/https transport (used when maxRedirects === 0) does not.
774
- // Count bytes ourselves so the limit is always honored.
775
- var nativeTransport = transport === http || transport === https;
776
- if (nativeTransport && config.maxBodyLength > -1) {
929
+ // Transform decodes string chunks to Buffers before counting, so every
930
+ // transport receives at most maxBodyLength encoded bytes.
931
+ if (config.maxBodyLength > -1) {
777
932
  var maxBodyLength = config.maxBodyLength;
778
933
  var uploadedBytes = 0;
779
934
  var bodyLimiter = new stream.Transform({
package/lib/core/Axios.js CHANGED
@@ -88,7 +88,11 @@ Axios.prototype.request = function request(configOrUrl, config) {
88
88
  config = configOrUrl || {};
89
89
  }
90
90
 
91
- config = mergeConfig(this.defaults, config);
91
+ try {
92
+ config = mergeConfig(this.defaults, config);
93
+ } catch (error) {
94
+ return Promise.reject(error);
95
+ }
92
96
  var caller = config.captureCallerStack === true ? captureCallerStack(request) : null;
93
97
 
94
98
  // Set config.method. The merged config is null-prototype, but instance
@@ -214,11 +218,16 @@ utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData
214
218
  Axios.prototype[method] = function(url, config) {
215
219
  var requestConfig = config || {};
216
220
 
217
- return this.request(mergeConfig(requestConfig, {
218
- method: method,
219
- url: url,
220
- data: utils.hasOwnProperty(requestConfig, 'data') ? requestConfig.data : undefined
221
- }));
221
+ try {
222
+ requestConfig = mergeConfig(requestConfig, {
223
+ method: method,
224
+ url: url,
225
+ data: utils.hasOwnProperty(requestConfig, 'data') ? requestConfig.data : undefined
226
+ });
227
+ } catch (error) {
228
+ return Promise.reject(error);
229
+ }
230
+ return this.request(requestConfig);
222
231
  };
223
232
  });
224
233
 
@@ -227,14 +236,20 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
227
236
 
228
237
  function generateHTTPMethod(isForm) {
229
238
  return function httpMethod(url, data, config) {
230
- return this.request(mergeConfig(config || {}, {
231
- method: method,
232
- headers: isForm ? {
233
- 'Content-Type': 'multipart/form-data'
234
- } : {},
235
- url: url,
236
- data: data
237
- }));
239
+ var requestConfig;
240
+ try {
241
+ requestConfig = mergeConfig(config || {}, {
242
+ method: method,
243
+ headers: isForm ? {
244
+ 'Content-Type': 'multipart/form-data'
245
+ } : {},
246
+ url: url,
247
+ data: data
248
+ });
249
+ } catch (error) {
250
+ return Promise.reject(error);
251
+ }
252
+ return this.request(requestConfig);
238
253
  };
239
254
  }
240
255
 
@@ -18,8 +18,8 @@ InterceptorManager.prototype.use = function use(fulfilled, rejected, options) {
18
18
  this.handlers.push({
19
19
  fulfilled: fulfilled,
20
20
  rejected: rejected,
21
- synchronous: options ? options.synchronous : false,
22
- runWhen: options ? options.runWhen : null
21
+ synchronous: options && utils.hasOwnProperty(options, 'synchronous') ? options.synchronous : false,
22
+ runWhen: options && utils.hasOwnProperty(options, 'runWhen') ? options.runWhen : null
23
23
  });
24
24
  return this.handlers.length - 1;
25
25
  };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var utils = require('../utils');
4
+ var assertParamsDepth = require('../helpers/assertParamsDepth');
4
5
 
5
6
  /**
6
7
  * Config-specific merge-function which creates a new config-object
@@ -13,6 +14,8 @@ var utils = require('../utils');
13
14
  module.exports = function mergeConfig(config1, config2) {
14
15
  // eslint-disable-next-line no-param-reassign
15
16
  config2 = config2 || {};
17
+ assertParamsDepth(utils.hasOwnProperty(config1, 'params') ? config1.params : undefined);
18
+ assertParamsDepth(utils.hasOwnProperty(config2, 'params') ? config2.params : undefined);
16
19
  // Use a null-prototype object so a polluted Object.prototype cannot leak
17
20
  // values (e.g. transport, adapter) into the returned config via inheritance.
18
21
  var config = Object.create(null);
package/lib/env/data.js CHANGED
@@ -1,3 +1,3 @@
1
1
  module.exports = {
2
- "version": "0.34.0"
2
+ "version": "0.36.0"
3
3
  };
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ var utils = require('../utils');
4
+ var AxiosError = require('../core/AxiosError');
5
+
6
+ // Match numeric array reads without scanning unoccupied slots or metadata.
7
+ function arrayIndexes(array) {
8
+ var length = array.length;
9
+ var indexes = Object.create(null);
10
+ var keys = [];
11
+ var source = array;
12
+ var fast = array;
13
+ while (source) {
14
+ var names = Object.getOwnPropertyNames(source);
15
+ for (var i = 0; i < names.length; i++) {
16
+ var key = names[i];
17
+ var index = +key;
18
+ if (index >= 0 && index < length && Math.floor(index) === index &&
19
+ String(index) === key && !indexes[key]) {
20
+ indexes[key] = true;
21
+ keys.push(key);
22
+ }
23
+ }
24
+ source = Object.getPrototypeOf(source);
25
+ // Detect unusual cyclic prototype chains with constant memory.
26
+ fast = fast && Object.getPrototypeOf(fast);
27
+ fast = fast && Object.getPrototypeOf(fast);
28
+ if (source && source === fast) {
29
+ throw new AxiosError('Circular prototype chain in params', AxiosError.ERR_BAD_OPTION_VALUE);
30
+ }
31
+ }
32
+ return keys.sort(function(a, b) { return a - b; });
33
+ }
34
+
35
+ module.exports = function assertParamsDepth(params) {
36
+ if (!utils.isObject(params)) return;
37
+ var pending = [{ value: params, depth: 0, complete: false }];
38
+ var ancestors = [];
39
+ var visited = null;
40
+ if (typeof WeakMap === 'function') {
41
+ visited = new WeakMap();
42
+ } else if (typeof Map === 'function') {
43
+ visited = new Map();
44
+ }
45
+ // Older runtimes can only compare immutable object identities by scanning.
46
+ // Keep that scan bounded; recently completed subgraphs avoid repeated work,
47
+ // while evicted shared subgraphs may be visited again.
48
+ var cacheSize = 128;
49
+ var visitedValues = [];
50
+ var visitedDepths = [];
51
+ var nextCacheIndex = 0;
52
+ while (pending.length) {
53
+ var entry = pending.pop();
54
+ var value = entry.value;
55
+ var index;
56
+ if (entry.complete) {
57
+ ancestors.pop();
58
+ if (visited) {
59
+ visited.set(value, entry.depth);
60
+ } else {
61
+ index = visitedValues.indexOf(value);
62
+ if (index === -1) {
63
+ index = nextCacheIndex;
64
+ nextCacheIndex = (nextCacheIndex + 1) % cacheSize;
65
+ }
66
+ visitedValues[index] = value;
67
+ visitedDepths[index] = entry.depth;
68
+ }
69
+ continue;
70
+ }
71
+ // The serializer enumerates any object at its root, then recursively
72
+ // visits only plain objects and arrays below that root.
73
+ if (entry.depth === 0 ? !utils.isObject(value) :
74
+ !utils.isPlainObject(value) && !utils.isArray(value)) {
75
+ continue;
76
+ }
77
+ if (entry.depth > 100) {
78
+ throw new AxiosError('params exceed the maximum depth of 100', AxiosError.ERR_BAD_OPTION_VALUE);
79
+ }
80
+ // The depth cap bounds this ancestry scan independently of params width.
81
+ if (ancestors.indexOf(value) !== -1) {
82
+ throw new AxiosError('Circular reference detected in params', AxiosError.ERR_BAD_OPTION_VALUE);
83
+ }
84
+ if (visited) {
85
+ if (visited.has(value) && visited.get(value) >= entry.depth) continue;
86
+ } else {
87
+ index = visitedValues.indexOf(value);
88
+ if (index !== -1 && visitedDepths[index] >= entry.depth) continue;
89
+ }
90
+ ancestors.push(value);
91
+ pending.push({ value: value, depth: entry.depth, complete: true });
92
+ var keys = utils.isArray(value) ? arrayIndexes(value) : Object.keys(value);
93
+ for (var i = 0; i < keys.length; i++) {
94
+ var key = keys[i];
95
+ if (key !== '__proto__' && key !== 'constructor' && key !== 'prototype') {
96
+ pending.push({ value: value[key], depth: entry.depth + 1, complete: false });
97
+ }
98
+ }
99
+ }
100
+ };
@@ -8,7 +8,19 @@
8
8
  * @returns {string} The combined URL
9
9
  */
10
10
  module.exports = function combineURLs(baseURL, relativeURL) {
11
- return relativeURL
12
- ? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '')
13
- : baseURL;
11
+ if (!relativeURL) {
12
+ return baseURL;
13
+ }
14
+
15
+ var base = baseURL.replace(/\/?\/$/, '');
16
+
17
+ if (/^\/*$/.test(base)) {
18
+ // A base made only of slashes points at the root of the current origin.
19
+ // Drop leading slashes, backslashes, tabs and newlines from the relative
20
+ // URL as well: URL parsers ignore tabs and newlines and treat '\' as '/',
21
+ // so leaving them would turn the result into a protocol-relative URL.
22
+ return '/' + relativeURL.replace(/^[\/\\\t\n\r]+/, '');
23
+ }
24
+
25
+ return base + '/' + relativeURL.replace(/^\/+/, '');
14
26
  };
@@ -18,7 +18,8 @@ function estimateDataURLDecodedBytes(url) {
18
18
 
19
19
  var meta = url.slice(5, comma);
20
20
  var body = url.slice(comma + 1);
21
- var isBase64 = /;base64/i.test(meta);
21
+ // Match the exact encoding suffix accepted by fromDataURI.
22
+ var isBase64 = /(?:^|;)base64$/.test(meta);
22
23
 
23
24
  if (isBase64) {
24
25
  var effectiveLen = body.length;
@@ -68,8 +69,7 @@ function estimateDataURLDecodedBytes(url) {
68
69
  }
69
70
  }
70
71
 
71
- var groups = Math.floor(effectiveLen / 4);
72
- var bytes = groups * 3 - (pad || 0);
72
+ var bytes = Math.floor((effectiveLen - pad) * 3 / 4);
73
73
  return bytes > 0 ? bytes : 0;
74
74
  }
75
75
 
@@ -39,13 +39,50 @@ function arrayToObject(arr) {
39
39
  return obj;
40
40
  }
41
41
 
42
+ function normalizeArray(value) {
43
+ if (!utils.isArray(value)) return value;
44
+
45
+ var keys = Object.keys(value);
46
+ if (keys.length !== value.length) return arrayToObject(value);
47
+
48
+ for (var i = 0; i < keys.length; i++) {
49
+ var index = +keys[i];
50
+ if (
51
+ index < 0 || index >= value.length ||
52
+ Math.floor(index) !== index || String(index) !== keys[i]
53
+ ) {
54
+ return arrayToObject(value);
55
+ }
56
+ }
57
+
58
+ return value;
59
+ }
60
+
61
+ function normalizeArrays(obj) {
62
+ var pending = [obj];
63
+ function normalizeEntry(target, key) {
64
+ var value = target[key] = normalizeArray(target[key]);
65
+ if (utils.isArray(value) || utils.isPlainObject(value)) {
66
+ pending.push(value);
67
+ }
68
+ }
69
+ while (pending.length) {
70
+ var target = pending.pop();
71
+ var keys = Object.keys(target);
72
+ for (var i = 0; i < keys.length; i++) {
73
+ normalizeEntry(target, keys[i]);
74
+ }
75
+ }
76
+ return obj;
77
+ }
78
+
42
79
  function formDataToJSON(formData) {
43
80
  function buildPath(path, value, target, index) {
44
81
  var name = path[index++];
45
82
 
46
83
  if (name === '__proto__') return true;
47
84
 
48
- var isNumericKey = Number.isFinite(+name);
85
+ var isNumericKey = isFinite(+name);
49
86
  var isLast = index >= path.length;
50
87
  name = !name && utils.isArray(target) ? target.length : name;
51
88
 
@@ -59,7 +96,7 @@ function formDataToJSON(formData) {
59
96
  return !isNumericKey;
60
97
  }
61
98
 
62
- if (!target[name] || !utils.isObject(target[name])) {
99
+ if (!utils.hasOwnProperty(target, name) || !utils.isObject(target[name])) {
63
100
  target[name] = [];
64
101
  }
65
102
 
@@ -82,7 +119,7 @@ function formDataToJSON(formData) {
82
119
  buildPath(path, value, obj, 0);
83
120
  });
84
121
 
85
- return obj;
122
+ return normalizeArrays(obj);
86
123
  }
87
124
 
88
125
  return null;