Skip to content

Commit 29f5a46

Browse files
committedMay 12, 2017
Revert "Use facets to prioritize when resolving type conflicts" (Fix #157)
This reverts commit 902ec07.
1 parent f7ccb94 commit 29f5a46

File tree

5 files changed

+13
-94
lines changed

5 files changed

+13
-94
lines changed
 

‎.eslintrc.json

-46
This file was deleted.

‎.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
node_modules
22
npm-debug.log
3-
*.sw* # VIM temp files
4-
.DS_Store # Mac desktop services store

‎build/test.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var mime = require('../mime');
66
var assert = require('assert');
7+
var path = require('path');
78

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

23-
//
24-
// Test types that are known to have conflicting definitions but different facet priorities
25-
//
26-
27-
assert.equal('application/octet-stream', mime.lookup('dmg'));
28-
assert.equal('application/bdoc', mime.lookup('bdoc'));
29-
assert.equal('application/octet-stream', mime.lookup('deb'));
30-
assert.equal('application/octet-stream', mime.lookup('iso'));
31-
assert.equal('application/octet-stream', mime.lookup('exe'));
32-
assert.equal('application/octet-stream', mime.lookup('exe'));
33-
assert.equal('application/octet-stream', mime.lookup('dll'));
34-
assert.equal('application/octet-stream', mime.lookup('msi'));
35-
assert.equal('application/vnd.palm', mime.lookup('pdb'));
36-
assert.equal('audio/mp3', mime.lookup('mp3'));
37-
assert.equal('audio/mp4', mime.lookup('m4a'));
38-
assert.equal('font/opentype', mime.lookup('otf'));
39-
assert.equal('image/bmp', mime.lookup('bmp'));
40-
4124
//
4225
// Test extensions
4326
//

‎mime.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1+
var path = require('path');
12
var fs = require('fs');
23

3-
// If two types claim the same extension, we resolve the conflict by checking
4-
// facet precedence. https://tools.ietf.org/html/rfc6838#section-3
5-
// Facets listed here are in order of decreasing precedence
6-
var FACETS = ['vnd.', 'x-', 'prs.'];
7-
var FACET_RE = new RegExp('/(' + FACETS.join('|') + ')');
8-
94
function Mime() {
105
// Map of extension -> mime type
116
this.types = Object.create(null);
@@ -23,27 +18,16 @@ function Mime() {
2318
*
2419
* @param map (Object) type definitions
2520
*/
26-
Mime.prototype.define = function(map) {
21+
Mime.prototype.define = function (map) {
2722
for (var type in map) {
2823
var exts = map[type];
29-
3024
for (var i = 0; i < exts.length; i++) {
31-
var ext = exts[i];
32-
var found = this.types[ext];
33-
34-
// If there's already a type for this extension ...
35-
if (found) {
36-
var pri0 = FACETS.indexOf(FACET_RE.test(found) && RegExp.$1);
37-
var pri1 = FACETS.indexOf(FACET_RE.test(type) && RegExp.$1);
38-
39-
if (process.env.DEBUG_MIME) console.warn('Type conflict for .' + ext +
40-
' (' + found + ' pri=' + pri0 + ', ' + type + ' pri=' + pri1 + ')');
41-
42-
// Prioritize based on facet precedence
43-
if (pri0 <= pri1) continue;
25+
if (process.env.DEBUG_MIME && this.types[exts[i]]) {
26+
console.warn((this._loading || "define()").replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
27+
this.types[exts[i]] + ' to ' + type);
4428
}
4529

46-
this.types[ext] = type;
30+
this.types[exts[i]] = type;
4731
}
4832

4933
// Default extension is the first one we encounter
@@ -64,9 +48,9 @@ Mime.prototype.define = function(map) {
6448
Mime.prototype.load = function(file) {
6549
this._loading = file;
6650
// Read file and split into lines
67-
var map = {};
68-
var content = fs.readFileSync(file, 'ascii');
69-
var lines = content.split(/[\r\n]+/);
51+
var map = {},
52+
content = fs.readFileSync(file, 'ascii'),
53+
lines = content.split(/[\r\n]+/);
7054

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

88-
return this.types[ext] || fallback || this.default_type; // eslint-disable-line camelcase
72+
return this.types[ext] || fallback || this.default_type;
8973
};
9074

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

10589
// Default type
106-
mime.default_type = mime.lookup('bin'); // eslint-disable-line camelcase
90+
mime.default_type = mime.lookup('bin');
10791

10892
//
10993
// Additional API specific to the default instance

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"mime-db": "^1.22.0"
2222
},
2323
"scripts": {
24-
"prepare": "node build/build.js > types.json",
24+
"prepublish": "node build/build.js > types.json",
2525
"test": "node build/test.js"
2626
},
2727
"keywords": [

0 commit comments

Comments
 (0)
Please sign in to comment.