diff --git a/HISTORY.md b/HISTORY.md index afd9c78d..4833a919 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========================= +* extract shared utility functions * remove `unpipe` package and use native `unpipe()` method 2.0.1 / 2024-09-10 diff --git a/lib/types/json.js b/lib/types/json.js index 30bf8cab..4647c0a5 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -12,13 +12,12 @@ * @private */ -var bytes = require('bytes') -var contentType = require('content-type') 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, 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) { @@ -196,21 +181,6 @@ function firstchar (str) { : undefined } -/** - * Get the charset of a request. - * - * @param {object} req - * @api private - */ - -function getCharset (req) { - try { - return (contentType.parse(req).parameters.charset || '').toLowerCase() - } catch (e) { - return undefined - } -} - /** * Normalize a SyntaxError for JSON.parse. * @@ -235,16 +205,3 @@ function normalizeJsonSyntaxError (error, obj) { return error } - -/** - * Get the simple type checker. - * - * @param {string} type - * @return {function} - */ - -function typeChecker (type) { - return function checkType (req) { - return Boolean(typeis(req, type)) - } -} diff --git a/lib/types/raw.js b/lib/types/raw.js index bfe274cf..611ac0c2 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -10,11 +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 { normalizeOptions } = require('../utils') /** * Module exports. @@ -32,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 @@ -89,16 +74,3 @@ function raw (options) { }) } } - -/** - * Get the simple type checker. - * - * @param {string} type - * @return {function} - */ - -function typeChecker (type) { - return function checkType (req) { - return Boolean(typeis(req, type)) - } -} diff --git a/lib/types/text.js b/lib/types/text.js index b153931b..3b51060d 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -10,12 +10,11 @@ * Module dependencies. */ -var bytes = require('bytes') -var contentType = require('content-type') var debug = require('debug')('body-parser:text') var isFinished = require('on-finished').isFinished var read = require('../read') var typeis = require('type-is') +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 @@ -94,31 +79,3 @@ function text (options) { }) } } - -/** - * Get the charset of a request. - * - * @param {object} req - * @api private - */ - -function getCharset (req) { - try { - return (contentType.parse(req).parameters.charset || '').toLowerCase() - } catch (e) { - return undefined - } -} - -/** - * Get the simple type checker. - * - * @param {string} type - * @return {function} - */ - -function typeChecker (type) { - return function checkType (req) { - return Boolean(typeis(req, type)) - } -} diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index 687745f8..725fec2e 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -12,14 +12,13 @@ * @private */ -var bytes = require('bytes') -var contentType = require('content-type') 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, 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) @@ -184,21 +169,6 @@ function createQueryParser (options, extended) { } } -/** - * Get the charset of a request. - * - * @param {object} req - * @api private - */ - -function getCharset (req) { - try { - return (contentType.parse(req).parameters.charset || '').toLowerCase() - } catch (e) { - return undefined - } -} - /** * Count the number of parameters, stopping once limit reached * @@ -222,16 +192,3 @@ function parameterCount (body, limit) { return count } - -/** - * Get the simple type checker. - * - * @param {string} type - * @return {function} - */ - -function typeChecker (type) { - return function checkType (req) { - return Boolean(typeis(req, type)) - } -} diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..6f255f43 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,84 @@ +/*! + * body-parser + * Copyright(c) 2014-2015 Douglas Christopher Wilson + * MIT Licensed + */ + +'use strict' + +/** + * Module dependencies. + */ + +var bytes = require('bytes') +var contentType = require('content-type') +var typeis = require('type-is') + +/** + * Module exports. + */ + +module.exports = { + getCharset, + normalizeOptions +} + +/** + * Get the charset of a request. + * + * @param {object} req + * @api private + */ + +function getCharset (req) { + try { + return (contentType.parse(req).parameters.charset || '').toLowerCase() + } catch (e) { + return undefined + } +} + +/** + * Get the simple type checker. + * + * @param {string} type + * @return {function} + */ + +function typeChecker (type) { + return function checkType (req) { + 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 + } +}