Skip to content

Commit 8e25a1a

Browse files
committed
🌊 Add Expo managed workflow support for SDK 39+
This is done by using the ExpoRandom native module if it exists.
1 parent 75829b2 commit 8e25a1a

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

getRandomBase64.expo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NativeModules } from 'react-native';
2+
3+
// In the Expo managed workflow the getRandomBase64 method is provided by ExpoRandom.getRandomBase64String
4+
if (!NativeModules.ExpoRandom) {
5+
throw new Error('Expo managed workflow support for react-native-get-random-values is only available in SDK 39 and higher.');
6+
}
7+
8+
export default NativeModules.ExpoRandom.getRandomBase64String;

getRandomBase64.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { NativeModules } from 'react-native';
2+
3+
export default NativeModules.RNGetRandomValues.getRandomBase64;

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const RNGetRandomValues = require('react-native').NativeModules.RNGetRandomValues
2-
const base64Decode = require('fast-base64-decode')
1+
import getRandomBase64 from './getRandomBase64'
2+
import base64Decode from 'fast-base64-decode'
33

44
class TypeMismatchError extends Error {}
55
class QuotaExceededError extends Error {}
@@ -31,7 +31,7 @@ function getRandomValues (array) {
3131
throw new QuotaExceededError('Can only request a maximum of 65536 bytes')
3232
}
3333

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

43-
base64Decode(RNGetRandomValues.getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength))
43+
base64Decode(getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength))
4444

4545
return array
4646
}

readme.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# `getRandomValues` for React Native
1+
# `crypo.getRandomValues` for React Native
22

3-
A small implementation of `getRandomValues` for React Native.
3+
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.
44

55
## Installation
66

77
```sh
88
npm install --save react-native-get-random-values
9-
cd ios && pod install && cd ..
9+
npx pod-install
1010
```
1111

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

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

23+
Now you can use `uuid` or other libraries that assume `crypto.getRandomValues` is available.
24+
25+
```javascript
26+
import { v4 as uuid } from 'uuid';
27+
console.log(uuid());
28+
```
29+
2130
## API
2231

2332
### `crypto.getRandomValues(typedArray)`

0 commit comments

Comments
 (0)