1 | <div align="center">
|
2 | <img src="logo.png" alt="mrmime" width="320" />
|
3 | </div>
|
4 |
|
5 | <div align="center">
|
6 | <a href="https://npmjs.org/package/mrmime">
|
7 | <img src="https://badgen.now.sh/npm/v/mrmime" alt="version" />
|
8 | </a>
|
9 | <a href="https://github.com/lukeed/mrmime/actions">
|
10 | <img src="https://github.com/lukeed/mrmime/workflows/CI/badge.svg" alt="CI" />
|
11 | </a>
|
12 | <a href="https://npmjs.org/package/mrmime">
|
13 | <img src="https://badgen.now.sh/npm/dm/mrmime" alt="downloads" />
|
14 | </a>
|
15 | <a href="https://packagephobia.now.sh/result?p=mrmime">
|
16 | <img src="https://packagephobia.now.sh/badge?p=mrmime" alt="install size" />
|
17 | </a>
|
18 | </div>
|
19 |
|
20 | <div align="center">
|
21 | A tiny (2.8kB) and fast utility for getting a MIME type from an extension or filename
|
22 | </div>
|
23 |
|
24 |
|
25 | ## Features
|
26 |
|
27 | * Lightweight – 2.8kB gzip<br>
|
28 | _Only includes standard mime types; all experimental and vendor-specific mimetypes removed._
|
29 |
|
30 | * [Performant](#benchmarks)<br>
|
31 | _All lookups are O(1) with minimal processing._
|
32 |
|
33 | * Comprehensive Dictionary<br>
|
34 | _Generated from [`mime-db`](https://github.com/jshttp/mime-db), which aggregates the IANA, NGINX, and Apache datasets._
|
35 |
|
36 | * Customizable<br>
|
37 | _Exposes the `mimes` dictionary for easy additions or overrides._
|
38 |
|
39 | * Supports Native ESM and [Deno](https://deno.land/x/mrmime)<br>
|
40 | _Ships with CommonJS and ESM support!_
|
41 |
|
42 |
|
43 | ## Install
|
44 |
|
45 | ```
|
46 | $ npm install --save mrmime
|
47 | ```
|
48 |
|
49 |
|
50 | ## Usage
|
51 |
|
52 | ```js
|
53 | import { lookup, mimes } from 'mrmime';
|
54 |
|
55 | // Get a MIME type
|
56 | // ---
|
57 | lookup('txt'); //=> "text/plain"
|
58 | lookup('.txt'); //=> "text/plain"
|
59 | lookup('a.txt'); //=> "text/plain"
|
60 |
|
61 | // Unknown extension
|
62 | // ---
|
63 | lookup('.xyz'); //=> undefined
|
64 |
|
65 | // Add extension to dictionary
|
66 | // ---
|
67 | mimes['xyz'] = 'hello/world';
|
68 | lookup('xyz'); //=> "hello/world"
|
69 | ```
|
70 |
|
71 |
|
72 | ## API
|
73 |
|
74 | ### lookup(input)
|
75 | Returns: `string` or `undefined`
|
76 |
|
77 | #### input
|
78 | Type: `string`
|
79 |
|
80 | The extension or filename to lookup.
|
81 |
|
82 | > **Important:**
|
83 | > * Any `input` value is cast to string, lowercased, and trimmed.
|
84 | > * If a filename or filepath is provided, only the extension will be used.
|
85 |
|
86 |
|
87 | ## Benchmarks
|
88 |
|
89 | > Running on Node v16.8.0
|
90 |
|
91 | ```
|
92 | Load times:
|
93 | mrmime 0.963ms
|
94 | mime/lite 3.281ms
|
95 | mime 6.751ms
|
96 |
|
97 | Benchmark :: plain ("ext")
|
98 | mime x 598,849 ops/sec ±0.28% (94 runs sampled)
|
99 | mime/lite x 536,643 ops/sec ±0.11% (97 runs sampled)
|
100 | mrmime x 835,885 ops/sec ±0.20% (97 runs sampled)
|
101 |
|
102 | Benchmark :: leading (".ext")
|
103 | mime x 368,656 ops/sec ±0.19% (99 runs sampled)
|
104 | mime/lite x 368,318 ops/sec ±0.13% (97 runs sampled)
|
105 | mrmime x 533,643 ops/sec ±0.10% (96 runs sampled)
|
106 |
|
107 | Benchmark :: filename ("file.ext")
|
108 | mime x 326,907 ops/sec ±0.17% (95 runs sampled)
|
109 | mime/lite x 327,479 ops/sec ±0.12% (98 runs sampled)
|
110 | mrmime x 512,823 ops/sec ±0.12% (99 runs sampled)
|
111 | ```
|
112 |
|
113 |
|
114 | ## Credits
|
115 |
|
116 | Of course, a thank-you to [`mime`](https://github.com/broofa/mime) serving the community all these years & for being a all-encompassing MIME type library. I've only ever needed lookup/`getType` functionality – and now ESM support – so `mrmime` can only ever support 1/3 of what `mime` offers, at best.
|
117 |
|
118 | This would not be possible without the team behind [`mime-db`](https://github.com/jshttp/mime-db), who have painstakingly maintained an amazing database for 7+ years.
|
119 |
|
120 | Artwork created by [mintinol](https://www.deviantart.com/mintinol), which I found [here](https://www.deviantart.com/mintinol/art/Mr-Mime-373927920).
|
121 |
|
122 | Finally, thanks to [Tim Branyen](https://github.com/tbranyen) for donating the package name :)
|
123 |
|
124 |
|
125 | ## License
|
126 |
|
127 | MIT © [Luke Edwards](https://lukeed.com)
|