rng-browser.js 927 B

12345678910111213141516171819202122232425
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = rng;
  6. // Unique ID creation requires a high quality random # generator. In the browser we therefore
  7. // require the crypto API and do not support built-in fallback to lower quality random number
  8. // generators (like Math.random()).
  9. let getRandomValues;
  10. const rnds8 = new Uint8Array(16);
  11. function rng() {
  12. // lazy load so that environments that need to polyfill have a chance to do so
  13. if (!getRandomValues) {
  14. // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
  15. getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
  16. if (!getRandomValues) {
  17. throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
  18. }
  19. }
  20. return getRandomValues(rnds8);
  21. }