fixing hex

This commit is contained in:
Rizky
2024-07-29 17:58:55 +07:00
parent fad1e6ffc1
commit 1f15dbe2bb
8 changed files with 330 additions and 30 deletions
+15 -7
View File
@@ -1,6 +1,8 @@
import { simpleHash } from "utils/cache";
import { g } from "utils/global";
import { loadCachedBr } from "utils/br-load";
import { binaryExtensions } from "./binary-ext";
import mime from "mime";
const parseQueryParams = (ctx: any) => {
const pageHref = ctx.req.url;
@@ -60,7 +62,13 @@ export const createResponse = (
const status =
typeof opt?.res?._status === "number" ? opt?.res?._status : undefined;
let content: any = typeof body === "string" ? body : JSON.stringify(body);
const content_type = opt?.headers?.["content-type"];
const is_binary = binaryExtensions.includes(
mime.getExtension(content_type) || ""
);
let content: any =
typeof body === "string" || is_binary ? body : JSON.stringify(body);
const headers = { ...(opt?.headers || {}) } as Record<string, string>;
if (opt?.cache_accept) {
@@ -71,10 +79,6 @@ export const createResponse = (
) {
const content_hash = simpleHash(content);
if (!g.cache.br[content_hash]) {
loadCachedBr(content_hash, content);
}
if (g.cache.br[content_hash]) {
cached = true;
content = g.cache.br[content_hash];
@@ -85,11 +89,9 @@ export const createResponse = (
if (opt?.high_compression) {
if (!cached && opt.cache_accept.toLowerCase().includes("gz")) {
const content_hash = simpleHash(content);
if (!g.cache.gz[content_hash]) {
g.cache.gz[content_hash] = Bun.gzipSync(content);
}
if (g.cache.gz[content_hash]) {
cached = true;
content = g.cache.gz[content_hash];
@@ -111,6 +113,12 @@ export const createResponse = (
: 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)) {
res.headers.append(k, v);
}
+263
View File
@@ -0,0 +1,263 @@
export const binaryExtensions = [
"3dm",
"3ds",
"3g2",
"3gp",
"7z",
"a",
"aac",
"adp",
"afdesign",
"afphoto",
"afpub",
"ai",
"aif",
"aiff",
"alz",
"ape",
"apk",
"appimage",
"ar",
"arj",
"asf",
"au",
"avi",
"bak",
"baml",
"bh",
"bin",
"bk",
"bmp",
"btif",
"bz2",
"bzip2",
"cab",
"caf",
"cgm",
"class",
"cmx",
"cpio",
"cr2",
"cur",
"dat",
"dcm",
"deb",
"dex",
"djvu",
"dll",
"dmg",
"dng",
"doc",
"docm",
"docx",
"dot",
"dotm",
"dra",
"DS_Store",
"dsk",
"dts",
"dtshd",
"dvb",
"dwg",
"dxf",
"ecelp4800",
"ecelp7470",
"ecelp9600",
"egg",
"eol",
"eot",
"epub",
"exe",
"f4v",
"fbs",
"fh",
"fla",
"flac",
"flatpak",
"fli",
"flv",
"fpx",
"fst",
"fvt",
"g3",
"gh",
"gif",
"graffle",
"gz",
"gzip",
"h261",
"h263",
"h264",
"icns",
"ico",
"ief",
"img",
"ipa",
"iso",
"jar",
"jpeg",
"jpg",
"jpgv",
"jpm",
"jxr",
"key",
"ktx",
"lha",
"lib",
"lvp",
"lz",
"lzh",
"lzma",
"lzo",
"m3u",
"m4a",
"m4v",
"mar",
"mdi",
"mht",
"mid",
"midi",
"mj2",
"mka",
"mkv",
"mmr",
"mng",
"mobi",
"mov",
"movie",
"mp3",
"mp4",
"mp4a",
"mpeg",
"mpg",
"mpga",
"mxu",
"nef",
"npx",
"numbers",
"nupkg",
"o",
"odp",
"ods",
"odt",
"oga",
"ogg",
"ogv",
"otf",
"ott",
"pages",
"pbm",
"pcx",
"pdb",
"pdf",
"pea",
"pgm",
"pic",
"png",
"pnm",
"pot",
"potm",
"potx",
"ppa",
"ppam",
"ppm",
"pps",
"ppsm",
"ppsx",
"ppt",
"pptm",
"pptx",
"psd",
"pya",
"pyc",
"pyo",
"pyv",
"qt",
"rar",
"ras",
"raw",
"resources",
"rgb",
"rip",
"rlc",
"rmf",
"rmvb",
"rpm",
"rtf",
"rz",
"s3m",
"s7z",
"scpt",
"sgi",
"shar",
"snap",
"sil",
"sketch",
"slk",
"smv",
"snk",
"so",
"stl",
"suo",
"sub",
"swf",
"tar",
"tbz",
"tbz2",
"tga",
"tgz",
"thmx",
"tif",
"tiff",
"tlz",
"ttc",
"ttf",
"txz",
"udf",
"uvh",
"uvi",
"uvm",
"uvp",
"uvs",
"uvu",
"viv",
"vob",
"war",
"wav",
"wax",
"wbmp",
"wdp",
"weba",
"webm",
"webp",
"whl",
"wim",
"wm",
"wma",
"wmv",
"wmx",
"woff",
"woff2",
"wrm",
"wvx",
"xbm",
"xif",
"xla",
"xlam",
"xls",
"xlsb",
"xlsm",
"xlsx",
"xlt",
"xltm",
"xltx",
"xm",
"xmind",
"xpi",
"xpm",
"xwd",
"xz",
"z",
"zip",
"zipx",
];
+2 -1
View File
@@ -7,9 +7,10 @@ export const serveWeb = async (arg: {
cache_accept: string;
}) => {
const type = mime.getType(arg.pathname);
return createResponse(arg.content, {
cache_accept: arg.cache_accept,
high_compression: true,
high_compression: false,
headers: !type ? undefined : { "content-type": type },
});
};