This commit is contained in:
Rizky
2024-02-10 15:58:04 +07:00
parent 18d389b73e
commit aa6cdbf539
9 changed files with 127 additions and 251 deletions
+2 -36
View File
@@ -26,26 +26,10 @@ export const _ = {
) {
const { res } = apiContext(this);
if (!g.web[action.id_site]) {
g.web[action.id_site] = {
current: 0,
domains: [],
deploying: null,
router: null,
deploys: [],
site_id: action.id_site,
cacheKey: 0,
cache: null,
};
}
const path = dir(`app/web/${action.id_site}`);
const path = dir(`app/web/`);
await dirAsync(path);
const web = g.web[action.id_site];
if (!web.domains) {
web.domains = [];
}
const web = g.web;
switch (action.type) {
case "check":
@@ -53,7 +37,6 @@ export const _ = {
now: Date.now(),
current: web.current,
deploys: web.deploys,
domains: web.domains,
db: {
url: g.dburl || "-",
},
@@ -100,23 +83,6 @@ datasource db {
}, 300);
}
break;
case "domain-add":
{
web.domains.push(action.domain);
await Bun.write(`${path}/domains.json`, JSON.stringify(web.domains));
g.domains = null;
res.send("ok");
}
break;
case "domain-del":
{
web.domains = web.domains.filter((e) => e !== action.domain);
await Bun.write(`${path}/domains.json`, web.domains);
g.domains = null;
res.send("ok");
}
break;
case "deploy-del":
{
web.deploys = web.deploys.filter((e) => e !== parseInt(action.ts));
+66 -50
View File
@@ -3,9 +3,10 @@ import { apiContext } from "service-srv";
import { g } from "utils/global";
import { dir } from "utils/dir";
const cache = {
dev: "",
prod: "",
const generated = {
"load.json": "",
"load.js.dev": "",
"load.js.prod": "",
};
export const _ = {
@@ -21,15 +22,8 @@ export const _ = {
"load.json": async () => {
res.setHeader("content-type", "application/json");
res.send(
JSON.stringify({
apiEntry: getApiEntry(),
apiTypes: (await getApiTypes()) || "",
prismaTypes: {
"prisma.d.ts": await getPrisma("prisma"),
"runtime/index.d.ts": await getPrisma("runtime"),
"runtime/library.d.ts": await getPrisma("library"),
},
})
await getContent("load.json"),
req.headers.get("accept-encoding") || ""
);
},
"load.js": async () => {
@@ -39,46 +33,16 @@ export const _ = {
? JSON.stringify(req.query_parameters["url"])
: "undefined";
if (!cache.dev) {
cache.dev = `\
(() => {
const baseurl = new URL(location.href);
baseurl.pathname = '';
const url = ${url} || baseurl.toString();
const w = window;
if (!w.prasiApi) {
w.prasiApi = {};
}
w.prasiApi[url] = {
apiEntry: ${JSON.stringify(getApiEntry())},
apiTypes: ${JSON.stringify((await getApiTypes()) || "")},
prismaTypes: {
"prisma.d.ts": ${await getPrisma("prisma")},
"runtime/index.d.ts": ${await getPrisma("runtime")},
"runtime/library.d.ts": ${await getPrisma("library")},
},
};
})();`;
cache.prod = `\
(() => {
const baseurl = new URL(location.href);
baseurl.pathname = '';
const url = ${url} || baseurl.toString();
const w = window;
if (!w.prasiApi) {
w.prasiApi = {};
}
w.prasiApi[url] = {
apiEntry: ${JSON.stringify(getApiEntry())},
};
})();`;
}
if (req.query_parameters["dev"]) {
res.send(cache.dev);
res.send(
await getContent("load.js.dev", url),
req.headers.get("accept-encoding") || ""
);
} else {
res.send(cache.prod);
res.send(
await getContent("load.js.prod", url),
req.headers.get("accept-encoding") || ""
);
}
},
};
@@ -102,6 +66,58 @@ export const getApiEntry = () => {
return res;
};
const getContent = async (type: keyof typeof generated, url?: string) => {
if (type === "load.json") {
if (!generated[type])
generated[type] = JSON.stringify({
apiEntry: getApiEntry(),
apiTypes: (await getApiTypes()) || "",
prismaTypes: {
"prisma.d.ts": await getPrisma("prisma"),
"runtime/index.d.ts": await getPrisma("runtime"),
"runtime/library.d.ts": await getPrisma("library"),
},
});
} else if (type === "load.js.dev") {
if (!generated[type])
generated[type] = `\
(() => {
const baseurl = new URL(location.href);
baseurl.pathname = '';
const url = ${url} || baseurl.toString();
const w = window;
if (!w.prasiApi) {
w.prasiApi = {};
}
w.prasiApi[url] = {
apiEntry: ${JSON.stringify(getApiEntry())},
apiTypes: ${JSON.stringify((await getApiTypes()) || "")},
prismaTypes: {
"prisma.d.ts": ${await getPrisma("prisma")},
"runtime/index.d.ts": ${await getPrisma("runtime")},
"runtime/library.d.ts": ${await getPrisma("library")},
},
};
})();`;
} else if (type === "load.js.prod") {
if (!generated[type])
generated[type] = `\
(() => {
const baseurl = new URL(location.href);
baseurl.pathname = '';
const url = ${url} || baseurl.toString();
const w = window;
if (!w.prasiApi) {
w.prasiApi = {};
}
w.prasiApi[url] = {
apiEntry: ${JSON.stringify(getApiEntry())},
});
})();`;
}
return generated[type];
};
const getApiTypes = async () => {
return (
`\