Skip to content

Commit

Permalink
Update getRandomValues polyfill
Browse files Browse the repository at this point in the history
Add logic to check if global exists. Move existing condition blocks into
this block. Note that these global checks are for non-browser
environments. For browser environments, add similar polyfill
via the window global variable.

Reference:
- brix/crypto-js#256 (comment)
  • Loading branch information
frostehhh committed Sep 24, 2023
1 parent cf3d57e commit fd5b004
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/core/src/polyfills/crypto.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import crypto from 'crypto'; // should have webcrypto.getRandomValues defined

if (typeof global.crypto !== 'object') {
global.crypto = crypto;
let context;

if (typeof global !== 'undefined') {
context = global;
} else if (typeof window !== 'undefined') {
context = window;
}

if (typeof context.crypto !== 'object') {
context.crypto = crypto;
}

if (typeof global.crypto.getRandomValues !== 'function') {
global.crypto.getRandomValues = getRandomValues;
if (typeof context.crypto.getRandomValues !== 'function') {
context.crypto.getRandomValues = getRandomValues;
}

function getRandomValues(array) {
Expand Down

0 comments on commit fd5b004

Please sign in to comment.