From f336097b6a75ac819d934dbc0db0f6901c4ad464 Mon Sep 17 00:00:00 2001 From: Emil Bay Date: Fri, 3 Nov 2017 08:17:38 +0100 Subject: [PATCH] Fix bug with undefined window in web workers Fixes #8 --- randombytes.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/randombytes.js b/randombytes.js index 6af2653..dc25851 100644 --- a/randombytes.js +++ b/randombytes.js @@ -1,9 +1,9 @@ var assert = require('nanoassert') var randombytes = (function () { var QUOTA = 65536 // limit for QuotaExceededException - var crypto = typeof window !== 'undefined' ? (window.crypto || window.msCrypto) : null + var crypto = typeof global !== 'undefined' ? crypto = (global.crypto || global.msCrypto) : null - function windowBytes (out, n) { + function browserBytes (out, n) { for (var i = 0; i < n; i += QUOTA) { crypto.getRandomValues(out.subarray(i, i + Math.min(n - i, QUOTA))) } @@ -18,7 +18,7 @@ var randombytes = (function () { } if (crypto && crypto.getRandomValues) { - return windowBytes + return browserBytes } else if (typeof require !== 'undefined') { // Node.js. crypto = require('cry' + 'pto');