Skip to content

Mark package as side effect free #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ const MAX_BYTES = 65536
// https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48
const MAX_UINT32 = 4294967295

function oldBrowser () {
throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11')
}

const _global = typeof globalThis !== 'undefined' ? globalThis : global
const crypto = _global.crypto || _global.msCrypto
export function randomBytes (size, cb) {
const _global = typeof globalThis !== 'undefined' ? globalThis : global
const crypto = _global.crypto || _global.msCrypto

if (crypto && crypto.getRandomValues) {
module.exports = randomBytes
} else {
module.exports = oldBrowser
}
if (!crypto || !crypto.getRandomValues) {
throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11')
}

function randomBytes (size, cb) {
// phantomjs needs to throw
if (size > MAX_UINT32) throw new RangeError('requested too many random bytes')

Expand All @@ -47,4 +41,4 @@ function randomBytes (size, cb) {
}

return bytes
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"tap-spec": "^5.0.0",
"tape": "^5.2.2",
"zuul": "^3.12.0"
}
},
"sideEffects": false
}