Skip to content

Commit

Permalink
refactor: normalize common options for all parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip9587 committed Oct 22, 2024
1 parent 623e6d6 commit a03daf7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 70 deletions.
19 changes: 2 additions & 17 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 2 additions & 18 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand All @@ -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)
Expand Down
35 changes: 34 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Module dependencies.
*/

var bytes = require('bytes')
var contentType = require('content-type')
var typeis = require('type-is')

Expand All @@ -19,7 +20,7 @@ var typeis = require('type-is')

module.exports = {
getCharset,
typeChecker
normalizeOptions
}

/**
Expand Down Expand Up @@ -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
}
}

0 comments on commit a03daf7

Please sign in to comment.