提交学习笔记专用
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.

130 lines
3.6 KiB

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