const DEFAULT_CONTRAST_BACKEND_ORIGIN = 'https://contrast.dev' export const LOOPBACK_PROBE_HOSTNAMES = ['127.0.0.1', '::1'] as const export function normalizeHostname(hostname: string): string { return hostname.replace(/^\[|\]$/g, '').toLowerCase() } export function buildLoopbackProbeBaseUrl( protocol: 'http:' | 'ws:', hostname: string, port: number, ): string { const normalizedHostname = normalizeHostname(hostname) const urlHost = normalizedHostname.includes(':') ? `[${normalizedHostname}]` : normalizedHostname return `${protocol}//${urlHost}:${port}` } export function isLoopbackHost(hostname: string): boolean { const normalized = normalizeHostname(hostname) return ( normalized === 'localhost' || normalized.endsWith('.localhost') || normalized === '0.0.0.0' || normalized === '::1' || /^127(?:\.\d{1,3}){3}$/.test(normalized) ) } export function buildBrowserAssetProxyUrl(targetUrl: string): string { const proxyPath = isLoopbackHost(new URL(targetUrl).hostname) ? '/__bundle-proxy' : '/__fetch-proxy' return `${proxyPath}?url=${encodeURIComponent(targetUrl)}` } export function isContrastBackendHost(hostname: string): boolean { const normalized = normalizeHostname(hostname) return ( isLoopbackHost(normalized) || normalized === 'contrast.dev' || normalized.endsWith('.contrast.dev') ) } export function isContrastBackendOrigin(origin: string): boolean { try { return isContrastBackendHost(new URL(origin).hostname) } catch { return false } } export function resolveContrastBackendOrigin(currentOrigin?: string | null): string { const origin = currentOrigin?.trim() if (!origin) return DEFAULT_CONTRAST_BACKEND_ORIGIN try { const parsed = new URL(origin) if (isLoopbackHost(parsed.hostname) || isContrastBackendHost(parsed.hostname)) { return parsed.origin } } catch {} return DEFAULT_CONTRAST_BACKEND_ORIGIN } export { DEFAULT_CONTRAST_BACKEND_ORIGIN }