UNPKG

5.64 kBMarkdownView Raw
1<!--
2 -- This file is auto-generated from src/README_js.md. Changes should be made there.
3 -->
4# Mime
5
6A comprehensive, compact MIME type module.
7
8[![Build Status](https://travis-ci.org/broofa/mime.svg?branch=master)](https://travis-ci.org/broofa/mime)
9
10## Version 2 Notes
11
12Version 2 is a breaking change from 1.x as the semver implies. Specifically:
13
14* `lookup()` renamed to `getType()`
15* `extension()` renamed to `getExtension()`
16* `charset()` and `load()` methods have been removed
17
18If you prefer the legacy version of this module please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/mime/tree/v1.4.0).
19
20## Install
21
22### NPM
23```
24npm install mime
25```
26
27### Browser
28
29It is recommended that you use a bundler such as
30[webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to
31package your code. However, browser-ready versions are available via wzrd.in.
32E.g. For the full version:
33
34 <script src="https://wzrd.in/standalone/mime@latest"></script>
35 <script>
36 mime.getType(...); // etc.
37 <script>
38
39Or, for the `mime/lite` version:
40
41 <script src="https://wzrd.in/standalone/mime%2flite@latest"></script>
42 <script>
43 mimelite.getType(...); // (Note `mimelite` here)
44 <script>
45
46## Quick Start
47
48For the full version (800+ MIME types, 1,000+ extensions):
49
50```javascript
51const mime = require('mime');
52
53mime.getType('txt'); // ⇨ 'text/plain'
54mime.getExtension('text/plain'); // ⇨ 'txt'
55```
56
57See [Mime API](#mime-api) below for API details.
58
59## Lite Version
60
61There is also a "lite" version of this module that omits vendor-specific
62(`*/vnd.*`) and experimental (`*/x-*`) types. It weighs in at ~2.5KB, compared
63to 8KB for the full version. To load the lite version:
64
65```javascript
66const mime = require('mime/lite');
67```
68
69## Mime .vs. mime-types .vs. mime-db modules
70
71For those of you wondering about the difference between these [popular] NPM modules,
72here's a brief rundown ...
73
74[`mime-db`](https://github.com/jshttp/mime-db) is "the source of
75truth" for MIME type information. It is not an API. Rather, it is a canonical
76dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings
77submitted by the Node.js community.
78
79[`mime-types`](https://github.com/jshttp/mime-types) is a thin
80wrapper around mime-db that provides an API drop-in compatible(ish) with `mime @ < v1.3.6` API.
81
82`mime` is, as of v2, a self-contained module bundled with a pre-optimized version
83of the `mime-db` dataset. It provides a simplified API with the following characteristics:
84
85* Intelligently resolved type conflicts (See [mime-score](https://github.com/broofa/mime-score) for details)
86* Method naming consistent with industry best-practices
87* Compact footprint. E.g. The minified+compressed sizes of the various modules:
88
89Module | Size
90--- | ---
91`mime-db` | 18 KB
92`mime-types` | same as mime-db
93`mime` | 8 KB
94`mime/lite` | 2 KB
95
96## Mime API
97
98Both `require('mime')` and `require('mime/lite')` return instances of the MIME
99class, documented below.
100
101Note: Inputs to this API are case-insensitive. Outputs (returned values) will
102be lowercase.
103
104### new Mime(typeMap, ... more maps)
105
106Most users of this module will not need to create Mime instances directly.
107However if you would like to create custom mappings, you may do so as follows
108...
109
110```javascript
111// Require Mime class
112const Mime = require('mime/Mime');
113
114// Define mime type -> extensions map
115const typeMap = {
116 'text/abc': ['abc', 'alpha', 'bet'],
117 'text/def': ['leppard']
118};
119
120// Create and use Mime instance
121const myMime = new Mime(typeMap);
122myMime.getType('abc'); // ⇨ 'text/abc'
123myMime.getExtension('text/def'); // ⇨ 'leppard'
124```
125
126If more than one map argument is provided, each map is `define()`ed (see below), in order.
127
128### mime.getType(pathOrExtension)
129
130Get mime type for the given path or extension. E.g.
131
132```javascript
133mime.getType('js'); // ⇨ 'application/javascript'
134mime.getType('json'); // ⇨ 'application/json'
135
136mime.getType('txt'); // ⇨ 'text/plain'
137mime.getType('dir/text.txt'); // ⇨ 'text/plain'
138mime.getType('dir\\text.txt'); // ⇨ 'text/plain'
139mime.getType('.text.txt'); // ⇨ 'text/plain'
140mime.getType('.txt'); // ⇨ 'text/plain'
141```
142
143`null` is returned in cases where an extension is not detected or recognized
144
145```javascript
146mime.getType('foo/txt'); // ⇨ null
147mime.getType('bogus_type'); // ⇨ null
148```
149
150### mime.getExtension(type)
151Get extension for the given mime type. Charset options (often included in
152Content-Type headers) are ignored.
153
154```javascript
155mime.getExtension('text/plain'); // ⇨ 'txt'
156mime.getExtension('application/json'); // ⇨ 'json'
157mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
158```
159
160### mime.define(typeMap[, force = false])
161
162Define [more] type mappings.
163
164`typeMap` is a map of type -> extensions, as documented in `new Mime`, above.
165
166By default this method will throw an error if you try to map a type to an
167extension that is already assigned to another type. Passing `true` for the
168`force` argument will suppress this behavior (overriding any previous mapping).
169
170```javascript
171mime.define({'text/x-abc': ['abc', 'abcd']});
172
173mime.getType('abcd'); // ⇨ 'text/x-abc'
174mime.getExtension('text/x-abc') // ⇨ 'abc'
175```
176
177## Command Line
178
179 mime [path_or_extension]
180
181E.g.
182
183 > mime scripts/jquery.js
184 application/javascript
185
186----
187Markdown generated from [src/README_js.md](src/README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)
\No newline at end of file