Spaces:
Paused
Paused
File size: 4,236 Bytes
95f4e64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<!--
-- This file is auto-generated from src/README_js.md. Changes should be made there.
-->
# Mime
[](https://www.npmjs.com/package/mime)
[](https://github.com/broofa/mime/actions/workflows/ci.yml?query=branch%3Amain)
An API for MIME type information.
- All `mime-db` types
- Compact and dependency-free [](https://bundlejs.com/?q=mime)
- Full TS support
> [!Note]
> `mime@4` is now `latest`. If you're upgrading from `mime@3`, note the following:
> * `mime@4` is API-compatible with `mime@3`, with ~~one~~ two exceptions:
> * Direct imports of `mime` properties [no longer supported](https://github.com/broofa/mime/issues/295)
> * `mime.define()` cannot be called on the default `mime` object
> * ESM module support is required. [ESM Module FAQ](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
> * Requires an [ES2020](https://caniuse.com/?search=es2020) or newer runtime
> * Built-in Typescript types (`@types/mime` no longer needed)
## Installation
```bash
npm install mime
```
## Quick Start
For the full version (800+ MIME types, 1,000+ extensions):
```javascript
import mime from 'mime';
mime.getType('txt'); // ⇨ 'text/plain'
mime.getExtension('text/plain'); // ⇨ 'txt'
```
### Lite Version [](https://bundlejs.com/?q=mime/lite)
`mime/lite` is a drop-in `mime` replacement, stripped of unofficial ("`prs.*`", "`x-*`", "`vnd.*`") types:
```javascript
import mime from 'mime/lite';
```
## API
### `mime.getType(pathOrExtension)`
Get mime type for the given file path or extension. E.g.
```javascript
mime.getType('js'); // ⇨ 'application/javascript'
mime.getType('json'); // ⇨ 'application/json'
mime.getType('txt'); // ⇨ 'text/plain'
mime.getType('dir/text.txt'); // ⇨ 'text/plain'
mime.getType('dir\\text.txt'); // ⇨ 'text/plain'
mime.getType('.text.txt'); // ⇨ 'text/plain'
mime.getType('.txt'); // ⇨ 'text/plain'
```
`null` is returned in cases where an extension is not detected or recognized
```javascript
mime.getType('foo/txt'); // ⇨ null
mime.getType('bogus_type'); // ⇨ null
```
### `mime.getExtension(type)`
Get file extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.
```javascript
mime.getExtension('text/plain'); // ⇨ 'txt'
mime.getExtension('application/json'); // ⇨ 'json'
mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
```
### `mime.getAllExtensions(type)`
> [!Note]
> New in `mime@4`
Get all file extensions for the given mime type.
```javascript --run default
mime.getAllExtensions('image/jpeg'); // ⇨ Set(3) { 'jpeg', 'jpg', 'jpe' }
```
## Custom `Mime` instances
The default `mime` objects are immutable. Custom, mutable versions can be created as follows...
### new Mime(type map [, type map, ...])
Create a new, custom mime instance. For example, to create a mutable version of the default `mime` instance:
```javascript
import { Mime } from 'mime/lite';
import standardTypes from 'mime/types/standard.js';
import otherTypes from 'mime/types/other.js';
const mime = new Mime(standardTypes, otherTypes);
```
Each argument is passed to the `define()` method, below. For example `new Mime(standardTypes, otherTypes)` is synonomous with `new Mime().define(standardTypes).define(otherTypes)`
### `mime.define(type map [, force = false])`
> [!Note]
> Only available on custom `Mime` instances
Define MIME type -> extensions.
Attempting to map a type to an already-defined extension will `throw` unless the `force` argument is set to `true`.
```javascript
mime.define({'text/x-abc': ['abc', 'abcd']});
mime.getType('abcd'); // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc') // ⇨ 'abc'
```
## Command Line
### Extension -> type
```bash
$ mime scripts/jquery.js
application/javascript
```
### Type -> extension
```bash
$ mime -r image/jpeg
jpeg
```
|