File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 6868 "peerDependenciesMeta" : {
6969 "@react-native-async-storage/async-storage" : {
7070 "optional" : true
71+ },
72+ "react-native-get-random-values" : {
73+ "optional" : true
7174 }
7275 },
7376 "engines" : {
Original file line number Diff line number Diff line change 1- import 'react-native-get-random-values' ;
21import { v4 as uuidv4 } from 'uuid' ;
32
3+ // `uuid` relies on a `crypto.getRandomValues` implementation, which React Native
4+ // does not provide out of the box. `react-native-get-random-values` is the
5+ // canonical polyfill, but it is an optional peer dependency: consumers may
6+ // already polyfill `crypto.getRandomValues` themselves. We attempt to load it
7+ // here, but never hard fail at import time so those apps keep working without it
8+ // installed. (Metro treats requires inside a try/catch as optional, so this does
9+ // not break bundling when the package is absent.)
10+ declare const require : ( module : string ) => unknown ;
11+
12+ try {
13+ require ( 'react-native-get-random-values' ) ;
14+ } catch {
15+ // No-op: a `crypto.getRandomValues` polyfill may already be installed globally.
16+ }
17+
418export const getUUID = ( ) : string => {
5- const UUID = uuidv4 ( ) . toString ( ) ;
6- return UUID ;
19+ try {
20+ return uuidv4 ( ) . toString ( ) ;
21+ } catch {
22+ throw new Error (
23+ "@segment/analytics-react-native requires a 'crypto.getRandomValues' " +
24+ "polyfill, which doesn't appear to be installed. Install " +
25+ "'react-native-get-random-values' and import before " +
26+ 'initializing the analytics client.'
27+ ) ;
28+ }
729} ;
Original file line number Diff line number Diff line change 7474 "peerDependenciesMeta" : {
7575 "@react-native-async-storage/async-storage" : {
7676 "optional" : true
77+ },
78+ "react-native-get-random-values" : {
79+ "optional" : true
7780 }
7881 },
7982 "dependencies" : {
You can’t perform that action at this time.
0 commit comments