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

Look for crypto library using self keyword instead of window #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrsun
Copy link

@jrsun jrsun commented Apr 22, 2022

The name of the global namespace varies in different execution environments. The self keyword allows accessing crypto in both the browser and WorkerGlobalScope (web workers/service workers), whereas window only works for the browser. This change allows the library to work in more environments than previously.

References

Window.self
WorkerGlobalScope.self
Web APIs available in Workers

The name of the global namespace varies in different execution environments. The `self` keyword allows accessing `crypto` in both the browser and WorkerGlobalScope (web workers/service workers), whereas `window` only works for the browser.

See: https://developer.mozilla.org/en-US/docs/Web/API/Window/self
https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self
@jrsun jrsun changed the title Look for crypto using self keyword Look for crypto library using self keyword instead of window Apr 22, 2022
@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.

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 this pull request may close these issues.

2 participants