diff --git a/lib/types/json.js b/lib/types/json.js index 2d136e50..15c54bb4 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -48,7 +48,7 @@ var JSON_SYNTAX_REGEXP = /#+/g */ function json (options) { - var normalizedOptions = normalizeOptions(options, 'application/json') + const normalizedOptions = normalizeOptions(options, 'application/json') var reviver = options?.reviver var strict = options?.strict !== false @@ -80,13 +80,14 @@ function json (options) { } } - return function jsonParser (req, res, next) { - read(req, res, next, parse, debug, { - ...normalizedOptions, + const readOptions = { + ...normalizedOptions, + // assert charset per RFC 7159 sec 8.1 + isValidCharset: (charset) => charset.slice(0, 4) === 'utf-' + } - // assert charset per RFC 7159 sec 8.1 - isValidCharset: (charset) => charset.slice(0, 4) === 'utf-' - }) + return function jsonParser (req, res, next) { + read(req, res, next, parse, debug, readOptions) } } diff --git a/lib/types/raw.js b/lib/types/raw.js index f0d989b5..04b1b88f 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -29,14 +29,15 @@ module.exports = raw */ function raw (options) { - var normalizedOptions = normalizeOptions(options, 'application/octet-stream') + const normalizedOptions = normalizeOptions(options, 'application/octet-stream') - return function rawParser (req, res, next) { - read(req, res, next, passthrough, debug, { - ...normalizedOptions, + const readOptions = { + ...normalizedOptions, + // Skip charset validation and parse the body as is + skipCharset: true + } - // Skip charset validation and parse the body as is - skipCharset: true - }) + return function rawParser (req, res, next) { + read(req, res, next, passthrough, debug, readOptions) } } diff --git a/lib/types/text.js b/lib/types/text.js index 8b61a049..d4c7e3b6 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -29,7 +29,7 @@ module.exports = text */ function text (options) { - var normalizedOptions = normalizeOptions(options, 'text/plain') + const normalizedOptions = normalizeOptions(options, 'text/plain') return function textParser (req, res, next) { read(req, res, next, passthrough, debug, normalizedOptions) diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index 4d8750aa..ae6b2478 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -33,7 +33,7 @@ module.exports = urlencoded */ function urlencoded (options) { - var normalizedOptions = normalizeOptions(options, 'application/x-www-form-urlencoded') + const normalizedOptions = normalizeOptions(options, 'application/x-www-form-urlencoded') if (normalizedOptions.defaultCharset !== 'utf-8' && normalizedOptions.defaultCharset !== 'iso-8859-1') { throw new TypeError('option defaultCharset must be either utf-8 or iso-8859-1') @@ -48,13 +48,14 @@ function urlencoded (options) { : {} } - return function urlencodedParser (req, res, next) { - read(req, res, next, parse, debug, { - ...normalizedOptions, + const readOptions = { + ...normalizedOptions, + // assert charset + isValidCharset: (charset) => charset === 'utf-8' || charset === 'iso-8859-1' + } - // assert charset - isValidCharset: (charset) => charset === 'utf-8' || charset === 'iso-8859-1' - }) + return function urlencodedParser (req, res, next) { + read(req, res, next, parse, debug, readOptions) } }