UNPKG

986 BJavaScriptView 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()).
10// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also,
11// find the complete implementation of crypto (msCrypto) on IE11.
12const getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
13const rnds8 = new Uint8Array(16);
14
15function rng() {
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}
\No newline at end of file