diff --git a/src/server.ts b/src/server.ts index ea500db..678c31a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,7 +1,7 @@ -import { readFile, readdir } from "node:fs/promises"; +import { readFile, readdir, stat } from "node:fs/promises"; import { join, parse } from "node:path"; import { Hono } from "hono"; -import { serveStatic } from "hono/serve-static"; +import { serveStatic } from "hono/middleware/serve-static"; const app = new Hono(); @@ -10,6 +10,27 @@ const templatePath = join("public", "index.html"); const cardPlaceholder = ""; const importPlaceholder = "/* FONT_IMPORT */"; +const serveStaticFromFs = (options: { root: string }) => + serveStatic({ + root: options.root, + getContent: async (path) => { + try { + return await readFile(path); + } catch (error) { + return null; + } + }, + isDir: async (path) => { + try { + const stats = await stat(path); + return stats.isDirectory(); + } catch (error) { + return undefined; + } + }, + join, + }); + const escapeHtml = (value: string) => value .replaceAll("&", "&") @@ -100,8 +121,8 @@ const indexHtml = templateHtml app.get("/", (c) => c.html(indexHtml)); -app.use("/fonts/*", serveStatic({ root: "." })); -app.use("/*", serveStatic({ root: "./public" })); +app.use("/fonts/*", serveStaticFromFs({ root: "." })); +app.use("/*", serveStaticFromFs({ root: "./public" })); const cssRoutes = async () => { const entries = await readdir(cssDir, { withFileTypes: true });