Skip to content

Commit 2f27257

Browse files
authored
perf: move read options outside parser middlewares (#648)
Move options object creation outside the returned parser middlewares to avoid recreating objects on every request.
1 parent b85fe58 commit 2f27257

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

lib/types/json.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var JSON_SYNTAX_REGEXP = /#+/g
4848
*/
4949

5050
function json (options) {
51-
var normalizedOptions = normalizeOptions(options, 'application/json')
51+
const normalizedOptions = normalizeOptions(options, 'application/json')
5252

5353
var reviver = options?.reviver
5454
var strict = options?.strict !== false
@@ -80,13 +80,14 @@ function json (options) {
8080
}
8181
}
8282

83-
return function jsonParser (req, res, next) {
84-
read(req, res, next, parse, debug, {
85-
...normalizedOptions,
83+
const readOptions = {
84+
...normalizedOptions,
85+
// assert charset per RFC 7159 sec 8.1
86+
isValidCharset: (charset) => charset.slice(0, 4) === 'utf-'
87+
}
8688

87-
// assert charset per RFC 7159 sec 8.1
88-
isValidCharset: (charset) => charset.slice(0, 4) === 'utf-'
89-
})
89+
return function jsonParser (req, res, next) {
90+
read(req, res, next, parse, debug, readOptions)
9091
}
9192
}
9293

lib/types/raw.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ module.exports = raw
2929
*/
3030

3131
function raw (options) {
32-
var normalizedOptions = normalizeOptions(options, 'application/octet-stream')
32+
const normalizedOptions = normalizeOptions(options, 'application/octet-stream')
3333

34-
return function rawParser (req, res, next) {
35-
read(req, res, next, passthrough, debug, {
36-
...normalizedOptions,
34+
const readOptions = {
35+
...normalizedOptions,
36+
// Skip charset validation and parse the body as is
37+
skipCharset: true
38+
}
3739

38-
// Skip charset validation and parse the body as is
39-
skipCharset: true
40-
})
40+
return function rawParser (req, res, next) {
41+
read(req, res, next, passthrough, debug, readOptions)
4142
}
4243
}

lib/types/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = text
2929
*/
3030

3131
function text (options) {
32-
var normalizedOptions = normalizeOptions(options, 'text/plain')
32+
const normalizedOptions = normalizeOptions(options, 'text/plain')
3333

3434
return function textParser (req, res, next) {
3535
read(req, res, next, passthrough, debug, normalizedOptions)

lib/types/urlencoded.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = urlencoded
3333
*/
3434

3535
function urlencoded (options) {
36-
var normalizedOptions = normalizeOptions(options, 'application/x-www-form-urlencoded')
36+
const normalizedOptions = normalizeOptions(options, 'application/x-www-form-urlencoded')
3737

3838
if (normalizedOptions.defaultCharset !== 'utf-8' && normalizedOptions.defaultCharset !== 'iso-8859-1') {
3939
throw new TypeError('option defaultCharset must be either utf-8 or iso-8859-1')
@@ -48,13 +48,14 @@ function urlencoded (options) {
4848
: {}
4949
}
5050

51-
return function urlencodedParser (req, res, next) {
52-
read(req, res, next, parse, debug, {
53-
...normalizedOptions,
51+
const readOptions = {
52+
...normalizedOptions,
53+
// assert charset
54+
isValidCharset: (charset) => charset === 'utf-8' || charset === 'iso-8859-1'
55+
}
5456

55-
// assert charset
56-
isValidCharset: (charset) => charset === 'utf-8' || charset === 'iso-8859-1'
57-
})
57+
return function urlencodedParser (req, res, next) {
58+
read(req, res, next, parse, debug, readOptions)
5859
}
5960
}
6061

0 commit comments

Comments
 (0)