• Stars
    star
    2,157
  • Rank 21,275 (Top 0.5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 14 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mime types for JavaScript

Mime

A comprehensive, compact MIME type module.

Mime CI NPM version NPM downloads

Install

NPM

npm install mime

Browser

It is recommended that you use a bundler such as webpack or browserify to package your code. However, browser-ready versions are available via skypack.dev as follows:

// Full version
<script type="module">
import mime from "https://cdn.skypack.dev/mime";
</script>
// "lite" version
<script type="module">
import mime from "https://cdn.skypack.dev/mime/lite";
</script>

Quick Start

For the full version (800+ MIME types, 1,000+ extensions):

const mime = require('mime');

mime.getType('txt');                    // ⇨ 'text/plain'
mime.getExtension('text/plain');        // ⇨ 'txt'

See Mime API below for API details.

Lite Version

The "lite" version of this module omits vendor-specific (*/vnd.*) and experimental (*/x-*) types. It weighs in at ~2.5KB, compared to 8KB for the full version. To load the lite version:

const mime = require('mime/lite');

Mime .vs. mime-types .vs. mime-db modules

For those of you wondering about the difference between these [popular] NPM modules, here's a brief rundown ...

mime-db is "the source of truth" for MIME type information. It is not an API. Rather, it is a canonical dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings submitted by the Node.js community.

mime-types is a thin wrapper around mime-db that provides an API drop-in compatible(ish) with mime @ < v1.3.6 API.

mime is, as of v2, a self-contained module bundled with a pre-optimized version of the mime-db dataset. It provides a simplified API with the following characteristics:

  • Intelligently resolved type conflicts (See mime-score for details)
  • Method naming consistent with industry best-practices
  • Compact footprint. E.g. The minified+compressed sizes of the various modules:
Module Size
mime-db 18 KB
mime-types same as mime-db
mime 8 KB
mime/lite 2 KB

Mime API

Both require('mime') and require('mime/lite') return instances of the MIME class, documented below.

Note: Inputs to this API are case-insensitive. Outputs (returned values) will be lowercase.

new Mime(typeMap, ... more maps)

Most users of this module will not need to create Mime instances directly. However if you would like to create custom mappings, you may do so as follows ...

// Require Mime class
const Mime = require('mime/Mime');

// Define mime type -> extensions map
const typeMap = {
  'text/abc': ['abc', 'alpha', 'bet'],
  'text/def': ['leppard']
};

// Create and use Mime instance
const myMime = new Mime(typeMap);
myMime.getType('abc');            // ⇨ 'text/abc'
myMime.getExtension('text/def');  // ⇨ 'leppard'

If more than one map argument is provided, each map is define()ed (see below), in order.

mime.getType(pathOrExtension)

Get mime type for the given path or extension. E.g.

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

mime.getType('foo/txt');        // ⇨ null
mime.getType('bogus_type');     // ⇨ null

mime.getExtension(type)

Get extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.

mime.getExtension('text/plain');               // ⇨ 'txt'
mime.getExtension('application/json');         // ⇨ 'json'
mime.getExtension('text/html; charset=utf8');  // ⇨ 'html'

mime.define(typeMap[, force = false])

Define [more] type mappings.

typeMap is a map of type -> extensions, as documented in new Mime, above.

By default this method will throw an error if you try to map a type to an extension that is already assigned to another type. Passing true for the force argument will suppress this behavior (overriding any previous mapping).

mime.define({'text/x-abc': ['abc', 'abcd']});

mime.getType('abcd');            // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc')  // ⇨ 'abc'

Command Line

mime [path_or_extension]

E.g.

> mime scripts/jquery.js
application/javascript

Markdown generated from src/README_js.md by RunMD Logo

More Repositories

1

BroofaJS

Misc. JS utilities that may be of broader interest
JavaScript
212
star
2

node-int64

Support for representing 64-bit integers in JavaScript
JavaScript
202
star
3

jslitmus

Simple, easy, javascript benchmark testing
JavaScript
168
star
4

runmd

Executable markdown files
JavaScript
121
star
5

checkcss

TypeScript
27
star
6

joos

OO in JavaScript
JavaScript
18
star
7

simplur

TypeScript
14
star
8

jarvis

A Search Box Assistant
JavaScript
11
star
9

socipedia

A crowd-sourced business directory web app
PHP
9
star
10

jslife

Conway's Life Simulation, in JavaScript
HTML
6
star
11

proccoli

A customizable web proxy server
JavaScript
5
star
12

airjs

Javascript implementation of the barometric function for determining air density and pressure as a function of altitude
JavaScript
4
star
13

grout

A micro-framework for using CommonJS modules in browsers
3
star
14

mime-score

Logic for prioritizing MIME types based on facet, source, and type
TypeScript
3
star
15

jsscore

A debatable indicator of Javascript performance
JavaScript
3
star
16

jsciiart

Generate ascii art in realtime from webcamera in the browser using JavaScript + WebRTC (getUserMedia)
JavaScript
3
star
17

lzwjs

lzwjs
JavaScript
3
star
18

resnap

Simple API for capturing and resetting Node's require() cache state
JavaScript
3
star
19

plague

JavaScript
2
star
20

streamie

App for testing streaming transports in IE
JavaScript
2
star
21

flightcard

TypeScript
2
star
22

pek

A modern data model for UI rendering
JavaScript
2
star
23

storagehooks

JavaScript
1
star
24

perf-lite

Lightweight language performance test
C++
1
star
25

gmaul

TypeScript
1
star
26

stringlang

Utility functions for analyzing strings by Unicode block
JavaScript
1
star
27

oshareport

The Oregon OSHA report form, reskinned for COVID enforcement
HTML
1
star
28

thrustcurve-db

JavaScript
1
star