/** * Derive the live WebSocket path from `NEXT_PUBLIC_LIVE_WS_PUBLIC_URL`. * * Only `ws://` or `wss://` URLs are accepted (mirrors the scheme guard in * `getLivePublicUrl()`). The pathname is extracted and used as the WS upgrade * path; if the URL has no pathname (or is `/`), falls back to `/live-ws`. * * Used by: * - `src/app/api/v1/ws/route.ts` — handshake response `path` field * - `src/hooks/useLiveDashboard.ts` — build-time path constant + runtime discovery * * No env var is introduced — this reads the existing `NEXT_PUBLIC_LIVE_WS_PUBLIC_URL`. */ export function deriveLiveWsPath(publicUrl?: string): string { if (!publicUrl) return "/live-ws"; if (!publicUrl.startsWith("ws://") && !publicUrl.startsWith("wss://")) return "/live-ws"; try { const parsed = new URL(publicUrl); const pathname = parsed.pathname; return pathname && pathname !== "/" ? pathname : "/live-ws"; } catch { return "/live-ws"; } } /** Convenience: read the env var at call time and derive the path. */ export function getLiveWsPath(): string { return deriveLiveWsPath(process.env.NEXT_PUBLIC_LIVE_WS_PUBLIC_URL); }