mirror of
https://github.com/block/buzz.git
synced 2026-11-12 12:22:06 +00:00
25 lines
844 B
TypeScript
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());
|
|
}
|