fix(web): the app opens over plain HTTP on a home network instead of showing a blank page

Browsers offer crypto.randomUUID only on HTTPS pages and localhost. The
player's client id called it at startup, so opening Minstrel at
http://<host>:4533, as the README's quick start says to, threw before
anything rendered. The id now falls back to crypto.getRandomValues,
which every page has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 10:01:35 -05:00
co-authored by Claude Opus 5.5
parent 8530b838b0
commit 6dda9ef219
2 changed files with 37 additions and 5 deletions
+21 -4
View File
@@ -1,13 +1,18 @@
import { afterEach, beforeEach, describe, expect, test } from 'vitest'; import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { getOrCreateClientId } from './clientId'; import { getOrCreateClientId, randomUUID } from './clientId';
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
beforeEach(() => sessionStorage.clear()); beforeEach(() => sessionStorage.clear());
afterEach(() => sessionStorage.clear()); afterEach(() => {
sessionStorage.clear();
vi.unstubAllGlobals();
});
describe('getOrCreateClientId', () => { describe('getOrCreateClientId', () => {
test('generates a UUID on first call and persists it in sessionStorage', () => { test('generates a UUID on first call and persists it in sessionStorage', () => {
const id = getOrCreateClientId(); const id = getOrCreateClientId();
expect(id).toMatch(/^[0-9a-f-]{36}$/); expect(id).toMatch(UUID_V4);
expect(sessionStorage.getItem('minstrel.client_id')).toBe(id); expect(sessionStorage.getItem('minstrel.client_id')).toBe(id);
}); });
@@ -17,3 +22,15 @@ describe('getOrCreateClientId', () => {
expect(a).toBe(b); expect(a).toBe(b);
}); });
}); });
describe('randomUUID', () => {
test('still makes an id on a plain-HTTP page, where crypto.randomUUID is missing', () => {
const real = globalThis.crypto;
vi.stubGlobal('crypto', { getRandomValues: real.getRandomValues.bind(real) });
const a = randomUUID();
const b = randomUUID();
expect(a).toMatch(UUID_V4);
expect(b).toMatch(UUID_V4);
expect(a).not.toBe(b);
});
});
+16 -1
View File
@@ -7,7 +7,22 @@ export function getOrCreateClientId(): string {
} catch { } catch {
// sessionStorage unavailable (non-browser env). Generate ephemerally. // sessionStorage unavailable (non-browser env). Generate ephemerally.
} }
const id = crypto.randomUUID(); const id = randomUUID();
try { sessionStorage.setItem(KEY, id); } catch { /* ignore */ } try { sessionStorage.setItem(KEY, id); } catch { /* ignore */ }
return id; return id;
} }
/**
* A version 4 UUID. Browsers offer crypto.randomUUID only on HTTPS pages and
* localhost, and Minstrel is often opened over plain HTTP on a home network
* (http://minstrel.lan:4533), so this falls back to crypto.getRandomValues,
* which every page has.
*/
export function randomUUID(): string {
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
const b = crypto.getRandomValues(new Uint8Array(16));
b[6] = (b[6] & 0x0f) | 0x40; // version 4
b[8] = (b[8] & 0x3f) | 0x80; // RFC 4122 variant
const hex = Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}