You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.5 KiB

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