Skip to content

Look for crypto globally instead of window #73

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

Closed
wants to merge 2 commits into 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

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.08%. Comparing base (a583120) to head (60432f4).
Report is 13 commits behind head on master.

Files with missing lines Patch % Lines
dist/index.umd.js 66.66% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #73   +/-   ##
=======================================
  Coverage   93.08%   93.08%           
=======================================
  Files           2        2           
  Lines         246      246           
  Branches       31       31           
=======================================
  Hits          229      229           
  Misses         17       17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

@perry-mitchell
Copy link
Member

Closing this, as it will be covered in an upcoming refactor - see #110.

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
5 participants