Skip to content

Commit fd5b004

Browse files
committed
Update getRandomValues polyfill
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)
1 parent cf3d57e commit fd5b004

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/core/src/polyfills/crypto.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import crypto from 'crypto'; // should have webcrypto.getRandomValues defined
22

3-
if (typeof global.crypto !== 'object') {
4-
global.crypto = crypto;
3+
let context;
4+
5+
if (typeof global !== 'undefined') {
6+
context = global;
7+
} else if (typeof window !== 'undefined') {
8+
context = window;
9+
}
10+
11+
if (typeof context.crypto !== 'object') {
12+
context.crypto = crypto;
513
}
614

7-
if (typeof global.crypto.getRandomValues !== 'function') {
8-
global.crypto.getRandomValues = getRandomValues;
15+
if (typeof context.crypto.getRandomValues !== 'function') {
16+
context.crypto.getRandomValues = getRandomValues;
917
}
1018

1119
function getRandomValues(array) {

0 commit comments

Comments
 (0)