rng-browser.js 1.1 KB

1234567891011121314151617181920212223242526
  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. Also,
  15. // find the complete implementation of crypto (msCrypto) on IE11.
  16. getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
  17. if (!getRandomValues) {
  18. throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
  19. }
  20. }
  21. return getRandomValues(rnds8);
  22. }