Socket
Socket
Sign inDemoInstall

mime

Package Overview
Dependencies
0
Maintainers
1
Versions
51
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-0.03%
Maintainers
1
Install size
97.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

4.0.2 (2024-04-24)

Bug Fixes

  • update mime-score version, fixes #297 (#299) (533872a)

Readme

Source

Mime

NPM downloads Mime CI

An API for MIME type information.

  • All mime-db types
  • Compact and dependency-free mime's badge
  • 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
    • mime.define() cannot be called on the default mime object
  • ESM module support is required. ESM Module FAQ.
  • Requires an ES2020 or newer runtime
  • Built-in Typescript types (@types/mime no longer needed)

Installation

npm install mime

Quick Start

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

import mime from 'mime';

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

Lite Version mime/lite's badge

mime/lite is a drop-in mime replacement, stripped of unofficial ("prs.*", "x-*", "vnd.*") types:

import mime from 'mime/lite';

API

mime.getType(pathOrExtension)

Get mime type for the given file 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 file 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.getAllExtensions(type)

[!Note] New in mime@4

Get all file extensions for the given mime type.

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:

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.

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

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

Command Line

Extension -> type

$ mime scripts/jquery.js
application/javascript

Type -> extension

$ mime -r image/jpeg
jpeg

Keywords

FAQs

Last updated on 24 Apr 2024

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