Skip to content

Commit

Permalink
add raw parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 19, 2014
1 parent bda6029 commit 9d4f7b6
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 13 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* add `raw` parser
* check accepted charset in content-type (accepts utf-8)
* check accepted encoding in content-encoding (accepts identity)
* deprecate `bodyParser()` middleware; use `.json()` and `.urlencoded()` as needed
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding

The `reviver` argument is passed directly to `JSON.parse` as the second argument. You can find more information on this argument [in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter).

### bodyParser.raw(options)

Returns middleware that parses all bodies as a `Buffer`. This parser supports automatic inflation of `gzip` and `deflate` encodings.

The options are:

- `limit` - maximum request body size. (default: `<100kb>`)
- `type` - request content-type to parse (default: `application/octet-stream`)
- `verify` - function to verify body content

The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `bin`), a mime type (like `application/octet-stream`), or a mime time with a wildcard (like `application/*`).

The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.

### bodyParser.urlencoded(options)

Returns middleware that only parses `urlencoded` bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings.
Expand Down
12 changes: 6 additions & 6 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function read(req, res, next, parse, options) {
options = options || {}
options.length = length

var encoding = options.encoding || 'utf-8'
var encoding = options.encoding !== null
? options.encoding || 'utf-8'
: null
var verify = options.verify

options.encoding = verify
Expand Down Expand Up @@ -76,8 +78,6 @@ function read(req, res, next, parse, options) {
return
}

var str

// verify
if (verify) {
try {
Expand All @@ -90,12 +90,12 @@ function read(req, res, next, parse, options) {

// parse
try {
str = typeof body !== 'string'
body = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
req.body = parse(body)
} catch (err){
err.body = str
err.body = body
err.status = 400
return next(err)
}
Expand Down
8 changes: 4 additions & 4 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ function json(options) {
throw new TypeError('option verify must be function')
}

function parse(str) {
if (0 === str.length) {
function parse(body) {
if (0 === body.length) {
throw new Error('invalid json, empty body')
}

if (strict) {
var first = firstchar(str)
var first = firstchar(body)

if (first !== '{' && first !== '[') {
throw new Error('invalid json')
}
}

return JSON.parse(str, reviver)
return JSON.parse(body, reviver)
}

return function jsonParser(req, res, next) {
Expand Down
58 changes: 58 additions & 0 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*!
* body-parser
* MIT Licensed
*/

/**
* Module dependencies.
*/

var bytes = require('bytes')
var read = require('../read')
var typeis = require('type-is')

/**
* Module exports.
*/

module.exports = raw

/**
* Create a middleware to parse raw bodies.
*
* @param {object} [options]
* @return {function}
* @api public
*/

function raw(options) {
options = options || {};

var limit = typeof options.limit !== 'number'
? bytes(options.limit || '100kb')
: options.limit
var type = options.type || 'application/octet-stream'
var verify = options.verify || false

if (verify !== false && typeof verify !== 'function') {
throw new TypeError('option verify must be function')
}

function parse(buf) {
return buf
}

return function rawParser(req, res, next) {
if (req._body) return next()
req.body = req.body || {}

if (!typeis(req, type)) return next()

// read
read(req, res, next, parse, {
encoding: null,
limit: limit,
verify: verify
})
}
}
6 changes: 3 additions & 3 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function urlencoded(options){
? qs.parse
: querystring.parse

function parse(str) {
return str.length
? queryparse(str)
function parse(body) {
return body.length
? queryparse(body)
: {}
}

Expand Down
Loading

0 comments on commit 9d4f7b6

Please sign in to comment.