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 { prepCode } from "../editor/code/prep-code";
import { activity } from "../entity/activity"; import { activity } from "../entity/activity";
import { SyncConnection } from "../type"; import { SyncConnection } from "../type";
import { broadcastCode } from "../editor/code/build";
export const site_load: SAction["site"]["load"] = async function ( export const site_load: SAction["site"]["load"] = async function (
this: SyncConnection, this: SyncConnection,
site_id: string, site_id: string
) { ) {
if (validate(site_id)) { if (validate(site_id)) {
const site = await db.site.findFirst({ where: { id: site_id } }); const site = await db.site.findFirst({ where: { id: site_id } });
if (site) { if (site) {
if (this.conf) this.conf.site_id = site.id; if (this.conf) this.conf.site_id = site.id;
const config = const config =
typeof site.config === "object" && site.config typeof site.config === "object" && site.config
? { api_url: (site.config as any).api_url || "" } ? { api_url: (site.config as any).api_url || "" }
: { 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({ const layout = await db.page.findFirst({
where: { where: {
id_site: site_id, id_site: site_id,
is_deleted: false, is_deleted: false,
is_default_layout: true, is_default_layout: true,
}, },
select: { id: true }, select: { id: true },
}); });
await prepCode(site_id, "site"); await prepCode(site_id, "site");
broadcastCode(site_id, this.ws);
return { return {
id: site.id, id: site.id,
name: site.name, name: site.name,
config: config as ESite["config"], config: config as ESite["config"],
domain: site.domain, domain: site.domain,
js: site.js || "", js: site.js || "",
responsive: site.responsive as ESite["responsive"], responsive: site.responsive as ESite["responsive"],
js_compiled: site.js_compiled || "", js_compiled: site.js_compiled || "",
layout: { id: layout?.id || "", snapshot: null, meta: undefined }, layout: { id: layout?.id || "", snapshot: null, meta: undefined },
code: { snapshot: null, mode: site.code_mode as "old" | "vsc" }, 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 { sendWS } from "../../sync-handler";
import { SyncType } from "../../type"; import { SyncType } from "../../type";
import { gzipAsync } from "../../entity/zlib"; import { gzipAsync } from "../../entity/zlib";
import { ServerWebSocket } from "bun";
import { WSData } from "../../../../../../pkgs/core/server/create";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
export const codeBuild = async (code: DBCode) => { 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 }>; 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]) { if (!code_id[id_site]) {
const res = await db.code.findMany({ where: { id_site } }); const res = await db.code.findMany({ where: { id_site } });
if (res.length > 0) { if (res.length > 0) {
@ -131,15 +136,14 @@ export const broadcastCode = async (id_site: string) => {
const id_code = code_id[id_site].site; const id_code = code_id[id_site].site;
const outfile = dir.path(`${g.datadir}/site/build/${id_code}/index.js`); const outfile = dir.path(`${g.datadir}/site/build/${id_code}/index.js`);
const out = Bun.file(outfile); const out = Bun.file(outfile);
const src = (await out.text()).replace( if (out) {
"//# sourceMappingURL=index.js.map", const src = (await out.text()).replace(
`//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map` "//# sourceMappingURL=index.js.map",
); `//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map`
const srcgz = await gzipAsync(encoder.encode(src)); );
activity.site const srcgz = await gzipAsync(encoder.encode(src));
.room(id_site)
.findAll() if (ws) {
.forEach((item, ws) => {
sendWS(ws, { sendWS(ws, {
type: SyncType.Event, type: SyncType.Event,
event: "code", event: "code",
@ -151,6 +155,24 @@ export const broadcastCode = async (id_site: string) => {
content: "OK", 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",
},
});
});
}
}
} }
}; };