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

[expo-random] Add sync equivalents for expo-random methods #9750

Merged
merged 16 commits into from
Aug 14, 2020

Conversation

brentvatne
Copy link
Member

Why

Provide building blocks to fix #7209

How

Added a sync version of the expo-random getRandomBytes method.

To do this I had to convert expo-random away from a unimodule, because we don't support sync methods yet. This was a bit awkward to do on the build/dependency configuration side because we lean on autolinking unimodules in our packages directory, so I'd appreciate feedback on whether I've configured that properly in the appropriate gradle files.

Test Plan

Open the "Random" example in NCL

@github-actions
Copy link
Contributor

Native Component List for this branch is ready

@brentvatne brentvatne force-pushed the @brent/expo-random-sync-methods branch from 30bd095 to a2faeb0 Compare August 14, 2020 04:49
@brentvatne brentvatne requested a review from ide August 14, 2020 05:00
@Override
public String getName() {
return "ExpoRandom";
}

@ExpoMethod
public void getRandomBase64StringAsync(int randomByteCount, final Promise promise) {
private String _getRandomBase64String(int randomByteCount) throws NoSuchAlgorithmException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave off the underscore for private Java methods

if (result == errSecSuccess) {
return [bytes base64EncodedStringWithOptions:0];
} else {
@throw([NSException exceptionWithName:@"expo-random" reason:@"Failed to create secure random string" userInfo:nil]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing, accept an optimal NSError double pointer and set its value.

export function getRandomBytes(byteCount: number): Uint8Array {
assertByteCount(byteCount, 'getRandomBytes');
const validByteCount = Math.floor(byteCount);
if (ExpoRandom.getRandomBytesAsync) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRandomBytes

packages/react-native.config.js Outdated Show resolved Hide resolved
Copy link
Contributor

@sjchmiela sjchmiela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! Happy to see integration with newest uuid! 👍

android/settings.gradle Outdated Show resolved Hide resolved
Comment on lines 42 to 43
include(":expo-random")
project(":expo-random").projectDir = new File("../packages/expo-random/android")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please move it to nearer include ':unimodules-test-core' etc.? This should be wrapped with // WHEN_DISTRIBUTING_REMOVE_{FROM,TO}_HERE, otherwise it'll end up in Android shell app (Turtle shouldn't try to use this project, instead, expokit prebuilt AARs are available there).

docs/pages/versions/unversioned/sdk/random.md Outdated Show resolved Hide resolved
packages/expo-random/CHANGELOG.md Outdated Show resolved Hide resolved
packages/expo-random/android/build.gradle Outdated Show resolved Hide resolved
packages/expo-random/package.json Outdated Show resolved Hide resolved
packages/expo-random/src/ExpoRandom.web.ts Outdated Show resolved Hide resolved
packages/react-native.config.js Outdated Show resolved Hide resolved
@brentvatne brentvatne force-pushed the @brent/expo-random-sync-methods branch from 079d890 to 3f0b065 Compare August 14, 2020 18:44
async getRandomBytesAsync(length: number): Promise<Uint8Array> {
const array = new Uint8Array(length);
return window.crypto.getRandomValues(array);
// @ts-ignore
return (window.crypto ?? window.msCrypto).getRandomValues(array)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (window.crypto ?? window.msCrypto).getRandomValues(array)
return (window.crypto ?? window.msCrypto).getRandomValues(array);

@brentvatne brentvatne changed the title Add sync equivalents for expo-random methods [expo-random] Add sync equivalents for expo-random methods Aug 14, 2020
@brentvatne brentvatne merged commit 3571d41 into master Aug 14, 2020
@martinheidegger
Copy link

👋 Hi! Just wanted to let you know that there is a conversation in react-native-get-random-values about the performance passing the binary data between the threads.

Related to the performance issue, I was wondering if it wouldn't be possible to change from a string to an array of numbers. This way the new Uint8Array(dataFromNative) could be used without the base64 conversion on both sides. More data would need to sent, but maybe it is still faster than the string array?

@ide
Copy link
Member

ide commented Aug 19, 2020

How much does it matter or is this mostly a developer-driven desire rather than a UX-driven one? Either way in the future we should be able to build something faster than strings and numbers, we're just looking for something reasonable that can make UUIDs which are tiny regardless.

@martinheidegger
Copy link

The get-random-values is used in crypto libraries like sodium-javascript which tend to process quite a bit of data which can get really slow (stopped, stuttering UI)

@brentvatne
Copy link
Member Author

@martinheidegger - i'm open to a pr to improve perf here, i don't have time to work on it myself though. the main objective of this pr was to unblock people who need crypto.getRandomValues polyfill for usage with uuid and so on

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.

Compatibility with uuid 7.x, nanoid, and others
4 participants