Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Code cleanup. Make charsets.lookup specific to default Mime instance.

  • Loading branch information...
commit 95614a654014384dd6af727459bb899f78a544a1 1 parent 52af207
@broofa authored
Showing with 28 additions and 28 deletions.
  1. +28 −28 mime.js
View
56 mime.js
@@ -1,16 +1,14 @@
-var path = require('path'),
- fs = require('fs');
-
+var path = require('path');
+var fs = require('fs');
function Mime() {
- // Map of extension to mime type
+ // Map of extension -> mime type
this.types = Object.create(null);
- // Map of mime type to extension
+ // Map of mime type -> extension
this.extensions = Object.create(null);
}
-
/**
* Define mimetype -> extension mappings. Each key is a mime-type that maps
* to an array of extensions associated with the type. The first extension is
@@ -35,7 +33,6 @@ Mime.prototype.define = function (map) {
}
};
-
/**
* Load an Apache2-style ".types" file
*
@@ -50,7 +47,7 @@ Mime.prototype.load = function(file) {
content = fs.readFileSync(file, 'ascii'),
lines = content.split(/[\r\n]+/);
- lines.forEach(function(line, lineno) {
+ lines.forEach(function(line) {
// Clean up whitespace/comments, and split into fields
var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
map[fields.shift()] = fields;
@@ -59,7 +56,6 @@ Mime.prototype.load = function(file) {
this.define(map);
};
-
/**
* Lookup a mime type based on extension
*/
@@ -69,7 +65,6 @@ Mime.prototype.lookup = function(path, fallback) {
return this.types[ext] || fallback || this.default_type;
};
-
/**
* Return file extension associated with a mime type
*/
@@ -77,28 +72,33 @@ Mime.prototype.extension = function(mimeType) {
return this.extensions[mimeType];
};
+// Default instance
+var mime = new Mime();
-/**
- * Lookup a charset based on mime type.
- */
-Mime.prototype.charsets = {
- lookup: function (mimeType, fallback) {
- // Assume text types are utf8. Modify mime logic as needed.
- return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
- }
-};
+// Load local copy of
+// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
+mime.load(path.join(__dirname, 'types/mime.types'));
+// Load additional types from node.js community
+mime.load(path.join(__dirname, 'types/node.types'));
-module.exports = new Mime();
-module.exports.Mime = Mime;
+// Default type
+mime.default_type = mime.lookup('bin');
+//
+// Additional API specific to the default instance
+//
-// Load our local copy of
-// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
-module.exports.load(path.join(__dirname, 'types/mime.types'));
+mime.Mime = Mime;
-// Overlay enhancements submitted by the node.js community
-module.exports.load(path.join(__dirname, 'types/node.types'));
+/**
+ * Lookup a charset based on mime type.
+ */
+mime.charsets = {
+ lookup: function(mimeType, fallback) {
+ // Assume text types are utf8
+ return (/^text\//).test(mimeType) ? 'UTF-8' : fallback;
+ }
+}
-// Set the default type
-module.exports.default_type = module.exports.types.bin;
+module.exports = mime;
Please sign in to comment.
Something went wrong with that request. Please try again.