ifcurl/forgejo/custom/public/assets/viewer-url.js
Bruno Postle d98d4c35d5 viewer: extract URL logic to viewer-url.js, add JS tests
parseIfcUrl, buildIfcUrl, toRawUrl extracted from viewer.html into
viewer-url.js so they can be imported and tested independently.
viewer.html imports from the module. 32 Node test-runner tests in
tests/test_viewer_url.mjs cover all three functions across GitHub,
GitLab, Forgejo/Gitea, SSH user@ prefix, and round-trip cases.

Caught a bug: + (selector union operator) was being URL-encoded as
%2B in buildIfcUrl; fixed by adding .replace(/%2B/g, "+") alongside
the existing comma fix, in both viewer-url.js and syncCameraUrl.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 07:31:19 +01:00

78 lines
3.1 KiB
JavaScript

// Pure ifc:// URL logic shared between viewer.html and tests.
export function parseIfcUrl(raw) {
if (!raw?.startsWith("ifc://")) return null;
const body = raw.slice("ifc://".length);
const slashIdx = body.indexOf("/");
const authority = slashIdx < 0 ? body : body.slice(0, slashIdx);
const pathAndQuery = slashIdx < 0 ? "" : body.slice(slashIdx + 1);
const atInAuth = authority.indexOf("@");
const userAt = atInAuth < 0 ? "" : authority.slice(0, atInAuth + 1);
const host = atInAuth < 0 ? authority : authority.slice(atInAuth + 1);
const qIdx = pathAndQuery.indexOf("?");
const pathPart = qIdx < 0 ? pathAndQuery : pathAndQuery.slice(0, qIdx);
const qs = new URLSearchParams(qIdx < 0 ? "" : pathAndQuery.slice(qIdx + 1));
const atIdx = pathPart.lastIndexOf("@");
const repoSuffix = atIdx < 0 ? pathPart : pathPart.slice(0, atIdx);
const ref = atIdx < 0 ? "" : pathPart.slice(atIdx + 1);
return {
repo: userAt + host + "/" + repoSuffix,
host, userAt, repoSuffix, ref,
path: qs.get("path") ?? "",
selector: qs.get("selector") ?? "",
camera: qs.get("camera") ?? "",
fov: qs.get("fov") ?? "",
scale: qs.get("scale") ?? "",
};
}
export function buildIfcUrl(repo, ref, path, selector) {
if (!repo || !ref) return null;
const qs = new URLSearchParams();
if (path) qs.set("path", path);
if (selector) qs.set("selector", selector);
const qsStr = qs.toString().replace(/%2C/g, ",").replace(/%2B/g, "+");
return `ifc://${repo}@${ref}${qsStr ? "?" + qsStr : ""}`;
}
export function toRawUrl(raw) {
const body = raw.slice("ifc://".length);
const slashIdx = body.indexOf("/");
const authority = slashIdx < 0 ? body : body.slice(0, slashIdx);
const pathAndQuery = slashIdx < 0 ? "" : body.slice(slashIdx + 1);
const atInAuth = authority.indexOf("@");
const host = atInAuth < 0 ? authority : authority.slice(atInAuth + 1);
const qIdx = pathAndQuery.indexOf("?");
const pathPart = qIdx < 0 ? pathAndQuery : pathAndQuery.slice(0, qIdx);
const qs = new URLSearchParams(qIdx < 0 ? "" : pathAndQuery.slice(qIdx + 1));
const filePath = qs.get("path") ?? "";
const atIdx = pathPart.lastIndexOf("@");
const repoPath = atIdx < 0 ? pathPart : pathPart.slice(0, atIdx);
const ref = atIdx < 0 ? "" : pathPart.slice(atIdx + 1);
const plainRef = ref.startsWith("heads/") ? ref.slice(6)
: ref.startsWith("tags/") ? ref.slice(5)
: ref;
if (host === "github.com" || host.endsWith(".github.com")) {
return `https://raw.githubusercontent.com/${repoPath}/${plainRef}/${filePath}`;
}
if (host === "gitlab.com" || host.includes("gitlab")) {
const proto = host.startsWith("localhost") ? "http" : "https";
return `${proto}://${host}/${repoPath}/-/raw/${plainRef}/${filePath}`;
}
const refSeg = ref.startsWith("heads/") ? "branch/" + ref.slice(6)
: ref.startsWith("tags/") ? "tag/" + ref.slice(5)
: "commit/" + ref;
const proto = host.startsWith("localhost") ? "http" : "https";
return `${proto}://${host}/${repoPath}/raw/${refSeg}/${filePath}`;
}