1
- var path = require ( 'path' ) ;
2
1
var fs = require ( 'fs' ) ;
3
2
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
+
4
9
function Mime ( ) {
5
10
// Map of extension -> mime type
6
11
this . types = Object . create ( null ) ;
@@ -18,16 +23,27 @@ function Mime() {
18
23
*
19
24
* @param map (Object) type definitions
20
25
*/
21
- Mime . prototype . define = function ( map ) {
26
+ Mime . prototype . define = function ( map ) {
22
27
for ( var type in map ) {
23
28
var exts = map [ type ] ;
29
+
24
30
for ( var i = 0 ; i < exts . length ; i ++ ) {
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 ) ;
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 ;
28
44
}
29
45
30
- this . types [ exts [ i ] ] = type ;
46
+ this . types [ ext ] = type ;
31
47
}
32
48
33
49
// Default extension is the first one we encounter
@@ -48,9 +64,9 @@ Mime.prototype.define = function (map) {
48
64
Mime . prototype . load = function ( file ) {
49
65
this . _loading = file ;
50
66
// Read file and split into lines
51
- var map = { } ,
52
- content = fs . readFileSync ( file , 'ascii' ) ,
53
- lines = content . split ( / [ \r \n ] + / ) ;
67
+ var map = { } ;
68
+ var content = fs . readFileSync ( file , 'ascii' ) ;
69
+ var lines = content . split ( / [ \r \n ] + / ) ;
54
70
55
71
lines . forEach ( function ( line ) {
56
72
// Clean up whitespace/comments, and split into fields
@@ -69,7 +85,7 @@ Mime.prototype.load = function(file) {
69
85
Mime . prototype . lookup = function ( path , fallback ) {
70
86
var ext = path . replace ( / .* [ \. \/ \\ ] / , '' ) . toLowerCase ( ) ;
71
87
72
- return this . types [ ext ] || fallback || this . default_type ;
88
+ return this . types [ ext ] || fallback || this . default_type ; // eslint-disable-line camelcase
73
89
} ;
74
90
75
91
/**
@@ -87,7 +103,7 @@ var mime = new Mime();
87
103
mime . define ( require ( './types.json' ) ) ;
88
104
89
105
// Default type
90
- mime . default_type = mime . lookup ( 'bin' ) ;
106
+ mime . default_type = mime . lookup ( 'bin' ) ; // eslint-disable-line camelcase
91
107
92
108
//
93
109
// Additional API specific to the default instance
0 commit comments