allen
allen2y ago

Crypto dependency

It seems that the convex client has a dependency on the Crypto API (https://developer.mozilla.org/en-US/docs/Web/API/Crypto) which is not available in the React Native runtime. I am encountering this when trying to optimistically insert a document into the local cache. Even if I don't lean on the crypto api directly like the documentation suggests (https://docs.convex.dev/client/react/optimistic-updates#complex-example) and provide a different random string, it still seems the convex client attempts to use this api. Before I go hunting for a polyfill or shim, I was wondering if there is another approach I can take.
Crypto - Web APIs | MDN
The Crypto interface represents basic cryptography features available in the current context. It allows access to a cryptographically strong random number generator and to cryptographic primitives.
Optimistic Updates | Convex Developer Hub
Even though Convex queries are completely reactive, sometimes you'll want to
10 Replies
sshader
sshader2y ago
You might be missing react-native-get-random-values in step 2 here? https://docs.convex.dev/quickstart/react-native
React Native Quickstart | Convex Developer Hub
Add Convex to a React Native Expo project
allen
allenOP2y ago
Ah i must have missed that last dep. I'll install and give it a shot. Thanks for the quick response! still throwing with ReferenceError: Property 'crypto' doesn't exist
sshader
sshader2y ago
Just to check -- did you add the import like in step 9?
allen
allenOP2y ago
Stack trace is a mess, but seems to lead to applyOptomisticUpdate method I have to follow all of the instructions?!
sshader
sshader2y ago
Separately, I think we might be able to recommend something else aside from crypto.randomUUID() for creating IDs for optimistic updates (and not require this dependency). You said you noticed this issue even when generating a random string for your optimistic update in some other way?
allen
allenOP2y ago
yes, it wasnt when invoking crypto from my code -- it was thrown somewhere in your lib
sshader
sshader2y ago
If you happen to have a stack trace handy that you wouldn't mind sharing, that would be great. Otherwise I'll look in to reproducing this on my end.
allen
allenOP2y ago
with that imported in the app its now throwing with undefined is not a function within applyOptimisticUpdate its bundled gobbledygook actually, its only throwing the undefined error if i try to use crypto. If i give it a random string, it seems your lib handles it fine now that the dependency is in the project. actually no its still throwing, its just that it doesnt block the mutation whereas when i use it, it fails the entire mutation let me play with it a bit and see if i cant get you something pinpointed After clearing build cache and rebuilding it seems the convex lib works ok, however, that polyfill only provides the getRandomValues method, not the randomUUID method in the docs, so I'll still need another way of getting a random string -- which is fine, but just making you aware so the docs can be tighter here.
sshader
sshader2y ago
Yeah thank you for the feedback! Sounds like we should not be recommending randomUUID in our docs (or at least providing a workaround that actually works)
allen
allenOP2y ago
perhaps if new Id() is called without a second parameter, Convex could just deal with generating an appropriate value so my app doesnt have to deal with this at all.

Did you find this page helpful?