Comparing version 1.2.5 to 1.2.6
159
mime.js
@@ -1,93 +0,104 @@ | ||
var path = require('path'), | ||
fs = require('fs'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var mime = module.exports = { | ||
// Map of extension to mime type | ||
types: Object.create(null), | ||
function Mime() { | ||
// Map of extension -> mime type | ||
this.types = Object.create(null); | ||
// Map of mime type to extension | ||
extensions :Object.create(null), | ||
// Map of mime type -> extension | ||
this.extensions = Object.create(null); | ||
} | ||
/** | ||
* Define mimetype -> extension mappings. Each key is a mime-type that maps | ||
* to an array of extensions associated with the type. The first extension is | ||
* used as the default extension for the type. | ||
* | ||
* e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']}); | ||
* | ||
* @param map (Object) type definitions | ||
*/ | ||
define: function(map) { | ||
for (var type in map) { | ||
var exts = map[type]; | ||
/** | ||
* Define mimetype -> extension mappings. Each key is a mime-type that maps | ||
* to an array of extensions associated with the type. The first extension is | ||
* used as the default extension for the type. | ||
* | ||
* e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']}); | ||
* | ||
* @param map (Object) type definitions | ||
*/ | ||
Mime.prototype.define = function (map) { | ||
for (var type in map) { | ||
var exts = map[type]; | ||
for (var i = 0; i < exts.length; i++) { | ||
mime.types[exts[i]] = type; | ||
} | ||
for (var i = 0; i < exts.length; i++) { | ||
this.types[exts[i]] = type; | ||
} | ||
// Default extension is the first one we encounter | ||
if (!mime.extensions[type]) { | ||
mime.extensions[type] = exts[0]; | ||
} | ||
// Default extension is the first one we encounter | ||
if (!this.extensions[type]) { | ||
this.extensions[type] = exts[0]; | ||
} | ||
}, | ||
} | ||
}; | ||
/** | ||
* Load an Apache2-style ".types" file | ||
* | ||
* This may be called multiple times (it's expected). Where files declare | ||
* overlapping types/extensions, the last file wins. | ||
* | ||
* @param file (String) path of file to load. | ||
*/ | ||
load: function(file) { | ||
// Read file and split into lines | ||
var map = {}, | ||
content = fs.readFileSync(file, 'ascii'), | ||
lines = content.split(/[\r\n]+/); | ||
/** | ||
* Load an Apache2-style ".types" file | ||
* | ||
* This may be called multiple times (it's expected). Where files declare | ||
* overlapping types/extensions, the last file wins. | ||
* | ||
* @param file (String) path of file to load. | ||
*/ | ||
Mime.prototype.load = function(file) { | ||
// Read file and split into lines | ||
var map = {}, | ||
content = fs.readFileSync(file, 'ascii'), | ||
lines = content.split(/[\r\n]+/); | ||
lines.forEach(function(line, lineno) { | ||
// Clean up whitespace/comments, and split into fields | ||
var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/); | ||
map[fields.shift()] = fields; | ||
}); | ||
lines.forEach(function(line) { | ||
// Clean up whitespace/comments, and split into fields | ||
var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/); | ||
map[fields.shift()] = fields; | ||
}); | ||
mime.define(map); | ||
}, | ||
this.define(map); | ||
}; | ||
/** | ||
* Lookup a mime type based on extension | ||
*/ | ||
lookup: function(path, fallback) { | ||
var ext = path.replace(/.*[\.\/]/, '').toLowerCase(); | ||
/** | ||
* Lookup a mime type based on extension | ||
*/ | ||
Mime.prototype.lookup = function(path, fallback) { | ||
var ext = path.replace(/.*[\.\/]/, '').toLowerCase(); | ||
return mime.types[ext] || fallback || mime.default_type | ||
}, | ||
return this.types[ext] || fallback || this.default_type; | ||
}; | ||
/** | ||
* Return file extension associated with a mime type | ||
*/ | ||
extension: function(mimeType) { | ||
return mime.extensions[mimeType]; | ||
}, | ||
/** | ||
* Lookup a charset based on mime type. | ||
*/ | ||
charsets: { | ||
lookup: function (mimeType, fallback) { | ||
// Assume text types are utf8. Modify mime logic as needed. | ||
return (/^text\//).test(mimeType) ? 'UTF-8' : fallback; | ||
} | ||
} | ||
/** | ||
* Return file extension associated with a mime type | ||
*/ | ||
Mime.prototype.extension = function(mimeType) { | ||
return this.extensions[mimeType]; | ||
}; | ||
// Load our local copy of | ||
// Default instance | ||
var mime = new Mime(); | ||
// Load local copy of | ||
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | ||
mime.load(path.join(__dirname, 'types/mime.types')); | ||
// Overlay enhancements submitted by the node.js community | ||
// Load additional types from node.js community | ||
mime.load(path.join(__dirname, 'types/node.types')); | ||
// Set the default type | ||
mime.default_type = mime.types.bin; | ||
// Default type | ||
mime.default_type = mime.lookup('bin'); | ||
// | ||
// Additional API specific to the default instance | ||
// | ||
mime.Mime = Mime; | ||
/** | ||
* Lookup a charset based on mime type. | ||
*/ | ||
mime.charsets = { | ||
lookup: function(mimeType, fallback) { | ||
// Assume text types are utf8 | ||
return (/^text\//).test(mimeType) ? 'UTF-8' : fallback; | ||
} | ||
} | ||
module.exports = mime; |
@@ -20,4 +20,4 @@ { | ||
"name": "mime", | ||
"repository": {"url": "http://github.com/bentomas/node-mime", "type": "git"}, | ||
"version": "1.2.5" | ||
"repository": {"url": "https://github.com/broofa/node-mime", "type": "git"}, | ||
"version": "1.2.6" | ||
} |
@@ -28,2 +28,4 @@ /** | ||
eq('text/plain', mime.lookup('TEXT.TXT')); | ||
eq('text/event-stream', mime.lookup('text/event-stream')); | ||
eq('application/x-web-app-manifest+json', mime.lookup('text.webapp')); | ||
@@ -30,0 +32,0 @@ // |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
60391
128