1414
1515var db = require ( 'mime-db' )
1616var extname = require ( 'path' ) . extname
17+ var mimeScore = require ( './mimeScore' )
1718
1819/**
1920 * Module variables.
@@ -35,6 +36,7 @@ exports.extension = extension
3536exports . extensions = Object . create ( null )
3637exports . lookup = lookup
3738exports . types = Object . create ( null )
39+ exports . _extensionConflicts = [ ]
3840
3941// Populate the extensions/types maps
4042populateMaps ( exports . extensions , exports . types )
@@ -80,9 +82,7 @@ function contentType (str) {
8082 return false
8183 }
8284
83- var mime = str . indexOf ( '/' ) === - 1
84- ? exports . lookup ( str )
85- : str
85+ var mime = str . indexOf ( '/' ) === - 1 ? exports . lookup ( str ) : str
8686
8787 if ( ! mime ) {
8888 return false
@@ -152,9 +152,6 @@ function lookup (path) {
152152 */
153153
154154function populateMaps ( extensions , types ) {
155- // source preference (least -> most)
156- var preference = [ 'nginx' , 'apache' , undefined , 'iana' ]
157-
158155 Object . keys ( db ) . forEach ( function forEachMimeType ( type ) {
159156 var mime = db [ type ]
160157 var exts = mime . extensions
@@ -169,20 +166,46 @@ function populateMaps (extensions, types) {
169166 // extension -> mime
170167 for ( var i = 0 ; i < exts . length ; i ++ ) {
171168 var extension = exts [ i ]
172-
173- if ( types [ extension ] ) {
174- var from = preference . indexOf ( db [ types [ extension ] ] . source )
175- var to = preference . indexOf ( mime . source )
176-
177- if ( types [ extension ] !== 'application/octet-stream' &&
178- ( from > to || ( from === to && types [ extension ] . slice ( 0 , 12 ) === 'application/' ) ) ) {
179- // skip the remapping
180- continue
181- }
169+ types [ extension ] = _preferredType ( extension , types [ extension ] , type )
170+
171+ // DELETE (eventually): Capture extension->type maps that change as a
172+ // result of switching to mime-score. This is just to help make reviewing
173+ // PR #119 easier, and can be removed once that PR is approved.
174+ const legacyType = _preferredTypeLegacy (
175+ extension ,
176+ types [ extension ] ,
177+ type
178+ )
179+ if ( legacyType !== types [ extension ] ) {
180+ exports . _extensionConflicts . push ( [ extension , legacyType , types [ extension ] ] )
182181 }
183-
184- // set the extension -> mime
185- types [ extension ] = type
186182 }
187183 } )
188184}
185+
186+ // Resolve type conflict using mime-score
187+ function _preferredType ( ext , type0 , type1 ) {
188+ var score0 = type0 ? mimeScore ( type0 , db [ type0 ] . source ) : 0
189+ var score1 = type1 ? mimeScore ( type1 , db [ type1 ] . source ) : 0
190+
191+ return score0 > score1 ? type0 : type1
192+ }
193+
194+ // Resolve type conflict using pre-mime-score logic
195+ function _preferredTypeLegacy ( ext , type0 , type1 ) {
196+ var SOURCE_RANK = [ 'nginx' , 'apache' , undefined , 'iana' ]
197+
198+ var score0 = type0 ? SOURCE_RANK . indexOf ( db [ type0 ] . source ) : 0
199+ var score1 = type1 ? SOURCE_RANK . indexOf ( db [ type1 ] . source ) : 0
200+
201+ if (
202+ exports . types [ extension ] !== 'application/octet-stream' &&
203+ ( score0 > score1 ||
204+ ( score0 === score1 &&
205+ exports . types [ extension ] ?. slice ( 0 , 12 ) === 'application/' ) )
206+ ) {
207+ return type0
208+ }
209+
210+ return score0 > score1 ? type0 : type1
211+ }
0 commit comments