From 617b29bd85beca1b596caf2bb1ec9f8c62249f31 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 24 Apr 2026 18:50:37 -0400 Subject: [PATCH] Fix crypto.randomUUID crash on non-HTTPS origins Replace all crypto.randomUUID() calls with a uuid() utility that falls back to Math.random-based generation when the Web Crypto API is unavailable (plain HTTP contexts). Co-Authored-By: Claude Opus 4.6 --- apps/web/src/pages/DataPage.tsx | 4 ++-- apps/web/src/store/index.ts | 5 +++-- packages/game-engine/src/systems/eventSystem.ts | 3 ++- packages/game-engine/src/systems/modelSystem.ts | 3 ++- packages/shared/src/utils/formatting.ts | 10 ++++++++++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/apps/web/src/pages/DataPage.tsx b/apps/web/src/pages/DataPage.tsx index a9f75de..b4add53 100644 --- a/apps/web/src/pages/DataPage.tsx +++ b/apps/web/src/pages/DataPage.tsx @@ -1,7 +1,7 @@ import { useState } from 'react'; import { Database, ShoppingCart, Zap } from 'lucide-react'; import { useGameStore } from '@/store'; -import { formatNumber, formatMoney } from '@ai-tycoon/shared'; +import { formatNumber, formatMoney, uuid } from '@ai-tycoon/shared'; import type { OwnedDataset, DataDomain } from '@ai-tycoon/shared'; interface MarketplaceDataset { @@ -49,7 +49,7 @@ export function DataPage() { const handlePurchase = (item: MarketplaceDataset) => { const dataset: OwnedDataset = { - id: crypto.randomUUID(), + id: uuid(), name: item.name, domain: item.domain, sizeTokens: item.sizeTokens, diff --git a/apps/web/src/store/index.ts b/apps/web/src/store/index.ts index 8b417d7..4508b36 100644 --- a/apps/web/src/store/index.ts +++ b/apps/web/src/store/index.ts @@ -19,6 +19,7 @@ import { GPU_CONFIGS, FUNDING_ROUNDS, OPEN_SOURCE_REPUTATION_BOOST, + uuid, } from '@ai-tycoon/shared'; import { INITIAL_RIVALS } from '@ai-tycoon/game-engine'; @@ -105,7 +106,7 @@ export const useGameStore = create()( addNotification: (n) => set((s) => ({ notifications: [ - { ...n, id: crypto.randomUUID(), read: false }, + { ...n, id: uuid(), read: false }, ...s.notifications.slice(0, 49), ], })), @@ -176,7 +177,7 @@ export const useGameStore = create()( if (s.economy.money < buildCost) return s; const dc: DataCenter = { - id: crypto.randomUUID(), + id: uuid(), name, location, gpus: [], diff --git a/packages/game-engine/src/systems/eventSystem.ts b/packages/game-engine/src/systems/eventSystem.ts index f150d3e..1050442 100644 --- a/packages/game-engine/src/systems/eventSystem.ts +++ b/packages/game-engine/src/systems/eventSystem.ts @@ -1,4 +1,5 @@ import type { GameState, EventState, ActiveEvent, EventDefinition, EventCondition } from '@ai-tycoon/shared'; +import { uuid } from '@ai-tycoon/shared'; export interface EventTickResult { events: EventState; @@ -73,7 +74,7 @@ export function processEvents( const activeEvent: ActiveEvent = { eventId: chosen.id, - instanceId: crypto.randomUUID(), + instanceId: uuid(), triggeredAtTick: tick, expiresAtTick: tick + chosen.expiryTicks, title: chosen.title, diff --git a/packages/game-engine/src/systems/modelSystem.ts b/packages/game-engine/src/systems/modelSystem.ts index bd8e2cc..ec534f0 100644 --- a/packages/game-engine/src/systems/modelSystem.ts +++ b/packages/game-engine/src/systems/modelSystem.ts @@ -1,4 +1,5 @@ import type { GameState, ModelsState, TrainedModel, ModelCapabilities } from '@ai-tycoon/shared'; +import { uuid } from '@ai-tycoon/shared'; export interface ModelTickResult { modelsState: ModelsState; @@ -79,7 +80,7 @@ function createTrainedModel( const parameterCount = Math.pow(10, generation) * (0.5 + Math.random()); return { - id: crypto.randomUUID(), + id: uuid(), name, generation, parameterCount, diff --git a/packages/shared/src/utils/formatting.ts b/packages/shared/src/utils/formatting.ts index ed89dd4..8852c93 100644 --- a/packages/shared/src/utils/formatting.ts +++ b/packages/shared/src/utils/formatting.ts @@ -24,6 +24,16 @@ export function formatFlops(n: number): string { return `${formatNumber(n)} FLOPS`; } +export function uuid(): string { + if (typeof crypto !== 'undefined' && crypto.randomUUID) { + return crypto.randomUUID(); + } + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16); + }); +} + export function formatDuration(ticks: number): string { if (ticks < 60) return `${ticks}s`; if (ticks < 3600) return `${Math.floor(ticks / 60)}m ${ticks % 60}s`;