From ed130a53afa9aaee07453c589b7263163b1cf12e Mon Sep 17 00:00:00 2001 From: Bartosz Kurek Date: Thu, 8 Mar 2018 00:21:13 +0100 Subject: [PATCH] Library adjusted to work with webworkers. --- lib/secure-random.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/secure-random.js b/lib/secure-random.js index 96a59d0..886290c 100644 --- a/lib/secure-random.js +++ b/lib/secure-random.js @@ -20,7 +20,14 @@ function secureRandom(count, options) { if (typeof process != 'undefined' && typeof process.pid == 'number') { return nodeRandom(count, options) } else { - var crypto = window.crypto || window.msCrypto + var crypto; + if(typeof window !== 'undefined'){ + crypto = window.crypto || window.msCrypto; + } + else if(self){ + crypto = self.crypto || self.msCrypto; + } + if (!crypto) throw new Error("Your browser does not support window.crypto.") return browserRandom(count, options) } @@ -46,7 +53,13 @@ function nodeRandom(count, options) { function browserRandom(count, options) { var nativeArr = new Uint8Array(count) - var crypto = window.crypto || window.msCrypto + var crypto; + if(typeof window !== 'undefined'){ + crypto = window.crypto || window.msCrypto; + } + else if(self){ + crypto = self.crypto || self.msCrypto; + } crypto.getRandomValues(nativeArr) switch (options.type) {