utils.js 9.0 KB

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