mimos

Mime database interface

This module has moved and is now available at @hapi/mimos. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.
Last updated 2 years ago by hueniverse .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mimos 
SYNC missed versions from official npm registry.

#mimos

Mime database interface.

Build Status Current Version

Lead Maintainer - Adam Bretz

Mimos is a convenience class for retrieving mime information objects.

Usage

new Mimos([options])

Creates a new Mimos object where:

  • [options] - an option object the following keys
    • [override] - an object hash that is merged into the built in mime information specified here. Each key value pair represents a single mime object. Each override value should follow this schema:
      • key - the key is the lower-cased correct mime-type. (Ex. "application/javascript").
      • value - the value should an object following the specifications outlined here. Additional values include:
        • type - specify the type value of result objects, defaults to key. See the example below for more clarification.
        • predicate - method with signature function(mime) when this mime type is found in the database, this function will run. This allows you make customizations to mime based on developer criteria.

mimos.path(path)

Returns mime object where:

  • path path to file including the file extension. Uses the extension values of the mime objects for lookup.
const mimos = new Mimos();
const mime = mimos.path('/static/public/app.js');
// mime
/*
{
  source: 'iana',
  charset: 'UTF-8',
  compressible: true,
  extensions: [ 'js' ],
  type: 'application/javascript'
}
*/

mimos.type(type)

Returns mime object where:

  • type the content-type to find mime information about. Uses the type values of the mime objects for lookup.
const mimos = new Mimos();
const mime = mimos.type('text/plain');
// mime
/*
{
  source: 'iana',
  compressible: true,
  extensions: ['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini'],
  type: 'text/plain'
}
*/

Override Example

In certain situations, it can be helpful to override the built in mime type information. The optional argument to the Mimos constructor is used to override and add mime information. Below is an example to help understand how this works.


const options = {
    override: {
        'node/module': {
            source: 'iana',
            compressible: true,
            extensions: ['node', 'module', 'npm'],
            type: 'node/module'
        },
        'application/javascript': {
            source: 'iana',
            charset: 'UTF-8',
            compressible: true,
            extensions: ['js', 'javascript'],
            type: 'text/javascript'
        },
        'text/html': {
            predicate: function(mime) {
                if (someCondition) {
                    mime.foo = 'test';
                }
                else {
                    mime.foo = 'bar';
                }
                return mime;
            }
        }
    }
}

const mimos = new Mimos(options);
console.dir(mimos.path('./node_modules/mimos.module'));
/*
{
  source: 'iana',
  compressible: true,
  extensions: ['node', 'module', 'npm'],
  type: 'node/module'
}
*/
console.dir(mimos.type('application/javascript'));
/*
Note: even though we asked for type 'application/javascript', the type value is 'text/javascript' because of the override. Always use the proper content-type for retrieval.
{
  source: 'iana',
  charset: 'UTF-8',
  compressible: true,
  extensions: ['js', 'javascript'],
  type: 'text/javascript'
}
*/
console.dir(mimos.type('text/html'));
/*
someCondition is true:
{
  source: 'iana',
  compressible: true,
  extensions: ['html','htm'],
  type: 'test/html',
  foo: 'test'
}

someCondition is false:
{
  source: 'iana',
  compressible: true,
  extensions: ['html','htm'],
  type: 'test/html',
  foo: 'bar'
}
*/

Current Tags

  • 4.0.2                                ...           latest (2 years ago)

12 Versions

  • 4.0.2 [deprecated]           ...           2 years ago
  • 4.0.1 [deprecated]           ...           2 years ago
  • 4.0.0 [deprecated]           ...           3 years ago
  • 3.0.3 [deprecated]           ...           4 years ago
  • 3.0.2 [deprecated]           ...           4 years ago
  • 3.0.1 [deprecated]           ...           4 years ago
  • 3.0.0 [deprecated]           ...           5 years ago
  • 2.0.2 [deprecated]           ...           6 years ago
  • 2.0.1 [deprecated]           ...           6 years ago
  • 2.0.0 [deprecated]           ...           6 years ago
  • 1.0.1 [deprecated]           ...           6 years ago
  • 1.0.0 [deprecated]           ...           6 years ago
Maintainers (1)
Downloads
Today 5
This Week 321
This Month 341
Last Day 21
Last Week 355
Last Month 1,616
Dependencies (2)
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |