Convert Figma logo to code with AI

jshttp logomime-db

Media Type Database

1,090
256
1,090
39

Top Related Projects

2,161

Mime types for JavaScript

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

Quick Overview

mime-db is a comprehensive database of MIME types and their associated metadata. It provides a JSON-based collection of MIME types, including extensions, compressibility information, and other relevant details. This project serves as a foundation for various applications and libraries that need to work with MIME types.

Pros

  • Extensive collection of MIME types with detailed metadata
  • Regularly updated to include new and emerging MIME types
  • Lightweight and easy to integrate into various projects
  • Well-maintained and actively supported by the community

Cons

  • Limited to MIME type information only, not providing additional file type functionality
  • May require manual updates in projects to stay current with the latest MIME types
  • Large dataset might be overkill for projects that only need a small subset of MIME types

Code Examples

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

// Get all MIME types
console.log(Object.keys(db));
// Check if a MIME type is compressible
const isCompressible = db['application/json'].compressible;
console.log(isCompressible); // true
// Get extensions for a MIME type
const extensions = db['image/jpeg'].extensions;
console.log(extensions); // ['jpeg', 'jpg', 'jpe']

Getting Started

To use mime-db in your project, follow these steps:

  1. Install the package using npm:

    npm install mime-db
    
  2. Import the database in your JavaScript file:

    const db = require('mime-db');
    
  3. Access MIME type information:

    const mimeType = 'application/json';
    console.log(db[mimeType]);
    

Now you can use the db object to access MIME type information throughout your project.

Competitor Comparisons

2,161

Mime types for JavaScript

Pros of mime

  • Simpler API with fewer dependencies
  • Includes both MIME type lookup and file extension lookup
  • Smaller package size, making it more lightweight

Cons of mime

  • Less comprehensive MIME type database
  • Not as frequently updated as mime-db
  • Lacks some advanced features like charset detection

Code Comparison

mime-db:

const db = require('mime-db')
console.log(db['application/json'])
// Output: { source: 'iana', compressible: true, extensions: ['json', 'map'] }

mime:

const mime = require('mime')
console.log(mime.getType('json'))
// Output: 'application/json'
console.log(mime.getExtension('application/json'))
// Output: 'json'

Summary

mime-db provides a more comprehensive and frequently updated MIME type database, while mime offers a simpler API with both MIME type and file extension lookup capabilities. mime-db is better suited for applications requiring a wide range of MIME types and detailed information, whereas mime is more appropriate for basic MIME type operations in lightweight applications.

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

Pros of file-type

  • Provides file type detection based on file content, not just extensions
  • Supports a wide range of file formats, including audio, video, and archives
  • Offers both synchronous and asynchronous APIs for flexibility

Cons of file-type

  • Focuses solely on file type detection, lacking comprehensive MIME type information
  • May have higher processing overhead for large files or bulk operations
  • Requires reading file content, which can be slower than extension-based checks

Code Comparison

file-type:

import {fileTypeFromFile} from 'file-type';

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

mime-db:

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

const mimeType = db['image/png'];
console.log(mimeType); // { source: 'iana', compressible: false, extensions: ['png'] }

Key Differences

  • mime-db is a comprehensive database of MIME types, while file-type focuses on file type detection
  • file-type analyzes file content for accurate type identification, whereas mime-db relies on predefined mappings
  • mime-db provides additional metadata for MIME types, such as compressibility and sources
  • file-type is more suitable for runtime file analysis, while mime-db is better for static MIME type lookups

Use Cases

  • Choose file-type for accurate file type detection based on content
  • Opt for mime-db when working with MIME types directly or requiring extensive MIME metadata
  • Consider using both in combination for a robust file handling system

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-db

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

This is a large database of mime types and information about them. It consists of a single, public JSON file and does not include any logic, allowing it to remain as un-opinionated as possible with an API. It aggregates data from the following sources:

Installation

npm install mime-db

Database Download

If you intend to use this in a web browser, you can conveniently access the JSON file via jsDelivr, a popular CDN (Content Delivery Network). To ensure stability and compatibility, it is advisable to specify a release tag instead of using the 'master' branch. This is because the JSON file's format might change in future updates, and relying on a specific release tag will prevent potential issues arising from these changes.

https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json

Usage

var db = require('mime-db')

// grab data on .js files
var data = db['application/javascript']

Data Structure

The JSON file is a map lookup for lowercased mime types. Each mime type has the following properties:

  • .source - where the mime type is defined. If not set, it's probably a custom media type.
  • .extensions[] - known extensions associated with this mime type.
  • .compressible - whether a file of this type can be gzipped.
  • .charset - the default charset associated with this type, if any.

If unknown, every property could be undefined.

Contributing

The primary way to contribute to this database is by updating the data in one of the upstream sources. The database is updated from the upstreams periodically and will pull in any changes.

Registering Media Types

The best way to get new media types included in this library is to register them with the IANA. The community registration procedure is outlined in RFC 6838 section 5. Types registered with the IANA are automatically pulled into this library.

Direct Inclusion

If that is not possible / feasible, they can be added directly here as a "custom" type. To do this, it is required to have a primary source that definitively lists the media type. If an extension is going to be listed as associateed with this media type, the source must definitively link the media type and extension as well.

To edit the database, only make PRs against src/custom-types.json or src/custom-suffix.json.

The src/custom-types.json file is a JSON object with the MIME type as the keys and the values being an object with the following keys:

  • compressible - leave out if you don't know, otherwise true/false to indicate whether the data represented by the type is typically compressible.
  • extensions - include an array of file extensions that are associated with the type.
  • notes - human-readable notes about the type, typically what the type is.
  • sources - include an array of URLs of where the MIME type and the associated extensions are sourced from. This needs to be a primary source; links to type aggregating sites and Wikipedia are not acceptable.

To update the build, run npm run build.

NPM DownloadsLast 30 Days