http-body-parser
Advanced tools
Comparing version 1.1.0 to 1.1.1
28
index.js
@@ -42,3 +42,3 @@ const JsonParser = require('./models/JsonParser.js'); | ||
if (typeIs(this.req, Parser.getTypes(Parser.options.extendsTypes))) { | ||
return new Parser(body, this.req.headers); | ||
return new Parser(body, this.req.headers, Parser.options.limit, Parser.options.path); | ||
} | ||
@@ -50,5 +50,3 @@ } | ||
// Middleware | ||
// options = {enableTypes:['json', 'form'], json: {}, text: {}} | ||
module.exports = (options = {}) => { | ||
module.exports.koa = (options = {}) => { | ||
return async(ctx, next) => { | ||
@@ -74,1 +72,23 @@ // new parserFactory | ||
} | ||
module.exports.express = (options = {}) => { | ||
return async(req, res, next) => { | ||
// new parserFactory | ||
let parserFactory = new ParserFactory(req, options.enableTypes); | ||
// add parser to factory | ||
parserFactory.addParser('json', JsonParser, options.json); | ||
parserFactory.addParser('form', FormParser, options.form); | ||
parserFactory.addParser('text', TextParser, options.text); | ||
parserFactory.addParser('multipart', MultipartParser, options.multipart); | ||
parserFactory.addParser('stream', StreamParser, options.stream); | ||
// get request body | ||
let body = await parserFactory.getBody(); | ||
// parse body | ||
let parser = parserFactory.getEnableParser(body); | ||
if (parser) { | ||
req.rawBody = body; | ||
req.body = parser.parse(); | ||
} | ||
await next(); | ||
}; | ||
} |
@@ -6,8 +6,10 @@ const querystring = require('querystring'); | ||
const uuid = require('uuid'); | ||
const os = require('os'); | ||
class MultipartParser { | ||
constructor(body, headers, limit = 1024 * 1024) { | ||
constructor(body, headers, limit = 1024 * 1024, path = os.tmpdir()) { | ||
this.body = body; | ||
this.headers = headers; | ||
this.limit = limit; | ||
this.path = path; | ||
this.boundary = Buffer.from('--' + headers['content-type'].split(';').pop().replace('boundary=', '').trim()); | ||
@@ -42,3 +44,3 @@ } | ||
name: filename_match[2], | ||
path: path.join('uploads', filename_match[2]), | ||
path: path.join(this.path, filename_match[2]), | ||
mimetype: null, | ||
@@ -48,3 +50,3 @@ size: value.length, | ||
}; | ||
fs.writeFileSync(path.join('uploads', filename_match[2]), value) | ||
fs.writeFileSync(path.join(this.path, filename_match[2]), value) | ||
} | ||
@@ -51,0 +53,0 @@ begin = end; |
@@ -5,8 +5,10 @@ const uuid = require('uuid'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
class StreamParser { | ||
constructor(body, headers, limit) { | ||
constructor(body, headers, limit = 1024 * 1024, path = os.tmpdir()) { | ||
this.body = body; | ||
this.headers = headers; | ||
this.limit = limit; | ||
this.path = path; | ||
} | ||
@@ -18,3 +20,3 @@ | ||
name: filename, | ||
path: path.join('uploads', filename), | ||
path: path.join(this.path, filename), | ||
mimetype: null, | ||
@@ -21,0 +23,0 @@ size: this.body.length, |
class TextParser { | ||
constructor(body, headers, limit) { | ||
constructor(body, headers, limit = 1024 * 1024) { | ||
this.body = body; | ||
@@ -4,0 +4,0 @@ this.headers = headers; |
{ | ||
"name": "http-body-parser", | ||
"version": "1.1.0", | ||
"description": "🎨 node,http,koa,koa2,express,express4,async,await,promise,json,text,stream,form,query,file,upload,body,parser", | ||
"version": "1.1.1", | ||
"description": "🎨 A body parser for node, koa, koa2, express. support json, form, text, multipart and stream type body.", | ||
"main": "index.js", | ||
@@ -44,2 +44,3 @@ "scripts": { | ||
"ava": "^0.19.1", | ||
"express": "^4.15.2", | ||
"koa": "^2.2.0", | ||
@@ -46,0 +47,0 @@ "supertest": "^3.0.0" |
@@ -1,2 +0,2 @@ | ||
koa-bodyparser | ||
http-body-parser | ||
=============== | ||
@@ -11,10 +11,10 @@ | ||
[npm-image]: https://img.shields.io/npm/v/koa-bodyparser.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-bodyparser | ||
[travis-image]: https://img.shields.io/travis/koajs/bodyparser.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/koajs/bodyparser | ||
[coveralls-image]: https://img.shields.io/coveralls/koajs/bodyparser.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/koajs/bodyparser?branch=master | ||
[david-image]: https://img.shields.io/david/koajs/bodyparser.svg?style=flat-square | ||
[david-url]: https://david-dm.org/koajs/bodyparser | ||
[npm-image]: https://img.shields.io/npm/v/http-body-parser.svg?style=flat-square | ||
[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 | ||
[node-image]: https://img.shields.io/badge/node.js-%3E=_7.6-green.svg?style=flat-square | ||
@@ -30,15 +30,14 @@ [node-url]: http://nodejs.org/download/ | ||
[![NPM](https://nodei.co/npm/koa-bodyparser.png?downloads=true)](https://nodei.co/npm/koa-bodyparser/) | ||
[![NPM](https://nodei.co/npm/http-body-parser.png?downloads=true)](https://nodei.co/npm/http-body-parser/) | ||
## Usage | ||
### Koa | ||
```js | ||
const Koa = require('koa'); | ||
const bodyParser = require('http-body-parser'); | ||
const bodyParser = require('http-body-parser').koa; | ||
const app = new Koa(); | ||
app.use(bodyParser({ | ||
app.use(bodyParser({})); | ||
})); | ||
app.use(async ctx => { | ||
@@ -51,2 +50,20 @@ // the parsed body will store in ctx.request.body | ||
### Express | ||
```js | ||
const express = require('express'); | ||
const bodyParser = require('http-body-parser').express; | ||
const app = express(); | ||
app.use(bodyParser({})); | ||
server.use('/', function(req, res) { | ||
res.send(req.body) | ||
}) | ||
``` | ||
## Features | ||
* Based on the ES6 syntax, the code is concise | ||
* No third party module dependent | ||
$ Support Koa and Express | ||
## Options | ||
@@ -72,6 +89,8 @@ | ||
stream: { | ||
limit: 1024*1024 | ||
limit: 1024*1024, | ||
path: `the operating system's default directory` | ||
}, | ||
multipart: { | ||
limit: 1024*1024 | ||
limit: 1024*1024, | ||
path: `the operating system's default directory` | ||
} | ||
@@ -82,5 +101,6 @@ } | ||
* **encode**: requested encoding. Default is `utf-8`. | ||
* **limit**: limit of the body. If the body ends up being larger than this limit, a 413 error code is returned. Default is `56kb`. | ||
* **limit**: limit of the body. If the body ends up being larger than this limit, a 413 error code is returned. | ||
* **strict**: when set to true, JSON parser will only accept arrays and objects. Default is `true`. | ||
* **extendTypes**: support extend types, eg: `['application/x-javascript']` | ||
* **extendTypes**: support extend types, eg: `application/x-javascript` | ||
* **path**: which folder the uploaded files should be stored, active on `multipart, stream` | ||
@@ -87,0 +107,0 @@ ## Raw Body |
@@ -6,3 +6,3 @@ const Koa = require('koa'); | ||
const request = require('supertest'); | ||
const bodyParser = require('../'); | ||
const bodyParser = require('../').koa; | ||
const image1 = path.join(__dirname, 'upload/image1.jpg'); | ||
@@ -16,3 +16,8 @@ const image2 = path.join(__dirname, 'upload/image2.jpg'); | ||
app.use(bodyParser({ | ||
enableTypes: ['json', 'form', 'text', 'multipart', 'stream'] | ||
enableTypes: [ | ||
'json', 'form', 'text', 'multipart', 'stream' | ||
], | ||
stream: { | ||
path: 'uploads/' | ||
} | ||
})) | ||
@@ -42,3 +47,3 @@ app.use(ctx => { | ||
t.is(res.status, 200); | ||
t.deepEqual(res.text, 'eqfox'); | ||
t.is(res.text, 'eqfox'); | ||
}); | ||
@@ -56,5 +61,6 @@ | ||
test('Stream', async t => { | ||
t.plan(1); | ||
t.plan(2); | ||
const res = await request(server).post('/').set('Content-Type', 'application/octet-stream').send(fs.readFileSync(image1)); | ||
t.is(res.status, 200); | ||
t.true(res.body.name != undefined); | ||
}); |
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
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
64026
14
549
105
4
4