Closed sergiocarneiro closed 1 year ago
This change makes the
getWebCrypto
function more robust and able to work in more runtime setups (e.g. Cloudflare Workers withnode_compat
enabled).
Hello @sergiocarneiro, I'm wondering what the exact problem is you were having with the current way of picking which WebCrypto
to use. Are you trying to make globalThis.crypto
the default the library tries to reach for Node's because it somehow causes issues with non-Node runtimes?
I'm wondering what the exact problem is you were having with the current way of picking which
WebCrypto
to use.
Hello @MasterKale. In my Cloudflare Worker, I have Node polyfills enabled. I'm not sure if it's a bug from their platform or not, but I know that what ends up happening is an environment that:
globalThis.crypto
node:crypto
crypto.webcrypto
is undefinedSo without this change I submitted, using functions such as getRegistrationOptions
throws the error "Cannot read properties of undefined (reading 'getRandomValues')"
— the method is available through globalThis.crypto
.
Are you trying to make
globalThis.crypto
the default?
Yes, and only override it if crypto.webcrypto
is defined, not if node:crypto
can be imported.
+1 to this issue--would love to use this in a Cloudflare worker, but without @sergiocarneiro's updates, I'm also fully blocked with the same "Cannot read properties of undefined (reading 'getRandomValues')"
issue.
@sergiocarneiro Thank you for the contribution. I took a stab at solving this problem with #468, and I also managed to add test coverage around getWebCrypto()
that should ensure support for runtimes like your CF worker w/Node compat. In particular this test should capture the nuance of the runtime issue you've been experiencing:
Tests just passed over there so I'm going to merge and cut a release with this fix tomorrow.
Fallback to
globalThis.crypto
whennode:crypto
exists butcrypto.webcrypto
does not.It also adds the ability to throw
MissingWebCrypto
whenwebcrypto
is missing.This change makes the
getWebCrypto
function more robust and able to work in more runtime setups (e.g. Cloudflare Workers withnode_compat
enabled).