Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mime-type

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mime-type

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

  • 3.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.5K
decreased by-0.45%
Maintainers
1
Weekly downloads
 
Created
Source

mime-type

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

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

fork from mime-types, these features added:

  • you can load mime-types via mime-db mime = new Mime(require('mime-db'))
    • or use mime = require('mime-type/with-db') directly, but first
    • you need npm install mime-db
  • mime = new Mime() business, so you could do lookup = mime.lookup.bind(mime).
  • 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:
var mime = require('mime-type')()
//or create an instance and load mime-db. you need `npm install mime-db`
var mime = require('mime-type/with-db')
//it equals to:
var db = require('mime-db')
var mime = require('mime-type')(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('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 duplicationo process way.

See mime.define.

License

MIT

Keywords

FAQs

Package last updated on 10 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc