|
1 | 1 | import { URL } from "universal-url"; |
2 | | -import mime from "mime-types"; |
3 | | -import path from "path"; |
4 | 2 |
|
5 | 3 | export type JSONURIFormat = { |
6 | 4 | name: "uri"; |
7 | 5 | contentType?: string; |
8 | 6 | }; |
9 | 7 |
|
| 8 | +function lookupMimeType(ext?: string): string | undefined { |
| 9 | + if (ext === undefined) { |
| 10 | + return undefined; |
| 11 | + } |
| 12 | + |
| 13 | + const extensionToMimeType: Record<string, string> = { |
| 14 | + json: "application/json", |
| 15 | + js: "application/javascript", |
| 16 | + html: "text/html", |
| 17 | + css: "text/css", |
| 18 | + txt: "text/plain", |
| 19 | + ts: "text/typescript", |
| 20 | + tsx: "text/typescript", |
| 21 | + aac: "audio/aac", |
| 22 | + abw: "application/x-abiword", |
| 23 | + arc: "application/x-freearc", |
| 24 | + avi: "video/x-msvideo", |
| 25 | + azw: "application/vnd.amazon.ebook", |
| 26 | + bin: "application/octet-stream", |
| 27 | + bmp: "image/bmp", |
| 28 | + bz: "application/x-bzip", |
| 29 | + bz2: "application/x-bzip2", |
| 30 | + cda: "application/x-cdf", |
| 31 | + csh: "application/x-csh", |
| 32 | + csv: "text/csv", |
| 33 | + doc: "application/msword", |
| 34 | + docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 35 | + eot: "application/vnd.ms-fontobject", |
| 36 | + epub: "application/epub+zip", |
| 37 | + gz: "application/gzip", |
| 38 | + gif: "image/gif", |
| 39 | + htm: "text/html", |
| 40 | + ico: "image/vnd.microsoft.icon", |
| 41 | + ics: "text/calendar", |
| 42 | + jar: "application/java-archive", |
| 43 | + jpeg: "image/jpeg ", |
| 44 | + jpg: "image/jpeg", |
| 45 | + jsonld: "application/ld+json", |
| 46 | + mid: "audio/midi", |
| 47 | + midi: "audio/midi", |
| 48 | + mjs: "text/javascript", |
| 49 | + mp3: "audio/mpeg", |
| 50 | + mp4: "video/mp4", |
| 51 | + mpeg: "video/mpeg", |
| 52 | + mpkg: "application/vnd.apple.installer+xml", |
| 53 | + odp: "application/vnd.oasis.opendocument.presentation", |
| 54 | + ods: "application/vnd.oasis.opendocument.spreadsheet", |
| 55 | + odt: "application/vnd.oasis.opendocument.text", |
| 56 | + oga: "audio/ogg", |
| 57 | + ogv: "video/ogg", |
| 58 | + ogx: "application/ogg", |
| 59 | + opus: "audio/opus", |
| 60 | + otf: "font/otf", |
| 61 | + png: "image/png", |
| 62 | + pdf: "application/pdf", |
| 63 | + php: "application/x-httpd-php", |
| 64 | + ppt: "application/vnd.ms-powerpoint", |
| 65 | + pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 66 | + rar: "application/vnd.rar", |
| 67 | + rtf: "application/rtf", |
| 68 | + sh: "application/x-sh", |
| 69 | + svg: "image/svg+xml", |
| 70 | + swf: "application/x-shockwave-flash", |
| 71 | + tar: "application/x-tar", |
| 72 | + tif: "image/tiff", |
| 73 | + tiff: "image/tiff", |
| 74 | + ttf: "font/ttf", |
| 75 | + vsd: "application/vnd.visio", |
| 76 | + wav: "audio/wav", |
| 77 | + weba: "audio/webm", |
| 78 | + webm: "video/webm", |
| 79 | + webp: "image/webp", |
| 80 | + woff: "font/woff", |
| 81 | + woff2: "font/woff2", |
| 82 | + xhtml: "application/xhtml+xml", |
| 83 | + xls: "application/vnd.ms-excel", |
| 84 | + xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 85 | + xml: "application/xml", |
| 86 | + xul: "application/vnd.mozilla.xul+xml", |
| 87 | + zip: "application/zip", |
| 88 | + "3gp": "video/3gpp", |
| 89 | + "3g2": "video/3gpp2", |
| 90 | + "7z": "application/x-7z-compressed", |
| 91 | + md: "text/markdown", |
| 92 | + }; |
| 93 | + |
| 94 | + return extensionToMimeType[ext]; |
| 95 | +} |
| 96 | + |
10 | 97 | export function inferUri(value: string): JSONURIFormat | undefined { |
11 | 98 | try { |
12 | 99 | const url = new URL(value); |
13 | 100 |
|
14 | 101 | // Get mimetype from extension |
15 | | - const ext = path.extname(url.pathname); |
16 | | - const mimeType = mime.lookup(ext); |
| 102 | + const ext = url.pathname.split(".").pop(); |
| 103 | + const mimeType = lookupMimeType(ext); |
17 | 104 |
|
18 | 105 | return { |
19 | 106 | name: "uri", |
|
0 commit comments