fixing ipc

This commit is contained in:
Rizky 2024-12-15 12:46:53 +07:00
parent 5d731ee191
commit e2a4a1a1f2
12 changed files with 135 additions and 29 deletions

View File

@ -0,0 +1,24 @@
import { g } from "utils/global";
import { ensureDeployExists } from "./deploy/ensure";
import type { PrasiContent } from "./types";
export const prasi_content_deploy: PrasiContent = {
async prepare(site_id) {
await ensureDeployExists(site_id);
},
async comps(comp_ids) {
return [];
},
async file(url, options) {
return { body: "", compression: "none" };
},
async layouts() {
return [];
},
async page_urls() {
return {};
},
async pages(page_ids) {
return [];
},
};

View File

@ -0,0 +1,22 @@
import type { PrasiContent } from "./types";
export const prasi_content_ipc: PrasiContent = {
prepare(site_id) {
console.log("mantap jiwa");
},
async comps(comp_ids) {
return [];
},
async file(url, options) {
return { body: "", compression: "none" };
},
async layouts() {
return [];
},
async page_urls() {
return {};
},
async pages(page_ids) {
return [];
},
};

43
internal/content/types.ts Normal file
View File

@ -0,0 +1,43 @@
export type PrasiContent = {
prepare: (site_id: string) => void | Promise<void>;
page_urls: () => Promise<Record<string, string>>;
pages: (page_ids: string[]) => Promise<IPage[]>;
comps: (comp_ids: string[]) => Promise<IComp[]>;
layouts: () => Promise<ILayout[]>;
file: (
url: string,
options?: {
accept: ("gzip" | "br" | "zstd")[];
}
) => Promise<{ body: any; compression: "none" | "gzip" | "br" | "zstd" }>;
};
export type ILayout = {
id: string;
name: string;
url: string;
content_tree: any;
is_default_layout: boolean;
};
export type IPage = {
id: string;
name: string;
url: string;
content_tree: any;
};
export type IComp = {
id: string;
content_tree: any;
};
export type ISiteInfo = {
id: string;
name: string;
config?: {
api_url: string;
};
responsive: string;
domain: string;
};

View File

@ -0,0 +1,9 @@
import { config } from "utils/config";
import { startup } from "utils/global";
startup("site", async () => {
await config.init("site:site.json");
const ts = config.current?.deploy.current;
if (ts) {
}
});

View File

@ -0,0 +1,6 @@
export const startServerWithIPC = () => {
return Bun.serve({
fetch(request, server) {},
websocket: { message(ws, message) {} },
});
};

View File

@ -1,16 +0,0 @@
import { c } from "utils/color";
import { config } from "utils/config";
import { g, startup } from "utils/global";
import { siteLog } from "utils/log";
import { loadCurrentDeploy } from "../deploy/load";
startup("site", async () => {
await config.init("site:site.json");
const ts = config.current?.deploy.current;
if (ts) {
await loadCurrentDeploy(ts);
siteLog(
`Site Loaded [${c.green}${g.site.pages.length} pages${c.esc}] [${c.blue}${g.site.comps.length} components${c.esc}]`
);
}
});

View File

@ -1,11 +1,18 @@
import { fs } from "utils/fs";
import { g } from "utils/global";
import { spawn } from "utils/spawn";
import { prasi_content_ipc } from "../content/content-ipc";
import { startServerWithIPC } from "./server-mode-ipc";
export const startServer = (is_dev: boolean) => {
g.server = spawn({
export const startServer = (is_dev: boolean, site_id: string) => {
if (g.server.mode === "deploy") {
g.server.process = spawn({
cmd: is_dev ? "bun run --watch server.js" : "bun run server.js",
cwd: fs.path("site:app"),
mode: "passthrough",
});
} else {
g.server.bun_server = startServerWithIPC();
prasi_content_ipc.prepare(site_id);
}
};

View File

@ -1,10 +1,10 @@
import { c } from "utils/color";
import { config } from "utils/config";
import { fs } from "utils/fs";
import { startup } from "utils/global";
import { g, startup } from "utils/global";
import { siteLog } from "utils/log";
import { prasi_content_deploy } from "./content/content-deploy";
import { ensureDBReady } from "./db/ensure";
import { ensureDeployExists } from "./deploy/ensure";
import { ensureServerReady } from "./server/ensure";
import { startServer } from "./server/start";
@ -18,10 +18,14 @@ startup("supervisor", async () => {
siteLog("No Site Loaded");
} else {
siteLog(`Site ID: ${site_id}`);
await ensureDeployExists(site_id);
if (g.server.mode === "deploy") {
await prasi_content_deploy.prepare(site_id);
}
await ensureServerReady(is_dev);
await ensureDBReady();
startServer(is_dev);
startServer(is_dev, site_id);
}
});

View File

@ -2,6 +2,7 @@ import { join, resolve } from "path";
import { fs } from "./fs";
import type { SiteConfig } from "./config";
import type { spawn } from "./spawn";
import type { Server } from "bun";
if (!(globalThis as any).prasi) {
(globalThis as any).prasi = {};
@ -9,8 +10,11 @@ if (!(globalThis as any).prasi) {
export const g = (globalThis as any).prasi as unknown as {
dir: { root: string };
server: ReturnType<typeof spawn>;
site: {
mode: "supervisor" | "site";
server:
| { mode: "deploy"; process: ReturnType<typeof spawn> }
| { mode: "ipc"; bun_server: Server };
site?: {
db?: SiteConfig["db"];
layouts: {
id: string;
@ -43,8 +47,11 @@ export const g = (globalThis as any).prasi as unknown as {
export const startup = (mode: "supervisor" | "site", fn: () => void) => {
g.dir = { root: "" };
g.mode = mode;
g.server.mode = process.argv.includes("--ipc") ? "ipc" : "deploy";
if (mode === "supervisor") {
const argv = process.argv.filter((e) => e !== "--dev");
const argv = process.argv.filter((e) => !e.startsWith("--"));
if (argv.length > 2) {
g.dir.root = resolve(argv[2]);
} else {