mime-types

repository·master·Indexed 23 days ago

https://github.com/jshttp/mime-types

A high-performance JavaScript utility for mapping file extensions and paths to MIME content-types and vice versa. Version 3.0.2 provides functions to lookup MIME types, generate Content-Type headers, retrieve default extensions, and find default charsets, as well as direct access to types and extensions mapping objects.

Tokens
1.1K
Snippets
5
Records
12
Agent score
31%

What's inside mime-types

  1. How to handle missing or unrecognized MIME types

    master

    Unlike the older mime@1.x module, mime-types does not provide fallbacks. If a lookup fails or the input is invalid, the functions return false. To provide a default value, use a logical OR operator:

    var type = mime.lookup('unrecognized') || 'application/octet-stream';
  2. Use mime.lookup(path) to find content-types

    master

    Use mime.lookup(path) to retrieve the MIME content-type associated with a file extension or a file path. Returns false if the type is not found.

    mime.lookup('json') // 'application/json'
    mime.lookup('.md') // 'text/markdown'
    mime.lookup('file.html') // 'text/html'
    mime.lookup('folder/file.js') // 'text/javascript'
    mime.lookup('folder/.htaccess') // false
    
    mime.lookup('cats') // false
  3. Use mime.contentType(type) to create content-type headers

    master

    Use mime.contentType(type) to generate a full Content-Type header.

    • If passed an extension, it uses mime.lookup to find the content-type.
    • If passed a content-type, it uses that directly.
    • If the resulting content-type lacks a charset parameter, it automatically appends the default charset retrieved via mime.charset.

    Note: If the input already contains a charset, it is preserved.

    mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
    mime.contentType('file.json') // 'application/json; charset=utf-8'
    mime.contentType('text/html') // 'text/html; charset=utf-8'
    mime.contentType('text/html; charset=iso-8859-1') // 'text/html; charset=iso-8859-1'
    
    // from a full path
    mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'
  4. Access mime.types and mime.extensions maps

    master

    The module provides two direct mapping objects for quick lookups:

    • mime.types: A map where keys are extensions and values are content-types.
    • mime.extensions: A map where keys are content-types and values are arrays of associated extensions.
  5. Get the default charset using charset()

    master

    Use charset(type) to retrieve the default character set for a specific MIME type.

    • For types with a defined charset in the database, it returns that charset.
    • For text/* types without a specific charset defined, it defaults to 'UTF-8'.
    • If no charset can be determined, it returns false.
  6. Lookup a MIME type using lookup()

    master
    Use lookup(path) to find the MIME type associated with a file path or an extension. The function extracts the extension from the provided string and returns the preferred MIME type. If no extension is found or the extension is unknown, it returns false.
  7. Create a Content-Type header using contentType()

    master

    Use contentType(str) to generate a full Content-Type header string. The input str can be either a MIME type (e.g., text/html) or a file extension (e.g., html).

    If a MIME type is provided, the function automatically appends the default charset (e.g., ; charset=utf-8) if one is available for that type. If an extension is provided, it first performs a lookup to find the MIME type and then applies the charset logic.

  8. Access the types and extensions maps directly

    master

    The module exports two internal maps that can be used for direct lookups:

    • types: A map where keys are extensions (without the dot) and values are the preferred MIME types.
    • extensions: A map where keys are MIME types and values are arrays of associated extensions.