-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic-web-root.mjs
More file actions
103 lines (96 loc) · 3.34 KB
/
Copy pathstatic-web-root.mjs
File metadata and controls
103 lines (96 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import fs from "node:fs/promises";
import path from "node:path";
export const LOCAL_WEB_ROOT_ENV = "GAMEFOUNDRY_LOCAL_WEB_ROOT";
export const WWW_LOCAL_WEB_ROOT = "www";
export const REPO_ROOT_LOCAL_WEB_ROOT = ".";
export const DEFAULT_LOCAL_WEB_ROOT = WWW_LOCAL_WEB_ROOT;
export function isInsideRoot(rootPath, absolutePath) {
const relativePath = path.relative(rootPath, absolutePath);
return relativePath === "" || (!relativePath.startsWith("..") && !path.isAbsolute(relativePath));
}
function normalizeWebRootValue(value) {
const normalizedValue = String(value || "").trim();
if (!normalizedValue) {
return DEFAULT_LOCAL_WEB_ROOT;
}
if (normalizedValue === "." || normalizedValue === "root" || normalizedValue === "repo-root") {
return REPO_ROOT_LOCAL_WEB_ROOT;
}
return normalizedValue.replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "") || DEFAULT_LOCAL_WEB_ROOT;
}
export function resolveLocalWebRoot({
env = process.env,
repoRoot,
webRoot,
} = {}) {
if (!repoRoot) {
throw new Error("resolveLocalWebRoot requires repoRoot.");
}
const resolvedRepoRoot = path.resolve(repoRoot);
const configuredValue = normalizeWebRootValue(webRoot ?? env?.[LOCAL_WEB_ROOT_ENV]);
const absolutePath = path.resolve(resolvedRepoRoot, configuredValue);
if (!isInsideRoot(resolvedRepoRoot, absolutePath)) {
throw new Error(`${LOCAL_WEB_ROOT_ENV} must resolve inside the repository root.`);
}
return Object.freeze({
absolutePath,
envName: LOCAL_WEB_ROOT_ENV,
relativePath: configuredValue,
source: webRoot === undefined && env?.[LOCAL_WEB_ROOT_ENV] === undefined ? "default" : "configured",
});
}
export function resolveBrowserRoutePath(decodedPath) {
const normalizedPath = path.normalize(decodedPath || "/").replace(/^(\.\.[/\\])+/, "");
const webPath = normalizedPath.replace(/\\/g, "/");
if (webPath === "/") {
return "/index.html";
}
if (webPath === "/tools" || webPath.startsWith("/tools/")) {
return `/toolbox${webPath.slice("/tools".length)}`;
}
if (webPath === "/admin/admin-notes.html") {
return "/src/dev-runtime/admin/notes.html";
}
return webPath;
}
function staticRouteCandidateRoots({ repoRoot, webRootConfig }) {
const roots = [webRootConfig.absolutePath];
if (webRootConfig.absolutePath !== repoRoot) {
roots.push(repoRoot);
}
return roots;
}
export async function resolveStaticRouteTarget({
decodedPath,
fsStat = fs.stat,
repoRoot,
webRoot,
} = {}) {
const resolvedRepoRoot = path.resolve(repoRoot);
const webRootConfig = resolveLocalWebRoot({ repoRoot: resolvedRepoRoot, webRoot });
const routePath = resolveBrowserRoutePath(decodedPath);
for (const rootPath of staticRouteCandidateRoots({ repoRoot: resolvedRepoRoot, webRootConfig })) {
const absolutePath = path.resolve(rootPath, `.${routePath}`);
if (!isInsideRoot(rootPath, absolutePath)) {
continue;
}
let targetPath = absolutePath;
let stat = await fsStat(targetPath).catch(() => null);
if (stat && stat.isDirectory()) {
targetPath = path.join(targetPath, "index.html");
stat = await fsStat(targetPath).catch(() => null);
}
if (stat && stat.isFile()) {
return Object.freeze({
routePath,
targetPath,
webRoot: webRootConfig,
});
}
}
return Object.freeze({
routePath,
targetPath: "",
webRoot: webRootConfig,
});
}