Skip to content

Commit

Permalink
Fix incorrectly treating extension-less file name as extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 7, 2015
1 parent f8e5b6a commit f5e2eb2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
unreleased
==========

* Fix incorrectly treating extension-less file name as extension
- i.e. `'path/to/json'` will no longer return `application/json`
* Refactor internals for readability and no argument reassignment

2.0.14 / 2015-06-06
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ All functions return `false` if input is invalid or not found.
Lookup the content-type associated with a file.

```js
mime.lookup('json') // 'application/json'
mime.lookup('.md') // 'text/x-markdown'
mime.lookup('file.html') // 'text/html'
mime.lookup('folder/file.js') // 'application/javascript'
mime.lookup('json') // 'application/json'
mime.lookup('.md') // 'text/x-markdown'
mime.lookup('file.html') // 'text/html'
mime.lookup('folder/file.js') // 'application/javascript'
mime.lookup('folder/.htaccess') // false

mime.lookup('cats') // false
```
Expand Down
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

var db = require('mime-db')
var extname = require('path').extname

/**
* Module exports.
Expand Down Expand Up @@ -110,25 +111,27 @@ function extension(type) {
}

/**
* Lookup the MIME type for a file name/extension.
* Lookup the MIME type for a file path/extension.
*
* @param {string} name
* @param {string} path
* @return {boolean|string}
*/

function lookup(name) {
if (!name || typeof name !== 'string') {
function lookup(path) {
if (!path || typeof path !== 'string') {
return false
}

// remove any leading paths, though we should just use path.basename
var ext = name.replace(/.*[\.\/\\]/, '').toLowerCase()
// get the extension ("ext" or ".ext" or full path)
var extension = extname('x.' + path)
.toLowerCase()
.substr(1)

if (!ext) {
if (!extension) {
return false
}

return exports.types[ext] || false
return exports.types[extension] || false
}

/**
Expand Down
8 changes: 0 additions & 8 deletions test/mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ function eq(a, b) {
console.log(Object.keys(mime.extensions).length + ' types');
console.log(Object.keys(mime.types).length + ' extensions\n');

//
// Test mime lookups
//

eq('text/plain', mime.lookup('.text.txt')); // hidden file
eq('text/plain', mime.lookup('/txt')); // extension-less ()
eq('text/plain', mime.lookup('\\txt')); // Windows, extension-less

//
// Test extensions
//
Expand Down
23 changes: 22 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var assert = require('assert')
var mimeTypes = require('..')

describe('mimeTypes', function () {
describe('.extension(path)', function () {
describe('.extension(type)', function () {
it('should return extension for mime type', function () {
assert.equal(mimeTypes.extension('text/html'), 'html')
})
Expand Down Expand Up @@ -75,19 +75,40 @@ describe('mimeTypes', function () {

it('should return mime type for relative path', function () {
assert.equal(mimeTypes.lookup('path/to/page.html'), 'text/html')
assert.equal(mimeTypes.lookup('path\\to\\page.html'), 'text/html')
})

it('should return mime type for absolute path', function () {
assert.equal(mimeTypes.lookup('/path/to/page.html'), 'text/html')
assert.equal(mimeTypes.lookup('C:\\path\\to\\page.html'), 'text/html')
})

it('should be case insensitive', function () {
assert.equal(mimeTypes.lookup('/path/to/PAGE.HTML'), 'text/html')
assert.equal(mimeTypes.lookup('C:\\path\\to\\PAGE.HTML'), 'text/html')
})

it('should return false for unknown extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/file.bogus'), false)
})

it('should return false for path without extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/json'), false)
})

describe('path with dotfile', function () {
it('should return false when extension-less', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/.json'), false)
})

it('should return mime type when there is extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/.config.json'), 'application/json')
})

it('should return mime type when there is extension, but no path', function () {
assert.strictEqual(mimeTypes.lookup('.config.json'), 'application/json')
})
})
})
})

Expand Down

0 comments on commit f5e2eb2

Please sign in to comment.