Skip to content

Commit

Permalink
Use mime-db as canonical source of mime types.
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Feb 4, 2015
1 parent dacee09 commit c507d50
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1,761 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
types.json
11 changes: 11 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var db = require('mime-db');

var mapByType = {};
Object.keys(db).forEach(function(key) {
var extensions = db[key].extensions;
if (extensions) {
mapByType[key] = extensions;
}
});

console.log(JSON.stringify(mapByType));
57 changes: 57 additions & 0 deletions build/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Usage: node test.js
*/

var mime = require('../mime');
var assert = require('assert');
var path = require('path');

//
// Test mime lookups
//

assert.equal('text/plain', mime.lookup('text.txt')); // normal file
assert.equal('text/plain', mime.lookup('TEXT.TXT')); // uppercase
assert.equal('text/plain', mime.lookup('dir/text.txt')); // dir + file
assert.equal('text/plain', mime.lookup('.text.txt')); // hidden file
assert.equal('text/plain', mime.lookup('.txt')); // nameless
assert.equal('text/plain', mime.lookup('txt')); // extension-only
assert.equal('text/plain', mime.lookup('/txt')); // extension-less ()
assert.equal('text/plain', mime.lookup('\\txt')); // Windows, extension-less
assert.equal('application/octet-stream', mime.lookup('text.nope')); // unrecognized
assert.equal('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default

//
// Test extensions
//

assert.equal('txt', mime.extension(mime.types.text));
assert.equal('html', mime.extension(mime.types.htm));
assert.equal('bin', mime.extension('application/octet-stream'));
assert.equal('bin', mime.extension('application/octet-stream '));
assert.equal('html', mime.extension(' text/html; charset=UTF-8'));
assert.equal('html', mime.extension('text/html; charset=UTF-8 '));
assert.equal('html', mime.extension('text/html; charset=UTF-8'));
assert.equal('html', mime.extension('text/html ; charset=UTF-8'));
assert.equal('html', mime.extension('text/html;charset=UTF-8'));
assert.equal('html', mime.extension('text/Html;charset=UTF-8'));
assert.equal(undefined, mime.extension('unrecognized'));

//
// Test node.types lookups
//

assert.equal('application/font-woff', mime.lookup('file.woff'));
assert.equal('application/octet-stream', mime.lookup('file.buffer'));
assert.equal('audio/mp4', mime.lookup('file.m4a'));
assert.equal('font/opentype', mime.lookup('file.otf'));

//
// Test charsets
//

assert.equal('UTF-8', mime.charsets.lookup('text/plain'));
assert.equal(undefined, mime.charsets.lookup(mime.types.js));
assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback'));

console.log('\nAll tests passed');
10 changes: 2 additions & 8 deletions mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function Mime() {
Mime.prototype.define = function (map) {
for (var type in map) {
var exts = map[type];

for (var i = 0; i < exts.length; i++) {
if (process.env.DEBUG_MIME && this.types[exts]) {
console.warn(this._loading.replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
Expand All @@ -47,7 +46,6 @@ Mime.prototype.define = function (map) {
* @param file (String) path of file to load.
*/
Mime.prototype.load = function(file) {

this._loading = file;
// Read file and split into lines
var map = {},
Expand Down Expand Up @@ -85,12 +83,8 @@ Mime.prototype.extension = function(mimeType) {
// Default instance
var mime = new Mime();

// Load local copy of
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
mime.load(path.join(__dirname, 'types/mime.types'));

// Load additional types from node.js community
mime.load(path.join(__dirname, 'types/node.types'));
// Define built-in types
mime.define(require('./types.json'));

// Default type
mime.default_type = mime.lookup('bin');
Expand Down
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@
"url": "https://raw.github.com/broofa/node-mime/master/LICENSE"
}
],
"devDependencies": {},
"keywords": ["util", "mime"],
"scripts": {
"install": "node build/build.js > types.json",
"test": "node build/test.js"
},
"devDependencies": {
"mime-db": "^1.2.0"
},
"keywords": [
"util",
"mime"
],
"main": "mime.js",
"name": "mime",
"repository": {"url": "https://github.com/broofa/node-mime", "type": "git"},
"version": "1.2.11"
"repository": {
"url": "https://github.com/broofa/node-mime",
"type": "git"
},
"version": "1.2.13"
}
84 changes: 0 additions & 84 deletions test.js

This file was deleted.

0 comments on commit c507d50

Please sign in to comment.