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'
55 return function rawParser(req, res, next) {
57 return debug('body already parsed'), next()
60 req.body = req.body || {}
62 // skip requests without bodies
63 if (!typeis.hasBody(req)) {
64 return debug('skip empty body'), next()
67 debug('content-type %j', req.headers['content-type'])
69 // determine if request should be parsed
70 if (!shouldParse(req)) {
71 return debug('skip parsing'), next()
75 read(req, res, next, parse, debug, {
85 * Get the simple type checker.
87 * @param {string} type
91 function typeChecker(type) {
92 return function checkType(req) {
93 return Boolean(typeis(req, type))