mime-types
Advanced tools
Comparing version 0.1.0 to 1.0.0
17
build.js
@@ -25,2 +25,12 @@ | ||
// http://en.wikipedia.org/wiki/Internet_media_type#Naming | ||
/** | ||
* Mime types and associated extensions are stored in the form: | ||
* | ||
* <type> <ext> <ext> <ext> | ||
* | ||
* And some are commented out with a leading `#` because they have no associated extensions. | ||
* This regexp checks whether a single line matches this format, ignoring lines that are just comments. | ||
* We could also just remove all lines that start with `#` if we want to make the JSON files smaller | ||
* and ignore all mime types without associated extensions. | ||
*/ | ||
var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/ | ||
@@ -34,6 +44,9 @@ text = text.split('\n') | ||
if (!match) return | ||
json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '').split(/\s+/).filter(Boolean) | ||
// remove the leading # and <type> and return all the <ext>s | ||
json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '') | ||
.split(/\s+/) | ||
.filter(Boolean) | ||
}) | ||
fs.writeFileSync('lib/' + path.basename(url).split('.')[0] + '.json', | ||
JSON.stringify(json, null, 2)) | ||
JSON.stringify(json, null, 2) + '\n') | ||
} | ||
@@ -40,0 +53,0 @@ |
@@ -17,3 +17,3 @@ | ||
exports.lookup = function (string) { | ||
if (!string) return false | ||
if (!string || typeof string !== "string") return false | ||
string = string.replace(/.*[\.\/\\]/, '').toLowerCase() | ||
@@ -25,3 +25,3 @@ if (!string) return false | ||
exports.extension = function (type) { | ||
if (!type) return false | ||
if (!type || typeof type !== "string") return false | ||
type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/) | ||
@@ -34,14 +34,26 @@ if (!type) return false | ||
exports.charsets = { | ||
lookup: function (type) { | ||
return /^text\//.test(type) | ||
? 'UTF-8' | ||
: false | ||
// type has to be an exact mime type | ||
exports.charset = function (type) { | ||
// special cases | ||
switch (type) { | ||
case 'application/json': return 'UTF-8' | ||
} | ||
// default text/* to utf-8 | ||
if (/^text\//.test(type)) return 'UTF-8' | ||
return false | ||
} | ||
// backwards compatibility | ||
exports.charsets = { | ||
lookup: exports.charset | ||
} | ||
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.charsets.lookup(type) | ||
var charset = exports.charset(type) | ||
if (charset) type += '; charset=' + charset.toLowerCase() | ||
@@ -48,0 +60,0 @@ } |
@@ -51,3 +51,6 @@ { | ||
"map" | ||
], | ||
"application/xml": [ | ||
"xsd" | ||
] | ||
} | ||
} |
{ | ||
"name": "mime-types", | ||
"description": "ultimate mime type utility", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"author": { | ||
@@ -11,2 +11,10 @@ "name": "Jonathan Ong", | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Jeremiah Senkpiel", | ||
"email": "fishrock123@rocketmail.com", | ||
"url": "https://searchbeam.jit.su", | ||
"twitter": "https://twitter.com/fishrock123" | ||
} | ||
], | ||
"repository": "expressjs/mime-types", | ||
@@ -13,0 +21,0 @@ "license": "MIT", |
@@ -1,17 +0,25 @@ | ||
# MIME Types | ||
# mime-types [![Build Status](https://travis-ci.org/expressjs/mime-types.svg?branch=master)](https://travis-ci.org/expressjs/mime-types) [![NPM version](https://badge.fury.io/js/mime-types.svg)](https://badge.fury.io/js/mime-types) | ||
The ultimate mime type utility. | ||
Similar to [mime](https://github.com/broofa/node-mime) except: | ||
The ultimate javascript content-type utility. | ||
- No `new Mime()` business, so you could do `var lookup = require('mime-types').lookup` | ||
- No fallbacks, so do `var type = mime.lookup('unrecognized') || 'application/octet-stream'` | ||
### Install | ||
```sh | ||
$ npm install mime-types | ||
``` | ||
#### Similar to [mime](https://github.com/broofa/node-mime) except: | ||
- No `new Mime()` business, so you could do `var lookup = require('mime-types').lookup`. | ||
- No fallbacks, so do `var type = mime.lookup('unrecognized') || 'application/octet-stream'`. | ||
- Additional mime types are added such as jade and stylus. Feel free to add more! | ||
- Browser support via Browserify and Component by converting lists to JSON files | ||
- Browser support via Browserify and Component by converting lists to JSON files. | ||
Otherwise, the API is compatible. | ||
## Adding Types | ||
### Adding Types | ||
If you'd like to add additional types, | ||
simply create a PR with a link to where it's defined. | ||
simply create a PR adding the type to `custom.json` and | ||
a reference link to the [sources](SOURCES.md). | ||
@@ -24,26 +32,53 @@ Do __NOT__ edit `mime.json` or `node.json`. | ||
```js | ||
var mime = require('mime-types') | ||
``` | ||
All functions return `false` if input is invalid or not found. | ||
### mime.lookup(path) | ||
Lookup the mime type associated with a file. | ||
If no type is found, returns `false`. | ||
Lookup the content-type associated with a file. | ||
```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('cats') // false | ||
``` | ||
### mime.contentType(type) | ||
Create a full content-type header given a content-type or extension. | ||
```js | ||
mime.contentType('markdown') // 'text/x-markdown; charset=utf-8' | ||
mime.contentType('file.json') // 'application/json; charset=utf-8' | ||
``` | ||
### mime.extension(type) | ||
Get the default extension for the type | ||
Get the default extension for a content-type. | ||
### mime.charsets.lookup(type) | ||
```js | ||
mime.extension('application/octet-stream') // 'bin' | ||
``` | ||
Lookup the given charset of a mime type. | ||
### mime.charset(type) | ||
### mime.contentType(type) | ||
Lookup the implied default charset of a content-type. | ||
Create a full `content-type` header given a mime-type or extension. | ||
```js | ||
mime.charset('text/x-markdown') // 'UTF-8' | ||
``` | ||
### mime.types[extension] = type | ||
Lookup a type via extension. | ||
A map of content-types by extension. | ||
### mime.extensions[type] = [extensions] | ||
Lookup all the associated extensions of a mime type. | ||
A map of extensions by content-type. | ||
@@ -57,4 +92,4 @@ ### mime.define(types) | ||
{ | ||
"<mime-type>": [extensions...], | ||
"<mime-type>": [extensions...] | ||
"<content-type>": [extensions...], | ||
"<content-type>": [extensions...] | ||
} | ||
@@ -64,1 +99,5 @@ ``` | ||
See the `.json` files in `lib/` for examples. | ||
## License | ||
[MIT](LICENSE) |
@@ -61,4 +61,6 @@ /** | ||
eq('UTF-8', mime.charsets.lookup('text/plain')); | ||
eq(false, mime.charsets.lookup(mime.types.js)); | ||
// eq('fallback', mime.charsets.lookup('application/octet-stream', 'fallback')); | ||
eq('UTF-8', mime.charset('text/plain')); | ||
eq(false, mime.charset(mime.types.js)); | ||
eq('UTF-8', mime.charset('application/json')) | ||
eq('UTF-8', mime.charsets.lookup('text/something')); | ||
// eq('fallback', mime.charset('application/octet-stream', 'fallback')); |
@@ -6,33 +6,96 @@ | ||
var set = mime.contentType | ||
var lookup = mime.lookup | ||
var extension = mime.extension | ||
var charset = mime.charset | ||
var contentType = mime.contentType | ||
describe('.lookup()', function () { | ||
it('jade', function () { | ||
assert.equal(mime.lookup('jade'), 'text/jade') | ||
assert.equal(mime.lookup('.jade'), 'text/jade') | ||
assert.equal(mime.lookup('file.jade'), 'text/jade') | ||
assert.equal(mime.lookup('folder/file.jade'), 'text/jade') | ||
assert.equal(lookup('jade'), 'text/jade') | ||
assert.equal(lookup('.jade'), 'text/jade') | ||
assert.equal(lookup('file.jade'), 'text/jade') | ||
assert.equal(lookup('folder/file.jade'), 'text/jade') | ||
}) | ||
it('should not error on non-string types', function () { | ||
assert.doesNotThrow(function () { | ||
lookup({ noteven: "once" }) | ||
lookup(null) | ||
lookup(true) | ||
lookup(Infinity) | ||
}) | ||
}) | ||
it('should return false for unknown types', function () { | ||
assert.equal(lookup('.jalksdjflakjsdjfasdf'), false) | ||
}) | ||
}) | ||
describe('.extension()', function () { | ||
it('should not error on non-string types', function () { | ||
assert.doesNotThrow(function () { | ||
extension({ noteven: "once" }) | ||
extension(null) | ||
extension(true) | ||
extension(Infinity) | ||
}) | ||
}) | ||
it('should return false for unknown types', function () { | ||
assert.equal(extension('.jalksdjflakjsdjfasdf'), false) | ||
}) | ||
}) | ||
describe('.charset()', function () { | ||
it('should not error on non-string types', function () { | ||
assert.doesNotThrow(function () { | ||
charset({ noteven: "once" }) | ||
charset(null) | ||
charset(true) | ||
charset(Infinity) | ||
}) | ||
}) | ||
it('should return false for unknown types', function () { | ||
assert.equal(charset('.jalksdjflakjsdjfasdf'), false) | ||
}) | ||
}) | ||
describe('.contentType()', function () { | ||
it('html', function () { | ||
assert.equal(set('html'), 'text/html; charset=utf-8') | ||
assert.equal(contentType('html'), 'text/html; charset=utf-8') | ||
}) | ||
it('text/html; charset=ascii', function () { | ||
assert.equal(set('text/html; charset=ascii'), 'text/html; charset=ascii') | ||
assert.equal(contentType('text/html; charset=ascii'), 'text/html; charset=ascii') | ||
}) | ||
it('json', function () { | ||
assert.equal(set('json'), 'application/json') | ||
assert.equal(contentType('json'), 'application/json; charset=utf-8') | ||
}) | ||
it('application/json', function () { | ||
assert.equal(set('application/json'), 'application/json') | ||
assert.equal(contentType('application/json'), 'application/json; charset=utf-8') | ||
}) | ||
it('jade', function () { | ||
assert.equal(set('jade'), 'text/jade; charset=utf-8') | ||
assert.equal(contentType('jade'), 'text/jade; charset=utf-8') | ||
}) | ||
it('should not error on non-string types', function () { | ||
assert.doesNotThrow(function () { | ||
contentType({ noteven: "once" }) | ||
contentType(null) | ||
contentType(true) | ||
contentType(Infinity) | ||
}) | ||
}) | ||
it('should return false for unknown types', function () { | ||
assert.equal(contentType('.jalksdjflakjsdjfasdf'), false) | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
86887
15
3659
1
101
1