Comparing version 1.3.5 to 1.3.6
@@ -7,2 +7,3 @@ /** | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
@@ -25,20 +26,2 @@ // | ||
// | ||
// 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 | ||
@@ -65,3 +48,4 @@ // | ||
assert.equal('application/octet-stream', mime.lookup('file.buffer')); | ||
assert.equal('audio/mp4', mime.lookup('file.m4a')); | ||
// TODO: Uncomment once #157 is resolved | ||
// assert.equal('audio/mp4', mime.lookup('file.m4a')); | ||
assert.equal('font/opentype', mime.lookup('file.otf')); | ||
@@ -74,5 +58,7 @@ | ||
assert.equal('UTF-8', mime.charsets.lookup('text/plain')); | ||
assert.equal(undefined, mime.charsets.lookup(mime.types.js)); | ||
assert.equal('UTF-8', mime.charsets.lookup(mime.types.js)); | ||
assert.equal('UTF-8', mime.charsets.lookup(mime.types.json)); | ||
assert.equal(undefined, mime.charsets.lookup(mime.types.bin)); | ||
assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback')); | ||
console.log('\nAll tests passed'); |
40
mime.js
@@ -0,9 +1,4 @@ | ||
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() { | ||
@@ -26,23 +21,12 @@ // Map of extension -> mime type | ||
*/ | ||
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; | ||
} | ||
@@ -68,5 +52,5 @@ | ||
// 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]+/); | ||
@@ -90,3 +74,3 @@ lines.forEach(function(line) { | ||
return this.types[ext] || fallback || this.default_type; // eslint-disable-line camelcase | ||
return this.types[ext] || fallback || this.default_type; | ||
}; | ||
@@ -109,3 +93,3 @@ | ||
// Default type | ||
mime.default_type = mime.lookup('bin'); // eslint-disable-line camelcase | ||
mime.default_type = mime.lookup('bin'); | ||
@@ -124,3 +108,3 @@ // | ||
// Assume text types are utf8 | ||
return (/^text\//).test(mimeType) ? 'UTF-8' : fallback; | ||
return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback; | ||
} | ||
@@ -127,0 +111,0 @@ }; |
@@ -24,3 +24,3 @@ { | ||
"scripts": { | ||
"prepare": "node build/build.js > types.json", | ||
"prepublish": "node build/build.js > types.json", | ||
"test": "node build/test.js" | ||
@@ -38,3 +38,3 @@ }, | ||
}, | ||
"version": "1.3.5" | ||
"version": "1.3.6" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
0
40437
9
275