wip fix prod
This commit is contained in:
parent
f0a99dbc6a
commit
d28fa4c141
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -105,15 +105,27 @@ 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>;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { createStore } from "idb-keyval";
|
||||||
|
|
||||||
|
export const prodCache = createStore(`prasi-prod`, `prasi-cache-prod`);
|
||||||
|
|
@ -1,18 +1,65 @@
|
||||||
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 = (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 raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob();
|
||||||
const res = JSON.parse(await (await decompressBlob(raw)).text()) as IRoot;
|
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
|
||||||
return res;
|
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 (
|
const raw = await (
|
||||||
await fetch(base.url`_prasi/pages`, {
|
await fetch(base.url`_prasi/pages`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ ids: [...new Set(page_ids)] }),
|
body: JSON.stringify({ ids }),
|
||||||
})
|
})
|
||||||
).blob();
|
).blob();
|
||||||
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
|
const res = JSON.parse(await (await decompressBlob(raw)).text()) as {
|
||||||
|
|
@ -20,7 +67,15 @@ export const loadPages = async (page_ids: string[]) => {
|
||||||
url: string;
|
url: string;
|
||||||
root: IRoot;
|
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[]) => {
|
export const loadUrls = async (urls: string[]) => {
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue