Skip to content

Commit

Permalink
lib: use self instead of window
Browse files Browse the repository at this point in the history
Use `self` instead of `window` for WebWorker compatibility.
  • Loading branch information
indutny authored Feb 6, 2017
2 parents f077f0a + 4b2286c commit a06c49d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ Rand.prototype.generate = function generate(len) {
return this._rand(len);
};

if (typeof window === 'object') {
if (window.crypto && window.crypto.getRandomValues) {
if (typeof self === 'object') {
if (self.crypto && self.crypto.getRandomValues) {
// Modern browsers
Rand.prototype._rand = function _rand(n) {
var arr = new Uint8Array(n);
window.crypto.getRandomValues(arr);
self.crypto.getRandomValues(arr);
return arr;
};
} else if (window.msCrypto && window.msCrypto.getRandomValues) {
} else if (self.msCrypto && self.msCrypto.getRandomValues) {
// IE
Rand.prototype._rand = function _rand(n) {
var arr = new Uint8Array(n);
window.msCrypto.getRandomValues(arr);
self.msCrypto.getRandomValues(arr);
return arr;
};
} else {
Expand All @@ -38,7 +38,7 @@ if (typeof window === 'object') {
};
}
} else {
// Node.js or Web worker
// Node.js or Web worker with no crypto support
try {
var crypto = require('crypto');

Expand Down

0 comments on commit a06c49d

Please sign in to comment.