From 83b41e51bb05440b6e8142cbddfcc05cda593cf3 Mon Sep 17 00:00:00 2001 From: Spencer Yoder <25213226+Spencer-Yoder@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:49:41 -0500 Subject: [PATCH] fix(deps): switch react-native-get-random-values to optional --- packages/core/package.json | 3 +++ packages/core/src/uuid.ts | 28 +++++++++++++++++++++++++--- packages/sovran/package.json | 3 +++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index d5cad93d6..f2b531527 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -68,6 +68,9 @@ "peerDependenciesMeta": { "@react-native-async-storage/async-storage": { "optional": true + }, + "react-native-get-random-values": { + "optional": true } }, "engines": { diff --git a/packages/core/src/uuid.ts b/packages/core/src/uuid.ts index 64d2d7648..8576e4c04 100644 --- a/packages/core/src/uuid.ts +++ b/packages/core/src/uuid.ts @@ -1,7 +1,29 @@ -import 'react-native-get-random-values'; import { v4 as uuidv4 } from 'uuid'; +// `uuid` relies on a `crypto.getRandomValues` implementation, which React Native +// does not provide out of the box. `react-native-get-random-values` is the +// canonical polyfill, but it is an optional peer dependency: consumers may +// already polyfill `crypto.getRandomValues` themselves. We attempt to load it +// here, but never hard fail at import time so those apps keep working without it +// installed. (Metro treats requires inside a try/catch as optional, so this does +// not break bundling when the package is absent.) +declare const require: (module: string) => unknown; + +try { + require('react-native-get-random-values'); +} catch { + // No-op: a `crypto.getRandomValues` polyfill may already be installed globally. +} + export const getUUID = (): string => { - const UUID = uuidv4().toString(); - return UUID; + try { + return uuidv4().toString(); + } catch { + throw new Error( + "@segment/analytics-react-native requires a 'crypto.getRandomValues' " + + "polyfill, which doesn't appear to be installed. Install " + + "'react-native-get-random-values' and import before " + + 'initializing the analytics client.' + ); + } }; diff --git a/packages/sovran/package.json b/packages/sovran/package.json index 8d1c58806..35ca36e34 100644 --- a/packages/sovran/package.json +++ b/packages/sovran/package.json @@ -74,6 +74,9 @@ "peerDependenciesMeta": { "@react-native-async-storage/async-storage": { "optional": true + }, + "react-native-get-random-values": { + "optional": true } }, "dependencies": {