Skip to content

Commit

Permalink
Revert "Use facets to prioritize when resolving type conflicts" (Fix #…
Browse files Browse the repository at this point in the history
…157)

This reverts commit 902ec07.
  • Loading branch information
broofa committed May 12, 2017
1 parent f7ccb94 commit 29f5a46
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 94 deletions.
46 changes: 0 additions & 46 deletions .eslintrc.json

This file was deleted.

2 changes: 0 additions & 2 deletions .gitignore
@@ -1,4 +1,2 @@
node_modules
npm-debug.log
*.sw* # VIM temp files
.DS_Store # Mac desktop services store
19 changes: 1 addition & 18 deletions build/test.js
Expand Up @@ -4,6 +4,7 @@

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

//
// Test mime lookups
Expand All @@ -20,24 +21,6 @@ assert.equal('text/plain', mime.lookup('\\txt')); // Windows, extension-l
assert.equal('application/octet-stream', mime.lookup('text.nope')); // unrecognized
assert.equal('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default

//
// Test types that are known to have conflicting definitions but different facet priorities
//

assert.equal('application/octet-stream', mime.lookup('dmg'));
assert.equal('application/bdoc', mime.lookup('bdoc'));
assert.equal('application/octet-stream', mime.lookup('deb'));
assert.equal('application/octet-stream', mime.lookup('iso'));
assert.equal('application/octet-stream', mime.lookup('exe'));
assert.equal('application/octet-stream', mime.lookup('exe'));
assert.equal('application/octet-stream', mime.lookup('dll'));
assert.equal('application/octet-stream', mime.lookup('msi'));
assert.equal('application/vnd.palm', mime.lookup('pdb'));
assert.equal('audio/mp3', mime.lookup('mp3'));
assert.equal('audio/mp4', mime.lookup('m4a'));
assert.equal('font/opentype', mime.lookup('otf'));
assert.equal('image/bmp', mime.lookup('bmp'));

//
// Test extensions
//
Expand Down
38 changes: 11 additions & 27 deletions mime.js
@@ -1,11 +1,6 @@
var path = require('path');
var fs = require('fs');

// If two types claim the same extension, we resolve the conflict by checking
// facet precedence. https://tools.ietf.org/html/rfc6838#section-3
// Facets listed here are in order of decreasing precedence
var FACETS = ['vnd.', 'x-', 'prs.'];
var FACET_RE = new RegExp('/(' + FACETS.join('|') + ')');

function Mime() {
// Map of extension -> mime type
this.types = Object.create(null);
Expand All @@ -23,27 +18,16 @@ function Mime() {
*
* @param map (Object) type definitions
*/
Mime.prototype.define = function(map) {
Mime.prototype.define = function (map) {
for (var type in map) {
var exts = map[type];

for (var i = 0; i < exts.length; i++) {
var ext = exts[i];
var found = this.types[ext];

// If there's already a type for this extension ...
if (found) {
var pri0 = FACETS.indexOf(FACET_RE.test(found) && RegExp.$1);
var pri1 = FACETS.indexOf(FACET_RE.test(type) && RegExp.$1);

if (process.env.DEBUG_MIME) console.warn('Type conflict for .' + ext +
' (' + found + ' pri=' + pri0 + ', ' + type + ' pri=' + pri1 + ')');

// Prioritize based on facet precedence
if (pri0 <= pri1) continue;
if (process.env.DEBUG_MIME && this.types[exts[i]]) {
console.warn((this._loading || "define()").replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
this.types[exts[i]] + ' to ' + type);
}

this.types[ext] = type;
this.types[exts[i]] = type;
}

// Default extension is the first one we encounter
Expand All @@ -64,9 +48,9 @@ Mime.prototype.define = function(map) {
Mime.prototype.load = function(file) {
this._loading = file;
// Read file and split into lines
var map = {};
var content = fs.readFileSync(file, 'ascii');
var lines = content.split(/[\r\n]+/);
var map = {},
content = fs.readFileSync(file, 'ascii'),
lines = content.split(/[\r\n]+/);

lines.forEach(function(line) {
// Clean up whitespace/comments, and split into fields
Expand All @@ -85,7 +69,7 @@ Mime.prototype.load = function(file) {
Mime.prototype.lookup = function(path, fallback) {
var ext = path.replace(/.*[\.\/\\]/, '').toLowerCase();

return this.types[ext] || fallback || this.default_type; // eslint-disable-line camelcase
return this.types[ext] || fallback || this.default_type;
};

/**
Expand All @@ -103,7 +87,7 @@ var mime = new Mime();
mime.define(require('./types.json'));

// Default type
mime.default_type = mime.lookup('bin'); // eslint-disable-line camelcase
mime.default_type = mime.lookup('bin');

//
// Additional API specific to the default instance
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"mime-db": "^1.22.0"
},
"scripts": {
"prepare": "node build/build.js > types.json",
"prepublish": "node build/build.js > types.json",
"test": "node build/test.js"
},
"keywords": [
Expand Down

0 comments on commit 29f5a46

Please sign in to comment.