fix brotli cached

This commit is contained in:
Rizky 2024-06-28 00:09:21 +07:00
parent e72f491193
commit 4367723a46
3 changed files with 36 additions and 11 deletions

View File

@ -12,15 +12,6 @@ import { prodIndex } from "utils/prod-index";
export const createServer = async () => {
g.router = createRouter({ strictTrailingSlash: true });
g.api = {};
g.cache = {
br: {},
gz: {},
br_progress: {
pending: {},
running: false,
timeout: null,
},
};
const scan = async (path: string, root?: string) => {
const apis = await listAsync(path);
if (apis) {

View File

@ -1,10 +1,12 @@
import brotliPromise from "brotli-wasm"; // Import the default export
import { g } from "./global";
import { dir } from "./dir";
import { simpleHash } from "./cache";
const encoder = new TextEncoder();
const brotli = await brotliPromise;
export const loadCachedBr = (hash: string, content: string) => {
console.log(!g.cache.br[hash]);
if (!g.cache.br[hash]) {
if (!g.cache.br_progress.pending[hash]) {
g.cache.br_progress.pending[hash] = content;
@ -13,6 +15,23 @@ export const loadCachedBr = (hash: string, content: string) => {
}
};
export const startBrCompress = () => {
if (g.deploy.content) {
const core = g.deploy.content.code.core;
const site = g.deploy.content.code.site;
const all = [...Object.values(core), ...Object.values(site)];
console.log(`brotli cache: compressing ${all.length} files`);
for (const content of all) {
const hash = simpleHash(content);
g.cache.br_progress.pending[hash] = content;
}
recurseCompressBr();
}
};
const recurseCompressBr = () => {
clearTimeout(g.cache.br_progress.timeout);
g.cache.br_progress.timeout = setTimeout(async () => {
@ -35,11 +54,12 @@ const recurseCompressBr = () => {
);
await Bun.write(file, g.cache.br[hash]);
}
delete g.cache.br_progress.pending[hash];
g.cache.br_progress.running = false;
recurseCompressBr();
} else {
console.log("brotli cache finished");
console.log("brotli cache: finished");
}
}, 50);
};

View File

@ -11,6 +11,7 @@ import { g } from "./global";
import { gunzipAsync } from "./gzip";
import { createRouter } from "radix3";
import { prodIndex } from "./prod-index";
import { startBrCompress } from "./br-load";
const decoder = new TextDecoder();
export const deploy = {
@ -38,6 +39,17 @@ export const deploy = {
);
if (g.deploy.content) {
g.cache = {
br: {},
gz: {},
br_progress: {
pending: {},
running: false,
timeout: null,
},
};
startBrCompress();
if (exists(dir("public"))) {
await removeAsync(dir("public"));
if (g.deploy.content.public) {
@ -75,7 +87,9 @@ export const deploy = {
delete require.cache[dir(`app/web/server/index.js`)];
await removeAsync(dir(`app/web/server`));
await dirAsync(dir(`app/web/server`));
for (const [k, v] of Object.entries(g.deploy.content.code.server)) {
for (const [k, v] of Object.entries(
g.deploy.content.code.server
)) {
await writeAsync(dir(`app/web/server/${k}`), v);
}