From b1682f5ab9d12395c1da7507b4ac3fb5d7168ca0 Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 19 Oct 2023 04:35:51 +0000 Subject: [PATCH] fix --- pkgs/core/server/create.ts | 25 +++-------------- pkgs/core/utils/parcel.ts | 55 -------------------------------------- 2 files changed, 3 insertions(+), 77 deletions(-) diff --git a/pkgs/core/server/create.ts b/pkgs/core/server/create.ts index f7e88b8d..65f55d1b 100644 --- a/pkgs/core/server/create.ts +++ b/pkgs/core/server/create.ts @@ -11,9 +11,8 @@ export const cache = { string, { type: string; - content: ArrayBuffer; - br?: Uint8Array; - gz?: Uint8Array; + content: ReadableStream; + br?: ReadableStream; } >, }; @@ -100,7 +99,7 @@ export const createServer = async () => { if (!cache.static[url.pathname]) { cache.static[url.pathname] = { type: lookup(url.pathname) || "text/plain", - content: await file.arrayBuffer(), + content: file.stream(), }; } const found = cache.static[url.pathname]; @@ -129,24 +128,6 @@ export const createServer = async () => { }; 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 4269d293..fe2c21ba 100644 --- a/pkgs/core/utils/parcel.ts +++ b/pkgs/core/utils/parcel.ts @@ -21,61 +21,6 @@ export const parcelBuild = async () => { "--dist-dir", dir.path(`app/static`), ]; - if (g.mode === "prod") { - setTimeout(() => { - inspectTreeAsync(dir.path("app/static")).then((tree) => { - if (tree) { - const walk = async (item: InspectTreeResult, parent: string[]) => { - if (item.type === "file") { - let path = `${parent.slice(1).join("/")}/${item.name}`; - if (!path.startsWith("/")) path = `/${path}`; - - if (!cache.static[path]) { - const file = Bun.file(dir.path(`/app/static${path}`)); - cache.static[path] = { - type: lookup(path) || "text/plain", - content: await file.arrayBuffer(), - }; - } - const found = cache.static[path]; - - const staticBr = Bun.file(dir.path(`app/static-br${path}`)); - if (await staticBr.exists()) { - found.br = new Uint8Array(await staticBr.arrayBuffer()); - } else { - const pubFile = Bun.file(dir.path(`/app/web/public${path}`)); - const pubBr = Bun.file(dir.path(`/app/web/public-br${path}`)); - if (await pubFile.exists()) { - if (await pubBr.exists()) { - found.br = new Uint8Array(await pubBr.arrayBuffer()); - } else { - found.br = brotli.compress( - new Uint8Array(cache.static[path].content) - ); - if (found.br) { - await writeAsync( - dir.path(`/app/web/public-br${path}`), - Buffer.from(found.br) - ); - } - } - } else { - found.br = brotli.compress( - new Uint8Array(cache.static[path].content) - ); - } - } - } else if (item.type === "dir") { - for (const child of item.children) { - await walk(child, [...parent, item.name]); - } - } - }; - walk(tree, []); - } - }); - }, 1000); - } if (g.mode === "dev") { g.log.info(`Building web with parcel`);