Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleaned up file parsing code

Fixed bug with trailing whitespace in types files
Changed merge logic when types are defined in multiple files.  If a file redefines a mime type, the default extension for that type is also updated.  (Note that this means the extension for the default octet-stream type has changed from 'bin' to 'buffer')
  • Loading branch information...
commit accd886268face9760f8604f0f758f88384c2d49 1 parent bae9a48
@broofa authored
Showing with 61 additions and 60 deletions.
  1. +5 −1 README.md
  2. +50 −53 mime.js
  3. +3 −3 node.types
  4. +1 −1  package.json
  5. +2 −2 test.js
View
6 README.md
@@ -10,7 +10,11 @@ A library for doing simple mime-type lookups.
... and extension lookups by mime-type
mime.extension('text/html'); // => 'html'
- mime.extension('application/octet-stream'); // => 'bin'
+ mime.extension('application/octet-stream'); // => 'buffer'
+
+'Need to define your own types? Just load up an Apache-format 'types' file (see mime.types or node.types for an example). Your types are "overlaid" onto the default mime.types and node.types.
+
+ mime.loadFile('./project.types');
It also includes rudimentary logic for determining charsets. (Useful in a web
framework):
View
103 mime.js
@@ -1,68 +1,65 @@
-var sys = require('sys');
-var path = require('path');
-var fs = require('fs');
+var path = require('path'),
+ fs = require('fs');
-exports.types = {};
-exports.extensions = {};
+var mime = module.exports = {
+ // Map of extension to mime type
+ types: {},
-/**
- * Load an Apache2-style type file
- */
-exports.load = function(mimeFile) {
- var content = fs.readFileSync(mimeFile, 'binary');
- var lines = content.split(/[\r\n]+/), line;
- // Each line
- while (line = lines.shift()) {
- line = line.replace(/#.*/); // Remove comments
- if (line) {
- line = line.split(/\s+/); // Split into fields
- var mimeType = line.shift(), ext; // field 0 = type
- // All remaining fields are extensions
- while (mimeType && (ext = line.shift())) {
- // Assign type for extension. If the same extension is declared for
- // multiple types, the last one wins.
- exports.types[ext] = mimeType;
+ // Map of mime type to extension
+ extensions :{},
- // For the extension map use the first extension listed
- if (!exports.extensions[mimeType]) {
- exports.extensions[mimeType] = ext;
+ // Load an Apache2-style ".types" file
+ //
+ // This may be called multiple times (it's expected). Where files declare
+ // overlapping types/extensions, the last file wins.
+ load: function(file) {
+ // Read file and split into lines
+ var content = fs.readFileSync(file, 'ascii'),
+ lines = content.split(/[\r\n]+/);
+
+ lines.forEach(function(line, lineno) {
+ // Clean up whitespace/comments, and split into fields
+ var fields = line.replace(/^\s*|\s*#.*|\s*$/, '')
+ .split(/\s+/);
+
+ if (fields.length >= 2) { // Must have type and at least 1 extension
+ // Map each extension to the mime type
+ for (var i = 1; i < fields.length; i++) {
+ mime.types[fields[i]] = fields[0];
}
+
+ // Map mime type to the first extension
+ mime.extensions[fields[0]] = fields[1];
}
- }
- }
-};
+ });
+ },
-/**
- * Lookup a mime type based on extension
- */
-exports.lookup = function(path, fallback) {
- var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
- return exports.types[ext] || fallback || exports.default_type;
-};
+ // Lookup a mime type based on extension
+ lookup: function(path, fallback) {
+ var ext = path.replace(/.*[\.\/]/, '').toLowerCase();
+ return mime.types[ext] || fallback || mime.default_type;
+ },
-/**
- * Return file extension associated with a mime type
- */
-exports.extension = function(mimeType) {
- return exports.extensions[mimeType];
-};
+ // Return file extension associated with a mime type
+ extension: function(mimeType) {
+ return mime.extensions[mimeType];
+ },
-/**
- * Lookup a charset based on mime type.
- */
-exports.charsets = {
- lookup: function (mimeType, fallback) {
- // Assume text types are utf8. Modify this logic as needed.
- return /^text\//.test(mimeType) ? 'UTF-8' : fallback;
+ // Lookup a charset based on mime type.
+ charsets: {
+ lookup: function (mimeType, fallback) {
+ // Assume text types are utf8. Modify mime logic as needed.
+ return /^text\//.test(mimeType) ? 'UTF-8' : fallback;
+ }
}
};
// Load our local copy of
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
-exports.load(path.join(__dirname, 'mime.types'));
+mime.load(path.join(__dirname, 'mime.types'));
-// Now "overlay" requested enhancements
-exports.load(path.join(__dirname, 'node.types'));
+// Overlay enhancements we've had requests for (and that seem to make sense)
+mime.load(path.join(__dirname, 'node.types'));
-// Set a default type
-exports.default_type = exports.types.bin;
+// Set the default type
+mime.default_type = mime.types.bin;
View
6 node.types
@@ -1,3 +1,3 @@
-application/mp4 m4p
-application/octet-stream buffer
-audio/mp4 m4a
+application/mp4 m4p
+application/octet-stream buffer
+audio/mp4 m4a
View
2  package.json
@@ -7,5 +7,5 @@
, "dependencies" : []
, "lib" : "."
, "main" : "mime.js"
-, "version" : "1.2.0"
+, "version" : "1.2.1"
}
View
4 test.js
@@ -1,5 +1,5 @@
var mime = require('./mime');
-
+debugger;
exports["test mime lookup"] = function(test) {
// easy
test.equal('text/plain', mime.lookup('text.txt'));
@@ -26,7 +26,7 @@ exports["test extension lookup"] = function(test) {
// easy
test.equal('txt', mime.extension(mime.types.text));
test.equal('html', mime.extension(mime.types.htm));
- test.equal('bin', mime.extension('application/octet-stream'));
+ test.equal('buffer', mime.extension('application/octet-stream'));
test.finish();
};
Please sign in to comment.
Something went wrong with that request. Please try again.