Skip to content
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

Crypto API isn't detected on alternative platforms #72

Open
blakeembrey opened this issue May 20, 2020 · 5 comments · May be fixed by #73
Open

Crypto API isn't detected on alternative platforms #72

blakeembrey opened this issue May 20, 2020 · 5 comments · May be fixed by #73

Comments

@blakeembrey
Copy link

I run across this using Cloudflare Workers. It should be possible to use this library by instead checking for a crypto global instead of trying to detect `window. Documentation from Cloudflare here: https://developers.cloudflare.com/workers/reference/apis/web-crypto/.

@blakeembrey blakeembrey linked a pull request May 20, 2020 that will close this issue
@perry-mitchell
Copy link

Hoping this aligns with the original point of this issue, but I've noticed too that on some platforms like Mobile Safari on iOS 8-10.3, crypto is window.webkitCrypto, not crypto or msCrypto as read in the source: https://github.com/ulid/javascript/blob/master/lib/index.ts#L123

Along with using a generic global lookup, perhaps expand the crypto lookup to include the webkit reference?

@SokichiFujita
Copy link

I also could not use ulid in Web Worker of Chrome or Safari by the same reason.

@perry-mitchell
Copy link

@SokichiFujita Added an issue for that at #83. Should be fixed by my PR at #82.

@DavidJFelix
Copy link

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.

@perry-mitchell
Copy link

@DavidJFelix I've made a supported fork here, in case that's of interest: ulidx. There's also another fork designed specifically for cloudflare workers: ulid-workers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants