superagent 10.2.3 → 10.4.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.
@@ -1585,9 +1585,11 @@ var parseValues = function parseQueryStringValues(str, options) {
1585
1585
  val = options.strictNullHandling ? null : '';
1586
1586
  } else {
1587
1587
  key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
1588
- val = utils.maybeMap(parseArrayValue(part.slice(pos + 1), options, isArray(obj[key]) ? obj[key].length : 0), function (encodedVal) {
1589
- return options.decoder(encodedVal, defaults.decoder, charset, 'value');
1590
- });
1588
+ if (key !== null) {
1589
+ val = utils.maybeMap(parseArrayValue(part.slice(pos + 1), options, isArray(obj[key]) ? obj[key].length : 0), function (encodedVal) {
1590
+ return options.decoder(encodedVal, defaults.decoder, charset, 'value');
1591
+ });
1592
+ }
1591
1593
  }
1592
1594
  if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
1593
1595
  val = interpretNumericEntities(String(val));
@@ -1595,11 +1597,13 @@ var parseValues = function parseQueryStringValues(str, options) {
1595
1597
  if (part.indexOf('[]=') > -1) {
1596
1598
  val = isArray(val) ? [val] : val;
1597
1599
  }
1598
- var existing = has.call(obj, key);
1599
- if (existing && options.duplicates === 'combine') {
1600
- obj[key] = utils.combine(obj[key], val);
1601
- } else if (!existing || options.duplicates === 'last') {
1602
- obj[key] = val;
1600
+ if (key !== null) {
1601
+ var existing = has.call(obj, key);
1602
+ if (existing && options.duplicates === 'combine') {
1603
+ obj[key] = utils.combine(obj[key], val, options.arrayLimit, options.plainObjects);
1604
+ } else if (!existing || options.duplicates === 'last') {
1605
+ obj[key] = val;
1606
+ }
1603
1607
  }
1604
1608
  }
1605
1609
  return obj;
@@ -1615,7 +1619,11 @@ var parseObject = function (chain, val, options, valuesParsed) {
1615
1619
  var obj;
1616
1620
  var root = chain[i];
1617
1621
  if (root === '[]' && options.parseArrays) {
1618
- obj = options.allowEmptyArrays && (leaf === '' || options.strictNullHandling && leaf === null) ? [] : utils.combine([], leaf);
1622
+ if (utils.isOverflow(leaf)) {
1623
+ obj = leaf;
1624
+ } else {
1625
+ obj = options.allowEmptyArrays && (leaf === '' || options.strictNullHandling && leaf === null) ? [] : utils.combine([], leaf, options.arrayLimit, options.plainObjects);
1626
+ }
1619
1627
  } else {
1620
1628
  obj = options.plainObjects ? {
1621
1629
  __proto__: null
@@ -1638,14 +1646,19 @@ var parseObject = function (chain, val, options, valuesParsed) {
1638
1646
  }
1639
1647
  return leaf;
1640
1648
  };
1641
- var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
1642
- if (!givenKey) {
1643
- return;
1644
- }
1649
+ var splitKeyIntoSegments = function splitKeyIntoSegments(givenKey, options) {
1645
1650
  var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
1651
+ if (options.depth <= 0) {
1652
+ if (!options.plainObjects && has.call(Object.prototype, key)) {
1653
+ if (!options.allowPrototypes) {
1654
+ return;
1655
+ }
1656
+ }
1657
+ return [key];
1658
+ }
1646
1659
  var brackets = /(\[[^[\]]*])/;
1647
1660
  var child = /(\[[^[\]]*])/g;
1648
- var segment = options.depth > 0 && brackets.exec(key);
1661
+ var segment = brackets.exec(key);
1649
1662
  var parent = segment ? key.slice(0, segment.index) : key;
1650
1663
  var keys = [];
1651
1664
  if (parent) {
@@ -1657,9 +1670,10 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesPars
1657
1670
  keys.push(parent);
1658
1671
  }
1659
1672
  var i = 0;
1660
- while (options.depth > 0 && (segment = child.exec(key)) !== null && i < options.depth) {
1673
+ while ((segment = child.exec(key)) !== null && i < options.depth) {
1661
1674
  i += 1;
1662
- if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
1675
+ var segmentContent = segment[1].slice(1, -1);
1676
+ if (!options.plainObjects && has.call(Object.prototype, segmentContent)) {
1663
1677
  if (!options.allowPrototypes) {
1664
1678
  return;
1665
1679
  }
@@ -1672,6 +1686,16 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesPars
1672
1686
  }
1673
1687
  keys.push('[' + key.slice(segment.index) + ']');
1674
1688
  }
1689
+ return keys;
1690
+ };
1691
+ var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
1692
+ if (!givenKey) {
1693
+ return;
1694
+ }
1695
+ var keys = splitKeyIntoSegments(givenKey, options);
1696
+ if (!keys) {
1697
+ return;
1698
+ }
1675
1699
  return parseObject(keys, val, options, valuesParsed);
1676
1700
  };
1677
1701
  var normalizeParseOptions = function normalizeParseOptions(opts) {
@@ -1993,8 +2017,23 @@ module.exports = function (object, opts) {
1993
2017
  'use strict';
1994
2018
 
1995
2019
  var formats = require('./formats');
2020
+ var getSideChannel = require('side-channel');
1996
2021
  var has = Object.prototype.hasOwnProperty;
1997
2022
  var isArray = Array.isArray;
2023
+ var overflowChannel = getSideChannel();
2024
+ var markOverflow = function markOverflow(obj, maxIndex) {
2025
+ overflowChannel.set(obj, maxIndex);
2026
+ return obj;
2027
+ };
2028
+ var isOverflow = function isOverflow(obj) {
2029
+ return overflowChannel.has(obj);
2030
+ };
2031
+ var getMaxIndex = function getMaxIndex(obj) {
2032
+ return overflowChannel.get(obj);
2033
+ };
2034
+ var setMaxIndex = function setMaxIndex(obj, maxIndex) {
2035
+ overflowChannel.set(obj, maxIndex);
2036
+ };
1998
2037
  var hexTable = function () {
1999
2038
  var array = [];
2000
2039
  for (var i = 0; i < 256; ++i) {
@@ -2036,7 +2075,11 @@ var merge = function merge(target, source, options) {
2036
2075
  if (isArray(target)) {
2037
2076
  target.push(source);
2038
2077
  } else if (target && typeof target === 'object') {
2039
- if (options && (options.plainObjects || options.allowPrototypes) || !has.call(Object.prototype, source)) {
2078
+ if (isOverflow(target)) {
2079
+ var newIndex = getMaxIndex(target) + 1;
2080
+ target[newIndex] = source;
2081
+ setMaxIndex(target, newIndex);
2082
+ } else if (options && (options.plainObjects || options.allowPrototypes) || !has.call(Object.prototype, source)) {
2040
2083
  target[source] = true;
2041
2084
  }
2042
2085
  } else {
@@ -2045,6 +2088,20 @@ var merge = function merge(target, source, options) {
2045
2088
  return target;
2046
2089
  }
2047
2090
  if (!target || typeof target !== 'object') {
2091
+ if (isOverflow(source)) {
2092
+ var sourceKeys = Object.keys(source);
2093
+ var result = options && options.plainObjects ? {
2094
+ __proto__: null,
2095
+ 0: target
2096
+ } : {
2097
+ 0: target
2098
+ };
2099
+ for (var m = 0; m < sourceKeys.length; m++) {
2100
+ var oldKey = parseInt(sourceKeys[m], 10);
2101
+ result[oldKey + 1] = source[sourceKeys[m]];
2102
+ }
2103
+ return markOverflow(result, getMaxIndex(source) + 1);
2104
+ }
2048
2105
  return [target].concat(source);
2049
2106
  }
2050
2107
  var mergeTarget = target;
@@ -2175,8 +2232,20 @@ var isBuffer = function isBuffer(obj) {
2175
2232
  }
2176
2233
  return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
2177
2234
  };
2178
- var combine = function combine(a, b) {
2179
- return [].concat(a, b);
2235
+ var combine = function combine(a, b, arrayLimit, plainObjects) {
2236
+ if (isOverflow(a)) {
2237
+ var newIndex = getMaxIndex(a) + 1;
2238
+ a[newIndex] = b;
2239
+ setMaxIndex(a, newIndex);
2240
+ return a;
2241
+ }
2242
+ var result = [].concat(a, b);
2243
+ if (result.length > arrayLimit) {
2244
+ return markOverflow(arrayToObject(result, {
2245
+ plainObjects: plainObjects
2246
+ }), result.length - 1);
2247
+ }
2248
+ return result;
2180
2249
  };
2181
2250
  var maybeMap = function maybeMap(val, fn) {
2182
2251
  if (isArray(val)) {
@@ -2196,12 +2265,13 @@ module.exports = {
2196
2265
  decode: decode,
2197
2266
  encode: encode,
2198
2267
  isBuffer: isBuffer,
2268
+ isOverflow: isOverflow,
2199
2269
  isRegExp: isRegExp,
2200
2270
  maybeMap: maybeMap,
2201
2271
  merge: merge
2202
2272
  };
2203
2273
 
2204
- },{"./formats":40}],45:[function(require,module,exports){
2274
+ },{"./formats":40,"side-channel":48}],45:[function(require,module,exports){
2205
2275
  'use strict';
2206
2276
 
2207
2277
  var inspect = require('object-inspect');
@@ -2556,13 +2626,14 @@ request.types = {
2556
2626
  'form-data': 'application/x-www-form-urlencoded'
2557
2627
  };
2558
2628
  request.serialize = {
2559
- 'application/x-www-form-urlencoded': obj => {
2560
- return qs.stringify(obj, {
2629
+ 'application/x-www-form-urlencoded'(object) {
2630
+ return qs.stringify(object, {
2561
2631
  indices: false,
2562
2632
  strictNullHandling: true
2563
2633
  });
2564
2634
  },
2565
- 'application/json': safeStringify
2635
+ 'application/json': safeStringify,
2636
+ 'application/csp-report': safeStringify
2566
2637
  };
2567
2638
  request.parse = {
2568
2639
  'application/x-www-form-urlencoded': parseString,
@@ -2608,8 +2679,10 @@ function Response(request_) {
2608
2679
  this._setHeaderProperties(this.header);
2609
2680
  if (this.text === null && request_._responseType) {
2610
2681
  this.body = this.xhr.response;
2682
+ } else if (this.req.method === 'HEAD') {
2683
+ this.body = null;
2611
2684
  } else {
2612
- this.body = this.req.method === 'HEAD' ? null : this._parseBody(this.text ? this.text : this.xhr.response);
2685
+ this.body = this._parseBody(this.text || this.xhr.response);
2613
2686
  }
2614
2687
  }
2615
2688
  mixin(Response.prototype, ResponseBase.prototype);
@@ -2659,7 +2732,7 @@ function Request(method, url) {
2659
2732
  error.original = err;
2660
2733
  if (self.xhr) {
2661
2734
  error.rawResponse = typeof self.xhr.responseType === 'undefined' ? self.xhr.responseText : self.xhr.response;
2662
- error.status = self.xhr.status ? self.xhr.status : null;
2735
+ error.status = self.xhr.status || null;
2663
2736
  error.statusCode = error.status;
2664
2737
  } else {
2665
2738
  error.rawResponse = null;
@@ -2707,12 +2780,17 @@ Request.prototype.auth = function (user, pass, options) {
2707
2780
  type: typeof btoa === 'function' ? 'basic' : 'auto'
2708
2781
  };
2709
2782
  }
2710
- const encoder = options.encoder ? options.encoder : string => {
2711
- if (typeof btoa === 'function') {
2712
- return btoa(string);
2713
- }
2714
- throw new Error('Cannot use basic auth, btoa is not a function');
2715
- };
2783
+ let {
2784
+ encoder
2785
+ } = options;
2786
+ if (!encoder) {
2787
+ encoder = string => {
2788
+ if (typeof btoa === 'function') {
2789
+ return btoa(string);
2790
+ }
2791
+ throw new Error('Cannot use basic auth, btoa is not a function');
2792
+ };
2793
+ }
2716
2794
  return this._auth(user, pass, options, encoder);
2717
2795
  };
2718
2796
  Request.prototype.query = function (value) {
@@ -2855,18 +2933,30 @@ Request.prototype._end = function () {
2855
2933
  }
2856
2934
  if (serialize) data = serialize(data);
2857
2935
  }
2858
- for (const field in this.header) {
2859
- if (this.header[field] === null) continue;
2860
- if (hasOwn(this.header, field)) xhr.setRequestHeader(field, this.header[field]);
2861
- }
2862
- if (this._responseType) {
2863
- xhr.responseType = this._responseType;
2936
+ try {
2937
+ for (const field in this.header) {
2938
+ if (this.header[field] === null) continue;
2939
+ if (hasOwn(this.header, field)) xhr.setRequestHeader(field, this.header[field]);
2940
+ }
2941
+ if (this._responseType) {
2942
+ xhr.responseType = this._responseType;
2943
+ }
2944
+ this.emit('request', this);
2945
+ xhr.send(typeof data === 'undefined' ? null : data);
2946
+ } catch (err) {
2947
+ try {
2948
+ xhr.abort();
2949
+ } catch (err) {}
2950
+ return this.callback(err);
2864
2951
  }
2865
- this.emit('request', this);
2866
- xhr.send(typeof data === 'undefined' ? null : data);
2867
2952
  };
2868
- request.agent = () => new Agent();
2869
- for (const method of ['GET', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE']) {
2953
+ const proxyAgent = new Proxy(Agent, {
2954
+ apply(target, thisArg, argumentsList) {
2955
+ return new target(...argumentsList);
2956
+ }
2957
+ });
2958
+ request.agent = proxyAgent;
2959
+ for (const method of ['GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE']) {
2870
2960
  Agent.prototype[method.toLowerCase()] = function (url, fn) {
2871
2961
  const request_ = new request.Request(method, url);
2872
2962
  this._setDefaults(request_);
@@ -3054,7 +3144,7 @@ RequestBase.prototype.then = function (resolve, reject) {
3054
3144
  }
3055
3145
  this._fullfilledPromise = new Promise((resolve, reject) => {
3056
3146
  self.on('abort', () => {
3057
- if (this._maxRetries && this._maxRetries > this._retries) {
3147
+ if (this.timedout && this._maxRetries && this._maxRetries > this._retries) {
3058
3148
  return;
3059
3149
  }
3060
3150
  if (this.timedout && this.timedoutError) {
@@ -3301,7 +3391,7 @@ ResponseBase.prototype._setHeaderProperties = function (header) {
3301
3391
  this.type = utils.type(ct);
3302
3392
  const parameters = utils.params(ct);
3303
3393
  for (const key in parameters) {
3304
- if (Object.prototype.hasOwnProperty.call(parameters, key)) this[key] = parameters[key];
3394
+ if (Object.prototype.hasOwnProperty.call(parameters, key) && !(key in this)) this[key] = parameters[key];
3305
3395
  }
3306
3396
  this.links = {};
3307
3397
  try {
@@ -3349,8 +3439,14 @@ exports.parseLinks = value => {
3349
3439
  for (const string_ of value.split(/ *, */)) {
3350
3440
  const parts = string_.split(/ *; */);
3351
3441
  const url = parts[0].slice(1, -1);
3352
- const rel = parts[1].split(/ *= */)[1].slice(1, -1);
3353
- object[rel] = url;
3442
+ for (const part of parts.slice(1)) {
3443
+ const [key, keyValue] = part.split(/ *= */, 2);
3444
+ if (key && key.toLowerCase() === 'rel' && keyValue) {
3445
+ const relationship = keyValue.replace(/^"|"$/g, '');
3446
+ if (relationship) object[relationship] = url;
3447
+ break;
3448
+ }
3449
+ }
3354
3450
  }
3355
3451
  return object;
3356
3452
  };
@@ -3366,17 +3462,17 @@ exports.cleanHeader = (header, changesOrigin) => {
3366
3462
  return header;
3367
3463
  };
3368
3464
  exports.normalizeHostname = hostname => {
3369
- const [, normalized] = hostname.match(/^\[([^\]]+)\]$/) || [];
3465
+ const [, normalized] = hostname.match(/^\[([^\]]+)]$/) || [];
3370
3466
  return normalized || hostname;
3371
3467
  };
3372
3468
  exports.isObject = object => {
3373
3469
  return object !== null && typeof object === 'object';
3374
3470
  };
3375
3471
  exports.hasOwn = Object.hasOwn || function (object, property) {
3376
- if (object == null) {
3472
+ if (object === null || object === undefined) {
3377
3473
  throw new TypeError('Cannot convert undefined or null to object');
3378
3474
  }
3379
- return Object.prototype.hasOwnProperty.call(new Object(object), property);
3475
+ return Object.prototype.hasOwnProperty.call(Object(object), property);
3380
3476
  };
3381
3477
  exports.mixin = (target, source) => {
3382
3478
  for (const key in source) {
@@ -3386,10 +3482,10 @@ exports.mixin = (target, source) => {
3386
3482
  }
3387
3483
  };
3388
3484
  exports.isGzipOrDeflateEncoding = res => {
3389
- return new RegExp(/^\s*(?:deflate|gzip)\s*$/).test(res.headers['content-encoding']);
3485
+ return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']);
3390
3486
  };
3391
3487
  exports.isBrotliEncoding = res => {
3392
- return new RegExp(/^\s*(?:br)\s*$/).test(res.headers['content-encoding']);
3488
+ return /^\s*br\s*$/.test(res.headers['content-encoding']);
3393
3489
  };
3394
3490
 
3395
3491
  },{}]},{},[50])(50)