1/**
2 * a small mime type library.
3 * @module
4 */
5
6/**
7 * retrieves the 'Content-Type' header to use given a file extension.
8 * accepts:
9 * - full file path or basename
10 * - extension (with or without dot)
11 */
12function contentTypeFor(file: string): string {
13 const slash = file.indexOf("/");
14 if (slash !== -1) file = file.slice(slash + 1);
15 const dot = file.lastIndexOf(".");
16 if (dot === -1) file = "." + file;
17 else if (dot > 0) {
18 let entry = fullNames.get(file);
19 if (entry) return entry;
20 file = file.slice(dot);
21 }
22 return extensions.get(file) ?? "application/octet-stream";
23}
24export { contentTypeFor as for };
25
26// special-cased file names
27const fullNames = new Map([
28 ["rss.xml", "application/rss+xml"],
29]);
30
31// this string literal, when minified, is shorter than key-value pairs.
32const extensions = new Map(
33 ( // deno-fmt-ignore
34 ".aac" + "#" + "audio/x-aac" + "\n"
35 + ".aif" + "#" + "audio/x-aiff" + "\n"
36 + ".aifc" + "#" + "audio/x-aiff" + "\n"
37 + ".aiff" + "#" + "audio/x-aiff" + "\n"
38 + ".asm" + "#" + "text/x-asm" + "\n"
39 + ".avi" + "#" + "video/x-msvideo" + "\n"
40 + ".avif" + "#" + "image/avif" + "\n"
41 + ".bat" + "#" + "application/x-msdownload" + "\n"
42 + ".c" + "#" + "text/x-c" + "\n"
43 + ".chat" + "#" + "text/x-clover-chatlog" + "\n"
44 + ".class" + "#" + "application/java-vm" + "\n"
45 + ".cmd" + "#" + "application/x-msdownload" + "\n"
46 + ".com" + "#" + "application/x-msdownload" + "\n"
47 + ".conf" + "#" + "text/plain" + "\n"
48 + ".cpp" + "#" + "text/x-c" + "\n"
49 + ".css" + "#" + "text/css" + "\n"
50 + ".csv" + "#" + "text/csv" + "\n"
51 + ".cxx" + "#" + "text/x-c" + "\n"
52 + ".def" + "#" + "text/plain" + "\n"
53 + ".diff" + "#" + "text/plain" + "\n"
54 + ".dll" + "#" + "application/x-msdownload" + "\n"
55 + ".dmg" + "#" + "application/octet-stream" + "\n"
56 + ".doc" + "#" + "application/msword" + "\n"
57 + ".docx" + "#" + "application/vnd.openxmlformats-officedocument.wordprocessingml.document" + "\n"
58 + ".eot" + "#" + "application/vnd.ms-fontobject" + "\n"
59 + ".epub" + "#" + "application/epub+zip" + "\n"
60 + ".exe" + "#" + "application/x-msdownload" + "\n"
61 + ".flv" + "#" + "video/x-flv" + "\n"
62 + ".fbx" + "#" + "application/fbx" + "\n"
63 + ".gz" + "#" + "application/x-gzip" + "\n"
64 + ".h" + "#" + "text/x-c" + "\n"
65 + ".h264" + "#" + "video/h264" + "\n"
66 + ".hh" + "#" + "text/x-c" + "\n"
67 + ".htm" + "#" + "text/html;charset=utf-8" + "\n"
68 + ".html" + "#" + "text/html;charset=utf-8" + "\n"
69 + ".ico" + "#" + "image/x-icon" + "\n"
70 + ".ics" + "#" + "text/calendar" + "\n"
71 + ".in" + "#" + "text/plain" + "\n"
72 + ".jar" + "#" + "application/java-archive" + "\n"
73 + ".java" + "#" + "text/x-java-source" + "\n"
74 + ".jpeg" + "#" + "image/jpeg" + "\n"
75 + ".jpg" + "#" + "image/jpeg" + "\n"
76 + ".jpgv" + "#" + "video/jpeg" + "\n"
77 + ".jxl" + "#" + "image/jxl" + "\n"
78 + ".js" + "#" + "application/javascript;charset=utf-8" + "\n"
79 + ".json" + "#" + "application/json" + "\n"
80 + ".latex" + "#" + "application/x-latex" + "\n"
81 + ".list" + "#" + "text/plain" + "\n"
82 + ".log" + "#" + "text/plain" + "\n"
83 + ".m4a" + "#" + "audio/mp4" + "\n"
84 + ".man" + "#" + "text/troff" + "\n"
85 + ".mid" + "#" + "audio/midi" + "\n"
86 + ".midi" + "#" + "audio/midi" + "\n"
87 + ".mov" + "#" + "video/quicktime" + "\n"
88 + ".mp3" + "#" + "audio/mpeg" + "\n"
89 + ".mp4" + "#" + "video/mp4" + "\n"
90 + ".msh" + "#" + "model/mesh" + "\n"
91 + ".msi" + "#" + "application/x-msdownload" + "\n"
92 + ".obj" + "#" + "application/octet-stream" + "\n"
93 + ".ogg" + "#" + "audio/ogg" + "\n"
94 + ".otf" + "#" + "application/x-font-otf" + "\n"
95 + ".pdf" + "#" + "application/pdf" + "\n"
96 + ".png" + "#" + "image/png" + "\n"
97 + ".ppt" + "#" + "application/vnd.ms-powerpoint" + "\n"
98 + ".pptx" + "#" + "application/vnd.openxmlformats-officedocument.presentationml.presentation" + "\n"
99 + ".psd" + "#" + "image/vnd.adobe.photoshop" + "\n"
100 + ".py" + "#" + "text/x-python" + "\n"
101 + ".rar" + "#" + "application/x-rar-compressed" + "\n"
102 + ".rss" + "#" + "application/rss+xml" + "\n"
103 + ".rtf" + "#" + "application/rtf" + "\n"
104 + ".rtx" + "#" + "text/richtext" + "\n"
105 + ".s" + "#" + "text/x-asm" + "\n"
106 + ".pem" + "#" + "application/x-pem-file\"" + "\n"
107 + ".ser" + "#" + "application/java-serialized-object" + "\n"
108 + ".sh" + "#" + "application/x-sh" + "\n"
109 + ".sig" + "#" + "application/pgp-signature" + "\n"
110 + ".silo" + "#" + "model/mesh" + "\n"
111 + ".svg" + "#" + "image/svg+xml" + "\n"
112 + ".t" + "#" + "text/troff" + "\n"
113 + ".tar" + "#" + "application/x-tar" + "\n"
114 + ".text" + "#" + "text/plain" + "\n"
115 + ".tgz" + "#" + "application/x-gzip" + "\n"
116 + ".tif" + "#" + "image/tiff" + "\n"
117 + ".tiff" + "#" + "image/tiff" + "\n"
118 + ".torrent" + "#" + "application/x-bittorrent" + "\n"
119 + ".ttc" + "#" + "application/x-font-ttf" + "\n"
120 + ".ttf" + "#" + "application/x-font-ttf" + "\n"
121 + ".txt" + "#" + "text/plain" + "\n"
122 + ".urls" + "#" + "text/uri-list" + "\n"
123 + ".v" + "#" + "text/x-v" + "\n"
124 + ".wav" + "#" + "audio/x-wav" + "\n"
125 + ".wmv" + "#" + "video/x-ms-wmv" + "\n"
126 + ".woff2" + "#" + "font/woff2" + "\n"
127 + ".xls" + "#" + "application/vnd.ms-excel" + "\n"
128 + ".xlsx" + "#" + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + "\n"
129 + ".xml" + "#" + "application/xml" + "\n"
130 + ".xps" + "#" + "application/vnd.ms-xpsdocument" + "\n"
131 + +""
132 ).slice(0, -1).split("\n").map((line) => line.split("#") as [string, string]),
133);