Skip to content

Commit 9531b6a

Browse files
committed
Fix crypto support detection doesn't work with jest
Some testing framework define a global `window` object but not necessarily `window.crypto`. That's the case of Facebook's Jest. ulid should use Node's `crypto` module even when `window` is defined but `window.crypto` or `window.msCrypto` are not defined.
1 parent a372928 commit 9531b6a

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ function factory(prng) {
8181

8282
/* istanbul ignore next */
8383
function _prng(root) {
84+
var crypto = root && (root.crypto || root.msCrypto)
8485

85-
if (root) {
86+
if (crypto) {
8687
try {
87-
var crypto = root.crypto || root.msCrypto
8888
return function() {
8989
return crypto.getRandomValues(new Uint16Array(1))[0] / 0xFFFF
9090
}
9191
}
9292
catch (e) {}
9393
} else {
9494
try {
95-
var crypto = require("crypto")
95+
crypto = require("crypto")
9696
return function() {
9797
return crypto.randomBytes(2).readUInt16LE() / 0xFFFF
9898
}

0 commit comments

Comments
 (0)