Commit c9b4e652 authored by Leo Iannacone's avatar Leo Iannacone

Imported Upstream version 1.2.1

parent b5065d67
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# Logs and databases #
######################
*.log
*.sql
*.sqlite
# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
# Node.js #
###########
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
node_modules
npm-debug.log
# Components #
##############
/build
/components
# ImageMagick #
###############
*.cache
*.mpc
# Other #
#########
test/*.2
test/*/*.2
test/*.mp4
test/images/originalSideways.jpg.2
\ No newline at end of file
test.js
\ No newline at end of file
test.js
.travis.yml
language: node_js
node_js:
- "0.10"
- "0.11"
language: node_js
\ No newline at end of file
- "0.8"
- "0.10"
- "0.11"
matrix:
allow_failures:
- node_js: "0.11"
fast_finish: true
1.2.1 / 2014-06-03
==================
* Switch dependency from `mime` to `mime-types@1.0.0`
1.2.0 / 2014-05-11
==================
* support suffix matching:
- `+json` matches `application/vnd+json`
- `*/vnd+json` matches `application/vnd+json`
- `application/*+json` matches `application/vnd+json`
1.1.0 / 2014-04-12
==================
......
# Type Is [![Build Status](https://travis-ci.org/expressjs/type-is.png)](https://travis-ci.org/expressjs/type-is)
# type-is [![Build Status](https://travis-ci.org/expressjs/type-is.svg?branch=master)](https://travis-ci.org/expressjs/type-is) [![NPM version](https://badge.fury.io/js/type-is.svg)](https://badge.fury.io/js/type-is)
Infer the content type of a request.
Extracted from [koa](https://github.com/koajs/koa) for general use.
Infer the content-type of a request.
Here's an example body parser:
### Install
```sh
$ npm install type-is
```
## API
```js
var http = require('http')
var is = require('type-is')
http.createServer(function (req, res) {
is(req, ['text/*'])
})
```
### type = is(request, types)
`request` is the node HTTP request. `types` is an array of types.
```js
// req.headers.content-type = 'application/json'
is(req, ['json']) // 'json'
is(req, ['html', 'json']) // 'json'
is(req, ['application/*']) // 'application/json'
is(req, ['application/json']) // 'application/json'
is(req, ['html']) // false
```
#### Each type can be:
- An extension name such as `json`. This name will be returned if matched.
- A mime type such as `application/json`.
- A mime type with a wildcard such as `*/json` or `application/*`. The full mime type will be returned if matched
- A suffix such as `+json`. This can be combined with a wildcard such as `*/vnd+json` or `application/*+json`. The full mime type will be returned if matched.
`false` will be returned if no type matches.
## Examples
#### Example body parser
```js
var is = require('type-is');
......@@ -14,7 +56,7 @@ function bodyParser(req, res, next) {
var hasRequestBody = 'content-type' in req.headers
|| 'transfer-encoding' in req.headers;
if (!hasRequestBody) return next();
switch (is(req, ['urlencoded', 'json', 'multipart'])) {
case 'urlencoded':
// parse urlencoded body
......@@ -31,37 +73,6 @@ function bodyParser(req, res, next) {
}
```
## API
### var type = is(request, types)
```js
var is = require('type-is')
http.createServer(function (req, res) {
is(req, ['text/*'])
})
```
`request` is the node HTTP request. `types` is an array of types. Each type can be:
- An extension name such as `json`. This name will be returned if matched.
- A mime type such as `application/json`.
- A mime type with a wildcard such as `*/json` or `application/*`. The full mime type will be returned if matched
`false` will be returned if no type matches.
Examples:
```js
// req.headers.content-type = 'application/json'
is(req, ['json']) // -> 'json'
is(req, ['html', 'json']) // -> 'json'
is(req, ['application/*']) // -> 'application/json'
is(req, ['application/json']) // -> 'application/json'
is(req, ['html']) // -> false
```
## License
The MIT License (MIT)
......
var mime = require('mime');
var mime = require('mime-types');
var slice = [].slice;
......@@ -35,9 +35,13 @@ function typeis(value, types) {
if (!types || !types.length) return value;
var type;
for (var i = 0; i < types.length; i++)
if (mimeMatch(normalize(type = types[i]), value))
return ~type.indexOf('*') ? value : type;
for (var i = 0; i < types.length; i++) {
if (mimeMatch(normalize(type = types[i]), value)) {
return type[0] === '+' || ~type.indexOf('*')
? value
: type
}
}
// no matches
return false;
......@@ -120,13 +124,15 @@ function normalize(type) {
break;
}
return ~type.indexOf('/') ? type : mime.lookup(type);
return type[0] === '+' || ~type.indexOf('/')
? type
: mime.lookup(type)
}
/**
* Check if `exected` mime type
* matches `actual` mime type with
* wildcard support.
* wildcard and +suffix support.
*
* @param {String} expected
* @param {String} actual
......@@ -137,12 +143,35 @@ function normalize(type) {
function mimeMatch(expected, actual) {
if (expected === actual) return true;
actual = actual.split('/');
if (expected[0] === '+') {
// support +suffix
return Boolean(actual[1])
&& expected.length <= actual[1].length
&& expected === actual[1].substr(0 - expected.length)
}
if (!~expected.indexOf('*')) return false;
actual = actual.split('/');
expected = expected.split('/');
if ('*' === expected[0] && expected[1] === actual[1]) return true;
if ('*' === expected[1] && expected[0] === actual[0]) return true;
return false;
if (expected[0] === '*') {
// support */yyy
return expected[1] === actual[1]
}
if (expected[1] === '*') {
// support xxx/*
return expected[0] === actual[0]
}
if (expected[1][0] === '*' && expected[1][1] === '+') {
// support xxx/*+zzz
return expected[0] === actual[0]
&& expected[1].length <= actual[1].length + 1
&& expected[1].substr(1) === actual[1].substr(1 - expected[1].length)
}
return false
}
{
"name": "type-is",
"description": "Infer the content type if a request",
"version": "1.1.0",
"description": "Infer the content-type of a request.",
"version": "1.2.1",
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
"url": "http://jongleberry.com",
"twitter": "https://twitter.com/jongleberry"
},
"contributors": [
{
"name": "Douglas Christopher Wilson",
"email": "doug@somethingdoug.com"
}
],
"license": "MIT",
"repository": "expressjs/type-is",
"dependencies": {
"mime": "~1.2.11"
"mime-types": "1.0.0"
},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"engines": {
"node": ">= 0.8"
},
"scripts": {
"test": "mocha --require should --reporter spec --bail"
}
......
......@@ -79,6 +79,19 @@ describe('is(req, type)', function(){
})
})
describe('given +suffix', function(){
it('should match suffix types', function(){
var r = req('application/vnd+json')
is(r, '+json').should.equal('application/vnd+json')
is(r, 'application/vnd+json').should.equal('application/vnd+json')
is(r, 'application/*+json').should.equal('application/vnd+json')
is(r, '*/vnd+json').should.equal('application/vnd+json')
is(r, 'application/json').should.be.false
is(r, 'text/*+json').should.be.false
})
})
describe('when Content-Type: application/x-www-form-urlencoded', function(){
it('should match "urlencoded"', function(){
var r = req('application/x-www-form-urlencoded')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment