Skip to content

Commit dedde6d

Browse files
committed
refactor(random-bytes): support environments with no crypto
1 parent 80973e4 commit dedde6d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/parser/utils.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ function normalizedFunctionString(fn) {
1212

1313
let randomBytes;
1414
if (typeof window !== 'undefined') {
15-
randomBytes = size => window.crypto.getRandomValues(new Uint8Array(size));
15+
if (window.crypto && window.crypto.getRandomValues) {
16+
randomBytes = size => window.crypto.getRandomValues(new Uint8Array(size));
17+
} else {
18+
randomBytes = size => {
19+
const result = new Uint8Array(size);
20+
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
21+
return result;
22+
};
23+
}
1624
} else {
1725
randomBytes = require('crypto').randomBytes;
1826
}

0 commit comments

Comments
 (0)