This commit is contained in:
Rizky 2024-12-11 13:18:21 +07:00
parent e93779ebf7
commit 4687513434
3 changed files with 32 additions and 11 deletions

View File

@ -57,6 +57,10 @@ export const createResponse = (
headers?: any; headers?: any;
res?: any; res?: any;
high_compression?: boolean; high_compression?: boolean;
rewrite?: (arg: {
body: Bun.BodyInit;
headers: Headers | any;
}) => Bun.BodyInit;
} }
) => { ) => {
const status = const status =
@ -66,11 +70,18 @@ export const createResponse = (
const is_binary = binaryExtensions.includes( const is_binary = binaryExtensions.includes(
mime.getExtension(content_type) || "" mime.getExtension(content_type) || ""
); );
let content: any =
typeof body === "string" || is_binary ? body : JSON.stringify(body);
const headers = { ...(opt?.headers || {}) } as Record<string, string>; const headers = { ...(opt?.headers || {}) } as Record<string, string>;
let pre_content = body;
if (opt?.rewrite) {
pre_content = opt.rewrite({ body: pre_content, headers });
}
let content: any =
typeof pre_content === "string" || is_binary
? pre_content
: JSON.stringify(pre_content);
if (opt?.cache_accept) { if (opt?.cache_accept) {
let cached = false; let cached = false;
@ -116,12 +127,6 @@ export const createResponse = (
: undefined : undefined
); );
if (opt?.headers?.["content-type"]?.includes("woff")) {
new Response(body, {
headers: { "content-type": headers["content-type"] },
});
}
for (const [k, v] of Object.entries(headers)) { for (const [k, v] of Object.entries(headers)) {
res.headers.append(k, v); res.headers.append(k, v);
} }

View File

@ -79,11 +79,18 @@ export const createServer = async () => {
const url = new URL(req.url) as URL; const url = new URL(req.url) as URL;
url.pathname = url.pathname.replace(/\/+/g, "/"); url.pathname = url.pathname.replace(/\/+/g, "/");
const prasi = {}; const prasi = {};
const index = prodIndex(g.deploy.config.site_id, prasi); const index = prodIndex(g.deploy.config.site_id, prasi);
const handle = async (req: Request) => { const handle = async (
req: Request,
opt?: {
rewrite?: (arg: {
body: Bun.BodyInit;
headers: Headers | any;
}) => Bun.BodyInit;
}
) => {
const api = await serveAPI(url, req); const api = await serveAPI(url, req);
if (api) { if (api) {
@ -97,6 +104,7 @@ export const createServer = async () => {
content: index.render(), content: index.render(),
pathname: "index.html", pathname: "index.html",
cache_accept: req.headers.get("accept-encoding") || "", cache_accept: req.headers.get("accept-encoding") || "",
opt,
}); });
} }
@ -117,6 +125,7 @@ export const createServer = async () => {
content: index.render(), content: index.render(),
pathname: "index.html", pathname: "index.html",
cache_accept: req.headers.get("accept-encoding") || "", cache_accept: req.headers.get("accept-encoding") || "",
opt,
}); });
} }
@ -131,6 +140,7 @@ export const createServer = async () => {
content, content,
pathname, pathname,
cache_accept: req.headers.get("accept-encoding") || "", cache_accept: req.headers.get("accept-encoding") || "",
opt,
}); });
} }
} }

View File

@ -5,6 +5,12 @@ export const serveWeb = async (arg: {
pathname: string; pathname: string;
content: string; content: string;
cache_accept: string; cache_accept: string;
opt?: {
rewrite?: (arg: {
body: Bun.BodyInit;
headers: Headers | any;
}) => Bun.BodyInit;
};
}) => { }) => {
const type = mime.getType(arg.pathname); const type = mime.getType(arg.pathname);