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 globally instead of window #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

blakeembrey
Copy link

Fixes #72.

@blakeembrey blakeembrey changed the title Look for crypto in globally instead of window Look for crypto globally instead of window May 20, 2020
@codecov-commenter
Copy link

codecov-commenter commented May 20, 2020

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.

const browserCrypto = root && (root.crypto || root.msCrypto)
const webCrypto = root &&
(root.crypto || root.msCrypto) ||
(typeof crypto !== "undefined" ? crypto : null)
Copy link
Author

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.

@happyteque
Copy link

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?

@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.

Crypto API isn't detected on alternative platforms
4 participants