Skip to content

Commit

Permalink
WIP simple parse with qs
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Aug 16, 2019
1 parent b15bae5 commit 2c0f316
Showing 1 changed file with 8 additions and 42 deletions.
50 changes: 8 additions & 42 deletions lib/types/urlencoded.js
Expand Up @@ -17,6 +17,7 @@ var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:urlencoded')
var deprecate = require('depd')('body-parser')
var qs = require('qs')
var read = require('../read')
var typeis = require('type-is')

Expand All @@ -26,12 +27,6 @@ var typeis = require('type-is')

module.exports = urlencoded

/**
* Cache of parser modules.
*/

var parsers = Object.create(null)

/**
* Create a middleware to parse urlencoded bodies.
*
Expand Down Expand Up @@ -133,7 +128,6 @@ function extendedparser (options) {
var parameterLimit = options.parameterLimit !== undefined
? options.parameterLimit
: 1000
var parse = parser('qs')

if (isNaN(parameterLimit) || parameterLimit < 1) {
throw new TypeError('option parameterLimit must be a positive number')
Expand All @@ -156,7 +150,7 @@ function extendedparser (options) {
var arrayLimit = Math.max(100, paramCount)

debug('parse extended urlencoding')
return parse(body, {
return qs.parse(body, {
allowPrototypes: true,
arrayLimit: arrayLimit,
depth: Infinity,
Expand Down Expand Up @@ -204,37 +198,6 @@ function parameterCount (body, limit) {
return count
}

/**
* Get parser for module name dynamically.
*
* @param {string} name
* @return {function}
* @api private
*/

function parser (name) {
var mod = parsers[name]

if (mod !== undefined) {
return mod.parse
}

// this uses a switch for static require analysis
switch (name) {
case 'qs':
mod = require('qs')
break
case 'querystring':
mod = require('querystring')
break
}

// store to prevent invoking require()
parsers[name] = mod

return mod.parse
}

/**
* Get the simple query parser.
*
Expand All @@ -245,7 +208,6 @@ function simpleparser (options) {
var parameterLimit = options.parameterLimit !== undefined
? options.parameterLimit
: 1000
var parse = parser('querystring')

if (isNaN(parameterLimit) || parameterLimit < 1) {
throw new TypeError('option parameterLimit must be a positive number')
Expand All @@ -265,8 +227,12 @@ function simpleparser (options) {
})
}

debug('parse urlencoding')
return parse(body, undefined, undefined, { maxKeys: parameterLimit })
debug('parse simple urlencoding')
return qs.parse(body, {
allowPrototypes: true,
depth: -1,
parameterLimit: parameterLimit
})
}
}

Expand Down

0 comments on commit 2c0f316

Please sign in to comment.