This commit is contained in:
Rizky 2024-01-23 12:01:15 +07:00
parent 83317ec5a0
commit 6c0bc1066e
2 changed files with 68 additions and 44 deletions

View File

@ -4,45 +4,47 @@ import { SAction } from "../actions";
import { prepCode } from "../editor/code/prep-code";
import { activity } from "../entity/activity";
import { SyncConnection } from "../type";
import { broadcastCode } from "../editor/code/build";
export const site_load: SAction["site"]["load"] = async function (
this: SyncConnection,
site_id: string,
this: SyncConnection,
site_id: string
) {
if (validate(site_id)) {
const site = await db.site.findFirst({ where: { id: site_id } });
if (site) {
if (this.conf) this.conf.site_id = site.id;
if (validate(site_id)) {
const site = await db.site.findFirst({ where: { id: site_id } });
if (site) {
if (this.conf) this.conf.site_id = site.id;
const config =
typeof site.config === "object" && site.config
? { api_url: (site.config as any).api_url || "" }
: { api_url: "" };
const config =
typeof site.config === "object" && site.config
? { api_url: (site.config as any).api_url || "" }
: { api_url: "" };
activity.site.room(site.id).join({ ws: this.ws });
activity.site.room(site.id).join({ ws: this.ws });
const layout = await db.page.findFirst({
where: {
id_site: site_id,
is_deleted: false,
is_default_layout: true,
},
select: { id: true },
});
const layout = await db.page.findFirst({
where: {
id_site: site_id,
is_deleted: false,
is_default_layout: true,
},
select: { id: true },
});
await prepCode(site_id, "site");
await prepCode(site_id, "site");
broadcastCode(site_id, this.ws);
return {
id: site.id,
name: site.name,
config: config as ESite["config"],
domain: site.domain,
js: site.js || "",
responsive: site.responsive as ESite["responsive"],
js_compiled: site.js_compiled || "",
layout: { id: layout?.id || "", snapshot: null, meta: undefined },
code: { snapshot: null, mode: site.code_mode as "old" | "vsc" },
};
}
}
return {
id: site.id,
name: site.name,
config: config as ESite["config"],
domain: site.domain,
js: site.js || "",
responsive: site.responsive as ESite["responsive"],
js_compiled: site.js_compiled || "",
layout: { id: layout?.id || "", snapshot: null, meta: undefined },
code: { snapshot: null, mode: site.code_mode as "old" | "vsc" },
};
}
}
};

View File

@ -9,6 +9,8 @@ import { activity } from "../../entity/activity";
import { sendWS } from "../../sync-handler";
import { SyncType } from "../../type";
import { gzipAsync } from "../../entity/zlib";
import { ServerWebSocket } from "bun";
import { WSData } from "../../../../../../pkgs/core/server/create";
const encoder = new TextEncoder();
export const codeBuild = async (code: DBCode) => {
@ -112,7 +114,10 @@ export const codeBuild = async (code: DBCode) => {
const code_id = {} as Record<string, { site: string; ssr: string }>;
export const broadcastCode = async (id_site: string) => {
export const broadcastCode = async (
id_site: string,
ws?: ServerWebSocket<WSData>
) => {
if (!code_id[id_site]) {
const res = await db.code.findMany({ where: { id_site } });
if (res.length > 0) {
@ -131,15 +136,14 @@ export const broadcastCode = async (id_site: string) => {
const id_code = code_id[id_site].site;
const outfile = dir.path(`${g.datadir}/site/build/${id_code}/index.js`);
const out = Bun.file(outfile);
const src = (await out.text()).replace(
"//# sourceMappingURL=index.js.map",
`//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map`
);
const srcgz = await gzipAsync(encoder.encode(src));
activity.site
.room(id_site)
.findAll()
.forEach((item, ws) => {
if (out) {
const src = (await out.text()).replace(
"//# sourceMappingURL=index.js.map",
`//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map`
);
const srcgz = await gzipAsync(encoder.encode(src));
if (ws) {
sendWS(ws, {
type: SyncType.Event,
event: "code",
@ -151,6 +155,24 @@ export const broadcastCode = async (id_site: string) => {
content: "OK",
},
});
});
} else {
activity.site
.room(id_site)
.findAll()
.forEach((item, ws) => {
sendWS(ws, {
type: SyncType.Event,
event: "code",
data: {
name: "site",
id: id_code,
event: "code-done",
src: srcgz,
content: "OK",
},
});
});
}
}
}
};