-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add build step to fetch mime.types from Apache and build types.json
- Loading branch information
Showing
9 changed files
with
130 additions
and
1,683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var request = require('request'); | ||
|
||
// The canonical URL at which Apache's mime type mappings are located | ||
var APACHE_TYPES = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'; | ||
|
||
// Local file for node.js community-specific mappings | ||
var NODE_TYPES = 'node.types'; | ||
|
||
/** | ||
* Parse a types string (formatted like Apache's mime.types) for type | ||
* information | ||
* | ||
* @param typesMap {?Object} | ||
* @param typesString {String} | ||
*/ | ||
function parseTypes(typesMap, typesString) { | ||
return typesString.split(/[\r\n]+/) | ||
.map(function(line) { | ||
return line.toLowerCase() | ||
.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/); | ||
}) | ||
.filter(function(fields) { | ||
return fields.length > 1; | ||
}) | ||
.reduce(function(types, fields) { | ||
var type = fields.shift(); | ||
types[type] = types.hasOwnProperty(type) ? | ||
types[type].concat(fields) : fields; | ||
return types; | ||
}, typesMap || {}); | ||
} | ||
|
||
|
||
function handleApacheResponse(err, res, apacheTypes) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
if (res.statusCode != 200) { | ||
throw new Error('Unexpected HTTP statusCode = ' + res.statusCode); | ||
} | ||
|
||
// Combine mime.types and node.types | ||
var mapByType = [ | ||
apacheTypes, | ||
fs.readFileSync(path.join(__dirname, NODE_TYPES), 'ascii') | ||
].reduce(parseTypes, {}); | ||
|
||
console.log(JSON.stringify(mapByType, null, 2)); | ||
} | ||
|
||
request.get(APACHE_TYPES, handleApacheResponse); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Usage: node test.js | ||
*/ | ||
|
||
var mime = require('../mime'); | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
|
||
// | ||
// Test mime lookups | ||
// | ||
|
||
assert.equal('text/plain', mime.lookup('text.txt')); // normal file | ||
assert.equal('text/plain', mime.lookup('TEXT.TXT')); // uppercase | ||
assert.equal('text/plain', mime.lookup('dir/text.txt')); // dir + file | ||
assert.equal('text/plain', mime.lookup('.text.txt')); // hidden file | ||
assert.equal('text/plain', mime.lookup('.txt')); // nameless | ||
assert.equal('text/plain', mime.lookup('txt')); // extension-only | ||
assert.equal('text/plain', mime.lookup('/txt')); // extension-less () | ||
assert.equal('text/plain', mime.lookup('\\txt')); // Windows, extension-less | ||
assert.equal('application/octet-stream', mime.lookup('text.nope')); // unrecognized | ||
assert.equal('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default | ||
|
||
// | ||
// Test extensions | ||
// | ||
|
||
assert.equal('txt', mime.extension(mime.types.text)); | ||
assert.equal('html', mime.extension(mime.types.htm)); | ||
assert.equal('bin', mime.extension('application/octet-stream')); | ||
assert.equal('bin', mime.extension('application/octet-stream ')); | ||
assert.equal('html', mime.extension(' text/html; charset=UTF-8')); | ||
assert.equal('html', mime.extension('text/html; charset=UTF-8 ')); | ||
assert.equal('html', mime.extension('text/html; charset=UTF-8')); | ||
assert.equal('html', mime.extension('text/html ; charset=UTF-8')); | ||
assert.equal('html', mime.extension('text/html;charset=UTF-8')); | ||
assert.equal('html', mime.extension('text/Html;charset=UTF-8')); | ||
assert.equal(undefined, mime.extension('unrecognized')); | ||
|
||
// | ||
// Test node.types lookups | ||
// | ||
|
||
assert.equal('application/font-woff', mime.lookup('file.woff')); | ||
assert.equal('application/octet-stream', mime.lookup('file.buffer')); | ||
assert.equal('audio/mp4', mime.lookup('file.m4a')); | ||
assert.equal('font/opentype', mime.lookup('file.otf')); | ||
|
||
// | ||
// Test charsets | ||
// | ||
|
||
assert.equal('UTF-8', mime.charsets.lookup('text/plain')); | ||
assert.equal(undefined, mime.charsets.lookup(mime.types.js)); | ||
assert.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback')); | ||
|
||
console.log('\nAll tests passed'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.