Sha256: 4e9cc80a7ee8bd667c68c264b4c374b28e731246ddb6ec22c3968daf837e30a2

Contents?: true

Size: 1.84 KB

Versions: 34

Compression:

Stored size: 1.84 KB

Contents

/*!
 * body-parser
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 */

'use strict'

/**
 * Module dependencies.
 */

var bytes = require('bytes')
var debug = require('debug')('body-parser:raw')
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) {
  var opts = options || {}

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

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

  // create the appropriate type checking function
  var shouldParse = typeof type !== 'function'
    ? typeChecker(type)
    : type

  function parse (buf) {
    return buf
  }

  return function rawParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // read
    read(req, res, next, parse, debug, {
      encoding: null,
      inflate: inflate,
      limit: limit,
      verify: verify
    })
  }
}

/**
 * Get the simple type checker.
 *
 * @param {string} type
 * @return {function}
 */

function typeChecker (type) {
  return function checkType (req) {
    return Boolean(typeis(req, type))
  }
}

Version data entries

34 entries across 33 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.18.0 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.18.2 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.16.1 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.15.2 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.18.4 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.18.1 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.14.0 test/dummy/node_modules/body-parser/lib/types/raw.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/body-parser/lib/types/raw.js
tang-0.2.1 spec/tang_app/node_modules/body-parser/lib/types/raw.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/body-parser/lib/types/raw.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/body-parser/lib/types/raw.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/body-parser/lib/types/raw.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/body-parser/lib/types/raw.js
tang-0.2.0 spec/tang_app/node_modules/body-parser/lib/types/raw.js
tang-0.1.0 spec/tang_app/node_modules/body-parser/lib/types/raw.js
tang-0.0.9 spec/tang_app/node_modules/body-parser/lib/types/raw.js
enju_library-0.3.8 spec/dummy/node_modules/body-parser/lib/types/raw.js
ilog-0.4.1 node_modules/body-parser/lib/types/raw.js