utils.js 4.6 KB

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