This commit is contained in:
Rizky 2023-10-19 04:35:51 +00:00
parent ca925f4b26
commit b1682f5ab9
2 changed files with 3 additions and 77 deletions

View File

@ -11,9 +11,8 @@ export const cache = {
string, string,
{ {
type: string; type: string;
content: ArrayBuffer; content: ReadableStream<Uint8Array>;
br?: Uint8Array; br?: ReadableStream<Uint8Array>;
gz?: Uint8Array;
} }
>, >,
}; };
@ -100,7 +99,7 @@ export const createServer = async () => {
if (!cache.static[url.pathname]) { if (!cache.static[url.pathname]) {
cache.static[url.pathname] = { cache.static[url.pathname] = {
type: lookup(url.pathname) || "text/plain", type: lookup(url.pathname) || "text/plain",
content: await file.arrayBuffer(), content: file.stream(),
}; };
} }
const found = cache.static[url.pathname]; const found = cache.static[url.pathname];
@ -129,24 +128,6 @@ export const createServer = async () => {
}; };
const responseCached = (req: Request, found: (typeof cache.static)[string]) => { 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); const res = new Response(found.content);
res.headers.set("content-type", found.type); res.headers.set("content-type", found.type);
return res; return res;

View File

@ -21,61 +21,6 @@ export const parcelBuild = async () => {
"--dist-dir", "--dist-dir",
dir.path(`app/static`), 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") { if (g.mode === "dev") {
g.log.info(`Building web with parcel`); g.log.info(`Building web with parcel`);