Name: js-handler/node_modules/restify/node_modules/mime/mime.js 
1:
var path = require('path');
2:
var fs = require('fs');
3:
 
4:
function Mime() {
5:
  // Map of extension -> mime type
6:
  this.types = Object.create(null);
7:
 
8:
  // Map of mime type -> extension
9:
  this.extensions = Object.create(null);
10:
}
11:
 
12:
/**
13:
 * Define mimetype -> extension mappings.  Each key is a mime-type that maps
14:
 * to an array of extensions associated with the type.  The first extension is
15:
 * used as the default extension for the type.
16:
 *
17:
 * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
18:
 *
19:
 * @param map (Object) type definitions
20:
 */
21:
Mime.prototype.define = function (map) {
22:
  for (var type in map) {
23:
    var exts = map[type];
24:
 
25:
    for (var i = 0; i < exts.length; i++) {
26:
      if (process.env.DEBUG_MIME && this.types[exts]) {
27:
        console.warn(this._loading.replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
28:
          this.types[exts] + ' to ' + type);
29:
      }
30:
 
31:
      this.types[exts[i]] = type;
32:
    }
33:
 
34:
    // Default extension is the first one we encounter
35:
    if (!this.extensions[type]) {
36:
      this.extensions[type] = exts[0];
37:
    }
38:
  }
39:
};
40:
 
41:
/**
42:
 * Load an Apache2-style ".types" file
43:
 *
44:
 * This may be called multiple times (it's expected).  Where files declare
45:
 * overlapping types/extensions, the last file wins.
46:
 *
47:
 * @param file (String) path of file to load.
48:
 */
49:
Mime.prototype.load = function(file) {
50:
 
51:
  this._loading = file;
52:
  // Read file and split into lines
53:
  var map = {},
54:
      content = fs.readFileSync(file, 'ascii'),
55:
      lines = content.split(/[\r\n]+/);
56:
 
57:
  lines.forEach(function(line) {
58:
    // Clean up whitespace/comments, and split into fields
59:
    var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
60:
    map[fields.shift()] = fields;
61:
  });
62:
 
63:
  this.define(map);
64:
 
65:
  this._loading = null;
66:
};
67:
 
68:
/**
69:
 * Lookup a mime type based on extension
70:
 */
71:
Mime.prototype.lookup = function(path, fallback) {
72:
  var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
73:
 
74:
  return this.types[ext] || fallback || this.default_type;
75:
};
76:
 
77:
/**
78:
 * Return file extension associated with a mime type
79:
 */
80:
Mime.prototype.extension = function(mimeType) {
81:
  return this.extensions[mimeType];
82:
};
83:
 
84:
// Default instance
85:
var mime = new Mime();
86:
 
87:
// Load local copy of
88:
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
89:
mime.load(path.join(__dirname, 'types/mime.types'));
90:
 
91:
// Load additional types from node.js community
92:
mime.load(path.join(__dirname, 'types/node.types'));
93:
 
94:
// Default type
95:
mime.default_type = mime.lookup('bin');
96:
 
97:
//
98:
// Additional API specific to the default instance
99:
//
100:
 
101:
mime.Mime = Mime;
102:
 
103:
/**
104:
 * Lookup a charset based on mime type.
105:
 */
106:
mime.charsets = {
107:
  lookup: function(mimeType, fallback) {
108:
    // Assume text types are utf8
109:
    return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
110:
  }
111:
};
112:
 
113:
module.exports = mime;