You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Copy-pasted from the source code of an unmaintained package:// https://github.com/browserify/randombytes/blob/master/browser.js//// Fixed `Uncaught ReferenceError: global is not defined`.// https://github.com/browserify/randombytes/issues/33//// Replaced `safe-buffer` with `buffer`.// eslint-disable-next-line unicorn/prefer-node-protocolimportBufferfrom'buffer';// limit of Crypto.getRandomValues()// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValuesconstMAX_BYTES=65_536;// Node supports requesting up to this number of bytes// https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48constMAX_UINT32=4_294_967_295;// eslint-disable-next-line no-undefconstcrypto=typeofwindow==='undefined' ? null : window.crypto;// https://nodejs.org/api/crypto.html#cryptorandombytessize-callbackexportdefaultfunctionrandomBytes_(size,callback){if(!crypto||!crypto.getRandomValues){thrownewError('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11');}returnrandomBytes(size,callback);}functionrandomBytes(size,cb){// phantomjs needs to throwif(size>MAX_UINT32){thrownewRangeError('requested too many random bytes');}constbytes=Buffer.allocUnsafe(size);if(size>0){// getRandomValues fails on IE if size == 0if(size>MAX_BYTES){// this is the max bytes crypto.getRandomValues// can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValuesfor(letgenerated=0;generated<size;generated+=MAX_BYTES){// buffer.slice automatically checks if the end is past the end of// the buffer so we don't have to herecrypto.getRandomValues(bytes.slice(generated,generated+MAX_BYTES));}}else{crypto.getRandomValues(bytes);}}if(typeofcb==='function'){returnprocess.nextTick(()=>{cb(null,bytes);});}returnbytes;}
Activity
razorness commentedon Dec 13, 2022
Same with Vite.js:
Uncaught ReferenceError: global is not definedcatamphetamine commentedon Jun 29, 2023
Fixed code: