Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mime-type

snowyu58.3kMIT5.0.3TypeScript support: included

the custom more powerful mime-type utility can work with mime-db.

mime, mime-db, types

readme

mime-type

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

The custom more powerful mime-type utility and built-in mime-db.

fork from mime-types, these features added:

  • you can load mime-types via mime = require('mime-type/with-db') directly
  • mime = new Mime() business, so you could do lookup = mime.lookup.bind(mime).
  • you can lookup the extension with glob match via .lookup(extention) functionality
  • you can add the mime-type via .define(type, mime) functionality
  • you can add many mime-type via .load(mimes) functionality
  • you can search the mime-type via .glob(pattern) functionality
  • you can remove a mime-type via .delete(type) functionality
  • you can clear mime-types via .clear(filter) functionality
  • .exist(type) functionality to check whether a mime-type is exist.
  • .extensions will be deprecated, use mime[type].extensions instead.
  • All functions return undefined if input is invalid or not found.

Otherwise, the API is compatible.

Install

$ npm install mime-type

API

//create an empty mime-type:
import { MimeType } from 'mime-type'
const mime = new MimeType()
//or using mimeType which create an instance and load builtin mime-db directly.
import {mimeType} from 'mime-type/with-db'
//it equals to (you need `npm install mime-db` first):
import db from 'mime-db'
const mime = new MimeType(db)

All functions return undefined if input is invalid or not found.

mime.lookup(path)

Lookup the content-type associated with a file.

mime.lookup('json')             // 'application/json'
mime.lookup('.md')              // 'text/x-markdown'
mime.lookup('file.html')        // 'text/html'
mime.lookup('folder/file.js')   // 'application/javascript'
mime.lookup('folder/.htaccess') // false

mime.lookup('.og?')  // [ 'application/ogg', 'audio/ogg', 'audio/ogg', 'video/ogg' ]
mime.lookup('cats') // false

mime.glob(pattern)

Return all MIME types which matching a pattern(See Minimatch).

mime.glob('*/*')             // ['application/octet-stream']
mime.glob('*/*markdown')     // ['text/x-markdown']
mime.glob('text/j*')         // ['text/jade', 'text/jsx']
mime.glob('unknown/x')       // []

mime.exist(type)

test whether a mime-type is exist. It is an alias for mime.hasOwnProperty

mime.exist('text/x-markdown') // true
mime.exist('unknown/xxx')     // false

mime.define(type, object, duplicationWay?)

define a new mime-type. the duplicationWay is optional the process way of duplication extensions:

  • mime.dupDefault: the default process way.
  • mime.dupOverwrite: the news overwrite the olds
  • mime.dupSkip: just skip it.
  • mime.dupAppend: append the news to the exist extensions.

return the added extensions list if successful or undefined.

mime.define('script/coffee', {
  extensions: ['coffee', 'litcoffee', 'coffee.md']
}, mime.dupAppend)
mime.lookup ('coffee') //[ 'text/coffeescript', 'script/coffee' ]

mime.delete(type)

remove a specified mime-type

mime.delete('script/coffee') //true

mime.clear(filter)

clear all or specified mime-types

the filter could be a string pattern or a function

return the count of deleted mime-types.

mime.clear() //clear all mime-types
mime.clear('text/*') //clear the specified mime-types
mime.clear(function(type, mime){
  return type.substr(0,5) === 'text/'
})

mime.load(mimes)

load a lot of mime-types. return the count of loaded mime-types.

mime.clear() //clear all mime-types
mime.load({
  'script/coffee': {
    extensions: ['coffee', 'coffee.md', 'litcoffee'],
    compressible: true,
    charset: 'utf-8',
    defaultExtension: 'coffee.md'
  },
  'script/python': {
    extensions: ['py', 'py.md', 'litpy'],
    compressible: true,
    charset: 'utf-8'
  }
})

mime.contentType(type)

Create a full content-type header given a content-type or extension.

mime.contentType('markdown')  // 'text/x-markdown; charset=utf-8'
mime.contentType('file.json') // 'application/json; charset=utf-8'

// from a full path
mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'

mime.extension(type)

Get the default extension for a content-type.

mime.extension('application/octet-stream') // 'bin'

mime.charset(type)

Lookup the implied default charset of a content-type.

mime.charset('text/x-markdown') // 'UTF-8'

var type = mime.types[extension]

A map of content-types by extension.

[extensions...] = mime.extensions[type]

A map of extensions by content-type.

var mimeObject = mime[type]

A map of mime object(IMimeType) by content-type.

export interface IMimeType {
  source: string;
  charset?: string;
  compressible?: boolean;
  extensions: string[]|string;
}

mime.dup: DuplicationProcessWay

the default duplication process way.

See mime.define.

Notes

  • 4.0.0: nodejs >= 8.6
    • use micromatch instead of minimatch. It supports nodejs@8.6 above only.
  • 3.0.0: nodejs < 8.6

License

MIT

changelog

2.1.2 / 2015-06-25

  • deps: mime-db@~1.14.0
    • Add new mime types

2.1.1 / 2015-06-08

  • perf: fix deopt during mapping

2.1.0 / 2015-06-07

  • Fix incorrectly treating extension-less file name as extension
    • i.e. 'path/to/json' will no longer return application/json
  • Fix .charset(type) to accept parameters
  • Fix .charset(type) to match case-insensitive
  • Improve generation of extension to MIME mapping
  • Refactor internals for readability and no argument reassignment
  • Prefer application/* MIME types from the same source
  • Prefer any type over application/octet-stream
  • deps: mime-db@~1.13.0
    • Add nginx as a source
    • Add new mime types

2.0.14 / 2015-06-06

  • deps: mime-db@~1.12.0
    • Add new mime types

2.0.13 / 2015-05-31

  • deps: mime-db@~1.11.0
    • Add new mime types

2.0.12 / 2015-05-19

  • deps: mime-db@~1.10.0
    • Add new mime types

2.0.11 / 2015-05-05

  • deps: mime-db@~1.9.1
    • Add new mime types

2.0.10 / 2015-03-13

  • deps: mime-db@~1.8.0
    • Add new mime types

2.0.9 / 2015-02-09

  • deps: mime-db@~1.7.0
    • Add new mime types
    • Community extensions ownership transferred from node-mime

2.0.8 / 2015-01-29

  • deps: mime-db@~1.6.0
    • Add new mime types

2.0.7 / 2014-12-30

  • deps: mime-db@~1.5.0
    • Add new mime types
    • Fix various invalid MIME type entries

2.0.6 / 2014-12-30

  • deps: mime-db@~1.4.0
    • Add new mime types
    • Fix various invalid MIME type entries
    • Remove example template MIME types

2.0.5 / 2014-12-29

  • deps: mime-db@~1.3.1
    • Fix missing extensions

2.0.4 / 2014-12-10

  • deps: mime-db@~1.3.0
    • Add new mime types

2.0.3 / 2014-11-09

  • deps: mime-db@~1.2.0
    • Add new mime types

2.0.2 / 2014-09-28

  • deps: mime-db@~1.1.0
    • Add new mime types
    • Add additional compressible
    • Update charsets

2.0.1 / 2014-09-07

  • Support Node.js 0.6

2.0.0 / 2014-09-02

  • Use mime-db
  • Remove .define()

1.0.2 / 2014-08-04

  • Set charset=utf-8 for text/javascript

1.0.1 / 2014-06-24

  • Add text/jsx type

1.0.0 / 2014-05-12

  • Return false for unknown types
  • Set charset=utf-8 for application/json

0.1.0 / 2014-05-02

  • Initial release