import path from "node:path"; import { normalizeWorkspacePath } from "./computer-support.js"; export const PROJECT_WORKSPACE_PREFIX = "project"; export function isProjectWorkspacePath(storedPath: string): boolean { const normalized = normalizeWorkspacePath(storedPath); return ( normalized === PROJECT_WORKSPACE_PREFIX || normalized.startsWith(`${PROJECT_WORKSPACE_PREFIX}/`) ); } export function projectWorkspaceRelativePath(storedPath: string): string { const normalized = normalizeWorkspacePath(storedPath); if (normalized === PROJECT_WORKSPACE_PREFIX) return ""; if (!normalized.startsWith(`${PROJECT_WORKSPACE_PREFIX}/`)) { throw new Error("Path is not under the project workspace"); } return normalized.slice(`${PROJECT_WORKSPACE_PREFIX}/`.length); } export function resolveProjectHostPath(workspaceRoot: string, storedPath: string): string { const relative = projectWorkspaceRelativePath(storedPath); const resolvedRoot = path.resolve(workspaceRoot); const candidate = relative ? path.resolve(resolvedRoot, relative) : resolvedRoot; const relativeToRoot = path.relative(resolvedRoot, candidate); if (relativeToRoot.startsWith("..") || path.isAbsolute(relativeToRoot)) { throw new Error("Path escapes the project workspace"); } return candidate; }