Skip to content

Commit f61122e

Browse files
committed
Refactor
1 parent 74cb460 commit f61122e

File tree

7 files changed

+50
-70
lines changed

7 files changed

+50
-70
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ to `'100kb'`.
9191
##### parser
9292

9393
The `parser` option is the function called against the request body to convert
94-
it to a Javascript object. If a `reviver` is supplied, it is supplied as the
94+
it to a JavaScript object. If a `reviver` is supplied, it is supplied as the
9595
second argument to this function.
9696

9797
```
@@ -309,7 +309,7 @@ The `depth` option is used to configure the maximum depth of the `qs` library wh
309309
##### parser
310310

311311
The `parser` option, if supplied, is used to in place of the default parser to
312-
convert the request body into a Javascript object. If this option is supplied,
312+
convert the request body into a JavaScript object. If this option is supplied,
313313
both the `extended` and `parameterLimit` options are ignored.
314314

315315
```

Diff for: index.js

-14
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ Object.defineProperty(exports, 'urlencoded', {
7373
get: createParserGetter('urlencoded')
7474
})
7575

76-
/**
77-
* Generic parser used to build parsers.
78-
* @public
79-
*/
80-
81-
Object.defineProperty(exports, 'generic', {
82-
configurable: true,
83-
enumerable: true,
84-
get: createParserGetter('generic')
85-
})
86-
8776
/**
8877
* Create a middleware to parse json and urlencoded bodies.
8978
*
@@ -134,9 +123,6 @@ function loadParser (parserName) {
134123
case 'urlencoded':
135124
parser = require('./lib/types/urlencoded')
136125
break
137-
case 'generic':
138-
parser = require('./lib/generic-parser')
139-
break
140126
}
141127

142128
// store to prevent invoking require()

Diff for: lib/generic-parser.js renamed to lib/factory.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ var typeis = require('type-is')
2424
* Module exports.
2525
*/
2626

27-
module.exports = generic
27+
module.exports = createBodyParser
2828

2929
/**
3030
* Use this to create a middleware that parses request bodies
3131
*
32-
* @param {object} [options]
32+
* @param {function} parse
33+
* @param {object} options
34+
* @param {object} defaultOptions
3335
* @return {function}
3436
* @public
3537
*/
3638

37-
function generic (parserOptions, parserOverrides) {
39+
function createBodyParser (parse, options, defaultOptions) {
3840
// Squash the options and the overrides down into one object
39-
var opts = Object.create(parserOptions)
40-
Object.assign(opts, parserOverrides)
41+
var opts = { ...defaultOptions || {}, ...options }
4142

4243
var limit = typeof opts.limit !== 'number'
4344
? bytes.parse(opts.limit || '100kb')
4445
: opts.limit
4546
var charset = opts.charset
4647
var inflate = opts.inflate !== false
4748
var verify = opts.verify || false
48-
var parse = opts.parse || defaultParse
4949
var defaultReqCharset = opts.defaultCharset || 'utf-8'
5050
var type = opts.type
5151

@@ -63,7 +63,7 @@ function generic (parserOptions, parserOverrides) {
6363
? charsetValidator(charset)
6464
: charset
6565

66-
return function genericParser (req, res, next) {
66+
return function (req, res, next) {
6767
if (isFinished(req)) {
6868
debug('body already parsed')
6969
next()
@@ -114,10 +114,6 @@ function generic (parserOptions, parserOverrides) {
114114
}
115115
}
116116

117-
function defaultParse (buf) {
118-
return buf
119-
}
120-
121117
/**
122118
* Get the charset of a request.
123119
*

Diff for: lib/types/json.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @private
1313
*/
1414

15-
var genericParser = require('../..').generic
15+
var createBodyParser = require('../factory')
1616
var debug = require('debug')('body-parser:json')
1717

1818
/**
@@ -52,16 +52,9 @@ function json (options) {
5252
var reviver = opts.reviver
5353
var strict = opts.strict !== false
5454
var parser = opts.parser || JSON.parse
55-
var type = opts.type || 'application/json'
5655

57-
return genericParser(opts, {
58-
type: type,
59-
60-
charset: function validateCharset (charset) {
61-
return charset.slice(0, 4) === 'utf-'
62-
},
63-
64-
parse: function parse (buf) {
56+
return createBodyParser(
57+
function (buf) {
6558
if (buf.length === 0) {
6659
// special-case empty json body, as it's a common client-side mistake
6760
// TODO: maybe make this configurable or part of "strict" option
@@ -86,8 +79,16 @@ function json (options) {
8679
stack: e.stack
8780
})
8881
}
82+
},
83+
opts,
84+
{
85+
parser: JSON.parse,
86+
type: 'application/json',
87+
charset: function (charset) {
88+
return charset.slice(0, 4) === 'utf-'
89+
}
8990
}
90-
})
91+
)
9192
}
9293

9394
/**

Diff for: lib/types/raw.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Module dependencies.
1111
*/
1212

13-
var genericParser = require('../..').generic
13+
var createBodyParser = require('../factory')
1414

1515
/**
1616
* Module exports.
@@ -27,11 +27,11 @@ module.exports = raw
2727
*/
2828

2929
function raw (options) {
30-
var opts = options || {}
31-
32-
var type = opts.type || 'application/octet-stream'
33-
34-
return genericParser(opts, {
35-
type: type
36-
})
30+
return createBodyParser(
31+
function (buf) { return buf },
32+
options,
33+
{
34+
type: 'application/octet-stream'
35+
}
36+
)
3737
}

Diff for: lib/types/text.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Module dependencies.
1111
*/
1212

13-
var genericParser = require('../..').generic
13+
var createBodyParser = require('../factory')
1414

1515
/**
1616
* Module exports.
@@ -27,14 +27,13 @@ module.exports = text
2727
*/
2828

2929
function text (options) {
30-
var opts = options || {}
31-
32-
var defaultCharset = opts.defaultCharset || 'utf-8'
33-
var type = opts.type || 'text/plain'
34-
35-
return genericParser(opts, {
36-
type: type,
37-
charset: function validateCharset () { return true },
38-
defaultCharset: defaultCharset
39-
})
30+
return createBodyParser(
31+
function (buf) { return buf },
32+
options,
33+
{
34+
type: 'text/plain',
35+
charset: function () { return true },
36+
defaultCharset: 'utf-8'
37+
}
38+
)
4039
}

Diff for: lib/types/urlencoded.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
var createError = require('http-errors')
1616
var debug = require('debug')('body-parser:urlencoded')
17-
var genericParser = require('../..').generic
17+
var createBodyParser = require('../factory')
1818
var qs = require('qs')
1919

2020
/**
@@ -34,23 +34,21 @@ function urlencoded (options) {
3434
var opts = options || {}
3535

3636
var extended = Boolean(opts.extended)
37-
var type = opts.type || 'application/x-www-form-urlencoded'
3837

3938
var queryparse = opts.parser || createQueryParser(opts, extended)
4039

41-
return genericParser(opts, {
42-
type: type,
43-
44-
charset: function validateCharset (charset) {
45-
return charset === 'utf-8' || charset === 'iso-8859-1'
40+
return createBodyParser(
41+
function (body, encoding) {
42+
return body.length ? queryparse(body, encoding) : {}
4643
},
47-
48-
parse: function parse (body, encoding) {
49-
return body.length
50-
? queryparse(body, encoding)
51-
: {}
44+
opts,
45+
{
46+
type: 'application/x-www-form-urlencoded',
47+
charset: function (charset) {
48+
return charset === 'utf-8' || charset === 'iso-8859-1'
49+
}
5250
}
53-
})
51+
)
5452
}
5553

5654
/**

0 commit comments

Comments
 (0)