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..5b4a5ff3 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -13,12 +13,12 @@ */ 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, typeChecker } = require('../utils') /** * Module exports. @@ -196,21 +196,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 +220,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..f3931116 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -15,6 +15,7 @@ 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') /** * Module exports. @@ -89,16 +90,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..8cbd3338 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -11,11 +11,11 @@ */ 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, typeChecker } = require('../utils') /** * Module exports. @@ -94,31 +94,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..a974f34c 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -13,13 +13,13 @@ */ 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, typeChecker } = require('../utils') /** * Module exports. @@ -184,21 +184,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 +207,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..3e97f78f --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,51 @@ +/*! + * body-parser + * Copyright(c) 2014-2015 Douglas Christopher Wilson + * MIT Licensed + */ + +'use strict' + +/** + * Module dependencies. + */ + +var contentType = require('content-type') +var typeis = require('type-is') + +/** + * Module exports. + */ + +module.exports = { + getCharset, + typeChecker +} + +/** + * 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)) + } +}