axios 0.33.0 → 0.35.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.
- package/CHANGELOG.md +20 -0
- package/README.md +70 -1
- package/dist/axios.js +416 -50
- package/dist/axios.js.map +1 -1
- package/dist/axios.min.js +1 -1
- package/dist/axios.min.js.map +1 -1
- package/dist/esm/axios.js +416 -50
- package/dist/esm/axios.js.map +1 -1
- package/dist/esm/axios.min.js +1 -1
- package/dist/esm/axios.min.js.map +1 -1
- package/index.d.ts +5 -1
- package/lib/adapters/http.js +107 -14
- package/lib/adapters/xhr.js +2 -2
- package/lib/cancel/fromSignal.js +20 -0
- package/lib/core/Axios.js +107 -24
- package/lib/core/dispatchRequest.js +5 -4
- package/lib/core/mergeConfig.js +10 -0
- package/lib/env/data.js +1 -1
- package/lib/helpers/assertParamsDepth.js +100 -0
- package/lib/helpers/estimateDataURLDecodedBytes.js +3 -3
- package/lib/helpers/formDataToJSON.js +40 -3
- package/lib/helpers/getDirectAgent.js +85 -0
- package/lib/helpers/shouldBypassProxy.js +229 -2
- package/lib/helpers/toFormData.js +39 -12
- package/lib/utils.js +101 -5
- package/package.json +1 -1
|
@@ -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 =
|
|
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
|
|
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;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils');
|
|
4
|
+
var AxiosError = require('../core/AxiosError');
|
|
5
|
+
var bind = require('./bind');
|
|
6
|
+
var directAgents = new WeakMap();
|
|
7
|
+
var directHooks = ['createConnection', 'getName'];
|
|
8
|
+
var nativePoolHooks = [
|
|
9
|
+
'addRequest', 'createSocket', 'removeSocket', 'keepSocketAlive', 'reuseSocket',
|
|
10
|
+
'_getSession', '_cacheSession', '_evictSession'
|
|
11
|
+
];
|
|
12
|
+
var agentControls = [
|
|
13
|
+
'keepAlive', 'keepAliveMsecs', 'maxSockets', 'maxFreeSockets',
|
|
14
|
+
'maxTotalSockets', 'scheduling', 'maxCachedSessions',
|
|
15
|
+
'agentKeepAliveTimeoutBuffer', 'defaultPort'
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
module.exports = function getDirectAgent(configuredAgent, transport) {
|
|
19
|
+
if (!process.allowedNodeEnvironmentFlags ||
|
|
20
|
+
!process.allowedNodeEnvironmentFlags.has('--use-env-proxy')) {
|
|
21
|
+
return configuredAgent;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var agent = configuredAgent || transport.globalAgent;
|
|
25
|
+
var options = agent && agent.options;
|
|
26
|
+
if (!(agent instanceof transport.Agent) ||
|
|
27
|
+
agent.protocol !== transport.globalAgent.protocol) {
|
|
28
|
+
return configuredAgent;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Node snapshots proxyEnv at construction. Once adapted, retain the direct
|
|
32
|
+
// pool even if callers remove or replace the source's public proxy options.
|
|
33
|
+
var direct = directAgents.get(agent);
|
|
34
|
+
if (!direct && (!options || !utils.hasOwnProperty(options, 'proxyEnv') ||
|
|
35
|
+
typeof options.proxyEnv !== 'object' || options.proxyEnv === null)) {
|
|
36
|
+
return configuredAgent;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Pool lifecycle hooks mutate their receiver's queues and socket state.
|
|
40
|
+
// Binding them to the source pool or copying them without subclass state
|
|
41
|
+
// cannot preserve their behavior on an independently managed direct pool.
|
|
42
|
+
for (var i = 0; i < nativePoolHooks.length; i++) {
|
|
43
|
+
var hook = nativePoolHooks[i];
|
|
44
|
+
if (utils.isFunction(transport.Agent.prototype[hook]) && agent[hook] !== transport.Agent.prototype[hook]) {
|
|
45
|
+
throw new AxiosError(
|
|
46
|
+
'proxy: false cannot adapt an agent overriding ' + hook + '; use an agent constructed without proxyEnv',
|
|
47
|
+
AxiosError.ERR_BAD_OPTION_VALUE
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
var directOptions = Object.assign(Object.create(null), options, { proxyEnv: undefined });
|
|
53
|
+
if (!direct) {
|
|
54
|
+
direct = new transport.Agent(directOptions);
|
|
55
|
+
// The configured agent remains the application's cleanup handle for both
|
|
56
|
+
// pools, including when requests are made after destroy().
|
|
57
|
+
var destroy = agent.destroy;
|
|
58
|
+
agent.destroy = function destroyWithDirectAgent() {
|
|
59
|
+
try {
|
|
60
|
+
return destroy.apply(this, arguments);
|
|
61
|
+
} finally {
|
|
62
|
+
direct.destroy();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
directAgents.set(agent, direct);
|
|
66
|
+
} else {
|
|
67
|
+
direct.options = directOptions;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// These hooks supply connections and pool keys without owning the direct
|
|
71
|
+
// pool's queues. Preserve the source receiver, including subclass state.
|
|
72
|
+
directHooks.forEach(function(key) {
|
|
73
|
+
direct[key] = agent[key] === transport.Agent.prototype[key]
|
|
74
|
+
? transport.Agent.prototype[key] : bind(agent[key], agent);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Public pool controls can change after construction or between requests.
|
|
78
|
+
// Keep the cached pool in step with the application-owned agent.
|
|
79
|
+
agentControls.forEach(function(key) {
|
|
80
|
+
if (utils.hasOwnProperty(agent, key)) {
|
|
81
|
+
direct[key] = agent[key];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return direct;
|
|
85
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var URL = require('url').URL;
|
|
4
|
+
var domainToASCII = require('url').domainToASCII;
|
|
4
5
|
|
|
5
6
|
var DEFAULT_PORTS = {
|
|
6
7
|
http: 80,
|
|
@@ -40,6 +41,15 @@ function parseNoProxyEntry(entry) {
|
|
|
40
41
|
return [entryHost, entryPort];
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
// Parses a dotted-quad into its four octet strings, or null when the input is
|
|
45
|
+
// not an unambiguous decimal IPv4 address.
|
|
46
|
+
//
|
|
47
|
+
// Octets with a leading zero are rejected. The Node URL parser reads them the
|
|
48
|
+
// way inet_aton does, so `http://010.0.0.1/` arrives here as `8.0.0.1`, while a
|
|
49
|
+
// decimal read of the same literal would be `10.0.0.1`. Accepting the ambiguous
|
|
50
|
+
// form would let a no_proxy entry match a host the operator did not name.
|
|
51
|
+
// Rejecting fails closed: the entry falls back to literal hostname comparison,
|
|
52
|
+
// which will not match, so the request keeps using the proxy.
|
|
43
53
|
function parseIPv4Octets(hostname) {
|
|
44
54
|
var octets = hostname.split('.');
|
|
45
55
|
|
|
@@ -51,6 +61,10 @@ function parseIPv4Octets(hostname) {
|
|
|
51
61
|
if (!/^\d+$/.test(octets[i]) || Number(octets[i]) > 255) {
|
|
52
62
|
return null;
|
|
53
63
|
}
|
|
64
|
+
|
|
65
|
+
if (octets[i].length > 1 && octets[i].charAt(0) === '0') {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
54
68
|
}
|
|
55
69
|
|
|
56
70
|
return octets;
|
|
@@ -92,6 +106,20 @@ function normalizeIPv4MappedIPv6(hostname) {
|
|
|
92
106
|
return hostname;
|
|
93
107
|
}
|
|
94
108
|
|
|
109
|
+
// Trailing dots are trimmed without a regular expression. An anchored `/\.+$/`
|
|
110
|
+
// backtracks quadratically over a long run of dots that is not at the end of
|
|
111
|
+
// the string, and hostnames reaching this helper include redirect Location
|
|
112
|
+
// values, which are attacker-controlled.
|
|
113
|
+
function stripTrailingDots(hostname) {
|
|
114
|
+
var end = hostname.length;
|
|
115
|
+
|
|
116
|
+
while (end > 0 && hostname.charAt(end - 1) === '.') {
|
|
117
|
+
end--;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return end === hostname.length ? hostname : hostname.slice(0, end);
|
|
121
|
+
}
|
|
122
|
+
|
|
95
123
|
function normalizeNoProxyHost(hostname) {
|
|
96
124
|
if (!hostname) {
|
|
97
125
|
return hostname;
|
|
@@ -101,11 +129,203 @@ function normalizeNoProxyHost(hostname) {
|
|
|
101
129
|
hostname = hostname.slice(1, -1);
|
|
102
130
|
}
|
|
103
131
|
|
|
104
|
-
hostname = hostname
|
|
132
|
+
hostname = stripTrailingDots(hostname);
|
|
133
|
+
|
|
134
|
+
if (hostname.indexOf(':') !== -1) {
|
|
135
|
+
try {
|
|
136
|
+
hostname = new URL('http://[' + hostname + ']/').hostname.slice(1, -1);
|
|
137
|
+
} catch (err) {
|
|
138
|
+
return hostname;
|
|
139
|
+
}
|
|
140
|
+
} else if (/[\u0080-\uffff]/.test(hostname)) {
|
|
141
|
+
var prefix = /^[*.]+/.exec(hostname);
|
|
142
|
+
var start = prefix ? prefix[0] : '';
|
|
143
|
+
var ascii = domainToASCII(hostname.slice(start.length));
|
|
144
|
+
if (!ascii) return '';
|
|
145
|
+
hostname = start + stripTrailingDots(ascii);
|
|
146
|
+
}
|
|
105
147
|
|
|
106
148
|
return normalizeIPv4MappedIPv6(hostname);
|
|
107
149
|
}
|
|
108
150
|
|
|
151
|
+
function ipv4ToBytes(hostname) {
|
|
152
|
+
var octets = parseIPv4Octets(hostname);
|
|
153
|
+
|
|
154
|
+
if (!octets) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return [Number(octets[0]), Number(octets[1]), Number(octets[2]), Number(octets[3])];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function pushIPv6Group(bytes, group) {
|
|
162
|
+
if (!/^[0-9a-f]{1,4}$/.test(group)) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
var value = parseInt(group, 16);
|
|
167
|
+
|
|
168
|
+
bytes.push((value >> 8) & 0xff, value & 0xff);
|
|
169
|
+
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function ipv6ToBytes(hostname) {
|
|
174
|
+
var halves = hostname.split('::');
|
|
175
|
+
|
|
176
|
+
if (halves.length > 2) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
var compressed = halves.length === 2;
|
|
181
|
+
var head = halves[0] ? halves[0].split(':') : [];
|
|
182
|
+
var tail = compressed && halves[1] ? halves[1].split(':') : [];
|
|
183
|
+
|
|
184
|
+
// A dotted quad may only stand for the final 32 bits of an address, so it has
|
|
185
|
+
// to be the last group in the text. When `::` follows the head, the head is not
|
|
186
|
+
// the end of the address and a dotted quad there is malformed. Expanding it
|
|
187
|
+
// anyway read `1.2.3.4::/64` as the unrelated network 102:304::/64, so a
|
|
188
|
+
// mistyped entry silently bypassed the proxy for hosts the operator never
|
|
189
|
+
// listed. Returning null fails closed: the entry falls back to hostname
|
|
190
|
+
// comparison, which never matches a range.
|
|
191
|
+
if (compressed && !tail.length && head.length &&
|
|
192
|
+
head[head.length - 1].indexOf('.') !== -1) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
var groups = tail.length ? tail : head;
|
|
197
|
+
|
|
198
|
+
// Expand a trailing dotted-quad tail such as ::ffff:127.0.0.1 into two groups.
|
|
199
|
+
if (groups.length && groups[groups.length - 1].indexOf('.') !== -1) {
|
|
200
|
+
var quad = ipv4ToBytes(groups[groups.length - 1]);
|
|
201
|
+
|
|
202
|
+
if (!quad) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
groups.pop();
|
|
207
|
+
groups.push((((quad[0] << 8) | quad[1]) >>> 0).toString(16));
|
|
208
|
+
groups.push((((quad[2] << 8) | quad[3]) >>> 0).toString(16));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
var headBytes = [];
|
|
212
|
+
var tailBytes = [];
|
|
213
|
+
var i;
|
|
214
|
+
|
|
215
|
+
for (i = 0; i < head.length; i++) {
|
|
216
|
+
if (!pushIPv6Group(headBytes, head[i])) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (i = 0; i < tail.length; i++) {
|
|
222
|
+
if (!pushIPv6Group(tailBytes, tail[i])) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var missing = 16 - headBytes.length - tailBytes.length;
|
|
228
|
+
|
|
229
|
+
// `::` stands for one or more groups of zeros, so a compressed address that
|
|
230
|
+
// already spells out all eight groups is malformed. Accepting it let
|
|
231
|
+
// `1:2:3:4:5:6:7:8::/0` parse as a valid /0 network and bypass the proxy for
|
|
232
|
+
// every IPv6 destination.
|
|
233
|
+
if (compressed ? missing <= 0 : missing !== 0) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
for (i = 0; i < missing; i++) {
|
|
238
|
+
headBytes.push(0);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return headBytes.concat(tailBytes);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function hostToBytes(hostname) {
|
|
245
|
+
return hostname.indexOf(':') !== -1 ? ipv6ToBytes(hostname) : ipv4ToBytes(hostname);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Parses a CIDR-form no_proxy entry such as 10.0.0.0/8 or [fd00::]/8.
|
|
249
|
+
// Returns null for anything that is not a well-formed network range so the
|
|
250
|
+
// caller can fall back to hostname matching.
|
|
251
|
+
//
|
|
252
|
+
// Range entries are matched on address alone. Unlike host entries they carry no
|
|
253
|
+
// port, because a trailing `:port` leaves a prefix that is not a bare number and
|
|
254
|
+
// so falls back to hostname matching, which will not match a range.
|
|
255
|
+
//
|
|
256
|
+
// An IPv4-mapped IPv6 host is normalised to its IPv4 form before it reaches
|
|
257
|
+
// here, so list those addresses as IPv4 ranges (10.0.0.0/8), not as
|
|
258
|
+
// ::ffff:0:0/96, which will not match.
|
|
259
|
+
function parseCIDREntry(entry) {
|
|
260
|
+
var slashIndex = entry.indexOf('/');
|
|
261
|
+
|
|
262
|
+
if (slashIndex === -1) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
var prefixText = entry.slice(slashIndex + 1);
|
|
267
|
+
|
|
268
|
+
if (!/^\d{1,3}$/.test(prefixText)) {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var host = entry.slice(0, slashIndex);
|
|
273
|
+
|
|
274
|
+
if (host.charAt(0) === '[' && host.charAt(host.length - 1) === ']') {
|
|
275
|
+
host = host.slice(1, -1);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
var bytes = hostToBytes(host);
|
|
279
|
+
|
|
280
|
+
if (!bytes) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
var prefix = parseInt(prefixText, 10);
|
|
285
|
+
|
|
286
|
+
if (prefix > bytes.length * 8) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return [bytes, prefix];
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function isInCIDR(hostname, cidr) {
|
|
294
|
+
var networkBytes = cidr[0];
|
|
295
|
+
var prefix = cidr[1];
|
|
296
|
+
var bytes = hostToBytes(hostname);
|
|
297
|
+
|
|
298
|
+
if (!bytes || bytes.length !== networkBytes.length) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
var index = 0;
|
|
303
|
+
var remaining = prefix;
|
|
304
|
+
|
|
305
|
+
while (remaining >= 8) {
|
|
306
|
+
if (bytes[index] !== networkBytes[index]) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
remaining -= 8;
|
|
311
|
+
index++;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (remaining > 0) {
|
|
315
|
+
var mask = (0xff << (8 - remaining)) & 0xff;
|
|
316
|
+
|
|
317
|
+
if ((bytes[index] & mask) !== (networkBytes[index] & mask)) {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Deliberately more permissive than parseIPv4Octets: this only asks whether the
|
|
326
|
+
// first octet is exactly 127, and a leading zero in any later octet cannot
|
|
327
|
+
// change that answer, so a hand-written no_proxy entry like 127.000.000.001
|
|
328
|
+
// still resolves to loopback.
|
|
109
329
|
function isLoopbackIPv4(hostname) {
|
|
110
330
|
var octets = hostname.split('.');
|
|
111
331
|
|
|
@@ -135,7 +355,8 @@ module.exports = function shouldBypassProxy(location) {
|
|
|
135
355
|
return false;
|
|
136
356
|
}
|
|
137
357
|
|
|
138
|
-
var noProxy = (process.env.
|
|
358
|
+
var noProxy = (process.env.npm_config_no_proxy || process.env.NPM_CONFIG_NO_PROXY ||
|
|
359
|
+
process.env.no_proxy || process.env.NO_PROXY || '').toLowerCase();
|
|
139
360
|
|
|
140
361
|
if (!noProxy) {
|
|
141
362
|
return false;
|
|
@@ -154,6 +375,12 @@ module.exports = function shouldBypassProxy(location) {
|
|
|
154
375
|
return false;
|
|
155
376
|
}
|
|
156
377
|
|
|
378
|
+
var cidr = parseCIDREntry(entry);
|
|
379
|
+
|
|
380
|
+
if (cidr) {
|
|
381
|
+
return isInCIDR(hostname, cidr);
|
|
382
|
+
}
|
|
383
|
+
|
|
157
384
|
var entryParts = parseNoProxyEntry(entry);
|
|
158
385
|
var entryHost = normalizeNoProxyHost(entryParts[0]);
|
|
159
386
|
var entryPort = entryParts[1];
|
|
@@ -33,6 +33,40 @@ function isSpecCompliant(thing) {
|
|
|
33
33
|
return thing && utils.isFunction(thing.append) && thing[Symbol.toStringTag] === 'FormData' && thing[Symbol.iterator];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
var UNSAFE_OPTION_KEYS = ['__proto__', 'constructor', 'prototype'];
|
|
37
|
+
|
|
38
|
+
// Materialises the serializer options onto a null-prototype object. Reading the
|
|
39
|
+
// options from a plain object let a polluted Object.prototype supply a visitor,
|
|
40
|
+
// a depth limit, a Blob implementation or key-format flags that the caller never
|
|
41
|
+
// asked for, and also let a polluted key mask an option the caller did set.
|
|
42
|
+
// Only own, defined properties of the caller's options are carried over.
|
|
43
|
+
function toFormDataOptions(source) {
|
|
44
|
+
var options = Object.create(null);
|
|
45
|
+
|
|
46
|
+
options.metaTokens = true;
|
|
47
|
+
options.dots = false;
|
|
48
|
+
options.indexes = false;
|
|
49
|
+
|
|
50
|
+
// eslint-disable-next-line no-eq-null,eqeqeq
|
|
51
|
+
if (source == null) {
|
|
52
|
+
return options;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var names = Object.getOwnPropertyNames(source);
|
|
56
|
+
|
|
57
|
+
for (var i = 0; i < names.length; i++) {
|
|
58
|
+
var name = names[i];
|
|
59
|
+
|
|
60
|
+
if (UNSAFE_OPTION_KEYS.indexOf(name) !== -1 || utils.isUndefined(source[name])) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
options[name] = source[name];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return options;
|
|
68
|
+
}
|
|
69
|
+
|
|
36
70
|
/**
|
|
37
71
|
* Convert a data object to FormData
|
|
38
72
|
* @param {Object} obj
|
|
@@ -54,14 +88,7 @@ function toFormData(obj, formData, options) {
|
|
|
54
88
|
formData = formData || new (envFormData || FormData)();
|
|
55
89
|
|
|
56
90
|
// eslint-disable-next-line no-param-reassign
|
|
57
|
-
options =
|
|
58
|
-
metaTokens: true,
|
|
59
|
-
dots: false,
|
|
60
|
-
indexes: false
|
|
61
|
-
}, false, function defined(option, source) {
|
|
62
|
-
// eslint-disable-next-line no-eq-null,eqeqeq
|
|
63
|
-
return !utils.isUndefined(source[option]);
|
|
64
|
-
});
|
|
91
|
+
options = toFormDataOptions(options);
|
|
65
92
|
|
|
66
93
|
var metaTokens = options.metaTokens;
|
|
67
94
|
// eslint-disable-next-line no-use-before-define
|
|
@@ -108,7 +135,7 @@ function toFormData(obj, formData, options) {
|
|
|
108
135
|
return;
|
|
109
136
|
}
|
|
110
137
|
|
|
111
|
-
stack.
|
|
138
|
+
utils.defineOwnProperty(stack, stack.length, value);
|
|
112
139
|
|
|
113
140
|
utils.forEach(value, function each(el) {
|
|
114
141
|
assertValueDepth(el, depth + 1);
|
|
@@ -137,8 +164,8 @@ function toFormData(obj, formData, options) {
|
|
|
137
164
|
value = JSON.stringify(value);
|
|
138
165
|
} else if (
|
|
139
166
|
(utils.isArray(value) && isFlatArray(value)) ||
|
|
140
|
-
(utils.isFileList(value) || utils.endsWith(key, '[]') && (arr = utils.toArray(value))
|
|
141
|
-
|
|
167
|
+
((utils.isFileList(value) || utils.endsWith(key, '[]')) && (arr = utils.toArray(value)))
|
|
168
|
+
) {
|
|
142
169
|
// eslint-disable-next-line no-param-reassign
|
|
143
170
|
key = removeBrackets(key);
|
|
144
171
|
|
|
@@ -185,7 +212,7 @@ function toFormData(obj, formData, options) {
|
|
|
185
212
|
throw Error('Circular reference detected in ' + path.join('.'));
|
|
186
213
|
}
|
|
187
214
|
|
|
188
|
-
stack.
|
|
215
|
+
utils.defineOwnProperty(stack, stack.length, value);
|
|
189
216
|
|
|
190
217
|
utils.forEach(value, function each(el, key) {
|
|
191
218
|
var result = !(utils.isUndefined(el) || el === null) && visitor.call(
|
package/lib/utils.js
CHANGED
|
@@ -393,6 +393,15 @@ function stripBOM(content) {
|
|
|
393
393
|
return content;
|
|
394
394
|
}
|
|
395
395
|
|
|
396
|
+
function defineOwnProperty(target, key, value) {
|
|
397
|
+
var descriptor = Object.create(null);
|
|
398
|
+
descriptor.value = value;
|
|
399
|
+
descriptor.writable = true;
|
|
400
|
+
descriptor.enumerable = true;
|
|
401
|
+
descriptor.configurable = true;
|
|
402
|
+
Object.defineProperty(target, key, descriptor);
|
|
403
|
+
}
|
|
404
|
+
|
|
396
405
|
/**
|
|
397
406
|
* Inherit the prototype methods from one constructor into another
|
|
398
407
|
* @param {function} constructor
|
|
@@ -400,14 +409,35 @@ function stripBOM(content) {
|
|
|
400
409
|
* @param {object} [props]
|
|
401
410
|
* @param {object} [descriptors]
|
|
402
411
|
*/
|
|
403
|
-
|
|
404
412
|
function inherits(constructor, superConstructor, props, descriptors) {
|
|
405
413
|
constructor.prototype = Object.create(
|
|
406
414
|
superConstructor.prototype,
|
|
407
415
|
descriptors
|
|
408
416
|
);
|
|
409
|
-
|
|
410
|
-
|
|
417
|
+
if (Object.prototype.hasOwnProperty.call(constructor.prototype, 'constructor')) {
|
|
418
|
+
constructor.prototype.constructor = constructor;
|
|
419
|
+
} else {
|
|
420
|
+
defineOwnProperty(constructor.prototype, 'constructor', constructor);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (props) {
|
|
424
|
+
var keys = Object.keys(props);
|
|
425
|
+
if (Object.getOwnPropertySymbols) {
|
|
426
|
+
keys = keys.concat(Object.getOwnPropertySymbols(props).filter(function(key) {
|
|
427
|
+
return Object.prototype.propertyIsEnumerable.call(props, key);
|
|
428
|
+
}));
|
|
429
|
+
}
|
|
430
|
+
keys.forEach(function(key) {
|
|
431
|
+
var value = props[key];
|
|
432
|
+
if (Object.prototype.hasOwnProperty.call(constructor.prototype, key)) {
|
|
433
|
+
constructor.prototype[key] = value;
|
|
434
|
+
} else {
|
|
435
|
+
// Assignment would invoke an inherited setter or fail on a read-only
|
|
436
|
+
// property installed by another library, such as Error.prototype.toJSON.
|
|
437
|
+
defineOwnProperty(constructor.prototype, key, value);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
411
441
|
}
|
|
412
442
|
|
|
413
443
|
/**
|
|
@@ -478,10 +508,75 @@ function toArray(thing) {
|
|
|
478
508
|
if (!thing) return null;
|
|
479
509
|
if (isArray(thing)) return thing;
|
|
480
510
|
var i = thing.length;
|
|
481
|
-
if (
|
|
511
|
+
if (typeof i !== 'number' || i < 0 || i > 0xffffffff || Math.floor(i) !== i) return null;
|
|
512
|
+
|
|
513
|
+
// Legacy FileList hosts expose indexed reads without reflecting their keys.
|
|
514
|
+
if (isFileList(thing)) {
|
|
515
|
+
var files = [];
|
|
516
|
+
while (i-- > 0) {
|
|
517
|
+
var file = thing[i];
|
|
518
|
+
if (file === null || isUndefined(file)) return null;
|
|
519
|
+
defineOwnProperty(files, i, file);
|
|
520
|
+
}
|
|
521
|
+
return files;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// Count actual indexes, including hidden collection properties, before
|
|
525
|
+
// allocating from the declared length. Box primitives for ES5 object APIs.
|
|
526
|
+
var source = Object(thing);
|
|
527
|
+
var indexes = Object.create(null);
|
|
528
|
+
var count = 0;
|
|
529
|
+
var seen = [];
|
|
530
|
+
var owns = Object.prototype.hasOwnProperty;
|
|
531
|
+
|
|
532
|
+
while (source && count < i) {
|
|
533
|
+
if (seen.indexOf(source) !== -1) return null;
|
|
534
|
+
defineOwnProperty(seen, seen.length, source);
|
|
535
|
+
|
|
536
|
+
if (source === Object.prototype) break;
|
|
537
|
+
|
|
538
|
+
var prototype = Object.getPrototypeOf(source);
|
|
539
|
+
if (prototype === null) {
|
|
540
|
+
var constructorDescriptor = Object.getOwnPropertyDescriptor(source, 'constructor');
|
|
541
|
+
var constructorValue = constructorDescriptor && owns.call(constructorDescriptor, 'value') &&
|
|
542
|
+
constructorDescriptor.value;
|
|
543
|
+
var constructorPrototype = typeof constructorValue === 'function' &&
|
|
544
|
+
Object.getOwnPropertyDescriptor(constructorValue, 'prototype');
|
|
545
|
+
|
|
546
|
+
// Recognize another realm's Object.prototype without excluding
|
|
547
|
+
// application-defined null-prototype parents or invoking accessors.
|
|
548
|
+
if (
|
|
549
|
+
constructorPrototype && owns.call(constructorPrototype, 'value') &&
|
|
550
|
+
constructorPrototype.value === source &&
|
|
551
|
+
Function.prototype.toString.call(constructorValue) === Function.prototype.toString.call(Object)
|
|
552
|
+
) {
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
var keys = Object.getOwnPropertyNames(source);
|
|
558
|
+
for (var j = 0; j < keys.length; j++) {
|
|
559
|
+
var key = keys[j];
|
|
560
|
+
var index = +key;
|
|
561
|
+
if (
|
|
562
|
+
index >= 0 &&
|
|
563
|
+
index < i &&
|
|
564
|
+
Math.floor(index) === index &&
|
|
565
|
+
String(index) === key &&
|
|
566
|
+
owns.call(source, key) &&
|
|
567
|
+
!indexes[key]
|
|
568
|
+
) {
|
|
569
|
+
indexes[key] = true;
|
|
570
|
+
count++;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
source = prototype;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (count !== i) return null;
|
|
482
577
|
var arr = new Array(i);
|
|
483
578
|
while (i-- > 0) {
|
|
484
|
-
arr
|
|
579
|
+
defineOwnProperty(arr, i, thing[i]);
|
|
485
580
|
}
|
|
486
581
|
return arr;
|
|
487
582
|
}
|
|
@@ -551,6 +646,7 @@ module.exports = {
|
|
|
551
646
|
trim: trim,
|
|
552
647
|
stripBOM: stripBOM,
|
|
553
648
|
inherits: inherits,
|
|
649
|
+
defineOwnProperty: defineOwnProperty,
|
|
554
650
|
toFlatObject: toFlatObject,
|
|
555
651
|
kindOf: kindOf,
|
|
556
652
|
kindOfTest: kindOfTest,
|