Socket
Socket
Sign inDemoInstall

mime

Package Overview
Dependencies
0
Maintainers
0
Versions
52
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mime

A comprehensive library for mime-type mapping


Version published
Weekly downloads
61M
decreased by-1.28%
Maintainers
0
Install size
51.8 kB
Created
Weekly downloads
 

Package description

What is mime?

The mime npm package is a utility for handling and transforming file MIME types. It provides functionality to lookup the MIME type for a file based on its extension, determine the default extension for a MIME type, and define custom MIME type mappings.

What are mime's main functionalities?

Lookup MIME type by extension

This feature allows you to get the MIME type for a given file extension. In the code sample, we are looking up the MIME type for a '.json' file, which returns 'application/json'.

const mime = require('mime');
const mimeType = mime.getType('json'); // 'application/json'

Lookup extension by MIME type

This feature enables you to find the default file extension for a given MIME type. In the code sample, we are finding the extension for the MIME type 'application/json', which returns 'json'.

const mime = require('mime');
const extension = mime.getExtension('application/json'); // 'json'

Define custom MIME type mappings

This feature allows you to define custom MIME type mappings. In the code sample, we are adding a custom MIME type 'text/x-some-format' with multiple extensions. The second argument 'true' indicates that the custom types should take precedence over the built-in types.

const mime = require('mime');
mime.define({ 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'] }, true);

Other packages similar to mime

Changelog

Source

v1.2.11 (15/08/2013)

  • [closed] Update mime.types #65
  • [closed] Publish a new version #63
  • [closed] README should state upfront that "application/octet-stream" is default for unknown extension #55
  • [closed] Suggested improvement to the charset API #52

Readme

Source

mime

Support for mapping between file extensions and MIME types. This module uses the latest version of the Apache "mime.types" file (maps over 620 types to 800+ extensions). It is also trivially easy to add your own types and extensions, should you need to do that.

Install

Install with npm:

npm install mime

API

mime.lookup(path) - lookup the type for a file or extension

var mime = require('mime');

mime.lookup('/path/to/file.txt');         // => 'text/plain'
mime.lookup('file.txt');                  // => 'text/plain'
mime.lookup('.txt');                      // => 'text/plain'
mime.lookup('htm');                       // => 'text/html'

mime.extension(type) - lookup the default extension for type

mime.extension('text/html');                 // => 'html'
mime.extension('application/octet-stream');  // => 'bin'

mime.charsets.lookup() - map mime-type to charset

mime.charsets.lookup('text/plain');        // => 'UTF-8'

(The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)

"Can you add support for [some type/extension]?"

Start by adding support for the type in your project using the mime.define() or mime.load() methods (documented below).

If there's a type that is shared across node.js modules, by different people, create an issue here and we'll add it if it makes sense.

If the type in question applies to projects outside the node.js community (e.g. if IANA approves a new type) file a bug with Apache and create an issue here that links to it.

mime.define() - Add custom mime/extension mappings

mime.define({
    'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
    'application/x-my-type': ['x-mt', 'x-mtt'],
    // etc ...
});

mime.lookup('x-sft');                 // => 'text/x-some-format'
mime.extension('text/x-some-format'); // => 'x-sf'

mime.load(filepath) - Load mappings from an Apache ".types" format file

mime.load('./my_project.types');

Keywords

FAQs

Last updated on 20 Jan 2011

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc