Skip to content

Commit 1da5f79

Browse files
committed
init
0 parents  commit 1da5f79

13 files changed

+3782
-0
lines changed

.gitignore

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
# Icon?
33+
ehthumbs.db
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014 Jonathan Ong [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
build:
3+
node --harmony-generators build.js
4+
5+
test:
6+
node test/mime.js
7+
mocha --require should --reporter spec test/test.js
8+
9+
.PHONY: build test

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# MIME Types
2+
3+
The ultimate mime type utility.
4+
Similar to [mime](https://github.com/broofa/node-mime) except:
5+
6+
- No `new Mime()` business, so you could do `var lookup = require('mime-types').lookup`
7+
- No fallbacks, so do `var type = mime.lookup('unrecognized') || 'application/octet-stream'`
8+
- Additional mime types are added such as jade and stylus. Feel free to add more!
9+
- Browser support via Browserify and Component by converting lists to JSON files
10+
11+
Otherwise, the API is compatible.
12+
13+
## Adding Types
14+
15+
If you'd like to add additional types,
16+
simply create a PR with a link to where it's defined.
17+
18+
Do __NOT__ edit `mime.json` or `node.json`.
19+
Those are pulled using `build.js`.
20+
You should only touch `custom.json`.
21+
22+
## API
23+
24+
### mime.lookup(path)
25+
26+
Lookup the mime type associated with a file.
27+
If no type is found, returns `false`.
28+
29+
### mime.extension(type)
30+
31+
Get the default extension for the type
32+
33+
### mime.charsets.lookup(type)
34+
35+
Lookup the given charset of a mime type.
36+
37+
### mime.contentType(type)
38+
39+
Create a full `content-type` header given a mime-type or extension.
40+
41+
### mime.types[extension] = type
42+
43+
Lookup a type via extension.
44+
45+
### mime.extensions[type] = [extensions]
46+
47+
Lookup all the associated extensions of a mime type.
48+
49+
### mime.define(types)
50+
51+
Globally add definitions.
52+
`types` must be an object of the form:
53+
54+
```js
55+
{
56+
"<mime-type>": [extensions...],
57+
"<mime-type>": [extensions...]
58+
}
59+
```
60+
61+
See the `.json` files in `lib/` for examples.

build.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
4+
* https://github.com/broofa/node-mime/blob/master/types/node.types
5+
*
6+
* Convert these text files to JSON for browser usage.
7+
*/
8+
9+
var co = require('co')
10+
var fs = require('fs')
11+
var path = require('path')
12+
var cogent = require('cogent')
13+
14+
function* get(url) {
15+
var res = yield* cogent(url, {
16+
string: true
17+
})
18+
19+
if (res.statusCode !== 200)
20+
throw new Error('got status code ' + res.statusCode + ' from ' + url)
21+
22+
var text = res.text
23+
var json = {}
24+
// http://en.wikipedia.org/wiki/Internet_media_type#Naming
25+
var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/
26+
text = text.split('\n')
27+
.filter(Boolean)
28+
.forEach(function (line) {
29+
line = line.trim()
30+
if (!line) return
31+
var match = re.exec(line)
32+
if (!match) return
33+
json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '').split(/\s+/).filter(Boolean)
34+
})
35+
fs.writeFileSync('lib/' + path.basename(url).split('.')[0] + '.json',
36+
JSON.stringify(json, null, 2))
37+
}
38+
39+
co(function* () {
40+
yield [
41+
get('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'),
42+
get('https://raw.githubusercontent.com/broofa/node-mime/master/types/node.types')
43+
]
44+
})()

component.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "mime-types",
3+
"description": "ultimate mime type utility",
4+
"version": "0.1.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"repository": "expressjs/mime-types",
12+
"license": "MIT",
13+
"main": "lib/index.js",
14+
"scripts": ["lib/index.js"],
15+
"json": ["mime.json", "node.json", "custom.json"]
16+
}

lib/custom.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"text/jade": [
3+
"jade"
4+
],
5+
"text/stylus": [
6+
"stylus",
7+
"styl"
8+
],
9+
"text/less": [
10+
"less"
11+
],
12+
"text/x-sass": [
13+
"sass"
14+
],
15+
"text/x-scss": [
16+
"scss"
17+
],
18+
"text/coffeescript": [
19+
"coffee"
20+
],
21+
"text/x-handlebars-template": [
22+
"hbs"
23+
]
24+
}

lib/index.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
// types[extension] = type
3+
exports.types = Object.create(null)
4+
// extensions[type] = [extensions]
5+
exports.extensions = Object.create(null)
6+
// define more mime types
7+
exports.define = define
8+
9+
// store the json files
10+
exports.json = {
11+
mime: require('./mime.json'),
12+
node: require('./node.json'),
13+
custom: require('./custom.json'),
14+
}
15+
16+
exports.lookup = function (string) {
17+
if (!string) return false
18+
string = string.replace(/.*[\.\/\\]/, '').toLowerCase()
19+
if (!string) return false
20+
return exports.types[string] || false
21+
}
22+
23+
exports.extension = function (type) {
24+
if (!type) return false
25+
type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/)
26+
if (!type) return false
27+
var exts = exports.extensions[type[1].toLowerCase()]
28+
if (!exts || !exts.length) return false
29+
return exts[0]
30+
}
31+
32+
exports.charsets = {
33+
lookup: function (type) {
34+
return /^text\//.test(type)
35+
? 'UTF-8'
36+
: false
37+
}
38+
}
39+
40+
exports.contentType = function (type) {
41+
if (!~type.indexOf('/')) type = exports.lookup(type)
42+
if (!~type.indexOf('charset')) {
43+
var charset = exports.charsets.lookup(type)
44+
if (charset) type += '; charset=' + charset.toLowerCase()
45+
}
46+
return type
47+
}
48+
49+
define(exports.json.mime)
50+
define(exports.json.node)
51+
define(exports.json.custom)
52+
53+
function define(json) {
54+
Object.keys(json).forEach(function (type) {
55+
var exts = json[type] || []
56+
exports.extensions[type] = exports.extensions[type] || []
57+
exts.forEach(function (ext) {
58+
if (!~exports.extensions[type].indexOf(ext)) exports.extensions[type].push(ext)
59+
exports.types[ext] = type
60+
})
61+
})
62+
}

0 commit comments

Comments
 (0)