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