http-body-parser
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
/*! | ||
@@ -8,4 +10,2 @@ * type-is | ||
'use strict' | ||
/** | ||
@@ -16,4 +16,4 @@ * Module dependencies. | ||
var typer = require('media-typer') | ||
var mime = require('mime-types') | ||
var typer = require('media-typer'); | ||
var mime = require('mime-types'); | ||
@@ -25,7 +25,7 @@ /** | ||
module.exports = typeofrequest | ||
module.exports.is = typeis | ||
module.exports.hasBody = hasbody | ||
module.exports.normalize = normalize | ||
module.exports.match = mimeMatch | ||
module.exports = typeofrequest; | ||
module.exports.is = typeis; | ||
module.exports.hasBody = hasbody; | ||
module.exports.normalize = normalize; | ||
module.exports.match = mimeMatch; | ||
@@ -47,11 +47,11 @@ /** | ||
function typeis(value, types_) { | ||
var i | ||
var types = types_ | ||
var i = void 0; | ||
var types = types_; | ||
// remove parameters and normalize | ||
var val = tryNormalizeType(value) | ||
var val = tryNormalizeType(value); | ||
// no type or invalid | ||
if (!val) { | ||
return false | ||
return false; | ||
} | ||
@@ -61,5 +61,5 @@ | ||
if (types && !Array.isArray(types)) { | ||
types = new Array(arguments.length - 1) | ||
types = new Array(arguments.length - 1); | ||
for (i = 0; i < types.length; i++) { | ||
types[i] = arguments[i + 1] | ||
types[i] = arguments[i + 1]; | ||
} | ||
@@ -70,11 +70,9 @@ } | ||
if (!types || !types.length) { | ||
return val | ||
return val; | ||
} | ||
var type | ||
var type = void 0; | ||
for (i = 0; i < types.length; i++) { | ||
if (mimeMatch(normalize(type = types[i]), val)) { | ||
return type[0] === '+' || type.indexOf('*') !== -1 | ||
? val | ||
: type | ||
return type[0] === '+' || type.indexOf('*') !== -1 ? val : type; | ||
} | ||
@@ -84,3 +82,3 @@ } | ||
// no matches | ||
return false | ||
return false; | ||
} | ||
@@ -100,3 +98,3 @@ | ||
function hasbody(req) { | ||
return req.headers['transfer-encoding'] !== undefined || !isNaN(req.headers['content-length']) | ||
return req.headers['transfer-encoding'] !== undefined || !isNaN(req.headers['content-length']); | ||
} | ||
@@ -131,7 +129,7 @@ | ||
function typeofrequest(req, types_) { | ||
var types = types_ | ||
var types = types_; | ||
// no body | ||
if (!hasbody(req)) { | ||
return null | ||
return null; | ||
} | ||
@@ -141,5 +139,5 @@ | ||
if (arguments.length > 2) { | ||
types = new Array(arguments.length - 1) | ||
types = new Array(arguments.length - 1); | ||
for (var i = 0; i < types.length; i++) { | ||
types[i] = arguments[i + 1] | ||
types[i] = arguments[i + 1]; | ||
} | ||
@@ -149,5 +147,5 @@ } | ||
// request content type | ||
var value = req.headers['content-type'] | ||
var value = req.headers['content-type']; | ||
return typeis(value, types) | ||
return typeis(value, types); | ||
} | ||
@@ -174,3 +172,3 @@ | ||
// invalid type | ||
return false | ||
return false; | ||
} | ||
@@ -180,5 +178,5 @@ | ||
case 'urlencoded': | ||
return 'application/x-www-form-urlencoded' | ||
return 'application/x-www-form-urlencoded'; | ||
case 'multipart': | ||
return 'multipart/*' | ||
return 'multipart/*'; | ||
} | ||
@@ -188,8 +186,6 @@ | ||
// "+json" -> "*/*+json" expando | ||
return '*/*' + type | ||
return '*/*' + type; | ||
} | ||
return type.indexOf('/') === -1 | ||
? mime.lookup(type) | ||
: type | ||
return type.indexOf('/') === -1 ? mime.lookup(type) : type; | ||
} | ||
@@ -211,12 +207,12 @@ | ||
if (expected === false) { | ||
return false | ||
return false; | ||
} | ||
// split types | ||
var actualParts = actual.split('/') | ||
var expectedParts = expected.split('/') | ||
var actualParts = actual.split('/'); | ||
var expectedParts = expected.split('/'); | ||
// invalid format | ||
if (actualParts.length !== 2 || expectedParts.length !== 2) { | ||
return false | ||
return false; | ||
} | ||
@@ -226,3 +222,3 @@ | ||
if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) { | ||
return false | ||
return false; | ||
} | ||
@@ -232,3 +228,3 @@ | ||
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) | ||
return expectedParts[1].length <= actualParts[1].length + 1 && expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length); | ||
} | ||
@@ -238,6 +234,6 @@ | ||
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) { | ||
return false | ||
return false; | ||
} | ||
return true | ||
return true; | ||
} | ||
@@ -255,9 +251,9 @@ | ||
// parse the type | ||
var type = typer.parse(value) | ||
var type = typer.parse(value); | ||
// remove the parameters | ||
type.parameters = undefined | ||
type.parameters = undefined; | ||
// reformat it | ||
return typer.format(type) | ||
return typer.format(type); | ||
} | ||
@@ -275,6 +271,6 @@ | ||
try { | ||
return normalizeType(value) | ||
return normalizeType(value); | ||
} catch (err) { | ||
return null | ||
return null; | ||
} | ||
} | ||
} |
{ | ||
"name": "http-body-parser", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "🎨 A body parser for node, koa, koa2, express. support json, form, text, multipart and stream type body.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava test/*.js" | ||
"build": "babel src -d lib", | ||
"test": "ava", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -44,2 +46,7 @@ "repository": { | ||
"ava": "^0.19.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-env": "^1.4.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.1.3", | ||
"eslint-plugin-import": "^2.2.0", | ||
"express": "^4.15.2", | ||
@@ -51,3 +58,14 @@ "koa": "^2.2.0", | ||
"uuid": "^3.0.1" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"test/*.spec.js" | ||
], | ||
"tap": false, | ||
"babel": "inherit", | ||
"require": [ | ||
"babel-register", | ||
"babel-polyfill" | ||
] | ||
} | ||
} |
@@ -13,8 +13,8 @@ http-body-parser | ||
[npm-url]: https://npmjs.org/package/http-body-parser | ||
[travis-image]: https://img.shields.io/travis/eqfox/bodyparser.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/eqfox/bodyparser | ||
[coveralls-image]: https://img.shields.io/coveralls/eqfox/bodyparser.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/eqfox/bodyparser?branch=master | ||
[david-image]: https://img.shields.io/david/eqfox/bodyparser.svg?style=flat-square | ||
[david-url]: https://david-dm.org/eqfox/bodyparser | ||
[travis-image]: https://img.shields.io/travis/eqfox/http-body-parser.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/eqfox/http-body-parser | ||
[coveralls-image]: https://img.shields.io/coveralls/eqfox/http-body-parser.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/eqfox/http-body-parser?branch=master | ||
[david-image]: https://img.shields.io/david/eqfox/http-body-parser.svg?style=flat-square | ||
[david-url]: https://david-dm.org/eqfox/http-body-parser | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_7.6-green.svg?style=flat-square | ||
@@ -26,3 +26,3 @@ [node-url]: http://nodejs.org/download/ | ||
A body parser for koa, express. support `json`, `form`, `text`, `multipart` and `stream` type body. | ||
🔥 A body parser for koa, express. support `json`, `form`, `text`, `multipart` and `stream` type body. | ||
@@ -66,3 +66,3 @@ ## Install | ||
* No third party module dependent | ||
$ Support Koa and Express | ||
* Support Koa and Express | ||
@@ -69,0 +69,0 @@ ## Options |
@@ -6,38 +6,38 @@ const Koa = require('koa'); | ||
const request = require('supertest'); | ||
const bodyParser = require('../').koa; | ||
const image1 = path.join(__dirname, 'upload/image1.jpg'); | ||
const image2 = path.join(__dirname, 'upload/image2.jpg'); | ||
const bodyParser = require('../src/').koa; | ||
const image1 = path.join(__dirname, 'image1.jpg'); | ||
const image2 = path.join(__dirname, 'image2.jpg'); | ||
const app = new Koa(); | ||
app.use(async(ctx, next) => { | ||
await next() | ||
app.use(async (ctx, next) => { | ||
await next(); | ||
}); | ||
app.use(bodyParser({ | ||
enableTypes: [ | ||
'json', 'form', 'text', 'multipart', 'stream' | ||
'json', 'form', 'text', 'multipart', 'stream', | ||
], | ||
stream: { | ||
path: 'uploads/' | ||
} | ||
})) | ||
app.use(ctx => { | ||
ctx.body = ctx.request.body | ||
path: 'uploads/', | ||
}, | ||
})); | ||
app.use((ctx) => { | ||
ctx.body = ctx.request.body; | ||
}); | ||
let server = app.listen(); | ||
const server = app.listen(); | ||
test('JSON', async t => { | ||
test('JSON', async (t) => { | ||
t.plan(2); | ||
const res = await request(server).post('/').set('Content-Type', 'application/json').send({name: 'eqfox'}); | ||
const res = await request(server).post('/').set('Content-Type', 'application/json').send({ name: 'eqfox' }); | ||
t.is(res.status, 200); | ||
t.deepEqual(res.body, {name: 'eqfox'}); | ||
t.deepEqual(res.body, { name: 'eqfox' }); | ||
}); | ||
test('Form', async t => { | ||
test('Form', async (t) => { | ||
t.plan(2); | ||
const res = await request(server).post('/').set('Content-Type', 'application/x-www-form-urlencoded').send('name=eqfox'); | ||
t.is(res.status, 200); | ||
t.deepEqual(res.body, {name: 'eqfox'}); | ||
t.deepEqual(res.body, { name: 'eqfox' }); | ||
}); | ||
test('Text', async t => { | ||
test('Text', async (t) => { | ||
t.plan(2); | ||
@@ -49,3 +49,3 @@ const res = await request(server).post('/').set('Content-Type', 'text/plain').send('eqfox'); | ||
test('Multipart', async t => { | ||
test('Multipart', async (t) => { | ||
t.plan(4); | ||
@@ -59,3 +59,3 @@ const res = await request(server).post('/').field('name', 'eqfox').attach('image1', image1).attach('image2', image2); | ||
test('Stream', async t => { | ||
test('Stream', async (t) => { | ||
t.plan(2); | ||
@@ -62,0 +62,0 @@ const res = await request(server).post('/').set('Content-Type', 'application/octet-stream').send(fs.readFileSync(image1)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310485
23
722
9
1