second
[josuexyz/.git] / 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     for (var i = 0; i < exts.length; i++) {
25       if (process.env.DEBUG_MIME && this.types[exts[i]]) {
26         console.warn((this._loading || "define()").replace(/.*\//, ''), 'changes "' + exts[i] + '" extension type from ' +
27           this.types[exts[i]] + ' to ' + type);
28       }
29
30       this.types[exts[i]] = type;
31     }
32
33     // Default extension is the first one we encounter
34     if (!this.extensions[type]) {
35       this.extensions[type] = exts[0];
36     }
37   }
38 };
39
40 /**
41  * Load an Apache2-style ".types" file
42  *
43  * This may be called multiple times (it's expected).  Where files declare
44  * overlapping types/extensions, the last file wins.
45  *
46  * @param file (String) path of file to load.
47  */
48 Mime.prototype.load = function(file) {
49   this._loading = file;
50   // Read file and split into lines
51   var map = {},
52       content = fs.readFileSync(file, 'ascii'),
53       lines = content.split(/[\r\n]+/);
54
55   lines.forEach(function(line) {
56     // Clean up whitespace/comments, and split into fields
57     var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
58     map[fields.shift()] = fields;
59   });
60
61   this.define(map);
62
63   this._loading = null;
64 };
65
66 /**
67  * Lookup a mime type based on extension
68  */
69 Mime.prototype.lookup = function(path, fallback) {
70   var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase();
71
72   return this.types[ext] || fallback || this.default_type;
73 };
74
75 /**
76  * Return file extension associated with a mime type
77  */
78 Mime.prototype.extension = function(mimeType) {
79   var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase();
80   return this.extensions[type];
81 };
82
83 // Default instance
84 var mime = new Mime();
85
86 // Define built-in types
87 mime.define(require('./types.json'));
88
89 // Default type
90 mime.default_type = mime.lookup('bin');
91
92 //
93 // Additional API specific to the default instance
94 //
95
96 mime.Mime = Mime;
97
98 /**
99  * Lookup a charset based on mime type.
100  */
101 mime.charsets = {
102   lookup: function(mimeType, fallback) {
103     // Assume text types are utf8
104     return (/^text\/|^application\/(javascript|json)/).test(mimeType) ? 'UTF-8' : fallback;
105   }
106 };
107
108 module.exports = mime;