-
Notifications
You must be signed in to change notification settings - Fork 106
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
Look for crypto
globally instead of window
#73
base: master
Are you sure you want to change the base?
Conversation
crypto
in globally instead of window
crypto
globally instead of window
Codecov Report
@@ Coverage Diff @@
## master #73 +/- ##
=======================================
Coverage 93.08% 93.08%
=======================================
Files 2 2
Lines 246 246
Branches 31 31
=======================================
Hits 229 229
Misses 17 17
Continue to review full report at Codecov.
|
const browserCrypto = root && (root.crypto || root.msCrypto) | ||
const webCrypto = root && | ||
(root.crypto || root.msCrypto) || | ||
(typeof crypto !== "undefined" ? crypto : null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably just do typeof crypto
and typeof msCrypto
instead, but I didn't want to break backward compatibility with the root
that can currently be passed into this function.
I was going to supply the same PR... this is essential if you want to be able to use this with service workers. Any plans to merge this? |
Hey, if anyone else finds this issue like I did and is using cloudflare workers, here's what worked for me: import {monotonicFactory} from 'ulid'
// or import {factory} from 'ulid'
const prng = () => {
const buffer = new Uint8Array(1)
crypto.getRandomValues(buffer)
return buffer[0] / 0xff
}
export const ulid = monotonicFactory(prng) // or factory(prng) This will bypass the code that checks for browser crypto and allow you to set your own. The PRNG function is the same as used internally with a different global reference. I think this package may be abandoned, I'm working on potentially forking it. |
Fixes #72.