fix main server.js

This commit is contained in:
Rizky 2024-07-05 05:41:58 +07:00
parent b28b0462ff
commit 72e4c71d61
4 changed files with 86 additions and 73 deletions

File diff suppressed because one or more lines are too long

View File

@ -29,7 +29,8 @@ export const initServer = async (
}
code.internal.server[id_site] = {
rebuilding: false, ts: 0,
rebuilding: false,
ts: 0,
ctx: await context({
absWorkingDir: dir.data(root),
entryPoints: ["server.ts"],
@ -50,67 +51,68 @@ export const initServer = async (
// ],
banner: {
js: `\
const _fs = require('node:fs/promises');
const console =
typeof global.server_hook === "function"
? { ...global.console }
: global.console;
let db = new Proxy({}, {
get(_, key) {
const runtime = global.server_runtime["${id_site}"];
if (runtime && runtime.db) {
return runtime.db[key];
const _fs = require('node:fs/promises');
const console =
typeof global.server_hook === "function"
? { ...global.console }
: global.console;
let db = new Proxy({}, {
get(_, key) {
const runtime = global.server_runtime["${id_site}"];
if (runtime && runtime.db) {
return runtime.db[key];
}
}
});
let api = {};
if (typeof global.server_hook === "function") {
const log = global.console.log;
console.log = function (...arg) {
const out = "${code.path(id_site, "site", "src", "server.log")}";
_fs.appendFile(out, arg.map((e)=>{
const ancestors = [];
if (typeof e === 'object') return JSON.stringify(e, function (key, val) {
if (val) {
if (typeof val === 'function') {
return '[function]';
}
if (typeof val === 'object') {
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop();
}
if (ancestors.includes(val)) {
return "[circular]";
}
ancestors.push(val);
if (val.constructor &&
!['Object', 'Array'].includes(val.constructor.name)) {
if (val.constructor.name === 'Error') {
return '[Error] ' + val.message;
}
return '[Class] ' + val.constructor.name;
}
}
}
});
let api = {};
if (typeof global.server_hook === "function") {
const log = global.console.log;
console.log = function (...arg) {
const out = "${code.path(id_site, "site", "src", "server.log")}";
_fs.appendFile(out, arg.map((e)=>{
const ancestors = [];
if (typeof e === 'object') return JSON.stringify(e, function (key, val) {
if (val) {
if (typeof val === 'function') {
return '[function]';
}
if (typeof val === 'object') {
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop();
}
if (ancestors.includes(val)) {
return "[circular]";
}
ancestors.push(val);
if (val.constructor &&
!['Object', 'Array'].includes(val.constructor.name)) {
if (val.constructor.name === 'Error') {
return '[Error] ' + val.message;
}
return '[Class] ' + val.constructor.name;
}
}
}
return val;
}, 2);
return e;
}).join(" ") + "\\n");
}.bind(console);
} else {
db = global.db;
api = global.api;
}`,
return val;
}, 2);
return e;
}).join(" ") + "\\n");
}.bind(console);
} else {
db = global.db;
api = global.api;
}
`,
},
})
}),
};
code.internal.server[id_site].rebuilding = true;
try {
await code.internal.server[id_site].ctx.rebuild();
await server.init(id_site);
} catch (e) { }
} catch (e) {}
code.internal.server[id_site].rebuilding = false;
};

View File

@ -57,13 +57,14 @@ const serverMain = () => ({
this.handler[site_id] = svr.server;
svr.server.site_id = site_id;
if (typeof svr.server.init === "function") {
await svr.server.init({});
}
Bun.write(
Bun.file(code.path(site_id, "site", "src", "server.log")),
""
);
if (typeof svr.server.init === "function") {
await svr.server.init({});
}
} else {
const file = await Bun.file(server_src_path).text();
const log_path = code.path(site_id, "site", "src", "server.log");

View File

@ -175,9 +175,14 @@ export const fetchSendDb = async (params: any, dburl: string) => {
let result = await cachedQueryResult[hsum].promise;
cached = cachedQueryResult[hsum];
try {
result = JSON.parse(result);
cachedQueryResult[hsum].result = result;
return result;
if (typeof result === "string") {
result = JSON.parse(result);
cachedQueryResult[hsum].result = result;
return result;
} else {
cachedQueryResult[hsum].result = result;
return result;
}
} catch (e) {
console.error("DBQuery failed:", result);
}
@ -188,7 +193,12 @@ export const fetchSendDb = async (params: any, dburl: string) => {
let result = await cached.promise;
if (result) {
try {
return JSON.parse(result);
if (typeof result === "string") {
result = JSON.parse(result);
return result;
} else {
return result;
}
} catch (e) {
console.error("DBQuery failed:", result);
}