UNPKG

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