-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Compiled source # | ||
################### | ||
*.com | ||
*.class | ||
*.dll | ||
*.exe | ||
*.o | ||
*.so | ||
|
||
# Packages # | ||
############ | ||
# it's better to unpack these files and commit the raw source | ||
# git has its own built in compression methods | ||
*.7z | ||
*.dmg | ||
*.gz | ||
*.iso | ||
*.jar | ||
*.rar | ||
*.tar | ||
*.zip | ||
|
||
# Logs and databases # | ||
###################### | ||
*.log | ||
*.sql | ||
*.sqlite | ||
|
||
# OS generated files # | ||
###################### | ||
.DS_Store* | ||
# Icon? | ||
ehthumbs.db | ||
Thumbs.db | ||
|
||
# Node.js # | ||
########### | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Jonathan Ong [email protected] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
build: | ||
node --harmony-generators build.js | ||
|
||
test: | ||
node test/mime.js | ||
mocha --require should --reporter spec test/test.js | ||
|
||
.PHONY: build test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# MIME Types | ||
|
||
The ultimate mime type utility. | ||
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 | ||
|
||
Otherwise, the API is compatible. | ||
|
||
## Adding Types | ||
|
||
If you'd like to add additional types, | ||
simply create a PR with a link to where it's defined. | ||
|
||
Do __NOT__ edit `mime.json` or `node.json`. | ||
Those are pulled using `build.js`. | ||
You should only touch `custom.json`. | ||
|
||
## API | ||
|
||
### mime.lookup(path) | ||
|
||
Lookup the mime type associated with a file. | ||
If no type is found, returns `false`. | ||
|
||
### mime.extension(type) | ||
|
||
Get the default extension for the type | ||
|
||
### mime.charsets.lookup(type) | ||
|
||
Lookup the given charset of a mime type. | ||
|
||
### mime.contentType(type) | ||
|
||
Create a full `content-type` header given a mime-type or extension. | ||
|
||
### mime.types[extension] = type | ||
|
||
Lookup a type via extension. | ||
|
||
### mime.extensions[type] = [extensions] | ||
|
||
Lookup all the associated extensions of a mime type. | ||
|
||
### mime.define(types) | ||
|
||
Globally add definitions. | ||
`types` must be an object of the form: | ||
|
||
```js | ||
{ | ||
"<mime-type>": [extensions...], | ||
"<mime-type>": [extensions...] | ||
} | ||
``` | ||
|
||
See the `.json` files in `lib/` for examples. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
/** | ||
* http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | ||
* https://github.com/broofa/node-mime/blob/master/types/node.types | ||
* | ||
* Convert these text files to JSON for browser usage. | ||
*/ | ||
|
||
var co = require('co') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var cogent = require('cogent') | ||
|
||
function* get(url) { | ||
var res = yield* cogent(url, { | ||
string: true | ||
}) | ||
|
||
if (res.statusCode !== 200) | ||
throw new Error('got status code ' + res.statusCode + ' from ' + url) | ||
|
||
var text = res.text | ||
var json = {} | ||
// http://en.wikipedia.org/wiki/Internet_media_type#Naming | ||
var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/ | ||
text = text.split('\n') | ||
.filter(Boolean) | ||
.forEach(function (line) { | ||
line = line.trim() | ||
if (!line) return | ||
var match = re.exec(line) | ||
if (!match) return | ||
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)) | ||
} | ||
|
||
co(function* () { | ||
yield [ | ||
get('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'), | ||
get('https://raw.githubusercontent.com/broofa/node-mime/master/types/node.types') | ||
] | ||
})() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "mime-types", | ||
"description": "ultimate mime type utility", | ||
"version": "0.1.0", | ||
"author": { | ||
"name": "Jonathan Ong", | ||
"email": "[email protected]", | ||
"url": "http://jongleberry.com", | ||
"twitter": "https://twitter.com/jongleberry" | ||
}, | ||
"repository": "expressjs/mime-types", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"scripts": ["lib/index.js"], | ||
"json": ["mime.json", "node.json", "custom.json"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"text/jade": [ | ||
"jade" | ||
], | ||
"text/stylus": [ | ||
"stylus", | ||
"styl" | ||
], | ||
"text/less": [ | ||
"less" | ||
], | ||
"text/x-sass": [ | ||
"sass" | ||
], | ||
"text/x-scss": [ | ||
"scss" | ||
], | ||
"text/coffeescript": [ | ||
"coffee" | ||
], | ||
"text/x-handlebars-template": [ | ||
"hbs" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
// types[extension] = type | ||
exports.types = Object.create(null) | ||
// extensions[type] = [extensions] | ||
exports.extensions = Object.create(null) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jonathanong
Author
Member
|
||
// define more mime types | ||
exports.define = define | ||
|
||
// store the json files | ||
exports.json = { | ||
mime: require('./mime.json'), | ||
node: require('./node.json'), | ||
custom: require('./custom.json'), | ||
} | ||
|
||
exports.lookup = function (string) { | ||
if (!string) return false | ||
string = string.replace(/.*[\.\/\\]/, '').toLowerCase() | ||
if (!string) return false | ||
return exports.types[string] || false | ||
} | ||
|
||
exports.extension = function (type) { | ||
if (!type) return false | ||
type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/) | ||
This comment has been minimized.
Sorry, something went wrong.
Fishrock123
Member
|
||
if (!type) return false | ||
var exts = exports.extensions[type[1].toLowerCase()] | ||
if (!exts || !exts.length) return false | ||
return exts[0] | ||
} | ||
|
||
exports.charsets = { | ||
lookup: function (type) { | ||
return /^text\//.test(type) | ||
? 'UTF-8' | ||
: false | ||
} | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Fishrock123
Member
|
||
|
||
exports.contentType = function (type) { | ||
if (!~type.indexOf('/')) type = exports.lookup(type) | ||
if (!~type.indexOf('charset')) { | ||
var charset = exports.charsets.lookup(type) | ||
if (charset) type += '; charset=' + charset.toLowerCase() | ||
} | ||
return type | ||
} | ||
|
||
define(exports.json.mime) | ||
define(exports.json.node) | ||
define(exports.json.custom) | ||
|
||
function define(json) { | ||
Object.keys(json).forEach(function (type) { | ||
var exts = json[type] || [] | ||
exports.extensions[type] = exports.extensions[type] || [] | ||
exts.forEach(function (ext) { | ||
if (!~exports.extensions[type].indexOf(ext)) exports.extensions[type].push(ext) | ||
exports.types[ext] = type | ||
}) | ||
}) | ||
} |
Why not just
{}
?Edit: le sigh