fix: UUID fallback for non-secure HTTP contexts

crypto.randomUUID() is unavailable outside secure contexts (plain HTTP).
Add generateUUID() helper with manual UUID v4 fallback (RFC 4122).
This commit is contained in:
Woody 2026-05-18 14:47:07 +08:00
parent 821159a198
commit 0445fdba19
2 changed files with 21 additions and 2 deletions

18
frontend/src/lib/uuid.ts Normal file
View File

@ -0,0 +1,18 @@
/**
* Generate a UUID v4 string.
*
* Uses crypto.randomUUID() when available (secure contexts / HTTPS / localhost).
* Falls back to a manual UUID v4 generator for non-secure contexts (plain HTTP).
*/
export function generateUUID(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID()
}
// Manual UUID v4 fallback (RFC 4122)
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)
})
}

View File

@ -7,6 +7,7 @@ import { useSystemAudioASR } from '../hooks/useSystemAudioASR'
import { useMicASR } from '../hooks/useMicASR'
import { useFullTranscript } from '../hooks/useFullTranscript'
import { getVideoUrl } from '../lib/api'
import { generateUUID } from '../lib/uuid'
import { QueryInput } from '../components/QueryInput'
import { ExtractedQuestionsDisplay } from '../components/ExtractedQuestionsDisplay'
import { ResponsePanel } from '../components/ResponsePanel'
@ -39,12 +40,12 @@ export const LTTPage: React.FC = () => {
const systemAudioWsUrl = useMemo(() => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const host = import.meta.env.VITE_WS_HOST ?? window.location.host
return `${protocol}//${host}/ws/asr/${crypto.randomUUID()}?language=yue&source=system-audio`
return `${protocol}//${host}/ws/asr/${generateUUID()}?language=yue&source=system-audio`
}, [])
const micWsUrl = useMemo(() => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const host = import.meta.env.VITE_WS_HOST ?? window.location.host
return `${protocol}//${host}/ws/asr/${crypto.randomUUID()}?language=yue&source=mic`
return `${protocol}//${host}/ws/asr/${generateUUID()}?language=yue&source=mic`
}, [])
const systemAudioASR = useSystemAudioASR({ wsUrl: systemAudioWsUrl })