Files
Live_RC/src/backend_url.js
T

15 lines
818 B
JavaScript

// The saved backend URL is shared by every browser. "localhost" saved on the race laptop
// means the laptop itself, so a phone or another computer (page opened on the laptop's IP)
// uses the address it opened the page on instead.
const LOOPBACK_HOST = /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|::1)$/i;
export function resolveBackendUrl(saved, { protocol = "", hostname = "", origin = "" } = {}) {
const pageUrl = protocol.startsWith("http") && hostname ? origin : "http://127.0.0.1:8081";
const value = String(saved || "").trim().replace(/\/+$/, "");
if (!value) return pageUrl;
let savedHost = "";
try { savedHost = new URL(value).hostname; } catch { return pageUrl; }
if (LOOPBACK_HOST.test(savedHost) && !LOOPBACK_HOST.test(hostname) && protocol.startsWith("http")) return pageUrl;
return value;
}