Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 22, 2023
1 parent 5c56ddf commit 2082b95
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/uuidv7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

// polyfill for IE11
import { _isNumber, _isUndefined } from './utils'
import { _isNumber, _isUndefined, window } from './utils'

if (!Math.trunc) {
Math.trunc = function (v) {
Expand Down Expand Up @@ -205,7 +205,8 @@ declare const UUIDV7_DENY_WEAK_RNG: boolean
/** Stores `crypto.getRandomValues()` available in the environment. */
let getRandomValues: <T extends Uint8Array | Uint32Array>(buffer: T) => T = (buffer) => {
// fall back on Math.random() unless the flag is set to true
if (!_isUndefined(UUIDV7_DENY_WEAK_RNG) && UUIDV7_DENY_WEAK_RNG) {
// TRICKY: don't use the _isUndefined method here as can't pass the reference
if (typeof UUIDV7_DENY_WEAK_RNG !== 'undefined' && UUIDV7_DENY_WEAK_RNG) {
throw new Error('no cryptographically strong RNG available')
}

Expand All @@ -216,7 +217,7 @@ let getRandomValues: <T extends Uint8Array | Uint32Array>(buffer: T) => T = (buf
}

// detect Web Crypto API
if (!_isUndefined(crypto) && crypto.getRandomValues) {
if (!_isUndefined(window.crypto) && crypto.getRandomValues) {
getRandomValues = (buffer) => crypto.getRandomValues(buffer)
}

Expand Down

0 comments on commit 2082b95

Please sign in to comment.