Commit dd1d4151 authored by Bastien Roucariès's avatar Bastien Roucariès

New upstream version 1.6.16

parent 727382b8
coverage
node_modules
{
"extends": "standard"
}
coverage/
node_modules/
npm-debug.log
package-lock.json
......@@ -3,13 +3,37 @@ node_js:
- "0.6"
- "0.8"
- "0.10"
- "0.11"
matrix:
allow_failures:
- node_js: "0.11"
fast_finish: true
- "0.12"
- "1.8"
- "2.5"
- "3.3"
- "4.8"
- "5.12"
- "6.13"
- "7.10"
- "8.9"
- "9.5"
sudo: false
dist: precise
cache:
directories:
- node_modules
before_install:
# Skip updating shrinkwrap / lock
- "npm config set shrinkwrap false"
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)"
# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
script:
- "test $TRAVIS_NODE_VERSION != '0.6' || npm test"
- "test $TRAVIS_NODE_VERSION = '0.6' || npm run-script test-travis"
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
1.6.16 / 2018-02-16
===================
* deps: mime-types@~2.1.18
- Add `application/raml+yaml` with extension `.raml`
- Add `application/wasm` with extension `.wasm`
- Add `text/shex` with extension `.shex`
- Add extensions for JPEG-2000 images
- Add extensions from IANA for `message/*` types
- Add extension `.mjs` to `application/javascript`
- Add extension `.wadl` to `application/vnd.sun.wadl+xml`
- Add extension `.gz` to `application/gzip`
- Add glTF types and extensions
- Add new mime types
- Update extensions `.md` and `.markdown` to be `text/markdown`
- Update font MIME types
- Update `text/hjson` to registered `application/hjson`
1.6.15 / 2017-03-31
===================
* deps: mime-types@~2.1.15
- Add new mime types
1.6.14 / 2016-11-18
===================
* deps: mime-types@~2.1.13
- Add new mime types
1.6.13 / 2016-05-18
===================
* deps: mime-types@~2.1.11
- Add new mime types
1.6.12 / 2016-02-28
===================
* deps: mime-types@~2.1.10
- Add new mime types
- Fix extension of `application/dash+xml`
- Update primary extension for `audio/mp4`
1.6.11 / 2016-01-29
===================
* deps: mime-types@~2.1.9
- Add new mime types
1.6.10 / 2015-12-01
===================
* deps: mime-types@~2.1.8
- Add new mime types
1.6.9 / 2015-09-27
==================
* deps: mime-types@~2.1.7
- Add new mime types
1.6.8 / 2015-09-04
==================
* deps: mime-types@~2.1.6
- Add new mime types
1.6.7 / 2015-08-20
==================
* Fix type error when given invalid type to match against
* deps: mime-types@~2.1.5
- Add new mime types
1.6.6 / 2015-07-31
==================
* deps: mime-types@~2.1.4
- Add new mime types
1.6.5 / 2015-07-16
==================
* deps: mime-types@~2.1.3
- Add new mime types
1.6.4 / 2015-07-01
==================
* deps: mime-types@~2.1.2
- Add new mime types
* perf: enable strict mode
* perf: remove argument reassignment
1.6.3 / 2015-06-08
==================
* deps: mime-types@~2.1.1
- Add new mime types
* perf: reduce try block size
* perf: remove bitwise operations
1.6.2 / 2015-05-10
==================
* deps: mime-types@~2.0.11
- Add new mime types
1.6.1 / 2015-03-13
==================
* deps: mime-types@~2.0.10
- Add new mime types
1.6.0 / 2015-02-12
==================
* fix false-positives in `hasBody` `Transfer-Encoding` check
* support wildcard for both type and subtype (`*/*`)
1.5.7 / 2015-02-09
==================
* fix argument reassignment
* deps: mime-types@~2.0.9
- Add new mime types
1.5.6 / 2015-01-29
==================
* deps: mime-types@~2.0.8
- Add new mime types
1.5.5 / 2014-12-30
==================
* deps: mime-types@~2.0.7
- Add new mime types
- Fix missing extensions
- Fix various invalid MIME type entries
- Remove example template MIME types
- deps: mime-db@~1.5.0
1.5.4 / 2014-12-10
==================
* deps: mime-types@~2.0.4
- Add new mime types
- deps: mime-db@~1.3.0
1.5.3 / 2014-11-09
==================
* deps: mime-types@~2.0.3
- Add new mime types
- deps: mime-db@~1.2.0
1.5.2 / 2014-09-28
==================
......
The MIT License (MIT)
(The MIT License)
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
......@@ -10,6 +10,10 @@ Infer the content-type of a request.
### Install
This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
```sh
$ npm install type-is
```
......@@ -18,83 +22,125 @@ $ npm install type-is
```js
var http = require('http')
var is = require('type-is')
var typeis = require('type-is')
http.createServer(function (req, res) {
var istext = is(req, ['text/*'])
var istext = typeis(req, ['text/*'])
res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
})
```
### type = is(request, types)
### type = typeis(request, types)
`request` is the node HTTP request. `types` is an array of types.
<!-- eslint-disable no-undef -->
```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'
typeis(req, ['json']) // 'json'
typeis(req, ['html', 'json']) // 'json'
typeis(req, ['application/*']) // 'application/json'
typeis(req, ['application/json']) // 'application/json'
typeis(req, ['html']) // false
```
### typeis.hasBody(request)
Returns a Boolean if the given `request` has a body, regardless of the
`Content-Type` header.
Having a body has no relation to how large the body is (it may be 0 bytes).
This is similar to how file existence works. If a body does exist, then this
indicates that there is data to read from the Node.js request stream.
is(req, ['html']) // false
<!-- eslint-disable no-undef -->
```js
if (typeis.hasBody(req)) {
// read the body, since there is one
req.on('data', function (chunk) {
// ...
})
}
```
#### Each type can be:
### type = typeis.is(mediaType, types)
`mediaType` is the [media type](https://tools.ietf.org/html/rfc6838) string. `types` is an array of types.
<!-- eslint-disable no-undef -->
```js
var mediaType = 'application/json'
typeis.is(mediaType, ['json']) // 'json'
typeis.is(mediaType, ['html', 'json']) // 'json'
typeis.is(mediaType, ['application/*']) // 'application/json'
typeis.is(mediaType, ['application/json']) // 'application/json'
typeis.is(mediaType, ['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 mime type with a wildcard such as `*/*` or `*/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.
`false` will be returned if no type matches or the content type is invalid.
`null` will be returned if the request does not have a body.
## Examples
#### Example body parser
### Example body parser
```js
var is = require('type-is');
var express = require('express')
var typeis = require('type-is')
var app = express()
function bodyParser(req, res, next) {
if (!is.hasBody(req)) {
app.use(function bodyParser (req, res, next) {
if (!typeis.hasBody(req)) {
return next()
}
switch (is(req, ['urlencoded', 'json', 'multipart'])) {
switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
case 'urlencoded':
// parse urlencoded body
throw new Error('implement urlencoded body parsing')
break
case 'json':
// parse json body
throw new Error('implement json body parsing')
break
case 'multipart':
// parse multipart body
throw new Error('implement multipart body parsing')
break
default:
// 415 error code
res.statusCode = 415
res.end()
return
break
}
}
})
```
## License
[MIT](LICENSE)
[npm-image]: https://img.shields.io/npm/v/type-is.svg?style=flat
[npm-image]: https://img.shields.io/npm/v/type-is.svg
[npm-url]: https://npmjs.org/package/type-is
[node-version-image]: https://img.shields.io/node/v/type-is.svg?style=flat
[node-version-url]: http://nodejs.org/download/
[travis-image]: https://img.shields.io/travis/jshttp/type-is.svg?style=flat
[node-version-image]: https://img.shields.io/node/v/type-is.svg
[node-version-url]: https://nodejs.org/en/download/
[travis-image]: https://img.shields.io/travis/jshttp/type-is/master.svg
[travis-url]: https://travis-ci.org/jshttp/type-is
[coveralls-image]: https://img.shields.io/coveralls/jshttp/type-is.svg?style=flat
[coveralls-image]: https://img.shields.io/coveralls/jshttp/type-is/master.svg
[coveralls-url]: https://coveralls.io/r/jshttp/type-is?branch=master
[downloads-image]: https://img.shields.io/npm/dm/type-is.svg?style=flat
[downloads-image]: https://img.shields.io/npm/dm/type-is.svg
[downloads-url]: https://npmjs.org/package/type-is
/*!
* type-is
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module dependencies.
* @private
*/
var typer = require('media-typer')
var mime = require('mime-types')
module.exports = typeofrequest;
typeofrequest.is = typeis;
typeofrequest.hasBody = hasbody;
typeofrequest.normalize = normalize;
typeofrequest.match = mimeMatch;
/**
* Module exports.
* @public
*/
module.exports = typeofrequest
module.exports.is = typeis
module.exports.hasBody = hasbody
module.exports.normalize = normalize
module.exports.match = mimeMatch
/**
* Compare a `value` content-type with `types`.
......@@ -19,18 +37,18 @@ typeofrequest.match = mimeMatch;
*
* @param {String} value
* @param {Array} types
* @return String
* @public
*/
function typeis(value, types_) {
function typeis (value, types_) {
var i
var types = types_
// remove parameters and normalize
value = typenormalize(value)
var val = tryNormalizeType(value)
// no type or invalid
if (!value) {
if (!val) {
return false
}
......@@ -43,19 +61,21 @@ function typeis(value, types_) {
}
// no types, return the content type
if (!types || !types.length) return value;
if (!types || !types.length) {
return val
}
var type
for (i = 0; i < types.length; i++) {
if (mimeMatch(normalize(type = types[i]), value)) {
return type[0] === '+' || ~type.indexOf('*')
? value
if (mimeMatch(normalize(type = types[i]), val)) {
return type[0] === '+' || type.indexOf('*') !== -1
? val
: type
}
}
// no matches
return false;
return false
}
/**
......@@ -66,13 +86,12 @@ function typeis(value, types_) {
*
* @param {Object} request
* @return {Boolean}
* @api public
* @public
*/
function hasbody(req) {
var headers = req.headers;
if ('transfer-encoding' in headers) return true;
return !isNaN(headers['content-length']);
function hasbody (req) {
return req.headers['transfer-encoding'] !== undefined ||
!isNaN(req.headers['content-length'])
}
/**
......@@ -98,10 +117,10 @@ function hasbody(req) {
*
* @param {String|Array} types...
* @return {String|false|null}
* @api public
* @public
*/
function typeofrequest(req, types_) {
function typeofrequest (req, types_) {
var types = types_
// no body
......@@ -120,7 +139,7 @@ function typeofrequest(req, types_) {
// request content type
var value = req.headers['content-type']
return typeis(value, types);
return typeis(value, types)
}
/**
......@@ -136,75 +155,75 @@ function typeofrequest(req, types_) {
* and are thus ensured to work.
*
* @param {String} type
* @api private
* @private
*/
function normalize(type) {
function normalize (type) {
if (typeof type !== 'string') {
// invalid type
return false
}
switch (type) {
case 'urlencoded': return 'application/x-www-form-urlencoded';
case 'urlencoded':
return 'application/x-www-form-urlencoded'
case 'multipart':
type = 'multipart/*';
break;
return 'multipart/*'
}
return type[0] === '+' || ~type.indexOf('/')
? type
: mime.lookup(type)
if (type[0] === '+') {
// "+json" -> "*/*+json" expando
return '*/*' + type
}
return type.indexOf('/') === -1
? mime.lookup(type)
: type
}
/**
* Check if `exected` mime type
* Check if `expected` mime type
* matches `actual` mime type with
* wildcard and +suffix support.
*
* @param {String} expected
* @param {String} actual
* @return {Boolean}
* @api private
* @private
*/
function mimeMatch(expected, actual) {
function mimeMatch (expected, actual) {
// invalid type
if (expected === false) {
return false
}
// exact match
if (expected === actual) {
return true
}
actual = actual.split('/');
// split types
var actualParts = actual.split('/')
var expectedParts = expected.split('/')
if (expected[0] === '+') {
// support +suffix
return Boolean(actual[1])
&& expected.length <= actual[1].length
&& expected === actual[1].substr(0 - expected.length)
// invalid format
if (actualParts.length !== 2 || expectedParts.length !== 2) {
return false
}
if (!~expected.indexOf('*')) return false;
expected = expected.split('/');
if (expected[0] === '*') {
// support */yyy
return expected[1] === actual[1]
// validate type
if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) {
return false
}
if (expected[1] === '*') {
// support xxx/*
return expected[0] === actual[0]
// validate suffix wildcard
if (expectedParts[1].substr(0, 2) === '*+') {
return expectedParts[1].length <= actualParts[1].length + 1 &&
expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)
}
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)
// validate subtype
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) {
return false
}
return false
return true
}
/**
......@@ -212,14 +231,31 @@ function mimeMatch(expected, actual) {
*
* @param {string} value
* @return {string}
* @api private
* @private
*/
function normalizeType (value) {
// parse the type
var type = typer.parse(value)
// remove the parameters
type.parameters = undefined
// reformat it
return typer.format(type)
}
/**
* Try to normalize a type and remove parameters.
*
* @param {string} value
* @return {string}
* @private
*/
function typenormalize(value) {
function tryNormalizeType (value) {
try {
var type = typer.parse(value)
delete type.parameters
return typer.format(type)
return normalizeType(value)
} catch (err) {
return null
}
......
{
"name": "type-is",
"description": "Infer the content-type of a request.",
"version": "1.5.2",
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
"version": "1.6.16",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>"
"Douglas Christopher Wilson <doug@somethingdoug.com>",
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
],
"license": "MIT",
"repository": "jshttp/type-is",
"dependencies": {
"media-typer": "0.3.0",
"mime-types": "~2.0.2"
"mime-types": "~2.1.18"
},
"devDependencies": {
"istanbul": "~0.3.0",
"mocha": "1"
"eslint": "3.19.0",
"eslint-config-standard": "10.2.1",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-markdown": "1.0.0-beta.6",
"eslint-plugin-node": "5.2.1",
"eslint-plugin-promise": "3.6.0",
"eslint-plugin-standard": "3.0.1",
"istanbul": "0.4.5",
"mocha": "1.21.5"
},
"engines": {
"node": ">= 0.6"
......@@ -25,6 +32,7 @@
"index.js"
],
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --check-leaks --bail test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
......
{
"env": {
"mocha": true
}
}
......@@ -2,39 +2,45 @@
var assert = require('assert')
var typeis = require('..')
describe('typeis(req, type)', function(){
it('should ignore params', function(){
describe('typeis(req, type)', function () {
it('should ignore params', function () {
var req = createRequest('text/html; charset=utf-8')
assert.equal(typeis(req, ['text/*']), 'text/html')
})
it('should ignore params LWS', function(){
it('should ignore params LWS', function () {
var req = createRequest('