diff --git a/lib/parser/utils.js b/lib/parser/utils.js index 51270b6c..4a23ea5b 100644 --- a/lib/parser/utils.js +++ b/lib/parser/utils.js @@ -10,19 +10,21 @@ function normalizedFunctionString(fn) { return fn.toString().replace('function(', 'function ('); } -let randomBytes; -if (typeof window !== 'undefined') { - if (window.crypto && window.crypto.getRandomValues) { - randomBytes = size => window.crypto.getRandomValues(new Uint8Array(size)); - } else { - randomBytes = size => { - const result = new Uint8Array(size); - for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256); - return result; - }; - } +function insecureRandomBytes(size) { + const result = new Uint8Array(size); + for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256); + return result; +} + +let randomBytes = insecureRandomBytes; +if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) { + randomBytes = size => window.crypto.getRandomValues(new Uint8Array(size)); } else { - randomBytes = require('crypto').randomBytes; + try { + randomBytes = require('crypto').randomBytes; + } catch (e) { + // keep the fallback + } } module.exports = {