aboutsummaryrefslogtreecommitdiff
path: root/misctools
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-24 22:29:05 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-24 22:29:05 -0700
commitbe5789fe01eb510ba56c101e2cef82b37dc2ad07 (patch)
tree01ba5abbb80a289648d6952f542ee89ecf53b298 /misctools
parent120b2670da71f65e37b57b0137e38e06f1b55ada (diff)
downloadbun-be5789fe01eb510ba56c101e2cef82b37dc2ad07.tar.gz
bun-be5789fe01eb510ba56c101e2cef82b37dc2ad07.tar.zst
bun-be5789fe01eb510ba56c101e2cef82b37dc2ad07.zip
Auto-detect MimeType based on file extension
Diffstat (limited to 'misctools')
-rw-r--r--misctools/mime.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/misctools/mime.js b/misctools/mime.js
new file mode 100644
index 000000000..74f9773e7
--- /dev/null
+++ b/misctools/mime.js
@@ -0,0 +1,57 @@
+const json = await (
+ await fetch("https://raw.githubusercontent.com/jshttp/mime-db/master/db.json")
+).json();
+
+json["application/javascript"].extensions.push(
+ `ts`,
+ `tsx`,
+ `mts`,
+ `mtsx`,
+ `cts`,
+ `cjs`,
+ `mjs`,
+ `js`
+);
+
+delete json["application/node"];
+delete json["application/deno"];
+delete json["application/wasm"];
+
+var categories = new Set();
+var all = "pub const all = struct {";
+for (let key of Object.keys(json).sort()) {
+ const [category] = key.split("/");
+ categories.add(category);
+ all += `pub const @"${key}": MimeType = MimeType{.category = .@"${category}", .value = "${key}"};\n`;
+}
+
+const withExtensions = [
+ ...new Set([
+ ...Object.keys(json)
+ .filter((key) => {
+ return !!json[key]?.extensions?.length;
+ })
+ .flatMap((mime) => {
+ return [...new Set([...json[mime].extensions])].map((ext) => {
+ return [`.{.@"${ext}", all.@"${mime}"}`];
+ });
+ })
+ .sort(),
+ ]),
+];
+
+all += "\n";
+
+all += ` pub const extensions = ComptimeStringMap(MimeType, .{
+${withExtensions.join(",\n")},
+});
+};`;
+
+all += "\n";
+
+// all += `pub const Category = enum {
+// ${[...categories].map((a) => `@"${a}"`).join(", \n")}
+// };
+// `;
+
+console.log(all);