UNPKG

@paroicms/server

Version:
100 lines 3.9 kB
import { distDir as adminUiDistDir } from "@paroicms/admin-ui"; import { pathExists } from "@paroicms/internal-server-lib"; import { join } from "node:path"; import { getAssetsBaseUrl } from "../common/data-format.js"; import { render404Html } from "../common/serve-text-or.js"; import { getPluginAssetsUrl } from "../connector/plugin-loader/plugin-public-helpers.js"; import { appConf } from "../context.js"; import { HTTP_MAX_AGE } from "./http-constants.js"; import { serve404SimpleHtml, serve500Html } from "./http-helpers.js"; import { serveFile } from "./serve-file.js"; export async function renderedPageAssetsReqHandler(siteContext, httpContext) { const { req } = httpContext; let path = req.path; if (!path.startsWith("/assets/") || !["GET", "HEAD"].includes(req.method)) return false; let pathPrefix; let baseDir; const pluginAssetsPrefix = "/assets/plugin/"; if (path.startsWith(pluginAssetsPrefix)) { const sepIndex = path.indexOf("/", pluginAssetsPrefix.length); if (sepIndex === -1) { await render404Asset(siteContext, httpContext); return true; } const pluginPublicDirName = path.substring(pluginAssetsPrefix.length, sepIndex); const plugin = siteContext.pluginsBySlug.get(pluginPublicDirName); if (!plugin || !plugin.publicAssetsDir) { await render404Asset(siteContext, httpContext); return true; } baseDir = plugin.publicAssetsDir; pathPrefix = `${getPluginAssetsUrl(plugin)}/`; } else { pathPrefix = `${getAssetsBaseUrl(siteContext)}/`; baseDir = join(siteContext.themeDir, "assets"); } if (!path.startsWith(pathPrefix)) { await render404Asset(siteContext, httpContext); return true; } path = path.substring(pathPrefix.length); await serveFile(httpContext, path, { root: baseDir, immutable: appConf.immutableAssets, maxAge: appConf.immutableAssets ? HTTP_MAX_AGE : 0, }); return true; } export async function adminUiFilesReqHandler(_siteContext, httpContext) { const { req } = httpContext; let path = req.path; if (!["GET", "HEAD"].includes(req.method) || path.startsWith("/adm/api/v1") || (path !== "/adm" && !path.startsWith("/adm/"))) { return false; } path = path.substring("/adm/".length); if (path === "") path = "index.html"; if (!(await pathExists(join(adminUiDistDir, path)))) { path = "index.html"; } await serveFile(httpContext, path, { root: adminUiDistDir, on404: () => serve404SimpleHtml(httpContext), }); return true; } export async function adminUiPluginsReqHandler(siteContext, httpContext) { const { req } = httpContext; const prefix = "/adm/plugins/"; if (!["GET", "HEAD"].includes(req.method) || !req.path.startsWith(prefix)) return false; const relPathIndex = req.path.indexOf("/", prefix.length); if (relPathIndex === -1) { serve404SimpleHtml(httpContext); return true; } const urlPath = req.path.substring(prefix.length, relPathIndex); const relPath = req.path.substring(relPathIndex + 1); const plugin = siteContext.pluginsBySlug.get(urlPath); const adminUiAssetsDir = plugin?.adminUiAssetsDir; if (!adminUiAssetsDir || !(await pathExists(join(adminUiAssetsDir, relPath)))) { serve404SimpleHtml(httpContext); return true; } await serveFile(httpContext, relPath, { root: adminUiAssetsDir }); return true; } async function render404Asset(siteContext, httpContext) { try { await render404Html(siteContext, httpContext); } catch (error) { siteContext.logger.error("failed to render 404", error); serve500Html(httpContext); } } //# sourceMappingURL=static-files.req-handler.js.map