MCPcopy
hub / github.com/ulid/javascript / detectPRNG

Function detectPRNG

source/ulid.ts:46–63  ·  view source on GitHub ↗
(root?: any)

Source from the content-addressed store, hash-verified

44 * @returns The PRNG function
45 */
46export function detectPRNG(root?: any): PRNG {
47 const rootLookup = root || detectRoot();
48 const globalCrypto =
49 (rootLookup && (rootLookup.crypto || rootLookup.msCrypto)) ||
50 (typeof crypto !== "undefined" ? crypto : null);
51 if (typeof globalCrypto?.getRandomValues === "function") {
52 return () => {
53 const buffer = new Uint8Array(1);
54 globalCrypto.getRandomValues(buffer);
55 return buffer[0] / 256;
56 };
57 } else if (typeof globalCrypto?.randomBytes === "function") {
58 return () => globalCrypto.randomBytes(1).readUInt8() / 256;
59 } else if (crypto?.randomBytes) {
60 return () => crypto.randomBytes(1).readUInt8() / 256;
61 }
62 throw new ULIDError(ULIDErrorCode.PRNGDetectFailure, "Failed to find a reliable PRNG");
63}
64
65function detectRoot(): any {
66 if (inWebWorker()) return self;

Callers 2

monotonicFactoryFunction · 0.85
ulidFunction · 0.85

Calls 1

detectRootFunction · 0.85

Tested by

no test coverage detected