26 lines
691 B
JavaScript
26 lines
691 B
JavaScript
export class PathUtils {
|
|
static normalizeRelative(path) {
|
|
if (!path || path.startsWith("/") || path.includes("\\")) {
|
|
throw new Error("Invalid path format");
|
|
}
|
|
const parts = path.split("/");
|
|
const out = [];
|
|
for (const part of parts) {
|
|
if (!part || part === ".") continue;
|
|
if (part === "..") throw new Error("Path traversal is forbidden");
|
|
out.push(part);
|
|
}
|
|
return out.join("/");
|
|
}
|
|
|
|
static dirname(path) {
|
|
const index = path.lastIndexOf("/");
|
|
return index === -1 ? "" : path.slice(0, index);
|
|
}
|
|
|
|
static basename(path) {
|
|
const index = path.lastIndexOf("/");
|
|
return index === -1 ? path : path.slice(index + 1);
|
|
}
|
|
}
|