Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip9587 committed Nov 20, 2024
1 parent 74cb460 commit f61122e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 70 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand Down Expand Up @@ -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.

```
Expand Down
14 changes: 0 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,6 @@ Object.defineProperty(exports, 'urlencoded', {
get: createParserGetter('urlencoded')
})

/**
* Generic parser used to build parsers.
* @public
*/

Object.defineProperty(exports, 'generic', {
configurable: true,
enumerable: true,
get: createParserGetter('generic')
})

/**
* Create a middleware to parse json and urlencoded bodies.
*
Expand Down Expand Up @@ -134,9 +123,6 @@ function loadParser (parserName) {
case 'urlencoded':
parser = require('./lib/types/urlencoded')
break
case 'generic':
parser = require('./lib/generic-parser')
break
}

// store to prevent invoking require()
Expand Down
18 changes: 7 additions & 11 deletions lib/generic-parser.js → lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ 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')
: opts.limit
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

Expand All @@ -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()
Expand Down Expand Up @@ -114,10 +114,6 @@ function generic (parserOptions, parserOverrides) {
}
}

function defaultParse (buf) {
return buf
}

/**
* Get the charset of a request.
*
Expand Down
23 changes: 12 additions & 11 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @private
*/

var genericParser = require('../..').generic
var createBodyParser = require('../factory')
var debug = require('debug')('body-parser:json')

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

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Module dependencies.
*/

var genericParser = require('../..').generic
var createBodyParser = require('../factory')

/**
* Module exports.
Expand All @@ -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'
}
)
}
21 changes: 10 additions & 11 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Module dependencies.
*/

var genericParser = require('../..').generic
var createBodyParser = require('../factory')

/**
* Module exports.
Expand All @@ -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'
}
)
}
24 changes: 11 additions & 13 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand All @@ -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'
}
}
})
)
}

/**
Expand Down

0 comments on commit f61122e

Please sign in to comment.