browserify / randombytes

random bytes from browserify stand alone
MIT License
99 stars 47 forks source link

global is replaced by globalThis? #29

Closed garmin21 closed 1 year ago

garmin21 commented 2 years ago

There is a problem with the global reference used in the source code, it may not exist, because it is a compatibility problem, why not use globalThis?

justin0mcateer commented 1 year ago

I ran into this problem and needed to patch the package due to lack of 'global' in the browser environment.

Here is the (hack) patch I used:

diff --git a/node_modules/randombytes/browser.js b/node_modules/randombytes/browser.js
index 0fb0b71..7dc0e6c 100644
--- a/node_modules/randombytes/browser.js
+++ b/node_modules/randombytes/browser.js
@@ -13,7 +13,9 @@ function oldBrowser () {
 }

 var Buffer = require('safe-buffer').Buffer
-var crypto = global.crypto || global.msCrypto
+// var crypto = global.crypto || global.msCrypto
+var crypto = globalThis.crypto || globalThis.msCrypto
+

 if (crypto && crypto.getRandomValues) {
   module.exports = randomBytes
bneijt commented 1 year ago

I think it makes sense to use globalThis if available and fall back to the injected global if required, so I created a PR to that effect.