// The address the browser used to reach broke, for redirects and callback links. // request.url carries the server's own listen address instead (http://0.0.0.0:3000 in Docker, // http://localhost:3000 behind a proxy), so the Host header the browser or proxy sent wins. const HOST = /^[a-z0-9.-]+(:\d+)?$|^\[[0-9a-f:]+\](:\d+)?$/i; export function originOf(request: Request) { const own = new URL(request.url); const h = request.headers; const host = (h.get("x-forwarded-host") ?? h.get("host") ?? "").split(",")[0].trim(); if (!HOST.test(host)) return own.origin; const proto = (h.get("x-forwarded-proto") ?? "").split(",")[0].trim(); return `${proto === "https" || proto === "http" ? proto : own.protocol.slice(0, -1)}://${host}`; }