Comparing version
11
mime.js
@@ -5,7 +5,7 @@ var path = require('path'), | ||
var mime = module.exports = { | ||
/** Map of extension to mime type */ | ||
types: {}, | ||
// Map of extension to mime type | ||
types: Object.create(null), | ||
/** Map of mime type to extension */ | ||
extensions :{}, | ||
// Map of mime type to extension | ||
extensions :Object.create(null), | ||
@@ -64,3 +64,4 @@ /** | ||
var ext = path.replace(/.*[\.\/]/, '').toLowerCase(); | ||
return mime.types[ext] || fallback || mime.default_type; | ||
return mime.types[ext] || fallback || mime.default_type | ||
}, | ||
@@ -67,0 +68,0 @@ |
@@ -16,3 +16,3 @@ { | ||
"description": "A comprehensive library for mime-type mapping", | ||
"devDependencies": {"async_testing": ""}, | ||
"devDependencies": {}, | ||
"keywords": ["util", "mime"], | ||
@@ -22,3 +22,3 @@ "main": "mime.js", | ||
"repository": {"url": "http://github.com/bentomas/node-mime", "type": "git"}, | ||
"version": "1.2.4" | ||
"version": "1.2.5" | ||
} |
# mime | ||
Support for mapping between file extensions and MIME types. This module uses the latest version of the Apache "mime.types" file (maps over 620 types to 800+ extensions). It is also trivially easy to add your own types and extensions, should you need to do that. | ||
Comprehensive MIME type mapping API. Includes all 600+ types and 800+ extensions defined by the Apache project, plus additional types submitted by the node.js community. | ||
@@ -14,3 +14,3 @@ ## Install | ||
### mime.lookup(path) | ||
Get the mime type associated with a file. This is method is case-insensitive. Everything in path up to and including the last '/' or '.' is ignored, so you can pass it paths, filenames, or extensions, like so: | ||
Get the mime type associated with a file. Performs a case-insensitive lookup using the extension in `path` (the substring after the last '/' or '.'). E.g. | ||
@@ -21,6 +21,7 @@ var mime = require('mime'); | ||
mime.lookup('file.txt'); // => 'text/plain' | ||
mime.lookup('.txt'); // => 'text/plain' | ||
mime.lookup('.TXT'); // => 'text/plain' | ||
mime.lookup('htm'); // => 'text/html' | ||
### mime.extension(type) - lookup the default extension for type | ||
### mime.extension(type) | ||
Get the default extension for `type` | ||
@@ -30,4 +31,6 @@ mime.extension('text/html'); // => 'html' | ||
### mime.charsets.lookup() - map mime-type to charset | ||
### mime.charsets.lookup() | ||
Map mime-type to charset | ||
mime.charsets.lookup('text/plain'); // => 'UTF-8' | ||
@@ -37,7 +40,10 @@ | ||
## API - Customizing | ||
## API - Defining Custom Types | ||
The following APIs allow you to add your own type mappings within your project. If you feel a type should be included as part of node-mime, see [requesting new types](https://github.com/bentomas/node-mime/wiki/Requesting-New-Types). | ||
### mime.define() - Add custom mime/extension mappings | ||
### mime.define() | ||
Add custom mime/extension mappings | ||
mime.define({ | ||
@@ -50,6 +56,13 @@ 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'], | ||
mime.lookup('x-sft'); // => 'text/x-some-format' | ||
The first entry in the extensions array is returned by `mime.extension()`. E.g. | ||
mime.extension('text/x-some-format'); // => 'x-sf' | ||
### mime.load(filepath) - Load mappings from an Apache ".types" format file | ||
### mime.load(filepath) | ||
Load mappings from an Apache ".types" format file | ||
mime.load('./my_project.types'); | ||
The .types file format is simple - See the `types` dir for examples. |
102
test.js
/** | ||
* Requires the async_testing module | ||
* | ||
* Usage: node test.js | ||
*/ | ||
var mime = require('./mime'); | ||
exports["test mime lookup"] = function(test) { | ||
// easy | ||
test.equal('text/plain', mime.lookup('text.txt')); | ||
var assert = require('assert'); | ||
// hidden file or multiple periods | ||
test.equal('text/plain', mime.lookup('.text.txt')); | ||
function eq(a, b) { | ||
console.log('Test: ' + a + ' === ' + b); | ||
assert.strictEqual.apply(null, arguments); | ||
} | ||
// just an extension | ||
test.equal('text/plain', mime.lookup('.txt')); | ||
console.log(Object.keys(mime.extensions).length + ' types'); | ||
console.log(Object.keys(mime.types).length + ' extensions\n'); | ||
// just an extension without a dot | ||
test.equal('text/plain', mime.lookup('txt')); | ||
// | ||
// Test mime lookups | ||
// | ||
// default | ||
test.equal('application/octet-stream', mime.lookup('text.nope')); | ||
eq('text/plain', mime.lookup('text.txt')); | ||
eq('text/plain', mime.lookup('.text.txt')); | ||
eq('text/plain', mime.lookup('.txt')); | ||
eq('text/plain', mime.lookup('txt')); | ||
eq('application/octet-stream', mime.lookup('text.nope')); | ||
eq('fallback', mime.lookup('text.fallback', 'fallback')); | ||
eq('application/octet-stream', mime.lookup('constructor')); | ||
eq('text/plain', mime.lookup('TEXT.TXT')); | ||
// fallback | ||
test.equal('fallback', mime.lookup('text.fallback', 'fallback')); | ||
// | ||
// Test extensions | ||
// | ||
test.finish(); | ||
}; | ||
eq('txt', mime.extension(mime.types.text)); | ||
eq('html', mime.extension(mime.types.htm)); | ||
eq('bin', mime.extension('application/octet-stream')); | ||
eq(undefined, mime.extension('constructor')); | ||
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 node types | ||
// | ||
test.finish(); | ||
}; | ||
eq('application/octet-stream', mime.lookup('file.buffer')); | ||
eq('audio/mp4', mime.lookup('file.m4a')); | ||
exports["test mime lookup uppercase"] = function(test) { | ||
// easy | ||
test.equal('text/plain', mime.lookup('TEXT.TXT')); | ||
// | ||
// Test charsets | ||
// | ||
// just an extension | ||
test.equal('text/plain', mime.lookup('.TXT')); | ||
eq('UTF-8', mime.charsets.lookup('text/plain')); | ||
eq(undefined, mime.charsets.lookup(mime.types.js)); | ||
eq('fallback', mime.charsets.lookup('application/octet-stream', 'fallback')); | ||
// just an extension without a dot | ||
test.equal('text/plain', mime.lookup('TXT')); | ||
// default | ||
test.equal('application/octet-stream', mime.lookup('TEXT.NOPE')); | ||
// fallback | ||
test.equal('fallback', mime.lookup('TEXT.FALLBACK', 'fallback')); | ||
test.finish(); | ||
}; | ||
exports["test custom types"] = function(test) { | ||
test.equal('application/octet-stream', mime.lookup('file.buffer')); | ||
test.equal('audio/mp4', mime.lookup('file.m4a')); | ||
test.finish(); | ||
}; | ||
exports["test charset lookup"] = function(test) { | ||
// easy | ||
test.equal('UTF-8', mime.charsets.lookup('text/plain')); | ||
// none | ||
test.ok(typeof mime.charsets.lookup(mime.types.js) == 'undefined'); | ||
// fallback | ||
test.equal('fallback', mime.charsets.lookup('application/octet-stream', 'fallback')); | ||
test.finish(); | ||
}; | ||
if (module == require.main) { | ||
require('async_testing').run(__filename, process.ARGV); | ||
} | ||
console.log('\nOK'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
59716
0.66%0
-100%64
25.49%0
-100%119
-12.5%1
Infinity%