fix compression

This commit is contained in:
Rizky 2024-07-02 08:33:40 +07:00
parent c73678a7b6
commit 29e54d8009
2 changed files with 63 additions and 5 deletions

View File

@ -9,6 +9,8 @@ import { gzipAsync } from "../ws/sync/entity/zlib";
import { ensureLib } from "../ws/sync/code/utlis/ensure-lib"; import { ensureLib } from "../ws/sync/code/utlis/ensure-lib";
import { ensureFiles } from "../ws/sync/code/utlis/ensure-files"; import { ensureFiles } from "../ws/sync/code/utlis/ensure-files";
import { g } from "utils/global"; import { g } from "utils/global";
import broliPromise from "brotli-wasm";
import mime from "mime";
export const _ = { export const _ = {
url: "/prod/:site_id/**", url: "/prod/:site_id/**",
@ -129,11 +131,61 @@ export const _ = {
}, 100); }, 100);
}); });
} }
const body = Bun.gzipSync(await file.arrayBuffer());
return new Response(body, { const ts = file.lastModified;
headers: { "content-type": file.type, "content-encoding": "gzip" }, if (!g.code_index_cache) g.code_index_cache = {};
}); if (!g.code_index_cache[site_id]) g.code_index_cache[site_id] = {};
if (
!g.code_index_cache[site_id][build_path] ||
(g.code_index_cache[site_id][build_path] &&
g.code_index_cache[site_id][build_path].ts !== ts)
) {
if (!g.code_index_compressing) g.code_index_compressing = new Set();
const key = `${site_id}-${build_path}`;
if (!g.code_index_compressing.has(key)) {
g.code_index_compressing.add(key);
setTimeout(async () => {
if (!g.br) {
g.br = await broliPromise;
}
g.code_index_cache[site_id][build_path] = {
ts,
content: g.br.compress(
new Uint8Array(await file.arrayBuffer())
),
type: mime.getType(build_path) || "",
};
g.code_index_compressing.delete(key);
}, 100);
}
}
if (
g.code_index_cache[site_id] &&
g.code_index_cache[site_id][build_path] &&
g.code_index_cache[site_id][build_path].content
) {
return new Response(
g.code_index_cache[site_id][build_path].content,
{
headers: {
"content-encoding": "br",
"content-type": g.code_index_cache[site_id][build_path].type,
},
}
);
}
return new Response(
await gzipAsync(new Uint8Array(await file.arrayBuffer())),
{
headers: {
"content-encoding": "gzip",
"content-type": mime.getType(build_path) || "",
},
}
);
} }
case "route": { case "route": {
if (!g.route_cache) g.route_cache = {}; if (!g.route_cache) g.route_cache = {};
@ -267,7 +319,7 @@ export const _ = {
}, },
}; };
const responseCompressed = async (req: Request, body: string) => { const responseCompressed = async (req: Request, body: string | Uint8Array) => {
if (req.headers.get("accept-encoding")?.includes("gz")) { if (req.headers.get("accept-encoding")?.includes("gz")) {
return new Response(await gzipAsync(body), { return new Response(await gzipAsync(body), {
headers: { "content-encoding": "gzip" }, headers: { "content-encoding": "gzip" },

View File

@ -60,4 +60,10 @@ export const g = global as unknown as {
syncronize: typeof syncronize; syncronize: typeof syncronize;
static_cache: any; static_cache: any;
route_cache: Record<string, any>; route_cache: Record<string, any>;
code_index_compressing: Set<string>;
code_index_cache: Record<
string,
Record<string, { ts: number; content: any;type: string; }>
>;
br: any
}; };