Files
buzz/web/src/shared/lib/relay-url.ts
2026-05-05 19:20:13 +00:00

25 lines
844 B
TypeScript

/** Convert a WebSocket relay URL to its HTTP equivalent. */
export function relayHttpUrl(wsUrl: string): string {
if (wsUrl.startsWith("wss://")) {
return `https://${wsUrl.slice(6)}`;
}
if (wsUrl.startsWith("ws://")) {
return `http://${wsUrl.slice(5)}`;
}
return wsUrl;
}
/** Read the relay WebSocket URL from environment or derive from window.location. */
export function relayWsUrl(): string {
const envUrl = import.meta.env.VITE_RELAY_URL;
if (envUrl) return envUrl;
// Same-origin: derive from current page location (works when served from relay)
const proto = window.location.protocol === "https:" ? "wss:" : "ws:";
return `${proto}//${window.location.host}`;
}
/** HTTP base URL for the relay (derived from the WS URL). */
export function relayHttpBaseUrl(): string {
return relayHttpUrl(relayWsUrl());
}