From d80078aa58f4ad39a3ea037f946adf8751eedac1 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Thu, 16 Jan 2025 12:51:01 +0100 Subject: [PATCH] chore(1-3230): use homebrew version of uuid generation The one from the uuid library relies on an underlying crypto library which doesn't exist at least in certain GitHub runners and may also cause issues for react native applications. This impl sidesteps that issue. --- src/index.ts | 2 +- src/uuidv4.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/uuidv4.ts diff --git a/src/index.ts b/src/index.ts index 748f00e..eff6f41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ import { TinyEmitter } from 'tiny-emitter'; -import { v4 as uuidv4 } from 'uuid'; import Metrics from './metrics'; import type IStorageProvider from './storage-provider'; import InMemoryStorageProvider from './storage-provider-inmemory'; @@ -11,6 +10,7 @@ import { urlWithContextAsQuery, } from './util'; import { sdkVersion } from './version'; +import { uuidv4 } from './uuidv4'; const DEFINED_FIELDS = [ 'userId', diff --git a/src/uuidv4.ts b/src/uuidv4.ts new file mode 100644 index 0000000..7929359 --- /dev/null +++ b/src/uuidv4.ts @@ -0,0 +1,14 @@ +/** + * This function generates a UUID using Math.random(). + * The distribution of unique values is not guaranteed to be as robust + * as with a crypto module but works across all platforms (Node, React Native, browser JS). + * + * We use it for connection id generation which is not critical for security. + */ +export const uuidv4 = (): string => { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + const v = c === 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +};