Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
"optional": true
},
"react-native-get-random-values": {
"optional": true
}
},
"engines": {
Expand Down
28 changes: 25 additions & 3 deletions packages/core/src/uuid.ts
Original file line number Diff line number Diff line change
@@ -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.'
);
}
};
3 changes: 3 additions & 0 deletions packages/sovran/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
"optional": true
},
"react-native-get-random-values": {
"optional": true
}
},
"dependencies": {
Expand Down