From 8e25a1a880a37d276d72b43dff50d0e1925c67e3 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Wed, 19 Aug 2020 12:29:13 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=8A=20Add=20Expo=20managed=20workflow?= =?UTF-8?q?=20support=20for=20SDK=2039+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is done by using the ExpoRandom native module if it exists. --- getRandomBase64.expo.js | 8 ++++++++ getRandomBase64.js | 3 +++ index.js | 8 ++++---- readme.md | 15 ++++++++++++--- 4 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 getRandomBase64.expo.js create mode 100644 getRandomBase64.js diff --git a/getRandomBase64.expo.js b/getRandomBase64.expo.js new file mode 100644 index 0000000..d18bacc --- /dev/null +++ b/getRandomBase64.expo.js @@ -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; \ No newline at end of file diff --git a/getRandomBase64.js b/getRandomBase64.js new file mode 100644 index 0000000..f6db264 --- /dev/null +++ b/getRandomBase64.js @@ -0,0 +1,3 @@ +import { NativeModules } from 'react-native'; + +export default NativeModules.RNGetRandomValues.getRandomBase64; \ No newline at end of file diff --git a/index.js b/index.js index 4f9ded5..57f034a 100644 --- a/index.js +++ b/index.js @@ -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 {} @@ -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__) { @@ -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 } diff --git a/readme.md b/readme.md index c924669..92b2255 100644 --- a/readme.md +++ b/readme.md @@ -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`. @@ -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)`