Skip to content

Commit

Permalink
Fix browser random in WebWorkers (#2405).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Dec 24, 2021
1 parent 3d6a7ec commit 4ba63ac
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/random/src.ts/browser-random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ const logger = new Logger(version);
// Debugging line for testing browser lib in node
//const window = { crypto: { getRandomValues: () => { } } };

let anyGlobal: any = null;
try {
anyGlobal = (window as any);
if (anyGlobal == null) { throw new Error("try next"); }
} catch (error) {
try {
anyGlobal = (global as any);
if (anyGlobal == null) { throw new Error("try next"); }
} catch (error) {
anyGlobal = { };
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
function getGlobal(): any {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
};

const anyGlobal = getGlobal();

let crypto: any = anyGlobal.crypto || anyGlobal.msCrypto;
if (!crypto || !crypto.getRandomValues) {
Expand Down

0 comments on commit 4ba63ac

Please sign in to comment.