utils.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deprecate = exports.isObjectLike = exports.isDate = exports.haveBuffer = exports.isMap = exports.isRegExp = exports.isBigUInt64Array = exports.isBigInt64Array = exports.isUint8Array = exports.isAnyArrayBuffer = exports.randomBytes = exports.normalizedFunctionString = void 0;
  4. var buffer_1 = require("buffer");
  5. var global_1 = require("../utils/global");
  6. /**
  7. * Normalizes our expected stringified form of a function across versions of node
  8. * @param fn - The function to stringify
  9. */
  10. function normalizedFunctionString(fn) {
  11. return fn.toString().replace('function(', 'function (');
  12. }
  13. exports.normalizedFunctionString = normalizedFunctionString;
  14. function isReactNative() {
  15. var g = (0, global_1.getGlobal)();
  16. return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
  17. }
  18. var insecureRandomBytes = function insecureRandomBytes(size) {
  19. var insecureWarning = isReactNative()
  20. ? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
  21. : 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
  22. console.warn(insecureWarning);
  23. var result = buffer_1.Buffer.alloc(size);
  24. for (var i = 0; i < size; ++i)
  25. result[i] = Math.floor(Math.random() * 256);
  26. return result;
  27. };
  28. var detectRandomBytes = function () {
  29. if (process.browser) {
  30. if (typeof window !== 'undefined') {
  31. // browser crypto implementation(s)
  32. var target_1 = window.crypto || window.msCrypto; // allow for IE11
  33. if (target_1 && target_1.getRandomValues) {
  34. return function (size) { return target_1.getRandomValues(buffer_1.Buffer.alloc(size)); };
  35. }
  36. }
  37. if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
  38. // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
  39. return function (size) { return global.crypto.getRandomValues(buffer_1.Buffer.alloc(size)); };
  40. }
  41. return insecureRandomBytes;
  42. }
  43. else {
  44. var requiredRandomBytes = void 0;
  45. try {
  46. requiredRandomBytes = require('crypto').randomBytes;
  47. }
  48. catch (e) {
  49. // keep the fallback
  50. }
  51. // NOTE: in transpiled cases the above require might return null/undefined
  52. return requiredRandomBytes || insecureRandomBytes;
  53. }
  54. };
  55. exports.randomBytes = detectRandomBytes();
  56. function isAnyArrayBuffer(value) {
  57. return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
  58. }
  59. exports.isAnyArrayBuffer = isAnyArrayBuffer;
  60. function isUint8Array(value) {
  61. return Object.prototype.toString.call(value) === '[object Uint8Array]';
  62. }
  63. exports.isUint8Array = isUint8Array;
  64. function isBigInt64Array(value) {
  65. return Object.prototype.toString.call(value) === '[object BigInt64Array]';
  66. }
  67. exports.isBigInt64Array = isBigInt64Array;
  68. function isBigUInt64Array(value) {
  69. return Object.prototype.toString.call(value) === '[object BigUint64Array]';
  70. }
  71. exports.isBigUInt64Array = isBigUInt64Array;
  72. function isRegExp(d) {
  73. return Object.prototype.toString.call(d) === '[object RegExp]';
  74. }
  75. exports.isRegExp = isRegExp;
  76. function isMap(d) {
  77. return Object.prototype.toString.call(d) === '[object Map]';
  78. }
  79. exports.isMap = isMap;
  80. /** Call to check if your environment has `Buffer` */
  81. function haveBuffer() {
  82. return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
  83. }
  84. exports.haveBuffer = haveBuffer;
  85. // To ensure that 0.4 of node works correctly
  86. function isDate(d) {
  87. return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
  88. }
  89. exports.isDate = isDate;
  90. /**
  91. * @internal
  92. * this is to solve the `'someKey' in x` problem where x is unknown.
  93. * https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
  94. */
  95. function isObjectLike(candidate) {
  96. return typeof candidate === 'object' && candidate !== null;
  97. }
  98. exports.isObjectLike = isObjectLike;
  99. function deprecate(fn, message) {
  100. var warned = false;
  101. function deprecated() {
  102. var args = [];
  103. for (var _i = 0; _i < arguments.length; _i++) {
  104. args[_i] = arguments[_i];
  105. }
  106. if (!warned) {
  107. console.warn(message);
  108. warned = true;
  109. }
  110. return fn.apply(this, args);
  111. }
  112. return deprecated;
  113. }
  114. exports.deprecate = deprecate;
  115. //# sourceMappingURL=utils.js.map