utils.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. 'use strict';
  2. var formats = require('./formats');
  3. var getSideChannel = require('side-channel');
  4. var has = Object.prototype.hasOwnProperty;
  5. var isArray = Array.isArray;
  6. // Track objects created from arrayLimit overflow using side-channel
  7. // Stores the current max numeric index for O(1) lookup
  8. var overflowChannel = getSideChannel();
  9. var markOverflow = function markOverflow(obj, maxIndex) {
  10. overflowChannel.set(obj, maxIndex);
  11. return obj;
  12. };
  13. var isOverflow = function isOverflow(obj) {
  14. return overflowChannel.has(obj);
  15. };
  16. var getMaxIndex = function getMaxIndex(obj) {
  17. return overflowChannel.get(obj);
  18. };
  19. var setMaxIndex = function setMaxIndex(obj, maxIndex) {
  20. overflowChannel.set(obj, maxIndex);
  21. };
  22. var hexTable = (function () {
  23. var array = [];
  24. for (var i = 0; i < 256; ++i) {
  25. array[array.length] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
  26. }
  27. return array;
  28. }());
  29. var compactQueue = function compactQueue(queue) {
  30. while (queue.length > 1) {
  31. var item = queue.pop();
  32. var obj = item.obj[item.prop];
  33. if (isArray(obj)) {
  34. var compacted = [];
  35. for (var j = 0; j < obj.length; ++j) {
  36. if (typeof obj[j] !== 'undefined') {
  37. compacted[compacted.length] = obj[j];
  38. }
  39. }
  40. item.obj[item.prop] = compacted;
  41. }
  42. }
  43. };
  44. var arrayToObject = function arrayToObject(source, options) {
  45. var obj = options && options.plainObjects ? { __proto__: null } : {};
  46. for (var i = 0; i < source.length; ++i) {
  47. if (typeof source[i] !== 'undefined') {
  48. obj[i] = source[i];
  49. }
  50. }
  51. return obj;
  52. };
  53. var merge = function merge(target, source, options) {
  54. /* eslint no-param-reassign: 0 */
  55. if (!source) {
  56. return target;
  57. }
  58. if (typeof source !== 'object' && typeof source !== 'function') {
  59. if (isArray(target)) {
  60. var nextIndex = target.length;
  61. if (options && typeof options.arrayLimit === 'number' && nextIndex > options.arrayLimit) {
  62. return markOverflow(arrayToObject(target.concat(source), options), nextIndex);
  63. }
  64. target[nextIndex] = source;
  65. } else if (target && typeof target === 'object') {
  66. if (isOverflow(target)) {
  67. // Add at next numeric index for overflow objects
  68. var newIndex = getMaxIndex(target) + 1;
  69. target[newIndex] = source;
  70. setMaxIndex(target, newIndex);
  71. } else if (
  72. (options && (options.plainObjects || options.allowPrototypes))
  73. || !has.call(Object.prototype, source)
  74. ) {
  75. target[source] = true;
  76. }
  77. } else {
  78. return [target, source];
  79. }
  80. return target;
  81. }
  82. if (!target || typeof target !== 'object') {
  83. if (isOverflow(source)) {
  84. // Create new object with target at 0, source values shifted by 1
  85. var sourceKeys = Object.keys(source);
  86. var result = options && options.plainObjects
  87. ? { __proto__: null, 0: target }
  88. : { 0: target };
  89. for (var m = 0; m < sourceKeys.length; m++) {
  90. var oldKey = parseInt(sourceKeys[m], 10);
  91. result[oldKey + 1] = source[sourceKeys[m]];
  92. }
  93. return markOverflow(result, getMaxIndex(source) + 1);
  94. }
  95. var combined = [target].concat(source);
  96. if (options && typeof options.arrayLimit === 'number' && combined.length > options.arrayLimit) {
  97. return markOverflow(arrayToObject(combined, options), combined.length - 1);
  98. }
  99. return combined;
  100. }
  101. var mergeTarget = target;
  102. if (isArray(target) && !isArray(source)) {
  103. mergeTarget = arrayToObject(target, options);
  104. }
  105. if (isArray(target) && isArray(source)) {
  106. source.forEach(function (item, i) {
  107. if (has.call(target, i)) {
  108. var targetItem = target[i];
  109. if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
  110. target[i] = merge(targetItem, item, options);
  111. } else {
  112. target[target.length] = item;
  113. }
  114. } else {
  115. target[i] = item;
  116. }
  117. });
  118. return target;
  119. }
  120. return Object.keys(source).reduce(function (acc, key) {
  121. var value = source[key];
  122. if (has.call(acc, key)) {
  123. acc[key] = merge(acc[key], value, options);
  124. } else {
  125. acc[key] = value;
  126. }
  127. if (isOverflow(source) && !isOverflow(acc)) {
  128. markOverflow(acc, getMaxIndex(source));
  129. }
  130. if (isOverflow(acc)) {
  131. var keyNum = parseInt(key, 10);
  132. if (String(keyNum) === key && keyNum >= 0 && keyNum > getMaxIndex(acc)) {
  133. setMaxIndex(acc, keyNum);
  134. }
  135. }
  136. return acc;
  137. }, mergeTarget);
  138. };
  139. var assign = function assignSingleSource(target, source) {
  140. return Object.keys(source).reduce(function (acc, key) {
  141. acc[key] = source[key];
  142. return acc;
  143. }, target);
  144. };
  145. var decode = function (str, defaultDecoder, charset) {
  146. var strWithoutPlus = str.replace(/\+/g, ' ');
  147. if (charset === 'iso-8859-1') {
  148. // unescape never throws, no try...catch needed:
  149. return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
  150. }
  151. // utf-8
  152. try {
  153. return decodeURIComponent(strWithoutPlus);
  154. } catch (e) {
  155. return strWithoutPlus;
  156. }
  157. };
  158. var limit = 1024;
  159. /* eslint operator-linebreak: [2, "before"] */
  160. var encode = function encode(str, defaultEncoder, charset, kind, format) {
  161. // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
  162. // It has been adapted here for stricter adherence to RFC 3986
  163. if (str.length === 0) {
  164. return str;
  165. }
  166. var string = str;
  167. if (typeof str === 'symbol') {
  168. string = Symbol.prototype.toString.call(str);
  169. } else if (typeof str !== 'string') {
  170. string = String(str);
  171. }
  172. if (charset === 'iso-8859-1') {
  173. return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
  174. return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
  175. });
  176. }
  177. var out = '';
  178. for (var j = 0; j < string.length; j += limit) {
  179. var segment = string.length >= limit ? string.slice(j, j + limit) : string;
  180. var arr = [];
  181. for (var i = 0; i < segment.length; ++i) {
  182. var c = segment.charCodeAt(i);
  183. if (
  184. c === 0x2D // -
  185. || c === 0x2E // .
  186. || c === 0x5F // _
  187. || c === 0x7E // ~
  188. || (c >= 0x30 && c <= 0x39) // 0-9
  189. || (c >= 0x41 && c <= 0x5A) // a-z
  190. || (c >= 0x61 && c <= 0x7A) // A-Z
  191. || (format === formats.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
  192. ) {
  193. arr[arr.length] = segment.charAt(i);
  194. continue;
  195. }
  196. if (c < 0x80) {
  197. arr[arr.length] = hexTable[c];
  198. continue;
  199. }
  200. if (c < 0x800) {
  201. arr[arr.length] = hexTable[0xC0 | (c >> 6)]
  202. + hexTable[0x80 | (c & 0x3F)];
  203. continue;
  204. }
  205. if (c < 0xD800 || c >= 0xE000) {
  206. arr[arr.length] = hexTable[0xE0 | (c >> 12)]
  207. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  208. + hexTable[0x80 | (c & 0x3F)];
  209. continue;
  210. }
  211. i += 1;
  212. c = 0x10000 + (((c & 0x3FF) << 10) | (segment.charCodeAt(i) & 0x3FF));
  213. arr[arr.length] = hexTable[0xF0 | (c >> 18)]
  214. + hexTable[0x80 | ((c >> 12) & 0x3F)]
  215. + hexTable[0x80 | ((c >> 6) & 0x3F)]
  216. + hexTable[0x80 | (c & 0x3F)];
  217. }
  218. out += arr.join('');
  219. }
  220. return out;
  221. };
  222. var compact = function compact(value) {
  223. var queue = [{ obj: { o: value }, prop: 'o' }];
  224. var refs = [];
  225. for (var i = 0; i < queue.length; ++i) {
  226. var item = queue[i];
  227. var obj = item.obj[item.prop];
  228. var keys = Object.keys(obj);
  229. for (var j = 0; j < keys.length; ++j) {
  230. var key = keys[j];
  231. var val = obj[key];
  232. if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
  233. queue[queue.length] = { obj: obj, prop: key };
  234. refs[refs.length] = val;
  235. }
  236. }
  237. }
  238. compactQueue(queue);
  239. return value;
  240. };
  241. var isRegExp = function isRegExp(obj) {
  242. return Object.prototype.toString.call(obj) === '[object RegExp]';
  243. };
  244. var isBuffer = function isBuffer(obj) {
  245. if (!obj || typeof obj !== 'object') {
  246. return false;
  247. }
  248. return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  249. };
  250. var combine = function combine(a, b, arrayLimit, plainObjects) {
  251. // If 'a' is already an overflow object, add to it
  252. if (isOverflow(a)) {
  253. var newIndex = getMaxIndex(a) + 1;
  254. a[newIndex] = b;
  255. setMaxIndex(a, newIndex);
  256. return a;
  257. }
  258. var result = [].concat(a, b);
  259. if (result.length > arrayLimit) {
  260. return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1);
  261. }
  262. return result;
  263. };
  264. var maybeMap = function maybeMap(val, fn) {
  265. if (isArray(val)) {
  266. var mapped = [];
  267. for (var i = 0; i < val.length; i += 1) {
  268. mapped[mapped.length] = fn(val[i]);
  269. }
  270. return mapped;
  271. }
  272. return fn(val);
  273. };
  274. module.exports = {
  275. arrayToObject: arrayToObject,
  276. assign: assign,
  277. combine: combine,
  278. compact: compact,
  279. decode: decode,
  280. encode: encode,
  281. isBuffer: isBuffer,
  282. isOverflow: isOverflow,
  283. isRegExp: isRegExp,
  284. markOverflow: markOverflow,
  285. maybeMap: maybeMap,
  286. merge: merge
  287. };