segmentio / analytics-react-native

The hassle-free way to add analytics to your React-Native app.
https://segment.com/docs/sources/mobile/react-native/
MIT License
354 stars 181 forks source link

Compatibility with expo-crypto #899

Closed Coder8221 closed 3 months ago

Coder8221 commented 7 months ago

Description In setup of Segment in an expo managed project with typescript, after installing the required packages using this command: npm install --save @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values in App.tsx, adding in createClient() to initialise Segment, it causes the already included Crypto.getRandomValues() from expo-crypto method to break saying TypeError: undefined is not a function.

This only occurs in android and not ios.

Related packages and versions: "react-native": "0.71.7", "expo-crypto": "~12.2.1", "@segment/analytics-react-native": "^2.17.0", "@segment/sovran-react-native": "^1.0.4", "react-native-get-random-values": "^1.10.0",

Steps to reproduce Add createClient method with writeKey from segment and in the code include the following from package expo-crypto (import * as Crypto from 'expo-crypto';):

const r: Uint8Array = new Uint8Array(100);
Crypto.getRandomValues(r);

Run this on android simulator or on physical device and it will throw an error TypeError: undefined is not a function

Expected behavior createClient for segment and Crypto.getRandomValues from expo-crypto will work without breaking.

Actual behavior throws an error TypeError: undefined is not a function on Crypto.getRandomValues()

consuelo-sanna commented 4 months ago

For anyone experiencing this issue, the "easy" way to resolve this is by using react-native-get-random-values@1.8.0 - ref

other useful threads about it: https://github.com/expo/expo/issues/17270 https://github.com/expo/expo/issues/20906

oscb commented 3 months ago

Team is working on an option to roll your own crypto package as well as some other providers. This will enable to use our recommended package (react-native-get-random-values) or expo-crypto if targeting Expo. Similar to how we let you use the default async-storage or roll your own.

Closing this for now, will have some news in the future.