This commit is contained in:
Rizky 2025-01-10 19:39:11 +07:00
parent 1991dfd865
commit 8b8f3f0d8f
10 changed files with 64 additions and 13719 deletions

4
.gitignore vendored
View File

@ -175,4 +175,6 @@ dist
.DS_Store
site
site/*
site/*
internal/vm

View File

@ -17,6 +17,7 @@ export const _ = {
case "load.js":
}
if (action === "route") {
return json(await prasi.content.all_routes());
}

View File

@ -1,11 +1,19 @@
import type { SiteConfig } from "utils/deploy-config";
import { ensurePrismaReady } from "./ensure-prisma";
import { fs } from "utils/fs";
export const initDB = async (db: SiteConfig["db"]) => {
if (db.orm === "prisma") {
if (db.url) {
try {
await ensurePrismaReady(db);
const g = globalThis as any;
const prisma_path = fs.path(
"site:app/db/node_modules/.prisma/client/index.js"
);
const prisma = require(prisma_path);
g.db = new prisma.PrismaClient();
} catch (e: any) {
if (e && e.stderr instanceof Buffer) {
console.error(new TextDecoder().decode(e.stderr));

View File

@ -8,7 +8,7 @@ import { initDB } from "../db/init-db";
import { createHttpHandler } from "./handler/http-handler";
import { createWsHandler } from "./handler/ws-handler";
import { prasi, type PrasiContent } from "./prasi-var";
import { join } from "path";
export const init = async ({
site_id,
server,
@ -75,30 +75,20 @@ export const init = async ({
nova: init_prasi.paths.dir.nova,
};
prasi.site_id = site_id;
prasi.dev = dev;
prasi.dev = dev;
if (mode === "ipc") {
if (action === "init") return;
// const vm_path = join(
// backend_path,
// basename(init_prasi.paths.server).replace(".ts", ".js")
// );
// const src = await Bun.file(vm_path).text();
// const glb = global as any;
// const script = new Script(src, { filename: vm_path });
// const module = { exports: { server: null as any } };
// const cjs = script.runInContext(glb);
// cjs(exports, require, module);
// prasi.server = module.exports.server;
} else {
delete require.cache[backend_path];
const module = require(backend_path);
prasi.server = module.server;
}
const backend_file = join(
backend_path,
init_prasi.paths.server.replace(".ts", ".js")
);
delete require.cache[backend_file];
const module = require(backend_file);
prasi.server = module.server;
process.chdir(backend_path);
prasi.ext = {};
@ -119,7 +109,6 @@ export const init = async ({
action === "reload" ? "Reloaded" : "Started"
}.`
);
if (prasi.server?.init) {
await prasi.server.init({ port: server_instance.port });
}
@ -128,4 +117,5 @@ export const init = async ({
http: await createHttpHandler(prasi, mode === "ipc" ? "dev" : "prod"),
ws: createWsHandler(),
};
return prasi;
};

40
internal/main/ipc.ts Normal file
View File

@ -0,0 +1,40 @@
import type { PrasiGlobal } from "main/prasi-var";
import imported from "../vm/init";
process.on(
"message",
async (arg: { action: "init" } | { action: "req"; req: Request }) => {
const action = arg?.action;
if (action === "req") {
return;
}
const init_arg = {
...arg,
server: () => {
const server = Bun.serve({
websocket: { message(ws, message) {} },
fetch(req, server) {
return prasi.handler.http(req);
},
port: 0,
});
process.send?.({ action: "port", port: server.port });
return server
},
content: new Proxy(
{},
{
get(target, p, receiver) {
return () => {
console.log('mantap', p);
};
},
}
),
};
const prasi: PrasiGlobal = await imported.init(init_arg);
if (arg.action === "init") {
process.exit();
}
}
);

View File

@ -1,5 +0,0 @@
WARNING: This VM folder is auto generated from main/init.ts,
The purpose of directory is to be loaded as vm server from prasi editor.
Creating a VM only works when it is loaded as single file, hence it is built.
Do not directly change files in this directory, as it will be re-generated.

File diff suppressed because one or more lines are too long

View File

@ -1,23 +0,0 @@
import imported from "./init";
process.on("message", async (arg: any) => {
const init_arg = {
...arg,
server: (prasi_srv: any) => {},
content: new Proxy(
{},
{
get(target, p, receiver) {
return () => {
console.log(p);
};
},
}
),
};
await imported.init(init_arg);
if (arg.action === "init") {
process.send?.("ok");
}
});

View File

@ -3,7 +3,7 @@
"module": "index.ts",
"type": "module",
"scripts": {
"dev": "bun build --watch --target bun --format cjs --entry ./internal/main/init.ts --outdir ./internal/vm"
"dev": "mkdir internal/vm && cp internal/main/ipc.ts internal/vm && bun build --watch --target bun --format cjs --entry ./internal/main/init.ts --outdir ./internal/vm"
},
"devDependencies": {
"@types/bun": "latest"