fix
This commit is contained in:
parent
470b48297a
commit
e6443b87a1
|
|
@ -21,6 +21,18 @@ export const _ = {
|
||||||
_: () => {
|
_: () => {
|
||||||
res.send({ prasi: "v2" });
|
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 () => {
|
code: async () => {
|
||||||
if (gz) {
|
if (gz) {
|
||||||
const path = parts.slice(1).join("/");
|
const path = parts.slice(1).join("/");
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ try {
|
||||||
process.env.DATABASE_URL = db_env.DATABASE_URL;
|
process.env.DATABASE_URL = db_env.DATABASE_URL;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
|
g.compress = { mode: "all" };
|
||||||
g.mode = process.argv.includes("dev") ? "dev" : "prod";
|
g.mode = process.argv.includes("dev") ? "dev" : "prod";
|
||||||
g.datadir = g.mode === "prod" ? "../data" : ".data";
|
g.datadir = g.mode === "prod" ? "../data" : ".data";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,59 +6,68 @@ import { simpleHash } from "./cache";
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const brotli = await brotliPromise;
|
const brotli = await brotliPromise;
|
||||||
export const loadCachedBr = (hash: string, content: string) => {
|
export const loadCachedBr = (hash: string, content: string) => {
|
||||||
if (!g.cache.br[hash]) {
|
if (g.compress.mode === "all") {
|
||||||
if (!g.cache.br_progress.pending[hash]) {
|
if (!g.cache.br[hash]) {
|
||||||
g.cache.br_progress.pending[hash] = content;
|
if (!g.cache.br_progress.pending[hash]) {
|
||||||
recurseCompressBr();
|
g.cache.br_progress.pending[hash] = content;
|
||||||
|
recurseCompressBr();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const startBrCompress = () => {
|
export const startBrCompress = () => {
|
||||||
if (g.deploy.content) {
|
if (g.compress.mode === "all") {
|
||||||
const core = g.deploy.content.code.core;
|
if (g.deploy.content) {
|
||||||
const site = g.deploy.content.code.site;
|
const core = g.deploy.content.code.core;
|
||||||
|
const site = g.deploy.content.code.site;
|
||||||
|
|
||||||
const all = [...Object.values(core), ...Object.values(site)];
|
const all = [...Object.values(core), ...Object.values(site)];
|
||||||
console.log(`brotli cache: compressing ${all.length} files`);
|
console.log(`brotli cache: compressing ${all.length} files`);
|
||||||
|
|
||||||
for (const content of all) {
|
for (const content of all) {
|
||||||
const hash = simpleHash(content);
|
const hash = simpleHash(content);
|
||||||
g.cache.br_progress.pending[hash] = content;
|
g.cache.br_progress.pending[hash] = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
recurseCompressBr();
|
||||||
}
|
}
|
||||||
|
|
||||||
recurseCompressBr();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const recurseCompressBr = () => {
|
const recurseCompressBr = () => {
|
||||||
clearTimeout(g.cache.br_progress.timeout);
|
if (g.compress.mode === "all") {
|
||||||
g.cache.br_progress.timeout = setTimeout(async () => {
|
clearTimeout(g.cache.br_progress.timeout);
|
||||||
if (g.cache.br_progress.running) {
|
g.cache.br_progress.timeout = setTimeout(async () => {
|
||||||
return;
|
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete g.cache.br_progress.pending[hash];
|
g.cache.br_progress.running = true;
|
||||||
g.cache.br_progress.running = false;
|
const entries = Object.entries(g.cache.br_progress.pending);
|
||||||
recurseCompressBr();
|
if (entries.length > 0) {
|
||||||
} else {
|
const [hash, content] = entries.shift() as [
|
||||||
console.log("brotli cache: finished");
|
string,
|
||||||
}
|
string | Uint8Array
|
||||||
}, 50);
|
];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,9 @@ export const g = global as unknown as {
|
||||||
notif: {
|
notif: {
|
||||||
db: Database;
|
db: Database;
|
||||||
};
|
};
|
||||||
|
compress: {
|
||||||
|
mode: "all" | "only-gz" | "off";
|
||||||
|
};
|
||||||
api: Record<string, SingleRoute>;
|
api: Record<string, SingleRoute>;
|
||||||
api_gen: {
|
api_gen: {
|
||||||
"load.json": string;
|
"load.json": string;
|
||||||
|
|
@ -70,7 +73,11 @@ export const g = global as unknown as {
|
||||||
};
|
};
|
||||||
cache: {
|
cache: {
|
||||||
br: Record<string, Uint8Array>;
|
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>;
|
gz: Record<string, Uint8Array>;
|
||||||
};
|
};
|
||||||
createServer: (
|
createServer: (
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
|
||||||
result[k]._marker = v;
|
result[k]._marker = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue