wip fix prod

This commit is contained in:
Rizky 2024-02-09 18:33:52 +07:00
parent f0a99dbc6a
commit d28fa4c141
7 changed files with 99 additions and 33 deletions

View File

@ -109,10 +109,16 @@ window._prasi={basepath: "/prod/${site_id}",site_id:"${site_id}"}
if (validate(page_id)) { if (validate(page_id)) {
const page = await _db.page.findFirst({ const page = await _db.page.findFirst({
where: { id: page_id }, where: { id: page_id },
select: { content_tree: true }, select: { content_tree: true, url: true },
}); });
if (page) { if (page) {
return gzipAsync(JSON.stringify(page.content_tree) as any); return gzipAsync(
JSON.stringify({
id: page_id,
root: page.content_tree,
url: page.url,
}) as any
);
} }
} }
return null; return null;

View File

@ -105,17 +105,29 @@ export const code_action: SAction["code"]["action"] = async function (
await Bun.write( await Bun.write(
Bun.file(path.join(dir, "global.d.ts")), Bun.file(path.join(dir, "global.d.ts")),
`\ `\
import prisma from "./prisma";
import type * as SRVAPI from "gen/srv/api/srv"; import type * as SRVAPI from "gen/srv/api/srv";
import { Server, WebSocketHandler } from "bun";
import prisma from "./prisma";
declare global { declare global {
const db: prisma.PrismaClient; const db: prisma.PrismaClient;
type Api = typeof SRVAPI; type Api = typeof SRVAPI;
type ApiName = keyof Api; type ApiName = keyof Api;
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] }; const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] };
type PrasiServer = {
ws?: WebSocketHandler<{ url: string }>;
http: (arg: {
url: URL;
req: Request;
server: Server;
handle: (req: Request) => Promise<Response>;
}) => Promise<Response>;
};
} }
` `
); );
Bun.spawn({ Bun.spawn({

View File

@ -21,17 +21,9 @@ export const codeBuild = async (id_site: any) => {
await writeAsync( await writeAsync(
server_main, server_main,
`\ `\
import { Server, WebSocketHandler } from "bun"; import type {} from "./typings/global";
export const server: { export const server: PrasiServer = {
ws?: WebSocketHandler<{ url: string }>;
http: (arg: {
url: URL;
req: Request;
server: Server;
handle: (req: Request) => Promise<Response>;
}) => Promise<Response>;
} = {
async http({ req, handle, url, server }) { async http({ req, handle, url, server }) {
return await handle(req); return await handle(req);
}, },
@ -93,7 +85,6 @@ if (typeof global.server_hook === "function") {
name: "prasi", name: "prasi",
setup(setup) { setup(setup) {
setup.onEnd((res) => { setup.onEnd((res) => {
console.log("en bul");
server.init(id_site); server.init(id_site);
}); });
}, },

View File

@ -27,7 +27,6 @@ const serverMain = () => ({
init(site_id: string) { init(site_id: string) {
clearTimeout(this.init_timeout); clearTimeout(this.init_timeout);
this.init_timeout = setTimeout(() => { this.init_timeout = setTimeout(() => {
console.log("initing", site_id);
try { try {
const server_src_path = code.path( const server_src_path = code.path(
site_id, site_id,

View File

@ -0,0 +1,3 @@
import { createStore } from "idb-keyval";
export const prodCache = createStore(`prasi-prod`, `prasi-cache-prod`);

View File

@ -1,26 +1,81 @@
import { base } from "./base"; import { base } from "./base";
import { IRoot } from "../../../utils/types/root"; import { IRoot } from "../../../utils/types/root";
import { decompressBlob } from "./util"; import { decompressBlob } from "./util";
import { prodCache } from "./cache";
import { get, set } from "idb-keyval";
export const loadPage = async (page_id: string) => { export const loadPage = (page_id: string) => {
const raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob(); return new Promise<{
const res = JSON.parse(await (await decompressBlob(raw)).text()) as IRoot;
return res;
};
export const loadPages = async (page_ids: string[]) => {
const raw = await (
await fetch(base.url`_prasi/pages`, {
method: "POST",
body: JSON.stringify({ ids: [...new Set(page_ids)] }),
})
).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
id: string; id: string;
url: string; url: string;
root: IRoot; root: IRoot;
}[]; }>(async (done) => {
return res; let returned = false;
const cached = await get(`page-${page_id}`, prodCache);
if (cached) {
done(cached);
returned = true;
}
const raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
id: string;
url: string;
root: IRoot;
};
set(
`page-${page_id}`,
{ id: page_id, url: res.url, root: res.root },
prodCache
);
if (!returned) done(res);
});
};
export const loadPages = (page_ids: string[]) => {
return new Promise<
{
id: string;
url: string;
root: IRoot;
}[]
>(async (done) => {
const result: any = {};
const ids = [...new Set(page_ids)];
let is_complete = true;
for (const id of ids) {
const page = await get(`page-${id}`, prodCache);
if (page) {
result[id] = page;
} else {
is_complete = false;
}
}
if (is_complete) {
done(result);
}
const raw = await (
await fetch(base.url`_prasi/pages`, {
method: "POST",
body: JSON.stringify({ ids }),
})
).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
id: string;
url: string;
root: IRoot;
}[];
for (const [k, v] of Object.entries(res)) {
set(`page-${k}`, v, prodCache);
}
if (!is_complete) {
done(res);
}
});
}; };
export const loadUrls = async (urls: string[]) => { export const loadUrls = async (urls: string[]) => {

View File

@ -61,7 +61,7 @@ export const Root = () => {
if (!cache) { if (!cache) {
loadPage(page.id) loadPage(page.id)
.then(async (root) => { .then(async ({ root }) => {
const p = { const p = {
id: page.id, id: page.id,
url: page.url, url: page.url,