raw.js
1 /*! 2 * body-parser 3 * Copyright(c) 2014-2015 Douglas Christopher Wilson 4 * MIT Licensed 5 */ 6 7 'use strict' 8 9 /** 10 * Module dependencies. 11 */ 12 13 var debug = require('debug')('body-parser:raw') 14 var isFinished = require('on-finished').isFinished 15 var read = require('../read') 16 var typeis = require('type-is') 17 var { normalizeOptions } = require('../utils') 18 19 /** 20 * Module exports. 21 */ 22 23 module.exports = raw 24 25 /** 26 * Create a middleware to parse raw bodies. 27 * 28 * @param {object} [options] 29 * @return {function} 30 * @api public 31 */ 32 33 function raw (options) { 34 var { inflate, limit, verify, shouldParse } = normalizeOptions(options, 'application/octet-stream') 35 36 function parse (buf) { 37 return buf 38 } 39 40 return function rawParser (req, res, next) { 41 if (isFinished(req)) { 42 debug('body already parsed') 43 next() 44 return 45 } 46 47 if (!('body' in req)) { 48 req.body = undefined 49 } 50 51 // skip requests without bodies 52 if (!typeis.hasBody(req)) { 53 debug('skip empty body') 54 next() 55 return 56 } 57 58 debug('content-type %j', req.headers['content-type']) 59 60 // determine if request should be parsed 61 if (!shouldParse(req)) { 62 debug('skip parsing') 63 next() 64 return 65 } 66 67 // read 68 read(req, res, next, parse, debug, { 69 encoding: null, 70 inflate, 71 limit, 72 verify 73 }) 74 } 75 }