Skip to content

Commit 4d85c4c

Browse files
authored
refactor: cleanup parser options (#596)
1 parent d11899b commit 4d85c4c

File tree

4 files changed

+28
-38
lines changed

4 files changed

+28
-38
lines changed

lib/types/json.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ var JSON_SYNTAX_REGEXP = /#+/g
5151
*/
5252

5353
function json (options) {
54-
var opts = options || {}
55-
var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/json')
54+
var { inflate, limit, verify, shouldParse } = normalizeOptions(options, 'application/json')
5655

57-
var reviver = opts.reviver
58-
var strict = opts.strict !== false
56+
var reviver = options?.reviver
57+
var strict = options?.strict !== false
5958

6059
function parse (body) {
6160
if (body.length === 0) {
@@ -125,9 +124,9 @@ function json (options) {
125124
// read
126125
read(req, res, next, parse, debug, {
127126
encoding: charset,
128-
inflate: inflate,
129-
limit: limit,
130-
verify: verify
127+
inflate,
128+
limit,
129+
verify
131130
})
132131
}
133132
}

lib/types/raw.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ module.exports = raw
3131
*/
3232

3333
function raw (options) {
34-
var opts = options || {}
35-
var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/octet-stream')
34+
var { inflate, limit, verify, shouldParse } = normalizeOptions(options, 'application/octet-stream')
3635

3736
function parse (buf) {
3837
return buf
@@ -68,9 +67,9 @@ function raw (options) {
6867
// read
6968
read(req, res, next, parse, debug, {
7069
encoding: null,
71-
inflate: inflate,
72-
limit: limit,
73-
verify: verify
70+
inflate,
71+
limit,
72+
verify
7473
})
7574
}
7675
}

lib/types/text.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ module.exports = text
3131
*/
3232

3333
function text (options) {
34-
var opts = options || {}
35-
var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'text/plain')
34+
var { inflate, limit, verify, shouldParse } = normalizeOptions(options, 'text/plain')
3635

37-
var defaultCharset = opts.defaultCharset || 'utf-8'
36+
var defaultCharset = options?.defaultCharset || 'utf-8'
3837

3938
function parse (buf) {
4039
return buf
@@ -73,9 +72,9 @@ function text (options) {
7372
// read
7473
read(req, res, next, parse, debug, {
7574
encoding: charset,
76-
inflate: inflate,
77-
limit: limit,
78-
verify: verify
75+
inflate,
76+
limit,
77+
verify
7978
})
8079
}
8180
}

lib/types/urlencoded.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,15 @@ module.exports = urlencoded
3535
*/
3636

3737
function urlencoded (options) {
38-
var opts = options || {}
39-
var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/x-www-form-urlencoded')
38+
var { inflate, limit, verify, shouldParse } = normalizeOptions(options, 'application/x-www-form-urlencoded')
4039

41-
var extended = Boolean(opts.extended)
42-
var charsetSentinel = opts.charsetSentinel
43-
var interpretNumericEntities = opts.interpretNumericEntities
44-
45-
var defaultCharset = opts.defaultCharset || 'utf-8'
40+
var defaultCharset = options?.defaultCharset || 'utf-8'
4641
if (defaultCharset !== 'utf-8' && defaultCharset !== 'iso-8859-1') {
4742
throw new TypeError('option defaultCharset must be either utf-8 or iso-8859-1')
4843
}
4944

5045
// create the appropriate query parser
51-
var queryparse = createQueryParser(opts, extended)
46+
var queryparse = createQueryParser(options)
5247

5348
function parse (body, encoding) {
5449
return body.length
@@ -96,13 +91,10 @@ function urlencoded (options) {
9691

9792
// read
9893
read(req, res, next, parse, debug, {
99-
debug: debug,
10094
encoding: charset,
101-
inflate: inflate,
102-
limit: limit,
103-
verify: verify,
104-
charsetSentinel: charsetSentinel,
105-
interpretNumericEntities: interpretNumericEntities
95+
inflate,
96+
limit,
97+
verify
10698
})
10799
}
108100
}
@@ -113,13 +105,14 @@ function urlencoded (options) {
113105
* @param {object} options
114106
*/
115107

116-
function createQueryParser (options, extended) {
117-
var parameterLimit = options.parameterLimit !== undefined
118-
? options.parameterLimit
108+
function createQueryParser (options) {
109+
var extended = Boolean(options?.extended)
110+
var parameterLimit = options?.parameterLimit !== undefined
111+
? options?.parameterLimit
119112
: 1000
120-
var charsetSentinel = options.charsetSentinel
121-
var interpretNumericEntities = options.interpretNumericEntities
122-
var depth = extended ? (options.depth !== undefined ? options.depth : 32) : 0
113+
var charsetSentinel = options?.charsetSentinel
114+
var interpretNumericEntities = options?.interpretNumericEntities
115+
var depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0
123116

124117
if (isNaN(parameterLimit) || parameterLimit < 1) {
125118
throw new TypeError('option parameterLimit must be a positive number')

0 commit comments

Comments
 (0)