Skip to content

Commit

Permalink
🌊 Add Expo managed workflow support for SDK 39+
Browse files Browse the repository at this point in the history
This is done by using the ExpoRandom native module if it exists.
  • Loading branch information
brentvatne committed Aug 19, 2020
1 parent 75829b2 commit 8e25a1a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 8 additions & 0 deletions getRandomBase64.expo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NativeModules } from 'react-native';

// In the Expo managed workflow the getRandomBase64 method is provided by ExpoRandom.getRandomBase64String
if (!NativeModules.ExpoRandom) {
throw new Error('Expo managed workflow support for react-native-get-random-values is only available in SDK 39 and higher.');
}

export default NativeModules.ExpoRandom.getRandomBase64String;
3 changes: 3 additions & 0 deletions getRandomBase64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { NativeModules } from 'react-native';

export default NativeModules.RNGetRandomValues.getRandomBase64;
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const RNGetRandomValues = require('react-native').NativeModules.RNGetRandomValues
const base64Decode = require('fast-base64-decode')
import getRandomBase64 from './getRandomBase64'
import base64Decode from 'fast-base64-decode'

class TypeMismatchError extends Error {}
class QuotaExceededError extends Error {}
Expand Down Expand Up @@ -31,7 +31,7 @@ function getRandomValues (array) {
throw new QuotaExceededError('Can only request a maximum of 65536 bytes')
}

// Calling RNGetRandomValues.getRandomBase64 in debug mode leads to the error
// Calling getRandomBase64 in debug mode leads to the error
// "Calling synchronous methods on native modules is not supported in Chrome".
// So in that specific case we fall back to just using Math.random.
if (__DEV__) {
Expand All @@ -40,7 +40,7 @@ function getRandomValues (array) {
}
}

base64Decode(RNGetRandomValues.getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength))
base64Decode(getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength))

return array
}
Expand Down
15 changes: 12 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# `getRandomValues` for React Native
# `crypo.getRandomValues` for React Native

A small implementation of `getRandomValues` for React Native.
A small implementation of `crypto.getRandomValues` for React Native. This is useful to polyfill for libraries like [uuid](https://www.npmjs.com/package/uuid) that depend on it.

## Installation

```sh
npm install --save react-native-get-random-values
cd ios && pod install && cd ..
npx pod-install
```

> 💡 If you use the Expo managed workflow you will see "CocoaPods is not supported in this project" - this is fine, it's not necessary.
## Usage

This library works as a polyfill for the global `crypto.getRandomValues`.
Expand All @@ -18,6 +20,13 @@ This library works as a polyfill for the global `crypto.getRandomValues`.
import 'react-native-get-random-values'
```

Now you can use `uuid` or other libraries that assume `crypto.getRandomValues` is available.

```javascript
import { v4 as uuid } from 'uuid';
console.log(uuid());
```

## API

### `crypto.getRandomValues(typedArray)`
Expand Down

0 comments on commit 8e25a1a

Please sign in to comment.