Permalink
Browse files

Added mime#define to allow programatic type declaration

mime#load now uses mime#define
Updated node.types file to keep default extension for octet-stream as 'bin'
Tweaks to readme and package file
  • Loading branch information...
1 parent accd886 commit 46e7c079c1b42f948af80d274e9882543ee4f30a @broofa committed Jan 20, 2011
Showing with 90 additions and 35 deletions.
  1. +41 −9 README.md
  2. +46 −22 mime.js
  3. +1 −1 node.types
  4. +1 −1 package.json
  5. +1 −2 test.js
View
@@ -1,4 +1,16 @@
-A library for doing simple mime-type lookups.
+# mime
+
+A comprehensive library for mime-type mapping
+
+## Install
+
+Install with [npm](http://github.com/isaacs/npm):
+
+ npm install mime
+
+## Usage
+
+### Mapping file/extension to mime-type
var mime = require('mime');
@@ -7,20 +19,40 @@ A library for doing simple mime-type lookups.
mime.lookup('.txt'); // => 'text/plain'
mime.lookup('htm'); // => 'text/html'
-... and extension lookups by mime-type
+### Mapping mime-type to extension
mime.extension('text/html'); // => 'html'
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.
+### Determining charsets
- mime.loadFile('./project.types');
+ mime.charset.lookup('text/plain'); // => 'UTF-8'
-It also includes rudimentary logic for determining charsets. (Useful in a web
-framework):
+(The logic for charset lookup is pretty rudimentary. Feel free to suggest improvements.)
- mime.charset.lookup('text/plain'); // => 'UTF-8'
+## "Can you add support for [some type/extension]?"
-Install with [npm](http://github.com/isaacs/npm):
+Does anyone outside your project/team _actually_ care about it? If not, consider using the mime.load() or mime.define() APIs, below, to declare your types w/in your project.
- npm install mime
+... if so, does anyone the node.js community care about it? If not, then please file a bug here listing the modules and projects that would benefit. If it makes sense, the type can be added as part of the node.types file.
+
+... finally, if it's something that affects the broader community, please file a bug with the Apache project to amend their mime.types file, and create a bug here linking to the Apache bug.
+
+### mime.define()
+
+mime.define() takes a map of mime-type to extensions:
+
+ mime.define({
+ 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
+ 'application/x-my-type': ['x-mt', 'x-mtt'],
+ // etc ...
+ });
+
+ mime.lookup('x-sft'); // => 'text/x-some-format'
+ mime.extension('text/x-some-format'); // => 'x-sf'
+
+### mime.define()
+
+mime.load() can be used to load any Apache-format .types file:
+
+ mime.load('./my_project.types');
View
68 mime.js
@@ -2,50 +2,74 @@ var path = require('path'),
fs = require('fs');
var mime = module.exports = {
- // Map of extension to mime type
+ /** Map of extension to mime type */
types: {},
- // Map of mime type to extension
+ /** Map of mime type to extension */
extensions :{},
- // 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.
+ /**
+ * 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
+ * used as the default extension for the type.
+ *
+ * e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
+ *
+ * @param map (Object) type definitions
+ */
+ define: function(map) {
+ for (var type in map) {
+ var exts = map[type];
+
+ for (var i = 0; i < exts.length; i++) {
+ mime.types[exts[i]] = type;
+ }
+
+ mime.extensions[type] = exts[0];
+ }
+ },
+
+ /**
+ * 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.
+ *
+ * @param file (String) path of file to load.
+ */
load: function(file) {
// Read file and split into lines
- var content = fs.readFileSync(file, 'ascii'),
+ var map = {},
+ 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];
- }
+ var fields = line.replace(/\s*#.*|^\s*|\s*$/g, '').split(/\s+/);
+ map[fields.shift()] = fields;
});
+
+ mime.define(map);
},
- // Lookup a mime type based on extension
+ /**
+ * 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
+ /**
+ * Return file extension associated with a mime type
+ */
extension: function(mimeType) {
return mime.extensions[mimeType];
},
- // Lookup a charset based on mime type.
+ /**
+ * Lookup a charset based on mime type.
+ */
charsets: {
lookup: function (mimeType, fallback) {
// Assume text types are utf8. Modify mime logic as needed.
View
@@ -1,3 +1,3 @@
application/mp4 m4p
-application/octet-stream buffer
+application/octet-stream bin buffer
audio/mp4 m4a
View
@@ -1,5 +1,5 @@
{ "name" : "mime"
-, "description" : "A super simple utility library for dealing with mime-types"
+, "description" : "A comprehensive library for mime-type mapping"
, "url" : "http://github.com/bentomas/node-mime"
, "keywords" : ["util", "mime"]
, "author" : "Benjamin Thomas <benjamin@benjaminthomas.org>"
View
@@ -1,5 +1,4 @@
var mime = require('./mime');
-debugger;
exports["test mime lookup"] = function(test) {
// easy
test.equal('text/plain', mime.lookup('text.txt'));
@@ -26,7 +25,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('buffer', mime.extension('application/octet-stream'));
+ test.equal('bin', mime.extension('application/octet-stream'));
test.finish();
};

0 comments on commit 46e7c07

Please sign in to comment.