This commit is contained in:
Rizky 2024-07-06 18:31:19 +07:00
parent 470b48297a
commit e6443b87a1
5 changed files with 72 additions and 42 deletions

View File

@ -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("/");

View File

@ -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";

View File

@ -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);
}
};

View File

@ -49,6 +49,9 @@ export const g = global as unknown as {
notif: {
db: Database;
};
compress: {
mode: "all" | "only-gz" | "off";
};
api: Record<string, SingleRoute>;
api_gen: {
"load.json": string;
@ -70,7 +73,11 @@ export const g = global as unknown as {
};
cache: {
br: Record<string, Uint8Array>;
br_progress: { pending: Record<string, any>; running: boolean; timeout: any };
br_progress: {
pending: Record<string, any>;
running: boolean;
timeout: any;
};
gz: Record<string, Uint8Array>;
};
createServer: (

View File

@ -178,6 +178,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
result[k]._marker = v;
}
}
console.log(result);
return result;
}
}