mimos
Advanced tools
Comparing version 2.0.2 to 3.0.0
@@ -13,5 +13,5 @@ # How to contribute | ||
* Fork the repository on GitHub. | ||
* Fix the issue ensuring that your code follows the [style guide](https://github.com/hapijs/hapi/blob/master/docs/Style.md). | ||
* Fix the issue ensuring that your code follows the [style guide](https://github.com/hapijs/contrib/blob/master/Style.md). | ||
* Add tests for your new code ensuring that you have 100% code coverage (we can help you reach 100% but will not merge without it). | ||
* Run `npm test` to generate a report of test coverage | ||
* [Pull requests](http://help.github.com/send-pull-requests/) should be made to the [master branch](https://github.com/hapijs/mimos/tree/master). |
@@ -0,6 +1,8 @@ | ||
'use strict'; | ||
// Load modules | ||
var Path = require('path'); | ||
var Hoek = require('hoek'); | ||
var MimeDb = require('mime-db'); | ||
const Path = require('path'); | ||
const Hoek = require('hoek'); | ||
const MimeDb = require('mime-db'); | ||
@@ -10,3 +12,3 @@ | ||
var internals = {}; | ||
const internals = {}; | ||
@@ -21,5 +23,5 @@ | ||
Hoek.assert(this.constructor === internals.Mimos, 'Mimos must be created with new'); | ||
Hoek.assert(this && this.constructor === internals.Mimos, 'Mimos must be created with new'); | ||
var result = options.override ? internals.compile(options.override) : internals.base; | ||
const result = options.override ? internals.compile(options.override) : internals.base; | ||
this._byType = result.byType; | ||
@@ -32,6 +34,6 @@ this._byExtension = result.byExtension; | ||
var db = Hoek.clone(MimeDb); | ||
const db = Hoek.clone(MimeDb); | ||
Hoek.merge(db, override, true, false); | ||
var result = { | ||
const result = { | ||
byType: db, | ||
@@ -41,6 +43,6 @@ byExtension: {} | ||
var keys = Object.keys(result.byType); | ||
for (var i = 0, il = keys.length; i < il; i++) { | ||
var type = keys[i]; | ||
var mime = result.byType[type]; | ||
const keys = Object.keys(result.byType); | ||
for (let i = 0; i < keys.length; i++) { | ||
const type = keys[i]; | ||
const mime = result.byType[type]; | ||
mime.type = mime.type || type; | ||
@@ -53,4 +55,4 @@ mime.source = mime.source || 'mime-db'; | ||
for (var j = 0, jl = mime.extensions.length; j < jl; j++) { | ||
var ext = mime.extensions[j]; | ||
for (let j = 0; j < mime.extensions.length; j++) { | ||
const ext = mime.extensions[j]; | ||
result.byExtension[ext] = mime; | ||
@@ -69,7 +71,7 @@ } | ||
var extension = Path.extname(path).slice(1).toLowerCase(); | ||
var mime = this._byExtension[extension] || {}; | ||
const extension = Path.extname(path).slice(1).toLowerCase(); | ||
const mime = this._byExtension[extension] || {}; | ||
if (mime.predicate) { | ||
return mime.predicate(Hoek.clone(mime)); | ||
return mime.predicate(Hoek.clone(mime)); | ||
} | ||
@@ -84,3 +86,3 @@ | ||
type = type.split(';', 1)[0].trim().toLowerCase(); | ||
var mime = this._byType[type]; | ||
let mime = this._byType[type]; | ||
if (!mime) { | ||
@@ -103,2 +105,2 @@ mime = { | ||
return mime; | ||
}; | ||
}; |
{ | ||
"name": "mimos", | ||
"description": "Mime database interface", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"repository": "git://github.com/hapijs/mimos", | ||
"main": "index", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
@@ -13,21 +13,17 @@ "mime", | ||
"engines": { | ||
"node": ">=0.10.32" | ||
"node": ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "2.x.x", | ||
"hoek": "3.x.x", | ||
"mime-db": "1.x.x" | ||
}, | ||
"devDependencies": { | ||
"code": "1.x.x", | ||
"lab": "4.x.x" | ||
"code": "2.x.x", | ||
"lab": "7.x.x" | ||
}, | ||
"scripts": { | ||
"test": "make test-cov" | ||
"test": "lab -m 5000 -t 100 -v -La code", | ||
"test-cov-html": "lab -m 5000 -r html -o coverage.html -La code" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "BSD", | ||
"url": "http://github.com/hapijs/mimos/raw/master/LICENSE" | ||
} | ||
] | ||
"license": "BSD-3-Clause" | ||
} |
137
README.md
@@ -6,3 +6,138 @@ #mimos | ||
[![Build Status](https://secure.travis-ci.org/hapijs/mimos.png)](http://travis-ci.org/hapijs/mimos) | ||
[![Current Version](https://img.shields.io/npm/v/mimos.svg)](https://www.npmjs.com/package/mimos) | ||
Lead Maintainer - [Eran Hammer](https://github.com/hueniverse) | ||
Lead Maintainer - [Adam Bretz](https://github.com/arb) | ||
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](https://github.com/jshttp/mime-db). 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](https://github.com/jshttp/mime-db#data-structure). 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. | ||
```js | ||
var mimos = new Mimos(); | ||
var 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. | ||
```js | ||
var mimos = new Mimos(); | ||
var 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. | ||
```js | ||
var 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; | ||
} | ||
} | ||
} | ||
} | ||
var 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' | ||
} | ||
*/ | ||
``` |
@@ -0,7 +1,8 @@ | ||
'use strict'; | ||
// Load modules | ||
var Code = require('code'); | ||
var Hoek = require('hoek'); | ||
var Lab = require('lab'); | ||
var Mimos = require('..'); | ||
const Code = require('code'); | ||
const Lab = require('lab'); | ||
const Mimos = require('..'); | ||
@@ -11,3 +12,3 @@ | ||
var internals = {}; | ||
const internals = {}; | ||
@@ -17,14 +18,14 @@ | ||
var lab = exports.lab = Lab.script(); | ||
var describe = lab.describe; | ||
var it = lab.it; | ||
var expect = Code.expect; | ||
const lab = exports.lab = Lab.script(); | ||
const describe = lab.describe; | ||
const it = lab.it; | ||
const expect = Code.expect; | ||
describe('Mimos', function() { | ||
describe('Mimos', () => { | ||
describe('path()', function () { | ||
describe('path()', () => { | ||
it('returns the mime type from a file path', function (done) { | ||
it('returns the mime type from a file path', (done) => { | ||
var mimos = new Mimos(); | ||
const mimos = new Mimos(); | ||
@@ -41,5 +42,5 @@ expect(mimos.path('/static/javascript/app.js')).deep.equal({ | ||
it('returns empty object if a match can not be found', function (done) { | ||
it('returns empty object if a match can not be found', (done) => { | ||
var mimos = new Mimos(); | ||
const mimos = new Mimos(); | ||
@@ -50,7 +51,7 @@ expect(mimos.path('/static/javascript')).to.deep.equal({}); | ||
it('ignores extension upper case', function (done) { | ||
it('ignores extension upper case', (done) => { | ||
var lower = '/static/image/image.jpg'; | ||
var upper = '/static/image/image.JPG'; | ||
var mimos = new Mimos(); | ||
const lower = '/static/image/image.jpg'; | ||
const upper = '/static/image/image.JPG'; | ||
const mimos = new Mimos(); | ||
@@ -63,7 +64,7 @@ expect(mimos.path(lower).type).to.equal(mimos.path(upper).type); | ||
describe('type()', function () { | ||
describe('type()', () => { | ||
it('returns a found type', function (done) { | ||
it('returns a found type', (done) => { | ||
var mimos = new Mimos(); | ||
const mimos = new Mimos(); | ||
@@ -79,5 +80,5 @@ expect(mimos.type('text/plain')).to.deep.equal({ | ||
it('returns a missing type', function (done) { | ||
it('returns a missing type', (done) => { | ||
var mimos = new Mimos(); | ||
const mimos = new Mimos(); | ||
@@ -94,5 +95,5 @@ expect(mimos.type('hapi/test')).to.deep.equal({ | ||
it('accepts an override object to make adjustments to the internal mime database', function (done) { | ||
it('accepts an override object to make adjustments to the internal mime database', (done) => { | ||
var nodeModule = { | ||
const nodeModule = { | ||
source: 'iana', | ||
@@ -103,3 +104,3 @@ compressible: false, | ||
}; | ||
var dbOverwrite = { | ||
const dbOverwrite = { | ||
override: { | ||
@@ -110,3 +111,3 @@ 'node/module': nodeModule | ||
var mimos = new Mimos(dbOverwrite); | ||
const mimos = new Mimos(dbOverwrite); | ||
expect(mimos.type('node/module')).to.deep.equal(nodeModule); | ||
@@ -118,12 +119,12 @@ expect(mimos.path('/node_modules/node/module.npm')).to.deep.equal(nodeModule); | ||
it('allows built-in types to be replaced with user mime data', function (done) { | ||
it('allows built-in types to be replaced with user mime data', (done) => { | ||
var jsModule = { | ||
const jsModule = { | ||
source: 'iana', | ||
charset: 'UTF-8', | ||
compressible: true, | ||
extensions: [ 'js', 'javascript' ], | ||
extensions: ['js', 'javascript'], | ||
type: 'text/javascript' | ||
}; | ||
var dbOverwrite = { | ||
const dbOverwrite = { | ||
override: { | ||
@@ -134,3 +135,3 @@ 'application/javascript': jsModule | ||
var mimos = new Mimos(dbOverwrite); | ||
const mimos = new Mimos(dbOverwrite); | ||
@@ -143,5 +144,5 @@ expect(mimos.type('application/javascript')).to.deep.equal(jsModule); | ||
it('executes a predicate function if it is provided', function (done) { | ||
it('executes a predicate function if it is provided', (done) => { | ||
var jsModule = { | ||
const jsModule = { | ||
predicate: function (mime) { | ||
@@ -152,7 +153,7 @@ | ||
type: mime.type | ||
} | ||
}; | ||
}, | ||
type: 'text/javascript' | ||
}; | ||
var dbOverwrite = { | ||
const dbOverwrite = { | ||
override: { | ||
@@ -163,5 +164,5 @@ 'application/javascript': jsModule | ||
var mimos = new Mimos(dbOverwrite); | ||
const mimos = new Mimos(dbOverwrite); | ||
var typeResult = mimos.type('application/javascript'); | ||
const typeResult = mimos.type('application/javascript'); | ||
@@ -173,3 +174,3 @@ expect(typeResult).to.deep.equal({ | ||
var pathResult = mimos.path('/static/js/app.js'); | ||
const pathResult = mimos.path('/static/js/app.js'); | ||
@@ -184,7 +185,7 @@ expect(pathResult).to.deep.equal({ | ||
it('throws an error if created without new', function (done) { | ||
it('throws an error if created without new', (done) => { | ||
expect(function () { | ||
expect(() => { | ||
var mimos = Mimos(); | ||
Mimos(); | ||
}).to.throw('Mimos must be created with new'); | ||
@@ -195,7 +196,7 @@ done(); | ||
it('throws an error if the predicate option is not a functino', function (done) { | ||
it('throws an error if the predicate option is not a functino', (done) => { | ||
expect(function () { | ||
expect(() => { | ||
var mimos = new Mimos({ | ||
new Mimos({ | ||
override: { | ||
@@ -202,0 +203,0 @@ 'application/javascript': { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14727
207
143
8
+ Addedhoek@3.0.4(transitive)
- Removedhoek@2.16.3(transitive)
Updatedhoek@3.x.x