(root?: any)
| 44 | * @returns The PRNG function |
| 45 | */ |
| 46 | export 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 | |
| 65 | function detectRoot(): any { |
| 66 | if (inWebWorker()) return self; |
no test coverage detected