From 4367723a46e356f1ed228b5386f759686b29c69a Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 28 Jun 2024 00:09:21 +0700 Subject: [PATCH] fix brotli cached --- pkgs/server/create.ts | 9 --------- pkgs/utils/br-load.ts | 22 +++++++++++++++++++++- pkgs/utils/deploy.ts | 16 +++++++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/pkgs/server/create.ts b/pkgs/server/create.ts index 9912a01..7ae5ccd 100644 --- a/pkgs/server/create.ts +++ b/pkgs/server/create.ts @@ -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) { diff --git a/pkgs/utils/br-load.ts b/pkgs/utils/br-load.ts index 0da1c56..432712b 100644 --- a/pkgs/utils/br-load.ts +++ b/pkgs/utils/br-load.ts @@ -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); }; diff --git a/pkgs/utils/deploy.ts b/pkgs/utils/deploy.ts index c2e0ccf..ef666c4 100644 --- a/pkgs/utils/deploy.ts +++ b/pkgs/utils/deploy.ts @@ -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); }