utils.js 4.5 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. 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 = 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 (typeof window !== 'undefined') {
  30. // browser crypto implementation(s)
  31. var target_1 = window.crypto || window.msCrypto; // allow for IE11
  32. if (target_1 && target_1.getRandomValues) {
  33. return function (size) { return target_1.getRandomValues(buffer_1.Buffer.alloc(size)); };
  34. }
  35. }
  36. if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
  37. // allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
  38. return function (size) { return global.crypto.getRandomValues(buffer_1.Buffer.alloc(size)); };
  39. }
  40. var requiredRandomBytes;
  41. try {
  42. // eslint-disable-next-line @typescript-eslint/no-var-requires
  43. requiredRandomBytes = require('crypto').randomBytes;
  44. }
  45. catch (e) {
  46. // keep the fallback
  47. }
  48. // NOTE: in transpiled cases the above require might return null/undefined
  49. return requiredRandomBytes || insecureRandomBytes;
  50. };
  51. exports.randomBytes = detectRandomBytes();
  52. function isAnyArrayBuffer(value) {
  53. return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
  54. }
  55. exports.isAnyArrayBuffer = isAnyArrayBuffer;
  56. function isUint8Array(value) {
  57. return Object.prototype.toString.call(value) === '[object Uint8Array]';
  58. }
  59. exports.isUint8Array = isUint8Array;
  60. function isBigInt64Array(value) {
  61. return Object.prototype.toString.call(value) === '[object BigInt64Array]';
  62. }
  63. exports.isBigInt64Array = isBigInt64Array;
  64. function isBigUInt64Array(value) {
  65. return Object.prototype.toString.call(value) === '[object BigUint64Array]';
  66. }
  67. exports.isBigUInt64Array = isBigUInt64Array;
  68. function isRegExp(d) {
  69. return Object.prototype.toString.call(d) === '[object RegExp]';
  70. }
  71. exports.isRegExp = isRegExp;
  72. function isMap(d) {
  73. return Object.prototype.toString.call(d) === '[object Map]';
  74. }
  75. exports.isMap = isMap;
  76. /** Call to check if your environment has `Buffer` */
  77. function haveBuffer() {
  78. return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
  79. }
  80. exports.haveBuffer = haveBuffer;
  81. // To ensure that 0.4 of node works correctly
  82. function isDate(d) {
  83. return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
  84. }
  85. exports.isDate = isDate;
  86. /**
  87. * @internal
  88. * this is to solve the `'someKey' in x` problem where x is unknown.
  89. * https://github.com/typescript-eslint/typescript-eslint/issues/1071#issuecomment-541955753
  90. */
  91. function isObjectLike(candidate) {
  92. return typeof candidate === 'object' && candidate !== null;
  93. }
  94. exports.isObjectLike = isObjectLike;
  95. function deprecate(fn, message) {
  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