Skip to content

Commit 47fd5f7

Browse files
committed
fix(random-bytes): wrap crypto require in try/catch for fallback
1 parent 5b9ce62 commit 47fd5f7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lib/parser/utils.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ function normalizedFunctionString(fn) {
1010
return fn.toString().replace('function(', 'function (');
1111
}
1212

13-
let randomBytes;
14-
if (typeof window !== 'undefined') {
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-
}
13+
function insecureRandomBytes(size) {
14+
const result = new Uint8Array(size);
15+
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
16+
return result;
17+
}
18+
19+
let randomBytes = insecureRandomBytes;
20+
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
21+
randomBytes = size => window.crypto.getRandomValues(new Uint8Array(size));
2422
} else {
25-
randomBytes = require('crypto').randomBytes;
23+
try {
24+
randomBytes = require('crypto').randomBytes;
25+
} catch (e) {
26+
// keep the fallback
27+
}
2628
}
2729

2830
module.exports = {

0 commit comments

Comments
 (0)