Skip to content

Commit 86b1ac9

Browse files
authored
Merge pull request #13 from brentvatne/@brent/expo-managed-workflow-native-module
🎉 Add Expo managed workflow support for SDK 39+
2 parents 75829b2 + 85f4839 commit 86b1ac9

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

getRandomBase64.expo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { NativeModules } = require('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+
module.exports = NativeModules.ExpoRandom.getRandomBase64String

getRandomBase64.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('react-native').NativeModules.RNGetRandomValues.getRandomBase64

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const RNGetRandomValues = require('react-native').NativeModules.RNGetRandomValues
21
const base64Decode = require('fast-base64-decode')
2+
const getRandomBase64 = require('./getRandomBase64')
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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# `getRandomValues` for React Native
1+
# `crypto.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,14 @@ 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+
28+
console.log(uuid())
29+
```
30+
2131
## API
2232

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

0 commit comments

Comments
 (0)