@paroicms/server
Version:
The ParoiCMS server
44 lines • 1.64 kB
JavaScript
import { pathExists } from "@paroicms/internal-server-lib";
import { join } from "node:path";
import { serveFile } from "./serve-file.js";
const staticFilesDirName = "static-files";
export async function staticFilesReqHandler(siteContext, httpContext) {
const { req } = httpContext;
if (!["GET", "HEAD"].includes(req.method))
return false;
let requestedPath = req.path;
if (requestedPath.startsWith("/")) {
requestedPath = requestedPath.substring(1);
}
if (requestedPath === "")
return false;
const staticFilesDir = join(siteContext.siteDir, staticFilesDirName);
const staticFilePath = join(staticFilesDir, requestedPath);
if (!(await pathExists(staticFilePath)))
return false;
await serveFile(httpContext, requestedPath, {
root: staticFilesDir,
immutable: false,
maxAge: 0,
});
return true;
}
export async function unprotectedStaticFilesReqHandler(siteContext, httpContext) {
const { req } = httpContext;
if (!["GET", "HEAD"].includes(req.method))
return false;
if (req.path !== "/favicon.ico" && req.path !== "/robots.txt")
return false;
const requestedPath = req.path.substring(1);
const staticFilesDir = join(siteContext.siteDir, staticFilesDirName);
const staticFilePath = join(staticFilesDir, requestedPath);
if (!(await pathExists(staticFilePath)))
return false;
await serveFile(httpContext, requestedPath, {
root: staticFilesDir,
immutable: false,
maxAge: 0,
});
return true;
}
//# sourceMappingURL=static-site-files.req-handler.js.map