3 * Copyright(c) 2014-2015 Douglas Christopher Wilson
10 * Module dependencies.
13 var bytes = require('bytes')
14 var debug = require('debug')('body-parser:raw')
15 var read = require('../read')
16 var typeis = require('type-is')
25 * Create a middleware to parse raw bodies.
27 * @param {object} [options]
32 function raw (options) {
33 var opts = options || {}
35 var inflate = opts.inflate !== false
36 var limit = typeof opts.limit !== 'number'
37 ? bytes.parse(opts.limit || '100kb')
39 var type = opts.type || 'application/octet-stream'
40 var verify = opts.verify || false
42 if (verify !== false && typeof verify !== 'function') {
43 throw new TypeError('option verify must be function')
46 // create the appropriate type checking function
47 var shouldParse = typeof type !== 'function'
51 function parse (buf) {
55 return function rawParser (req, res, next) {
57 debug('body already parsed')
62 req.body = req.body || {}
64 // skip requests without bodies
65 if (!typeis.hasBody(req)) {
66 debug('skip empty body')
71 debug('content-type %j', req.headers['content-type'])
73 // determine if request should be parsed
74 if (!shouldParse(req)) {
81 read(req, res, next, parse, debug, {
91 * Get the simple type checker.
93 * @param {string} type
97 function typeChecker (type) {
98 return function checkType (req) {
99 return Boolean(typeis(req, type))