mime-types
Advanced tools
Comparing version 2.0.14 to 2.1.0
@@ -0,1 +1,16 @@ | ||
2.1.0 / 2015-06-07 | ||
================== | ||
* Fix incorrectly treating extension-less file name as extension | ||
- i.e. `'path/to/json'` will no longer return `application/json` | ||
* Fix `.charset(type)` to accept parameters | ||
* Fix `.charset(type)` to match case-insensitive | ||
* Improve generation of extension to MIME mapping | ||
* Refactor internals for readability and no argument reassignment | ||
* Prefer `application/*` MIME types from the same source | ||
* Prefer any type over `application/octet-stream` | ||
* deps: mime-db@~1.13.0 | ||
- Add nginx as a source | ||
- Add new mime types | ||
2.0.14 / 2015-06-06 | ||
@@ -2,0 +17,0 @@ =================== |
213
index.js
@@ -0,44 +1,66 @@ | ||
/*! | ||
* mime-types | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
'use strict' | ||
/** | ||
* Module dependencies. | ||
* @private | ||
*/ | ||
var db = require('mime-db') | ||
var extname = require('path').extname | ||
// types[extension] = type | ||
/** | ||
* Module variables. | ||
* @private | ||
*/ | ||
var extractTypeRegExp = /^\s*([^;\s]*)(?:;|\s|$)/ | ||
var textTypeRegExp = /^text\//i | ||
/** | ||
* Module exports. | ||
* @public | ||
*/ | ||
exports.charset = charset | ||
exports.charsets = { lookup: charset } | ||
exports.contentType = contentType | ||
exports.extension = extension | ||
exports.extensions = Object.create(null) | ||
exports.lookup = lookup | ||
exports.types = Object.create(null) | ||
// extensions[type] = [extensions] | ||
exports.extensions = Object.create(null) | ||
Object.keys(db).forEach(function (name) { | ||
var mime = db[name] | ||
var exts = mime.extensions | ||
if (!exts || !exts.length) return | ||
exports.extensions[name] = exts | ||
exts.forEach(function (ext) { | ||
exports.types[ext] = name | ||
}) | ||
}) | ||
// Populate the extensions/types maps | ||
populateMaps(exports.extensions, exports.types) | ||
exports.lookup = function (string) { | ||
if (!string || typeof string !== "string") return false | ||
// remove any leading paths, though we should just use path.basename | ||
string = string.replace(/.*[\.\/\\]/, '').toLowerCase() | ||
if (!string) return false | ||
return exports.types[string] || false | ||
} | ||
/** | ||
* Get the default charset for a MIME type. | ||
* | ||
* @param {string} type | ||
* @return {boolean|string} | ||
*/ | ||
exports.extension = function (type) { | ||
if (!type || typeof type !== "string") return false | ||
// to do: use media-typer | ||
type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/) | ||
if (!type) return false | ||
var exts = exports.extensions[type[1].toLowerCase()] | ||
if (!exts || !exts.length) return false | ||
return exts[0] | ||
} | ||
function charset(type) { | ||
if (!type || typeof type !== 'string') { | ||
return false | ||
} | ||
// type has to be an exact mime type | ||
exports.charset = function (type) { | ||
var mime = db[type] | ||
if (mime && mime.charset) return mime.charset | ||
// TODO: use media-typer | ||
var match = extractTypeRegExp.exec(type) | ||
var mime = match && db[match[1].toLowerCase()] | ||
if (mime && mime.charset) { | ||
return mime.charset | ||
} | ||
// default text/* to utf-8 | ||
if (/^text\//.test(type)) return 'UTF-8' | ||
if (match && textTypeRegExp.test(match[1])) { | ||
return 'UTF-8' | ||
} | ||
@@ -48,17 +70,118 @@ return false | ||
// backwards compatibility | ||
exports.charsets = { | ||
lookup: exports.charset | ||
/** | ||
* Create a full Content-Type header given a MIME type or extension. | ||
* | ||
* @param {string} str | ||
* @return {boolean|string} | ||
*/ | ||
function contentType(str) { | ||
// TODO: should this even be in this module? | ||
if (!str || typeof str !== 'string') { | ||
return false | ||
} | ||
var mime = str.indexOf('/') === -1 | ||
? exports.lookup(str) | ||
: str | ||
if (!mime) { | ||
return false | ||
} | ||
// TODO: use content-type or other module | ||
if (mime.indexOf('charset') === -1) { | ||
var charset = exports.charset(mime) | ||
if (charset) mime += '; charset=' + charset.toLowerCase() | ||
} | ||
return mime | ||
} | ||
// to do: maybe use set-type module or something | ||
exports.contentType = function (type) { | ||
if (!type || typeof type !== "string") return false | ||
if (!~type.indexOf('/')) type = exports.lookup(type) | ||
if (!type) return false | ||
if (!~type.indexOf('charset')) { | ||
var charset = exports.charset(type) | ||
if (charset) type += '; charset=' + charset.toLowerCase() | ||
/** | ||
* Get the default extension for a MIME type. | ||
* | ||
* @param {string} type | ||
* @return {boolean|string} | ||
*/ | ||
function extension(type) { | ||
if (!type || typeof type !== 'string') { | ||
return false | ||
} | ||
return type | ||
// TODO: use media-typer | ||
var match = extractTypeRegExp.exec(type) | ||
// get extensions | ||
var exts = match && exports.extensions[match[1].toLowerCase()] | ||
if (!exts || !exts.length) { | ||
return false | ||
} | ||
return exts[0] | ||
} | ||
/** | ||
* Lookup the MIME type for a file path/extension. | ||
* | ||
* @param {string} path | ||
* @return {boolean|string} | ||
*/ | ||
function lookup(path) { | ||
if (!path || typeof path !== 'string') { | ||
return false | ||
} | ||
// get the extension ("ext" or ".ext" or full path) | ||
var extension = extname('x.' + path) | ||
.toLowerCase() | ||
.substr(1) | ||
if (!extension) { | ||
return false | ||
} | ||
return exports.types[extension] || false | ||
} | ||
/** | ||
* Populate the extensions and types maps. | ||
* @private | ||
*/ | ||
function populateMaps(extensions, types) { | ||
// source preference (least -> most) | ||
var preference = ['nginx', 'apache', undefined, 'iana'] | ||
Object.keys(db).forEach(function forEachMimeType(type) { | ||
var mime = db[type] | ||
var exts = mime.extensions | ||
if (!exts || !exts.length) { | ||
return | ||
} | ||
// mime -> extensions | ||
extensions[type] = exts | ||
// extension -> mime | ||
exts.forEach(function forEachExtension(ext) { | ||
if (types[ext]) { | ||
var from = preference.indexOf(db[types[ext]].source) | ||
var to = preference.indexOf(mime.source) | ||
if (types[ext] !== 'application/octet-stream' | ||
&& from > to || (from === to && types[ext].substr(0, 12) === 'application/')) { | ||
// skip the remapping | ||
return | ||
} | ||
} | ||
// set the extension -> mime | ||
types[ext] = type | ||
}) | ||
}) | ||
} |
{ | ||
"name": "mime-types", | ||
"description": "The ultimate javascript content-type utility.", | ||
"version": "2.0.14", | ||
"version": "2.1.0", | ||
"contributors": [ | ||
@@ -17,6 +17,6 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"dependencies": { | ||
"mime-db": "~1.12.0" | ||
"mime-db": "~1.13.0" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0.3.9", | ||
"istanbul": "0.3.14", | ||
"mocha": "~1.21.5" | ||
@@ -23,0 +23,0 @@ }, |
@@ -45,6 +45,7 @@ # mime-types | ||
```js | ||
mime.lookup('json') // 'application/json' | ||
mime.lookup('.md') // 'text/x-markdown' | ||
mime.lookup('file.html') // 'text/html' | ||
mime.lookup('folder/file.js') // 'application/javascript' | ||
mime.lookup('json') // 'application/json' | ||
mime.lookup('.md') // 'text/x-markdown' | ||
mime.lookup('file.html') // 'text/html' | ||
mime.lookup('folder/file.js') // 'application/javascript' | ||
mime.lookup('folder/.htaccess') // false | ||
@@ -51,0 +52,0 @@ mime.lookup('cats') // false |
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
11133
147
104
+ Addedmime-db@1.13.0(transitive)
- Removedmime-db@1.12.0(transitive)
Updatedmime-db@~1.13.0