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)) {
const page = await _db.page.findFirst({
where: { id: page_id },
select: { content_tree: true },
select: { content_tree: true, url: true },
});
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;

View File

@ -105,15 +105,27 @@ export const code_action: SAction["code"]["action"] = async function (
await Bun.write(
Bun.file(path.join(dir, "global.d.ts")),
`\
import prisma from "./prisma";
import type * as SRVAPI from "gen/srv/api/srv";
import { Server, WebSocketHandler } from "bun";
import prisma from "./prisma";
declare global {
const db: prisma.PrismaClient;
type Api = typeof SRVAPI;
type ApiName = keyof 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>;
};
}
`
);

View File

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

View File

@ -27,7 +27,6 @@ const serverMain = () => ({
init(site_id: string) {
clearTimeout(this.init_timeout);
this.init_timeout = setTimeout(() => {
console.log("initing", site_id);
try {
const server_src_path = code.path(
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,18 +1,65 @@
import { base } from "./base";
import { IRoot } from "../../../utils/types/root";
import { decompressBlob } from "./util";
import { prodCache } from "./cache";
import { get, set } from "idb-keyval";
export const loadPage = (page_id: string) => {
return new Promise<{
id: string;
url: string;
root: IRoot;
}>(async (done) => {
let returned = false;
const cached = await get(`page-${page_id}`, prodCache);
if (cached) {
done(cached);
returned = true;
}
export const loadPage = async (page_id: string) => {
const raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text()) as IRoot;
return res;
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 = async (page_ids: string[]) => {
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: [...new Set(page_ids)] }),
body: JSON.stringify({ ids }),
})
).blob();
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
@ -20,7 +67,15 @@ export const loadPages = async (page_ids: string[]) => {
url: string;
root: IRoot;
}[];
return res;
for (const [k, v] of Object.entries(res)) {
set(`page-${k}`, v, prodCache);
}
if (!is_complete) {
done(res);
}
});
};
export const loadUrls = async (urls: string[]) => {

View File

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