Files
cadSet/text-to-cad/apps/cad-agent-studio/src/viewer-cadjs/lib/pathUtils.mjs
T
2026-07-27 16:00:36 +08:00

31 lines
911 B
JavaScript

import path from "node:path";
export function toPosixPath(value) {
return String(value || "").split(path.sep).join("/");
}
export function encodePathParam(value) {
return toPosixPath(value)
.split("/")
.map((part) => encodeURIComponent(part))
.join("/");
}
function relativePathStaysInsideRoot(relativePath) {
return relativePath === "" || (
relativePath !== ".." &&
!relativePath.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relativePath)
);
}
export function pathIsInside(childPath, parentPath) {
const relativePath = path.relative(path.resolve(parentPath), path.resolve(childPath));
return Boolean(relativePath) && relativePathStaysInsideRoot(relativePath);
}
export function pathIsInsideOrEqual(childPath, parentPath) {
const relativePath = path.relative(path.resolve(parentPath), path.resolve(childPath));
return relativePathStaysInsideRoot(relativePath);
}