Convert Figma logo to code with AI

broofa logomime

Mime types for JavaScript

2,161
316
2,161
0

Top Related Projects

The ultimate javascript content-type utility.

Detect the file type of a file, stream, or data

Quick Overview

The broofa/mime project is a comprehensive library for determining the MIME type of a file or file extension. It provides a simple and efficient way to identify the content type of various file formats, making it a useful tool for web development, file handling, and content management applications.

Pros

  • Extensive MIME Type Coverage: The library supports a wide range of MIME types, covering a diverse set of file formats, from common document types to multimedia and application-specific formats.
  • Cross-Platform Compatibility: The library is designed to work across different platforms, including Node.js, the browser, and other JavaScript environments.
  • Actively Maintained: The project is actively maintained, with regular updates and bug fixes, ensuring its continued reliability and relevance.
  • Lightweight and Dependency-Free: The library is lightweight and does not have any external dependencies, making it easy to integrate into projects without adding significant overhead.

Cons

  • Limited Extensibility: While the library covers a wide range of MIME types, it may not include support for some more obscure or custom file formats. Users may need to extend the library's functionality in such cases.
  • Potential for Outdated MIME Type Definitions: As new file formats and MIME types emerge, the library's definitions may not always be up-to-date, requiring manual updates or community contributions to keep it current.
  • Lack of Advanced Features: The library is focused on the core functionality of MIME type detection and does not provide additional features, such as file type validation or content analysis.
  • Potential Performance Impact: While the library is lightweight, in high-volume or performance-critical applications, the overhead of MIME type detection may need to be considered.

Code Examples

// Determine the MIME type of a file extension
const mime = require('mime-types');
const fileExtension = 'jpg';
const mimeType = mime.lookup(fileExtension);
console.log(mimeType); // Output: 'image/jpeg'
// Determine the MIME type of a file path
const mime = require('mime-types');
const filePath = '/path/to/file.pdf';
const mimeType = mime.lookup(filePath);
console.log(mimeType); // Output: 'application/pdf'
// Get the file extension from a MIME type
const mime = require('mime-types');
const mimeType = 'text/html';
const fileExtension = mime.extension(mimeType);
console.log(fileExtension); // Output: 'html'
// Check if a MIME type is valid
const mime = require('mime-types');
const mimeType = 'image/jpeg';
const isValid = mime.valid(mimeType);
console.log(isValid); // Output: true

Getting Started

To use the broofa/mime library in your project, follow these steps:

  1. Install the library using npm or yarn:
npm install mime-types
  1. Import the library in your JavaScript file:
const mime = require('mime-types');
  1. Use the library's methods to determine MIME types, file extensions, and perform other related operations:
// Determine the MIME type of a file extension
const fileExtension = 'jpg';
const mimeType = mime.lookup(fileExtension);
console.log(mimeType); // Output: 'image/jpeg'

// Get the file extension from a MIME type
const mimeType = 'text/html';
const fileExtension = mime.extension(mimeType);
console.log(fileExtension); // Output: 'html'
  1. Refer to the library's documentation for more advanced usage and additional features.

Competitor Comparisons

The ultimate javascript content-type utility.

Pros of mime-types

  • More comprehensive MIME type database with over 1000 types
  • Regular updates to keep the database current
  • Supports both extension-to-MIME and MIME-to-extension lookups

Cons of mime-types

  • Larger package size due to extensive database
  • Slightly more complex API compared to mime

Code Comparison

mime-types:

const mime = require('mime-types');

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

mime:

const mime = require('mime');

mime.getType('txt');      // 'text/plain'
mime.getExtension('text/plain'); // 'txt'
mime.define({'text/x-abc': ['abc']});

Key Differences

  • mime-types offers a more extensive database, while mime focuses on a smaller, core set of types
  • mime-types provides bidirectional lookups (extension-to-MIME and MIME-to-extension), whereas mime primarily focuses on extension-to-MIME lookups
  • mime allows for easier custom MIME type definitions
  • mime-types is part of the jshttp organization, which maintains several HTTP-related modules, potentially offering better integration with other jshttp projects

Both libraries serve similar purposes, but mime-types is more comprehensive and regularly updated, while mime is lighter and simpler to use for basic MIME type lookups.

Detect the file type of a file, stream, or data

Pros of file-type

  • Supports a wider range of file types, including audio, video, and archive formats
  • Provides more detailed file type information, including MIME type and extensions
  • Offers both synchronous and asynchronous APIs for flexibility

Cons of file-type

  • Larger package size due to extensive file type support
  • May have slightly slower performance for simple MIME type lookups
  • Requires Node.js environment, not suitable for browser-based applications

Code Comparison

file-type:

import {fileTypeFromFile} from 'file-type';

const type = await fileTypeFromFile('path/to/file.jpg');
console.log(type); // { ext: 'jpg', mime: 'image/jpeg' }

mime:

import mime from 'mime';

const type = mime.getType('path/to/file.jpg');
console.log(type); // 'image/jpeg'

Summary

file-type is a more comprehensive solution for file type detection, offering support for a wide range of formats and detailed information. It's ideal for applications requiring in-depth file analysis. mime, on the other hand, is a lightweight option focused primarily on MIME type lookups, making it suitable for simpler use cases and browser-based applications. The choice between the two depends on the specific requirements of your project, balancing between feature richness and simplicity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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');             // ⇨ 'text/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
text/javascript

Type -> extension

$ mime -r image/jpeg
jpeg

NPM DownloadsLast 30 Days