diff --git a/README.md b/README.md index 394d577c..f80f0b47 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ to `'100kb'`. ##### parser The `parser` option is the function called against the request body to convert -it to a Javascript object. If a `reviver` is supplied, it is supplied as the +it to a JavaScript object. If a `reviver` is supplied, it is supplied as the second argument to this function. ``` @@ -309,7 +309,7 @@ The `depth` option is used to configure the maximum depth of the `qs` library wh ##### parser The `parser` option, if supplied, is used to in place of the default parser to -convert the request body into a Javascript object. If this option is supplied, +convert the request body into a JavaScript object. If this option is supplied, both the `extended` and `parameterLimit` options are ignored. ``` diff --git a/lib/generic-parser.js b/lib/factory.js similarity index 90% rename from lib/generic-parser.js rename to lib/factory.js index deb96d10..5f04ab7a 100644 --- a/lib/generic-parser.js +++ b/lib/factory.js @@ -24,20 +24,21 @@ var typeis = require('type-is') * Module exports. */ -module.exports = generic +module.exports = createBodyParser /** * Use this to create a middleware that parses request bodies * - * @param {object} [options] + * @param {function} parse + * @param {object} options + * @param {object} defaultOptions * @return {function} * @public */ -function generic (parserOptions, parserOverrides) { +function createBodyParser (parse, options, defaultOptions) { // Squash the options and the overrides down into one object - var opts = Object.create(parserOptions) - Object.assign(opts, parserOverrides) + var opts = { ...defaultOptions || {}, ...options } var limit = typeof opts.limit !== 'number' ? bytes.parse(opts.limit || '100kb') @@ -45,7 +46,6 @@ function generic (parserOptions, parserOverrides) { var charset = opts.charset var inflate = opts.inflate !== false var verify = opts.verify || false - var parse = opts.parse || defaultParse var defaultReqCharset = opts.defaultCharset || 'utf-8' var type = opts.type @@ -63,7 +63,7 @@ function generic (parserOptions, parserOverrides) { ? charsetValidator(charset) : charset - return function genericParser (req, res, next) { + return function (req, res, next) { if (isFinished(req)) { debug('body already parsed') next() @@ -114,10 +114,6 @@ function generic (parserOptions, parserOverrides) { } } -function defaultParse (buf) { - return buf -} - /** * Get the charset of a request. * diff --git a/lib/types/json.js b/lib/types/json.js index ae1af796..44725fd1 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -12,7 +12,7 @@ * @private */ -var genericParser = require('../..').generic +var createBodyParser = require('../factory') var debug = require('debug')('body-parser:json') /** @@ -52,16 +52,9 @@ function json (options) { var reviver = opts.reviver var strict = opts.strict !== false var parser = opts.parser || JSON.parse - var type = opts.type || 'application/json' - return genericParser(opts, { - type: type, - - charset: function validateCharset (charset) { - return charset.slice(0, 4) === 'utf-' - }, - - parse: function parse (buf) { + return createBodyParser( + function (buf) { if (buf.length === 0) { // special-case empty json body, as it's a common client-side mistake // TODO: maybe make this configurable or part of "strict" option @@ -86,8 +79,16 @@ function json (options) { stack: e.stack }) } + }, + opts, + { + parser: JSON.parse, + type: 'application/json', + charset: function (charset) { + return charset.slice(0, 4) === 'utf-' + } } - }) + ) } /** diff --git a/lib/types/raw.js b/lib/types/raw.js index b40cfd5d..3a33e888 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -10,7 +10,7 @@ * Module dependencies. */ -var genericParser = require('../..').generic +var createBodyParser = require('../factory') /** * Module exports. @@ -27,11 +27,11 @@ module.exports = raw */ function raw (options) { - var opts = options || {} - - var type = opts.type || 'application/octet-stream' - - return genericParser(opts, { - type: type - }) + return createBodyParser( + function (buf) { return buf }, + options, + { + type: 'application/octet-stream' + } + ) } diff --git a/lib/types/text.js b/lib/types/text.js index 87e65793..2b8ef4ec 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -10,7 +10,7 @@ * Module dependencies. */ -var genericParser = require('../..').generic +var createBodyParser = require('../factory') /** * Module exports. @@ -27,14 +27,13 @@ module.exports = text */ function text (options) { - var opts = options || {} - - var defaultCharset = opts.defaultCharset || 'utf-8' - var type = opts.type || 'text/plain' - - return genericParser(opts, { - type: type, - charset: function validateCharset () { return true }, - defaultCharset: defaultCharset - }) + return createBodyParser( + function (buf) { return buf }, + options, + { + type: 'text/plain', + charset: function () { return true }, + defaultCharset: 'utf-8' + } + ) } diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index 95520a26..cd93f149 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -14,7 +14,7 @@ var createError = require('http-errors') var debug = require('debug')('body-parser:urlencoded') -var genericParser = require('../..').generic +var createBodyParser = require('../factory') var qs = require('qs') /** @@ -34,23 +34,21 @@ function urlencoded (options) { var opts = options || {} var extended = Boolean(opts.extended) - var type = opts.type || 'application/x-www-form-urlencoded' var queryparse = opts.parser || createQueryParser(opts, extended) - return genericParser(opts, { - type: type, - - charset: function validateCharset (charset) { - return charset === 'utf-8' || charset === 'iso-8859-1' + return createBodyParser( + function (body, encoding) { + return body.length ? queryparse(body, encoding) : {} }, - - parse: function parse (body, encoding) { - return body.length - ? queryparse(body, encoding) - : {} + opts, + { + type: 'application/x-www-form-urlencoded', + charset: function (charset) { + return charset === 'utf-8' || charset === 'iso-8859-1' + } } - }) + ) } /**