• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

expressjs / body-parser / 12779342431

15 Jan 2025 01:00AM UTC coverage: 98.723% (-0.3%) from 99.057%
12779342431

Pull #574

github

web-flow
Merge 8715c6694 into ee8bd68d1
Pull Request #574: yet another rebase! generic body parser

79 of 80 new or added lines in 5 files covered. (98.75%)

1 existing line in 1 file now uncovered.

232 of 235 relevant lines covered (98.72%)

2699.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/lib/generic-parser.js
1
/*!
2
 * body-parser
3
 * Copyright(c) 2014 Jonathan Ong
4
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
5
 * MIT Licensed
6
 */
7

8
'use strict'
9

10
/**
11
 * Module dependencies.
12
 * @private
13
 */
14

15
var bytes = require('bytes')
6✔
16
var contentType = require('content-type')
6✔
17
var createError = require('http-errors')
6✔
18
var debug = require('debug')('body-parser:generic')
6✔
19
var isFinished = require('on-finished').isFinished
6✔
20
var read = require('./read')
6✔
21
var typeis = require('type-is')
6✔
22

23
/**
24
 * Module exports.
25
 */
26

27
module.exports = generic
6✔
28

29
/**
30
 * Use this to create a middleware that parses request bodies
31
 *
32
 * @param {object} [options]
33
 * @return {function}
34
 * @public
35
 */
36

37
function generic (parserOptions, parserOverrides) {
38
  // Squash the options and the overrides down into one object
39
  var opts = Object.create(parserOptions)
894✔
40
  Object.assign(opts, parserOverrides)
894✔
41

42
  var limit = typeof opts.limit !== 'number'
894✔
43
    ? bytes.parse(opts.limit || '100kb')
44
    : opts.limit
45
  var charset = opts.charset
894✔
46
  var inflate = opts.inflate !== false
894✔
47
  var verify = opts.verify || false
894✔
48
  var parse = opts.parse || defaultParse
894✔
49
  var defaultReqCharset = opts.defaultCharset || 'utf-8'
894✔
50
  var type = opts.type
894✔
51

52
  if (verify !== false && typeof verify !== 'function') {
894✔
53
    throw new TypeError('option verify must be function')
24✔
54
  }
55

56
  // create the appropriate type checking function
57
  var shouldParse = typeof type !== 'function'
870✔
58
    ? typeChecker(type)
59
    : type
60

61
  // create the appropriate charset validating function
62
  var validCharset = typeof charset !== 'function'
870✔
63
    ? charsetValidator(charset)
64
    : charset
65

66
  return function genericParser (req, res, next) {
870✔
67
    if (isFinished(req)) {
1,404✔
68
      debug('body already parsed')
48✔
69
      next()
48✔
70
      return
48✔
71
    }
72

73
    if (!('body' in req)) {
1,356✔
74
      req.body = undefined
1,356✔
75
    }
76

77
    // skip requests without bodies
78
    if (!typeis.hasBody(req)) {
1,356✔
79
      debug('skip empty body')
30✔
80
      next()
30✔
81
      return
30✔
82
    }
83

84
    debug('content-type %j', req.headers['content-type'])
1,326✔
85

86
    // determine if request should be parsed
87
    if (!shouldParse(req)) {
1,326✔
88
      debug('skip parsing')
72✔
89
      next()
72✔
90
      return
72✔
91
    }
92

93
    // assert charset per RFC 7159 sec 8.1
94
    var reqCharset = null
1,254✔
95
    if (charset !== undefined) {
1,254✔
96
      reqCharset = getCharset(req) || defaultReqCharset
1,050✔
97
      if (!validCharset(reqCharset)) {
1,050✔
98
        debug('invalid charset')
24✔
99
        next(createError(415, 'unsupported charset "' + reqCharset.toUpperCase() + '"', {
24✔
100
          charset: reqCharset,
101
          type: 'charset.unsupported'
102
        }))
103
        return
24✔
104
      }
105
    }
106

107
    // read
108
    read(req, res, next, parse, debug, {
1,230✔
109
      encoding: reqCharset,
110
      inflate: inflate,
111
      limit: limit,
112
      verify: verify
113
    })
114
  }
115
}
116

117
function defaultParse (buf) {
118
  return buf
270✔
119
}
120

121
/**
122
 * Get the charset of a request.
123
 *
124
 * @param {object} req
125
 * @api private
126
 */
127

128
function getCharset (req) {
129
  try {
1,050✔
130
    return (contentType.parse(req).parameters.charset || '').toLowerCase()
1,050✔
131
  } catch (e) {
132
    return undefined
18✔
133
  }
134
}
135

136
/**
137
 * Get the simple type checker.
138
 *
139
 * @param {string} type
140
 * @return {function}
141
 */
142

143
function typeChecker (type) {
144
  return function checkType (req) {
798✔
145
    return Boolean(typeis(req, type))
1,278✔
146
  }
147
}
148

149
/**
150
 * Get the simple charset validator.
151
 *
152
 * @param {string} type
153
 * @return {function}
154
 */
155

156
function charsetValidator (charset) {
157
  return function validateCharset (reqCharset) {
144✔
NEW
158
    return charset === reqCharset
×
159
  }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc