fix: add generateUUID fallback for crypto.randomUUID in PackGeneratorService and document the solution.

This commit is contained in:
2025-12-14 21:45:38 +01:00
parent da643b787f
commit 0a8f78df3a
3 changed files with 28 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ The project has successfully migrated from a .NET backend to a Node.js Modular M
- **[2025-12-14] Cleanup**: Removed Tournament Mode and simplified pack display as requested. [Link](./devlog/2025-12-14-211500_remove_tournament_mode.md) - **[2025-12-14] Cleanup**: Removed Tournament Mode and simplified pack display as requested. [Link](./devlog/2025-12-14-211500_remove_tournament_mode.md)
- **[2025-12-14] UI Tweak**: Auto-configured generation mode based on source selection. [Link](./devlog/2025-12-14-212000_ui_simplification.md) - **[2025-12-14] UI Tweak**: Auto-configured generation mode based on source selection. [Link](./devlog/2025-12-14-212000_ui_simplification.md)
- **[2025-12-14] Multiplayer Game Plan**: Plan for Real Game & Online Multiplayer. [Link](./devlog/2025-12-14-212500_multiplayer_game_plan.md) - **[2025-12-14] Multiplayer Game Plan**: Plan for Real Game & Online Multiplayer. [Link](./devlog/2025-12-14-212500_multiplayer_game_plan.md)
- **[2025-12-14] Bug Fix**: Fixed `crypto.randomUUID` error for non-secure contexts. [Link](./devlog/2025-12-14-214400_fix_uuid_error.md)
## Active Modules ## Active Modules
1. **Cube Manager**: Fully functional (Parsing, Fetching, Pack Generation). 1. **Cube Manager**: Fully functional (Parsing, Fetching, Pack Generation).

View File

@@ -0,0 +1,15 @@
# Fix UUID Error in Insecure Contexts
## Problem
The user reported a `TypeError: crypto.randomUUID is not a function` when accessing the application from a public IP. This is because `crypto.randomUUID()` is part of the Web Crypto API, which is often restricted to secure contexts (HTTPS) or localhost. When accessing via `http://PUBLIC_IP:PORT`, the browser disables this API.
## Solution
We need to implement a fallback UUID generation method that works in non-secure contexts.
## Plan
1. Modify `src/client/src/services/PackGeneratorService.ts`.
2. Add a private method `generateUUID()` to the `PackGeneratorService` class (or a standalone helper function in the module) that:
* Checks if `crypto.randomUUID` is available.
* If yes, uses it.
* If no, uses a fallback algorithm (e.g., `Math.random()` based v4 UUID generation).
3. Replace the call `crypto.randomUUID()` with this new method.

View File

@@ -63,7 +63,7 @@ export class PackGeneratorService {
} }
const cardObj: DraftCard = { const cardObj: DraftCard = {
id: crypto.randomUUID(), id: this.generateUUID(),
scryfallId: cardData.id, scryfallId: cardData.id,
name: cardData.name, name: cardData.name,
rarity: rarity, rarity: rarity,
@@ -298,4 +298,15 @@ export class PackGeneratorService {
} }
return newArray; return newArray;
} }
private generateUUID(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Fallback for insecure contexts or older browsers
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
} }