javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Look for `crypto` globally instead of `window`

Open blakeembrey opened this issue 5 years ago • 3 comments

Fixes https://github.com/ulid/javascript/issues/72.

blakeembrey avatar May 20 '20 02:05 blakeembrey

Codecov Report

Merging #73 into master will not change coverage. The diff coverage is 66.66%.

Impacted file tree graph

@@           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 data Powered by Codecov. Last update a583120...60432f4. Read the comment docs.

codecov-commenter avatar May 20 '20 02:05 codecov-commenter

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?

happyteque avatar Jul 06 '20 15:07 happyteque

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.

DavidJFelix avatar Jun 01 '22 13:06 DavidJFelix