wip fix
This commit is contained in:
parent
287a096fd3
commit
f011fab5ec
|
|
@ -1,14 +1,7 @@
|
||||||
import { readAsync } from "fs-jetpack";
|
|
||||||
import { apiContext } from "service-srv";
|
import { apiContext } from "service-srv";
|
||||||
import { SinglePage, g } from "utils/global";
|
import { SinglePage, g } from "utils/global";
|
||||||
import { dir } from "utils/dir";
|
|
||||||
import { gzipAsync } from "utils/gzip";
|
import { gzipAsync } from "utils/gzip";
|
||||||
|
import { getContent } from "../server/prep-api-ts";
|
||||||
const generated = {
|
|
||||||
"load.json": "",
|
|
||||||
"load.js.dev": "",
|
|
||||||
"load.js.prod": "",
|
|
||||||
};
|
|
||||||
|
|
||||||
export const _ = {
|
export const _ = {
|
||||||
url: "/_prasi/**",
|
url: "/_prasi/**",
|
||||||
|
|
@ -137,108 +130,6 @@ export const _ = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getApiEntry = () => {
|
|
||||||
const res: any = {};
|
|
||||||
for (const [k, v] of Object.entries(g.api)) {
|
|
||||||
const name = k.substring(0, k.length - 3);
|
|
||||||
res[name] = { ...v, name, path: `app/srv/api/${v.path}` };
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
|
||||||
`\
|
|
||||||
declare module "gen/srv/api/entry" {
|
|
||||||
export * as srv from "gen/srv/api/srv";
|
|
||||||
}
|
|
||||||
|
|
||||||
` +
|
|
||||||
((await readAsync(dir("app/srv/exports.d.ts"))) || "")
|
|
||||||
.replace(/\"app\/srv\/api/gi, '"srv/api')
|
|
||||||
.replace(
|
|
||||||
'declare module "app/srv/exports"',
|
|
||||||
'declare module "gen/srv/api/srv"'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const getPrisma = async (path: string) => {
|
|
||||||
if (path === "prisma")
|
|
||||||
return JSON.stringify(
|
|
||||||
(
|
|
||||||
(await readAsync(dir("node_modules/.prisma/client/index.d.ts"))) || ""
|
|
||||||
).replace(`@prisma/client/runtime/library`, `./runtime/library`)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (path === "runtime")
|
|
||||||
return JSON.stringify(
|
|
||||||
await readAsync(
|
|
||||||
dir("node_modules/@prisma/client/runtime/index-browser.d.ts")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (path === "library")
|
|
||||||
return JSON.stringify(
|
|
||||||
await readAsync(dir("node_modules/@prisma/client/runtime/library.d.ts"))
|
|
||||||
);
|
|
||||||
|
|
||||||
return JSON.stringify({});
|
|
||||||
};
|
|
||||||
|
|
||||||
const responseCompressed = async (req: Request, body: string) => {
|
const responseCompressed = async (req: Request, body: string) => {
|
||||||
if (req.headers.get("accept-encoding")?.includes("gz")) {
|
if (req.headers.get("accept-encoding")?.includes("gz")) {
|
||||||
return new Response(await gzipAsync(body), {
|
return new Response(await gzipAsync(body), {
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ export const createResponse = (
|
||||||
const headers = {} as Record<string, string>;
|
const headers = {} as Record<string, string>;
|
||||||
if (cache_accept) {
|
if (cache_accept) {
|
||||||
const content_hash = simpleHash(content);
|
const content_hash = simpleHash(content);
|
||||||
|
|
||||||
if (cache_accept.toLowerCase().includes("br")) {
|
if (cache_accept.toLowerCase().includes("br")) {
|
||||||
if (g.cache.br[content_hash]) {
|
if (g.cache.br[content_hash]) {
|
||||||
content = g.cache.br[content_hash];
|
content = g.cache.br[content_hash];
|
||||||
|
|
@ -65,6 +66,13 @@ export const createResponse = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (!headers["content-encoding"]) {
|
||||||
|
// if (cache_accept.toLowerCase().includes("gz")) {
|
||||||
|
// headers["content-encoding"] = "gzip";
|
||||||
|
// content = gzipSync(content);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = new Response(
|
let res = new Response(
|
||||||
|
|
|
||||||
|
|
@ -52,4 +52,120 @@ export const ${name} = {
|
||||||
res = res.replace(`db: PrismaClient;`, "");
|
res = res.replace(`db: PrismaClient;`, "");
|
||||||
await Bun.write(targetFile, res);
|
await Bun.write(targetFile, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await getContent("load.js.dev");
|
||||||
|
console.log("API Loaded");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getApiEntry = () => {
|
||||||
|
const res: any = {};
|
||||||
|
for (const [k, v] of Object.entries(g.api)) {
|
||||||
|
const name = k.substring(0, k.length - 3);
|
||||||
|
res[name] = { ...v, name, path: `app/srv/api/${v.path}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getContent = async (
|
||||||
|
type: keyof typeof g.api_gen,
|
||||||
|
url?: string
|
||||||
|
) => {
|
||||||
|
if (!g.api_gen) {
|
||||||
|
g.api_gen = {
|
||||||
|
"load.js.dev": "",
|
||||||
|
"load.js.prod": "",
|
||||||
|
"load.json": "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === "load.json") {
|
||||||
|
if (!g.api_gen[type])
|
||||||
|
g.api_gen[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 (!g.api_gen[type])
|
||||||
|
g.api_gen[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 (!g.api_gen[type])
|
||||||
|
g.api_gen[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 g.api_gen[type];
|
||||||
|
};
|
||||||
|
|
||||||
|
const getApiTypes = async () => {
|
||||||
|
return (
|
||||||
|
`\
|
||||||
|
declare module "gen/srv/api/entry" {
|
||||||
|
export * as srv from "gen/srv/api/srv";
|
||||||
|
}
|
||||||
|
|
||||||
|
` +
|
||||||
|
((await readAsync(dir("app/srv/exports.d.ts"))) || "")
|
||||||
|
.replace(/\"app\/srv\/api/gi, '"srv/api')
|
||||||
|
.replace(
|
||||||
|
'declare module "app/srv/exports"',
|
||||||
|
'declare module "gen/srv/api/srv"'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getPrisma = async (path: string) => {
|
||||||
|
if (path === "prisma")
|
||||||
|
return JSON.stringify(
|
||||||
|
(
|
||||||
|
(await readAsync(dir("node_modules/.prisma/client/index.d.ts"))) || ""
|
||||||
|
).replace(`@prisma/client/runtime/library`, `./runtime/library`)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (path === "runtime")
|
||||||
|
return JSON.stringify(
|
||||||
|
await readAsync(
|
||||||
|
dir("node_modules/@prisma/client/runtime/index-browser.d.ts")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (path === "library")
|
||||||
|
return JSON.stringify(
|
||||||
|
await readAsync(dir("node_modules/@prisma/client/runtime/library.d.ts"))
|
||||||
|
);
|
||||||
|
|
||||||
|
return JSON.stringify({});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,11 @@ export const g = global as unknown as {
|
||||||
db: Database;
|
db: Database;
|
||||||
};
|
};
|
||||||
api: Record<string, SingleRoute>;
|
api: Record<string, SingleRoute>;
|
||||||
|
api_gen: {
|
||||||
|
"load.json": string;
|
||||||
|
"load.js.dev": string;
|
||||||
|
"load.js.prod": string;
|
||||||
|
};
|
||||||
web: {
|
web: {
|
||||||
site_id: string;
|
site_id: string;
|
||||||
current: number;
|
current: number;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue