This commit is contained in:
Rizky 2024-03-06 18:57:29 +07:00
parent 682f9da033
commit 5b647fdfb9
10 changed files with 109 additions and 139 deletions

View File

@ -29,8 +29,10 @@ export const _ = {
const codepath = arr.join("/"); const codepath = arr.join("/");
const build_path = code.path(site_id, "site", "build", codepath); const build_path = code.path(site_id, "site", "build", codepath);
const file = Bun.file(build_path); const file = Bun.file(build_path);
if (!(await file.exists())) if (!(await file.exists()))
return new Response("Code file not found", { status: 403 }); return new Response("Code file not found", { status: 403 });
return new Response(file); return new Response(file);
} }
case "route": { case "route": {

File diff suppressed because one or more lines are too long

View File

@ -32,6 +32,7 @@ glb.server_hook = async (arg) => {
if (arr.length >= 3 && validate(site_id)) { if (arr.length >= 3 && validate(site_id)) {
const res = await server.http(site_id, arg); const res = await server.http(site_id, arg);
if (res instanceof Response) { if (res instanceof Response) {
return res; return res;
} else { } else {

View File

@ -69,19 +69,15 @@ typeof global.server_hook === "function"
let db = new Proxy({}, { let db = new Proxy({}, {
get(_, key) { get(_, key) {
if (key === '___site_id') { const runtime = global.server_runtime["${id_site}"];
return (site_id) => { _.site_id = site_id } if (runtime && runtime.db) {
} return runtime.db[key];
if (_.site_id) {
const runtime = global.server_runtime[_.site_id];
if (runtime && runtime.db) {
return runtime.db[key];
}
} }
} }
}); });
let api = {}; let api = {};
if (typeof global.server_hook === "function") { if (typeof global.server_hook === "function") {
createServerRuntime("${id_site}");
const log = global.console.log; const log = global.console.log;
console.log = function (...arg) { console.log = function (...arg) {
const out = "${code.path(id_site, "site", "src", "server.log")}"; const out = "${code.path(id_site, "site", "src", "server.log")}";

View File

@ -1,44 +0,0 @@
import { g } from "utils/global";
import { dbProxy } from "../../../../../web/src/base/load/db/db-proxy";
if (!g.createServer) {
g.server_runtime = {};
g.createServer = (arg) => {
return async (site_id: string) => {
if (!g.server_runtime[site_id]) {
const site = await _db.site.findFirst({
where: {
id: site_id,
},
select: {
config: true,
},
});
if (site?.config && (site.config as any).api_url) {
try {
new URL((site.config as any).api_url);
g.server_runtime[site_id] = {
db: dbProxy((site.config as any).api_url),
api: null as any,
};
} catch (e) {}
}
}
const runtime = g.server_runtime[site_id];
if (runtime) {
const db = runtime.db as any;
if (
!!db._ &&
typeof db._._ === "function" &&
!!arg.db &&
typeof arg.db.___site_id === "function"
) {
arg.db.___site_id(site_id);
}
}
return arg;
};
};
}

View File

@ -8,7 +8,7 @@ import { WSData } from "../../../../../../pkgs/core/server/create";
import { codeBuild } from "./build-code"; import { codeBuild } from "./build-code";
import { prodIndex } from "../../../../util/prod-index"; import { prodIndex } from "../../../../util/prod-index";
import "./server-create"; import "./server-runtime";
const serverMain = () => ({ const serverMain = () => ({
handler: {} as Record<string, PrasiServer>, handler: {} as Record<string, PrasiServer>,
@ -39,6 +39,7 @@ const serverMain = () => ({
); );
delete require.cache[server_src_path]; delete require.cache[server_src_path];
const svr = require(server_src_path); const svr = require(server_src_path);
if (svr && svr.server) { if (svr && svr.server) {
if (typeof svr.server === "function") { if (typeof svr.server === "function") {
this.handler[site_id] = await svr.server(site_id); this.handler[site_id] = await svr.server(site_id);

View File

@ -0,0 +1,27 @@
import { g } from "utils/global";
import { dbProxy } from "../../../../../web/src/base/load/db/db-proxy";
if (!g.createServerRuntime) {
g.server_runtime = {};
g.createServerRuntime = async (site_id: string) => {
if (!g.server_runtime[site_id]) {
const site = await _db.site.findFirst({
where: {
id: site_id,
},
select: {
config: true,
},
});
if (site?.config && (site.config as any).api_url) {
try {
new URL((site.config as any).api_url);
g.server_runtime[site_id] = {
db: dbProxy((site.config as any).api_url),
api: null as any,
};
} catch (e) { }
}
}
};
}

View File

@ -7,32 +7,6 @@ export const dbProxy = (dburl: string) => {
{}, {},
{ {
get(_, table: string) { get(_, table: string) {
if (table === "_tables") {
return () => {
return fetchSendDb(
{
name,
action: "definition",
table: "*",
},
dburl
);
};
}
if (table === "_definition") {
return (table: string) => {
return fetchSendDb(
{
name,
action: "definition",
table,
},
dburl
);
};
}
if (table.startsWith("$")) { if (table.startsWith("$")) {
return (...params: any[]) => { return (...params: any[]) => {
return fetchSendDb( return fetchSendDb(

View File

@ -4,6 +4,7 @@ import axios from "axios";
return `BigInt::` + this.toString(); return `BigInt::` + this.toString();
}; };
let w = (typeof window !== "undefined" ? window : null) as any; let w = (typeof window !== "undefined" ? window : null) as any;
let g = global as any;
export const fetchViaProxy = async ( export const fetchViaProxy = async (
target_url: string, target_url: string,
@ -51,12 +52,11 @@ export const fetchViaProxy = async (
const cur_url = new URL(location.href); const cur_url = new URL(location.href);
let final_url = ""; let final_url = "";
if (to_url.host === cur_url.host) { if (to_url.host === cur_url.host || typeof g.server_hook === 'function') {
final_url = to_url.toString(); final_url = to_url.toString();
} else { } else {
final_url = `${cur_url.protocol}//${ final_url = `${cur_url.protocol}//${cur_url.host
cur_url.host }/_proxy/${encodeURIComponent(to_url.toString())}`;
}/_proxy/${encodeURIComponent(to_url.toString())}`;
} }
if (final_url) { if (final_url) {
@ -74,10 +74,10 @@ export const fetchViaProxy = async (
final_url, final_url,
data data
? { ? {
method: "POST", method: "POST",
body, body,
headers, headers,
} }
: undefined : undefined
); );
const raw = await res.text(); const raw = await res.text();
@ -89,12 +89,27 @@ export const fetchViaProxy = async (
} }
} }
} }
return null; const res = await fetch(
to_url,
data
? {
method: "POST",
body,
headers,
}
: undefined
);
const raw = await res.text();
try {
return JSON.parse(raw, replacer);
} catch (e) {
return raw;
}
}; };
const replacer = (key: string, value: string) => { const replacer = (key: string, value: string) => {
if (typeof value === "string" && value.startsWith("BigInt::")) { if (typeof value === "string" && value.startsWith("BigInt::")) {
return BigInt(value.substring(8)); return BigInt(value.substring(8));
} }
return value; return value;
}; };

View File

@ -35,9 +35,7 @@ export const g = global as unknown as {
db: ReturnType<typeof dbProxy>; db: ReturnType<typeof dbProxy>;
} }
>; >;
createServer: ( createServerRuntime: (site_id: string) => Promise<void>;
arg: PrasiServer & { api: any; db: any }
) => (site_id: string) => Promise<PrasiServer & { api: any; db: any }>;
ws_hook?: WebSocketHandler<WSData>; ws_hook?: WebSocketHandler<WSData>;
_db: PrismaClient; _db: PrismaClient;
dburl: string; dburl: string;