diff --git a/pkgs/core/server/create.ts b/pkgs/core/server/create.ts index 56661a0f..34a9aa02 100644 --- a/pkgs/core/server/create.ts +++ b/pkgs/core/server/create.ts @@ -4,8 +4,7 @@ import { wsHandler } from "../../../app/srv/ws/handler"; import { dir } from "../utils/dir"; import { g } from "../utils/global"; import { serveAPI } from "./serve-api"; -import brotliPromise from "brotli-wasm"; -const brotli = await brotliPromise; +import { lookup } from "mime-types"; export const cache = { static: {} as Record< @@ -89,36 +88,21 @@ export const createServer = async () => { try { const found = cache.static[url.pathname]; if (found && g.mode === "prod") { - const res = new Response(found.content); - res.headers.set("Content-Type", found.type); + return responseCached(req, found); } const file = Bun.file(dir.path(`${webPath}${url.pathname}`)); if ((await file.exists()) && file.type !== "application/octet-stream") { if (!cache.static[url.pathname]) { cache.static[url.pathname] = { - type: file.type, + type: lookup(url.pathname) || "text/plain", content: await file.arrayBuffer(), }; } const found = cache.static[url.pathname]; - const enc = req.headers.get("accept-encoding"); - if (enc && g.mode === "prod") { - if (enc.includes("br") && found.br) { - const res = new Response(found.br); - res.headers.set("Content-Encoding", "br"); - return res; - } else if (enc.includes("gz")) { - if (!found.gz) { - found.gz = gzipSync(new Uint8Array(found.content)); - } - const res = new Response(found.gz); - res.headers.set("Content-Encoding", "gzip"); - return res; - } + if (found) { + return responseCached(req, found); } - - return new Response(found.content); } } catch (e) { g.log.error(e); @@ -139,3 +123,27 @@ export const createServer = async () => { g.log.info(`Started at port: ${g.server.port}`); } }; + +const responseCached = (req: Request, found: (typeof cache.static)[string]) => { + const enc = req.headers.get("accept-encoding"); + if (enc && g.mode === "prod") { + if (enc.includes("br") && found.br) { + const res = new Response(found.br); + res.headers.set("content-type", found.type); + res.headers.set("content-encoding", "br"); + return res; + } else if (enc.includes("gz")) { + if (!found.gz) { + found.gz = gzipSync(new Uint8Array(found.content)); + } + const res = new Response(found.gz); + res.headers.set("content-type", found.type); + res.headers.set("content-encoding", "gzip"); + return res; + } + } + + const res = new Response(found.content); + res.headers.set("content-type", found.type); + return res; +}; diff --git a/pkgs/core/utils/parcel.ts b/pkgs/core/utils/parcel.ts index 76f4d9ea..4269d293 100644 --- a/pkgs/core/utils/parcel.ts +++ b/pkgs/core/utils/parcel.ts @@ -1,13 +1,10 @@ import { spawn } from "bun"; -import { - dirAsync, - inspectTreeAsync, - writeAsync -} from "fs-jetpack"; +import { dirAsync, inspectTreeAsync, writeAsync } from "fs-jetpack"; import { InspectTreeResult } from "fs-jetpack/types"; import { cache } from "../server/create"; import { dir } from "./dir"; import { g } from "./global"; +import { lookup } from "mime-types"; import brotliPromise from "brotli-wasm"; const brotli = await brotliPromise; @@ -36,7 +33,7 @@ export const parcelBuild = async () => { if (!cache.static[path]) { const file = Bun.file(dir.path(`/app/static${path}`)); cache.static[path] = { - type: item.type, + type: lookup(path) || "text/plain", content: await file.arrayBuffer(), }; }