Look for `crypto` globally instead of `window`
Fixes https://github.com/ulid/javascript/issues/72.
Codecov Report
Merging #73 into master will not change coverage. The diff coverage is
66.66%.
@@ Coverage Diff @@
## master #73 +/- ##
=======================================
Coverage 93.08% 93.08%
=======================================
Files 2 2
Lines 246 246
Branches 31 31
=======================================
Hits 229 229
Misses 17 17
| Impacted Files | Coverage Δ | |
|---|---|---|
| dist/index.umd.js | 87.40% <66.66%> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update a583120...60432f4. Read the comment docs.
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.