fix: resolve WebSocket failure and crypto.randomUUID crash on HTTP/LXC
- Replace crypto.randomUUID() with a polyfill (generateUUID) that falls back to crypto.getRandomValues or Math.random — fixes crash on HTTP non-secure contexts where randomUUID is unavailable - Fix WebSocket URL hardcoding port 8000 — use window.location.host so connections go through Nginx proxy in Docker/LXC instead of bypassing it - Add /api/v1/status/ws/ location block in nginx.conf with WebSocket upgrade headers (must precede /api/ to avoid missing Upgrade header)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { generateUUID } from '../uuid'
|
||||
|
||||
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
|
||||
describe('generateUUID', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('returns a valid v4 UUID using crypto.randomUUID when available', () => {
|
||||
const id = generateUUID()
|
||||
expect(id).toMatch(UUID_REGEX)
|
||||
})
|
||||
|
||||
it('returns a valid v4 UUID using crypto.getRandomValues fallback', () => {
|
||||
vi.spyOn(crypto, 'randomUUID' as never).mockImplementation(undefined as never)
|
||||
const id = generateUUID()
|
||||
expect(id).toMatch(UUID_REGEX)
|
||||
})
|
||||
|
||||
it('generates unique IDs', () => {
|
||||
const ids = new Set(Array.from({ length: 100 }, () => generateUUID()))
|
||||
expect(ids.size).toBe(100)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Generates a UUID v4.
|
||||
* Falls back to a manual implementation when crypto.randomUUID is unavailable
|
||||
* (HTTP non-secure contexts, older browsers).
|
||||
*/
|
||||
export function generateUUID(): string {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||
return crypto.randomUUID()
|
||||
}
|
||||
// Fallback: RFC 4122 v4 UUID using crypto.getRandomValues if available
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {
|
||||
const bytes = new Uint8Array(16)
|
||||
crypto.getRandomValues(bytes)
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80
|
||||
return [...bytes]
|
||||
.map((b, i) => ([4, 6, 8, 10].includes(i) ? '-' : '') + b.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
}
|
||||
// Last resort: Math.random based (not cryptographically secure)
|
||||
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user