Files
overlord/Overlord-Server/src/server/routes/assets-routes.ts
T

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-03-05 00:04:26 -06:00
import path from "path";
2026-03-25 09:50:41 -05:00
import { getConfig } from "../../config";
2026-03-05 00:04:26 -06:00
type AssetsRouteDeps = {
PUBLIC_ROOT: string;
secureHeaders: (contentType?: string) => Record<string, string>;
mimeType: (path: string) => string;
};
export async function handleAssetsRoutes(
req: Request,
url: URL,
deps: AssetsRouteDeps,
): Promise<Response | null> {
2026-03-25 09:50:41 -05:00
if (req.method === "GET" && url.pathname === "/assets/custom.css") {
const css = getConfig().appearance?.customCSS || "";
return new Response(css, {
headers: {
"Content-Type": "text/css; charset=utf-8",
"Cache-Control": "no-cache",
},
});
}
2026-03-05 15:25:52 -06:00
if (req.method === "GET" && url.pathname === "/favicon.ico") {
const file = Bun.file(path.join(deps.PUBLIC_ROOT, "assets", "favicon.ico"));
if (await file.exists()) {
return new Response(file, { headers: deps.secureHeaders("image/x-icon") });
}
return new Response("Not found", { status: 404 });
}
2026-03-05 00:04:26 -06:00
if (!(req.method === "GET" && url.pathname.startsWith("/assets/"))) {
return null;
}
let decodedPath = url.pathname;
try {
decodedPath = decodeURIComponent(url.pathname);
} catch {
return new Response("Bad request", { status: 400 });
}
if (decodedPath.includes("\u0000")) {
return new Response("Not found", { status: 404 });
}
const assetsRoot = path.join(deps.PUBLIC_ROOT, "assets");
const relativePath = decodedPath.replace(/^\/assets\//, "");
const resolvedPath = path.resolve(assetsRoot, relativePath);
const rootWithSep = assetsRoot.endsWith(path.sep) ? assetsRoot : `${assetsRoot}${path.sep}`;
if (!resolvedPath.startsWith(rootWithSep)) {
return new Response("Not found", { status: 404 });
}
const file = Bun.file(resolvedPath);
if (await file.exists()) {
2026-03-22 00:36:02 -05:00
const headers: Record<string, string> = { ...deps.secureHeaders(deps.mimeType(url.pathname)) };
if (relativePath === "notification-sw.js") {
headers["Service-Worker-Allowed"] = "/";
}
return new Response(file, { headers });
2026-03-05 00:04:26 -06:00
}
return new Response("Not found", { status: 404 });
}