fix caching result

This commit is contained in:
Rizky 2023-10-19 08:06:45 +07:00
parent 3f621f44ad
commit f70e3ea727
2 changed files with 32 additions and 27 deletions

View File

@ -4,8 +4,7 @@ import { wsHandler } from "../../../app/srv/ws/handler";
import { dir } from "../utils/dir"; import { dir } from "../utils/dir";
import { g } from "../utils/global"; import { g } from "../utils/global";
import { serveAPI } from "./serve-api"; import { serveAPI } from "./serve-api";
import brotliPromise from "brotli-wasm"; import { lookup } from "mime-types";
const brotli = await brotliPromise;
export const cache = { export const cache = {
static: {} as Record< static: {} as Record<
@ -89,36 +88,21 @@ export const createServer = async () => {
try { try {
const found = cache.static[url.pathname]; const found = cache.static[url.pathname];
if (found && g.mode === "prod") { if (found && g.mode === "prod") {
const res = new Response(found.content); return responseCached(req, found);
res.headers.set("Content-Type", found.type);
} }
const file = Bun.file(dir.path(`${webPath}${url.pathname}`)); const file = Bun.file(dir.path(`${webPath}${url.pathname}`));
if ((await file.exists()) && file.type !== "application/octet-stream") { if ((await file.exists()) && file.type !== "application/octet-stream") {
if (!cache.static[url.pathname]) { if (!cache.static[url.pathname]) {
cache.static[url.pathname] = { cache.static[url.pathname] = {
type: file.type, type: lookup(url.pathname) || "text/plain",
content: await file.arrayBuffer(), content: await file.arrayBuffer(),
}; };
} }
const found = cache.static[url.pathname]; const found = cache.static[url.pathname];
const enc = req.headers.get("accept-encoding"); if (found) {
if (enc && g.mode === "prod") { return responseCached(req, found);
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;
}
} }
return new Response(found.content);
} }
} catch (e) { } catch (e) {
g.log.error(e); g.log.error(e);
@ -139,3 +123,27 @@ export const createServer = async () => {
g.log.info(`Started at port: ${g.server.port}`); 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;
};

View File

@ -1,13 +1,10 @@
import { spawn } from "bun"; import { spawn } from "bun";
import { import { dirAsync, inspectTreeAsync, writeAsync } from "fs-jetpack";
dirAsync,
inspectTreeAsync,
writeAsync
} from "fs-jetpack";
import { InspectTreeResult } from "fs-jetpack/types"; import { InspectTreeResult } from "fs-jetpack/types";
import { cache } from "../server/create"; import { cache } from "../server/create";
import { dir } from "./dir"; import { dir } from "./dir";
import { g } from "./global"; import { g } from "./global";
import { lookup } from "mime-types";
import brotliPromise from "brotli-wasm"; import brotliPromise from "brotli-wasm";
const brotli = await brotliPromise; const brotli = await brotliPromise;
@ -36,7 +33,7 @@ export const parcelBuild = async () => {
if (!cache.static[path]) { if (!cache.static[path]) {
const file = Bun.file(dir.path(`/app/static${path}`)); const file = Bun.file(dir.path(`/app/static${path}`));
cache.static[path] = { cache.static[path] = {
type: item.type, type: lookup(path) || "text/plain",
content: await file.arrayBuffer(), content: await file.arrayBuffer(),
}; };
} }