142
St
startsInvasion / files
startsInvasion / libs / node_modules / reqwest / build / mime / mime.js
2.3 KB
1var path = require('path'), 2 fs = require('fs'); 3 4var mime = module.exports = { 5 /** Map of extension to mime type */ 6 types: {}, 7 8 /** Map of mime type to extension */ 9 extensions :{}, 10 11 /** 12 * Define mimetype -> extension mappings. Each key is a mime-type that maps 13 * to an array of extensions associated with the type. The first extension is 14 * used as the default extension for the type. 15 * 16 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']}); 17 * 18 * @param map (Object) type definitions 19 */ 20 define: function(map) { 21 for (var type in map) { 22 var exts = map[type]; 23 24 for (var i = 0; i < exts.length; i++) { 25 mime.types[exts[i]] = type; 26 } 27 28 mime.extensions[type] = exts[0]; 29 } 30 }, 31 32 /** 33 * Load an Apache2-style ".types" file 34 * 35 * This may be called multiple times (it's expected). Where files declare 36 * overlapping types/extensions, the last file wins. 37 * 38 * @param file (String) path of file to load. 39 */ 40 load: function(file) { 41 // Read file and split into lines 42 var map = {}, 43 content = fs.readFileSync(file, 'ascii'), 44 lines = content.split(/[\r\n]+/); 45 46 lines.forEach(function(line, lineno) { 47 // Clean up whitespace/comments, and split into fields 48 var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/); 49 map[fields.shift()] = fields; 50 }); 51 52 mime.define(map); 53 }, 54 55 /** 56 * Lookup a mime type based on extension 57 */ 58 lookup: function(path, fallback) { 59 var ext = path.replace(/.*[\.\/]/, '').toLowerCase(); 60 return mime.types[ext] || fallback || mime.default_type; 61 }, 62 63 /** 64 * Return file extension associated with a mime type 65 */ 66 extension: function(mimeType) { 67 return mime.extensions[mimeType]; 68 }, 69 70 /** 71 * Lookup a charset based on mime type. 72 */ 73 charsets: { 74 lookup: function (mimeType, fallback) { 75 // Assume text types are utf8. Modify mime logic as needed. 76 return /^text\//.test(mimeType) ? 'UTF-8' : fallback; 77 } 78 } 79}; 80 81// Load our local copy of 82// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types 83mime.load(path.join(__dirname, 'mime.types')); 84 85// Overlay enhancements we've had requests for (and that seem to make sense) 86mime.load(path.join(__dirname, 'node.types')); 87 88// Set the default type 89mime.default_type = mime.types.bin;