diff --git a/pkgs/api/_prasi.ts b/pkgs/api/_prasi.ts index 2f8c8d8..9ecff08 100644 --- a/pkgs/api/_prasi.ts +++ b/pkgs/api/_prasi.ts @@ -21,6 +21,18 @@ export const _ = { _: () => { res.send({ prasi: "v2" }); }, + compress: async () => { + const last = parts.pop(); + if (last === 'all') { + g.compress.mode = "all"; + } + if (last === 'only-gz') { + g.compress.mode = "only-gz"; + } + if (last === 'off') { + g.compress.mode = "off"; + } + }, code: async () => { if (gz) { const path = parts.slice(1).join("/"); diff --git a/pkgs/index.ts b/pkgs/index.ts index 5da5ec2..eeac49a 100644 --- a/pkgs/index.ts +++ b/pkgs/index.ts @@ -19,6 +19,7 @@ try { process.env.DATABASE_URL = db_env.DATABASE_URL; } catch (e) {} +g.compress = { mode: "all" }; g.mode = process.argv.includes("dev") ? "dev" : "prod"; g.datadir = g.mode === "prod" ? "../data" : ".data"; diff --git a/pkgs/utils/br-load.ts b/pkgs/utils/br-load.ts index 4407029..7475dea 100644 --- a/pkgs/utils/br-load.ts +++ b/pkgs/utils/br-load.ts @@ -6,59 +6,68 @@ import { simpleHash } from "./cache"; const encoder = new TextEncoder(); const brotli = await brotliPromise; export const loadCachedBr = (hash: string, content: string) => { - if (!g.cache.br[hash]) { - if (!g.cache.br_progress.pending[hash]) { - g.cache.br_progress.pending[hash] = content; - recurseCompressBr(); + if (g.compress.mode === "all") { + if (!g.cache.br[hash]) { + if (!g.cache.br_progress.pending[hash]) { + g.cache.br_progress.pending[hash] = content; + recurseCompressBr(); + } } } }; export const startBrCompress = () => { - if (g.deploy.content) { - const core = g.deploy.content.code.core; - const site = g.deploy.content.code.site; + if (g.compress.mode === "all") { + 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`); + 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; + for (const content of all) { + const hash = simpleHash(content); + g.cache.br_progress.pending[hash] = content; + } + + recurseCompressBr(); } - - recurseCompressBr(); } }; const recurseCompressBr = () => { - clearTimeout(g.cache.br_progress.timeout); - g.cache.br_progress.timeout = setTimeout(async () => { - if (g.cache.br_progress.running) { - return; - } - - g.cache.br_progress.running = true; - const entries = Object.entries(g.cache.br_progress.pending); - if (entries.length > 0) { - const [hash, content] = entries.shift() as [string, string | Uint8Array]; - - const file = Bun.file(dir(`${g.datadir}/br-cache/${hash}`)); - if (await file.exists()) { - g.cache.br[hash] = new Uint8Array(await file.arrayBuffer()); - } else { - g.cache.br[hash] = brotli.compress( - typeof content === "string" ? encoder.encode(content) : content, - { quality: 11 } - ); - await Bun.write(file, g.cache.br[hash]); + if (g.compress.mode === "all") { + clearTimeout(g.cache.br_progress.timeout); + g.cache.br_progress.timeout = setTimeout(async () => { + if (g.cache.br_progress.running) { + return; } - delete g.cache.br_progress.pending[hash]; - g.cache.br_progress.running = false; - recurseCompressBr(); - } else { - console.log("brotli cache: finished"); - } - }, 50); + g.cache.br_progress.running = true; + const entries = Object.entries(g.cache.br_progress.pending); + if (entries.length > 0) { + const [hash, content] = entries.shift() as [ + string, + string | Uint8Array + ]; + + const file = Bun.file(dir(`${g.datadir}/br-cache/${hash}`)); + if (await file.exists()) { + g.cache.br[hash] = new Uint8Array(await file.arrayBuffer()); + } else { + g.cache.br[hash] = brotli.compress( + typeof content === "string" ? encoder.encode(content) : content, + { quality: 11 } + ); + 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"); + } + }, 50); + } }; diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index 2eded45..d4a9f41 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -49,6 +49,9 @@ export const g = global as unknown as { notif: { db: Database; }; + compress: { + mode: "all" | "only-gz" | "off"; + }; api: Record; api_gen: { "load.json": string; @@ -70,7 +73,11 @@ export const g = global as unknown as { }; cache: { br: Record; - br_progress: { pending: Record; running: boolean; timeout: any }; + br_progress: { + pending: Record; + running: boolean; + timeout: any; + }; gz: Record; }; createServer: ( diff --git a/pkgs/utils/query.ts b/pkgs/utils/query.ts index 2391081..2f5d7af 100644 --- a/pkgs/utils/query.ts +++ b/pkgs/utils/query.ts @@ -178,6 +178,7 @@ export const execQuery = async (args: DBArg, prisma: any) => { result[k]._marker = v; } } + console.log(result); return result; } }