// Server-URL validation mirroring android/.../data/transport/ServerUrl.kt and // internal/client/config.go: only ws/wss with a non-empty hostname and no // userinfo/fragment. The endpoint path defaults to /connect, matching // internal/server/websocket.go `GET /connect`. export const ServerUrlKind = Object.freeze({ EMPTY: "EMPTY", MALFORMED: "MALFORMED", UNSUPPORTED_SCHEME: "UNSUPPORTED_SCHEME", MISSING_HOST: "MISSING_HOST", USER_INFO: "USER_INFO", FRAGMENT: "FRAGMENT", }); /** * @returns {{valid:true, wsUrl:string, httpUrl:string, host:string, port:number, secure:boolean} * |{valid:false, kind:string, message:string}} */ export function validateServerUrl(raw) { const trimmed = (raw ?? "").trim(); if (!trimmed) { return { valid: false, kind: ServerUrlKind.EMPTY, message: "Enter the Pipelines server URL, for example ws://192.168.1.20:8081/connect", }; } let url; try { url = new URL(trimmed); } catch { return { valid: false, kind: ServerUrlKind.MALFORMED, message: "That is not a valid URL.", }; } const scheme = url.protocol.replace(/:$/, "").toLowerCase(); if (scheme !== "ws" && scheme !== "wss") { return { valid: false, kind: ServerUrlKind.UNSUPPORTED_SCHEME, message: `Unsupported scheme "${scheme}"; use ws:// (trusted LAN/VPN) or wss://`, }; } if (url.username || url.password) { return { valid: false, kind: ServerUrlKind.USER_INFO, message: "Credentials are not supported; the server has no authentication. Remove userinfo from the URL", }; } if (url.hash) { return { valid: false, kind: ServerUrlKind.FRAGMENT, message: "Remove the # fragment; it is not part of the server endpoint", }; } if (!url.hostname) { return { valid: false, kind: ServerUrlKind.MISSING_HOST, message: "The URL is missing a host, for example ws://192.168.1.20:8081/connect", }; } const secure = scheme === "wss"; const port = url.port ? Number(url.port) : secure ? 443 : 80; let path = url.pathname || ""; if (!path || path === "/") path = "/connect"; const authority = url.host; // host + explicit port only when present const search = url.search || ""; const wsUrl = `${scheme}://${authority}${path}${search}`; const httpUrl = `${secure ? "https" : "http"}://${authority}${path}${search}`; return { valid: true, wsUrl, httpUrl, host: url.hostname, port, secure }; } /** * Resolves a server-relative artifact url (for example "/artifacts/55/4/shot.png") * against the configured server URL by mapping ws/wss to http/https and dropping * the /connect path. Never hardcodes a host: a blank or invalid configuration, or * a blank path, yields null so the UI can disable actions. An already-absolute * http(s) url is returned unchanged. Mirrors ServerUrl.resolveArtifactUrl. */ export function resolveArtifactUrl(configured, artifactUrl) { const relative = (artifactUrl ?? "").trim(); if (!relative) return null; if (relative.startsWith("http://") || relative.startsWith("https://")) { return relative; } const validated = validateServerUrl(configured); if (!validated.valid) return null; const { secure, host, port } = validated; const defaultPort = secure ? 443 : 80; const authority = port && port !== defaultPort ? `${host}:${port}` : host; const origin = `${secure ? "https" : "http"}://${authority}`; const path = relative.startsWith("/") ? relative : `/${relative}`; return origin + path; }