utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const specialProperties = ['__proto__', 'constructor', 'prototype'];
  6. /**
  7. * Clones objects
  8. *
  9. * @param {Object} obj the object to clone
  10. * @param {Object} options
  11. * @return {Object} the cloned object
  12. * @api private
  13. */
  14. const clone = exports.clone = function clone(obj, options) {
  15. if (obj === undefined || obj === null)
  16. return obj;
  17. if (Array.isArray(obj))
  18. return exports.cloneArray(obj, options);
  19. if (obj.constructor) {
  20. if (/ObjectI[dD]$/.test(obj.constructor.name)) {
  21. return 'function' == typeof obj.clone
  22. ? obj.clone()
  23. : new obj.constructor(obj.id);
  24. }
  25. if (obj.constructor.name === 'ReadPreference') {
  26. return new obj.constructor(obj.mode, clone(obj.tags, options));
  27. }
  28. if ('Binary' == obj._bsontype && obj.buffer && obj.value) {
  29. return 'function' == typeof obj.clone
  30. ? obj.clone()
  31. : new obj.constructor(obj.value(true), obj.sub_type);
  32. }
  33. if ('Date' === obj.constructor.name || 'Function' === obj.constructor.name)
  34. return new obj.constructor(+obj);
  35. if ('RegExp' === obj.constructor.name)
  36. return new RegExp(obj);
  37. if ('Buffer' === obj.constructor.name)
  38. return Buffer.from(obj);
  39. }
  40. if (isObject(obj))
  41. return exports.cloneObject(obj, options);
  42. if (obj.valueOf)
  43. return obj.valueOf();
  44. };
  45. /*!
  46. * ignore
  47. */
  48. exports.cloneObject = function cloneObject(obj, options) {
  49. const minimize = options && options.minimize,
  50. ret = {},
  51. keys = Object.keys(obj),
  52. len = keys.length;
  53. let hasKeys = false,
  54. val,
  55. k = '',
  56. i = 0;
  57. for (i = 0; i < len; ++i) {
  58. k = keys[i];
  59. // Not technically prototype pollution because this wouldn't merge properties
  60. // onto `Object.prototype`, but avoid properties like __proto__ as a precaution.
  61. if (specialProperties.indexOf(k) !== -1) {
  62. continue;
  63. }
  64. val = clone(obj[k], options);
  65. if (!minimize || ('undefined' !== typeof val)) {
  66. hasKeys || (hasKeys = true);
  67. ret[k] = val;
  68. }
  69. }
  70. return minimize
  71. ? hasKeys && ret
  72. : ret;
  73. };
  74. exports.cloneArray = function cloneArray(arr, options) {
  75. const ret = [],
  76. l = arr.length;
  77. let i = 0;
  78. for (; i < l; i++)
  79. ret.push(clone(arr[i], options));
  80. return ret;
  81. };
  82. /**
  83. * process.nextTick helper.
  84. *
  85. * Wraps the given `callback` in a try/catch. If an error is
  86. * caught it will be thrown on nextTick.
  87. *
  88. * node-mongodb-native had a habit of state corruption when
  89. * an error was immediately thrown from within a collection
  90. * method (find, update, etc) callback.
  91. *
  92. * @param {Function} [callback]
  93. * @api private
  94. */
  95. exports.tick = function tick(callback) {
  96. if ('function' !== typeof callback) return;
  97. return function() {
  98. // callbacks should always be fired on the next
  99. // turn of the event loop. A side benefit is
  100. // errors thrown from executing the callback
  101. // will not cause drivers state to be corrupted
  102. // which has historically been a problem.
  103. const args = arguments;
  104. soon(function() {
  105. callback.apply(this, args);
  106. });
  107. };
  108. };
  109. /**
  110. * Merges `from` into `to` without overwriting existing properties.
  111. *
  112. * @param {Object} to
  113. * @param {Object} from
  114. * @api private
  115. */
  116. exports.merge = function merge(to, from) {
  117. const keys = Object.keys(from);
  118. for (const key of keys) {
  119. if (specialProperties.indexOf(key) !== -1) {
  120. continue;
  121. }
  122. if ('undefined' === typeof to[key]) {
  123. to[key] = from[key];
  124. } else {
  125. if (exports.isObject(from[key])) {
  126. merge(to[key], from[key]);
  127. } else {
  128. to[key] = from[key];
  129. }
  130. }
  131. }
  132. };
  133. /**
  134. * Same as merge but clones the assigned values.
  135. *
  136. * @param {Object} to
  137. * @param {Object} from
  138. * @api private
  139. */
  140. exports.mergeClone = function mergeClone(to, from) {
  141. const keys = Object.keys(from);
  142. for (const key of keys) {
  143. if (specialProperties.indexOf(key) !== -1) {
  144. continue;
  145. }
  146. if ('undefined' === typeof to[key]) {
  147. to[key] = clone(from[key]);
  148. } else {
  149. if (exports.isObject(from[key])) {
  150. mergeClone(to[key], from[key]);
  151. } else {
  152. to[key] = clone(from[key]);
  153. }
  154. }
  155. }
  156. };
  157. /**
  158. * Read pref helper (mongo 2.2 drivers support this)
  159. *
  160. * Allows using aliases instead of full preference names:
  161. *
  162. * p primary
  163. * pp primaryPreferred
  164. * s secondary
  165. * sp secondaryPreferred
  166. * n nearest
  167. *
  168. * @param {String} pref
  169. */
  170. exports.readPref = function readPref(pref) {
  171. switch (pref) {
  172. case 'p':
  173. pref = 'primary';
  174. break;
  175. case 'pp':
  176. pref = 'primaryPreferred';
  177. break;
  178. case 's':
  179. pref = 'secondary';
  180. break;
  181. case 'sp':
  182. pref = 'secondaryPreferred';
  183. break;
  184. case 'n':
  185. pref = 'nearest';
  186. break;
  187. }
  188. return pref;
  189. };
  190. /**
  191. * Read Concern helper (mongo 3.2 drivers support this)
  192. *
  193. * Allows using string to specify read concern level:
  194. *
  195. * local 3.2+
  196. * available 3.6+
  197. * majority 3.2+
  198. * linearizable 3.4+
  199. * snapshot 4.0+
  200. *
  201. * @param {String|Object} concern
  202. */
  203. exports.readConcern = function readConcern(concern) {
  204. if ('string' === typeof concern) {
  205. switch (concern) {
  206. case 'l':
  207. concern = 'local';
  208. break;
  209. case 'a':
  210. concern = 'available';
  211. break;
  212. case 'm':
  213. concern = 'majority';
  214. break;
  215. case 'lz':
  216. concern = 'linearizable';
  217. break;
  218. case 's':
  219. concern = 'snapshot';
  220. break;
  221. }
  222. concern = { level: concern };
  223. }
  224. return concern;
  225. };
  226. /**
  227. * Object.prototype.toString.call helper
  228. */
  229. const _toString = Object.prototype.toString;
  230. exports.toString = function(arg) {
  231. return _toString.call(arg);
  232. };
  233. /**
  234. * Determines if `arg` is an object.
  235. *
  236. * @param {Object|Array|String|Function|RegExp|any} arg
  237. * @return {Boolean}
  238. */
  239. const isObject = exports.isObject = function(arg) {
  240. return '[object Object]' == exports.toString(arg);
  241. };
  242. /**
  243. * Object.keys helper
  244. */
  245. exports.keys = Object.keys;
  246. /**
  247. * Basic Object.create polyfill.
  248. * Only one argument is supported.
  249. *
  250. * Based on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
  251. */
  252. exports.create = 'function' == typeof Object.create
  253. ? Object.create
  254. : create;
  255. function create(proto) {
  256. if (arguments.length > 1) {
  257. throw new Error('Adding properties is not supported');
  258. }
  259. function F() { }
  260. F.prototype = proto;
  261. return new F;
  262. }
  263. /**
  264. * inheritance
  265. */
  266. exports.inherits = function(ctor, superCtor) {
  267. ctor.prototype = exports.create(superCtor.prototype);
  268. ctor.prototype.constructor = ctor;
  269. };
  270. /**
  271. * nextTick helper
  272. * compat with node 0.10 which behaves differently than previous versions
  273. */
  274. const soon = exports.soon = 'function' == typeof setImmediate
  275. ? setImmediate
  276. : process.nextTick;
  277. /**
  278. * Check if this object is an arguments object
  279. *
  280. * @param {Any} v
  281. * @return {Boolean}
  282. */
  283. exports.isArgumentsObject = function(v) {
  284. return Object.prototype.toString.call(v) === '[object Arguments]';
  285. };