Skip to content

Commit 83b41e5

Browse files
committed
fix(deps): switch react-native-get-random-values to optional
1 parent 74884be commit 83b41e5

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

packages/core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
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": {

packages/core/src/uuid.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
import 'react-native-get-random-values';
21
import { 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+
418
export 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
};

packages/sovran/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
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": {

0 commit comments

Comments
 (0)