diff --git a/lib/types/json.js b/lib/types/json.js index 5b4a5ff..4647c0a 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -12,13 +12,12 @@ * @private */ -var bytes = require('bytes') var createError = require('http-errors') var debug = require('debug')('body-parser:json') var isFinished = require('on-finished').isFinished var read = require('../read') var typeis = require('type-is') -var { getCharset, typeChecker } = require('../utils') +var { getCharset, normalizeOptions } = require('../utils') /** * Module exports. @@ -53,24 +52,10 @@ var JSON_SYNTAX_REGEXP = /#+/g function json (options) { var opts = options || {} + var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/json') - var limit = typeof opts.limit !== 'number' - ? bytes.parse(opts.limit || '100kb') - : opts.limit - var inflate = opts.inflate !== false var reviver = opts.reviver var strict = opts.strict !== false - var type = opts.type || 'application/json' - var verify = opts.verify || false - - if (verify !== false && typeof verify !== 'function') { - throw new TypeError('option verify must be function') - } - - // create the appropriate type checking function - var shouldParse = typeof type !== 'function' - ? typeChecker(type) - : type function parse (body) { if (body.length === 0) { diff --git a/lib/types/raw.js b/lib/types/raw.js index f393111..611ac0c 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -10,12 +10,11 @@ * Module dependencies. */ -var bytes = require('bytes') var debug = require('debug')('body-parser:raw') var isFinished = require('on-finished').isFinished var read = require('../read') var typeis = require('type-is') -var { typeChecker } = require('../utils') +var { normalizeOptions } = require('../utils') /** * Module exports. @@ -33,22 +32,7 @@ module.exports = raw function raw (options) { var opts = options || {} - - var inflate = opts.inflate !== false - var limit = typeof opts.limit !== 'number' - ? bytes.parse(opts.limit || '100kb') - : opts.limit - var type = opts.type || 'application/octet-stream' - var verify = opts.verify || false - - if (verify !== false && typeof verify !== 'function') { - throw new TypeError('option verify must be function') - } - - // create the appropriate type checking function - var shouldParse = typeof type !== 'function' - ? typeChecker(type) - : type + var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/octet-stream') function parse (buf) { return buf diff --git a/lib/types/text.js b/lib/types/text.js index 8cbd333..3b51060 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -10,12 +10,11 @@ * Module dependencies. */ -var bytes = require('bytes') var debug = require('debug')('body-parser:text') var isFinished = require('on-finished').isFinished var read = require('../read') var typeis = require('type-is') -var { getCharset, typeChecker } = require('../utils') +var { getCharset, normalizeOptions } = require('../utils') /** * Module exports. @@ -33,23 +32,9 @@ module.exports = text function text (options) { var opts = options || {} + var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'text/plain') var defaultCharset = opts.defaultCharset || 'utf-8' - var inflate = opts.inflate !== false - var limit = typeof opts.limit !== 'number' - ? bytes.parse(opts.limit || '100kb') - : opts.limit - var type = opts.type || 'text/plain' - var verify = opts.verify || false - - if (verify !== false && typeof verify !== 'function') { - throw new TypeError('option verify must be function') - } - - // create the appropriate type checking function - var shouldParse = typeof type !== 'function' - ? typeChecker(type) - : type function parse (buf) { return buf diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index a974f34..725fec2 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -12,14 +12,13 @@ * @private */ -var bytes = require('bytes') var createError = require('http-errors') var debug = require('debug')('body-parser:urlencoded') var isFinished = require('on-finished').isFinished var read = require('../read') var typeis = require('type-is') var qs = require('qs') -var { getCharset, typeChecker } = require('../utils') +var { getCharset, normalizeOptions } = require('../utils') /** * Module exports. @@ -37,21 +36,12 @@ module.exports = urlencoded function urlencoded (options) { var opts = options || {} + var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/x-www-form-urlencoded') var extended = Boolean(opts.extended) - var inflate = opts.inflate !== false - var limit = typeof opts.limit !== 'number' - ? bytes.parse(opts.limit || '100kb') - : opts.limit - var type = opts.type || 'application/x-www-form-urlencoded' - var verify = opts.verify || false var charsetSentinel = opts.charsetSentinel var interpretNumericEntities = opts.interpretNumericEntities - if (verify !== false && typeof verify !== 'function') { - throw new TypeError('option verify must be function') - } - var defaultCharset = opts.defaultCharset || 'utf-8' if (defaultCharset !== 'utf-8' && defaultCharset !== 'iso-8859-1') { throw new TypeError('option defaultCharset must be either utf-8 or iso-8859-1') @@ -60,11 +50,6 @@ function urlencoded (options) { // create the appropriate query parser var queryparse = createQueryParser(opts, extended) - // create the appropriate type checking function - var shouldParse = typeof type !== 'function' - ? typeChecker(type) - : type - function parse (body, encoding) { return body.length ? queryparse(body, encoding) diff --git a/lib/utils.js b/lib/utils.js index 3e97f78..6f255f4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -10,6 +10,7 @@ * Module dependencies. */ +var bytes = require('bytes') var contentType = require('content-type') var typeis = require('type-is') @@ -19,7 +20,7 @@ var typeis = require('type-is') module.exports = { getCharset, - typeChecker + normalizeOptions } /** @@ -49,3 +50,35 @@ function typeChecker (type) { return Boolean(typeis(req, type)) } } + +/** + * Normalizes the common options for all parsers. + * + * @param {object} options + * @param {string} defaultType + * @returns {object} + */ +function normalizeOptions (options, defaultType) { + var inflate = options.inflate !== false + var limit = typeof options.limit !== 'number' + ? bytes.parse(options.limit || '100kb') + : options.limit + var type = options.type || defaultType + var verify = options.verify || false + + if (verify !== false && typeof verify !== 'function') { + throw new TypeError('option verify must be function') + } + + // create the appropriate type checking function + var shouldParse = typeof type !== 'function' + ? typeChecker(type) + : type + + return { + inflate, + limit, + verify, + shouldParse + } +}