Skip to content

Commit 7eb00cd

Browse files
authored
Also use the qs module for the simple parser (#387)
* Always use the qs module, even in simple mode #326 (comment) * Create the simple and extended parser with the same function, reducing duplication * Don't mention the querystring module in the README * Fix lint
1 parent ddf9b75 commit 7eb00cd

File tree

2 files changed

+14
-87
lines changed

2 files changed

+14
-87
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,10 @@ any of the following keys:
228228

229229
##### extended
230230

231-
The `extended` option allows to choose between parsing the URL-encoded data
232-
with the `querystring` library (when `false`) or the `qs` library (when
233-
`true`). The "extended" syntax allows for rich objects and arrays to be
234-
encoded into the URL-encoded format, allowing for a JSON-like experience
235-
with URL-encoded. For more information, please
236-
[see the qs library](https://www.npmjs.org/package/qs#readme).
231+
The "extended" syntax allows for rich objects and arrays to be encoded into the
232+
URL-encoded format, allowing for a JSON-like experience with URL-encoded. For
233+
more information, please [see the qs
234+
library](https://www.npmjs.org/package/qs#readme).
237235

238236
Defaults to `false`.
239237

lib/types/urlencoded.js

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ var debug = require('debug')('body-parser:urlencoded')
1919
var isFinished = require('on-finished').isFinished
2020
var read = require('../read')
2121
var typeis = require('type-is')
22+
var qs = require('qs')
2223

2324
/**
2425
* Module exports.
2526
*/
2627

2728
module.exports = urlencoded
2829

29-
/**
30-
* Cache of parser modules.
31-
*/
32-
33-
var parsers = Object.create(null)
34-
3530
/**
3631
* Create a middleware to parse urlencoded bodies.
3732
*
@@ -56,9 +51,7 @@ function urlencoded (options) {
5651
}
5752

5853
// create the appropriate query parser
59-
var queryparse = extended
60-
? extendedparser(opts)
61-
: simpleparser(opts)
54+
var queryparse = createQueryParser(opts, extended)
6255

6356
// create the appropriate type checking function
6457
var shouldParse = typeof type !== 'function'
@@ -126,11 +119,10 @@ function urlencoded (options) {
126119
* @param {object} options
127120
*/
128121

129-
function extendedparser (options) {
122+
function createQueryParser (options, extended) {
130123
var parameterLimit = options.parameterLimit !== undefined
131124
? options.parameterLimit
132125
: 1000
133-
var parse = parser('qs')
134126

135127
if (isNaN(parameterLimit) || parameterLimit < 1) {
136128
throw new TypeError('option parameterLimit must be a positive number')
@@ -140,6 +132,8 @@ function extendedparser (options) {
140132
parameterLimit = parameterLimit | 0
141133
}
142134

135+
var depth = extended ? Infinity : 0
136+
143137
return function queryparse (body) {
144138
var paramCount = parameterCount(body, parameterLimit)
145139

@@ -150,13 +144,14 @@ function extendedparser (options) {
150144
})
151145
}
152146

153-
var arrayLimit = Math.max(100, paramCount)
147+
var arrayLimit = extended ? Math.max(100, paramCount) : 0
148+
149+
debug('parse ' + (extended ? 'extended ' : '') + 'urlencoding')
154150

155-
debug('parse extended urlencoding')
156-
return parse(body, {
151+
return qs.parse(body, {
157152
allowPrototypes: true,
158153
arrayLimit: arrayLimit,
159-
depth: Infinity,
154+
depth: depth,
160155
parameterLimit: parameterLimit
161156
})
162157
}
@@ -201,72 +196,6 @@ function parameterCount (body, limit) {
201196
return count
202197
}
203198

204-
/**
205-
* Get parser for module name dynamically.
206-
*
207-
* @param {string} name
208-
* @return {function}
209-
* @api private
210-
*/
211-
212-
function parser (name) {
213-
var mod = parsers[name]
214-
215-
if (mod !== undefined) {
216-
return mod.parse
217-
}
218-
219-
// this uses a switch for static require analysis
220-
switch (name) {
221-
case 'qs':
222-
mod = require('qs')
223-
break
224-
case 'querystring':
225-
mod = require('querystring')
226-
break
227-
}
228-
229-
// store to prevent invoking require()
230-
parsers[name] = mod
231-
232-
return mod.parse
233-
}
234-
235-
/**
236-
* Get the simple query parser.
237-
*
238-
* @param {object} options
239-
*/
240-
241-
function simpleparser (options) {
242-
var parameterLimit = options.parameterLimit !== undefined
243-
? options.parameterLimit
244-
: 1000
245-
var parse = parser('querystring')
246-
247-
if (isNaN(parameterLimit) || parameterLimit < 1) {
248-
throw new TypeError('option parameterLimit must be a positive number')
249-
}
250-
251-
if (isFinite(parameterLimit)) {
252-
parameterLimit = parameterLimit | 0
253-
}
254-
255-
return function queryparse (body) {
256-
var paramCount = parameterCount(body, parameterLimit)
257-
258-
if (paramCount === undefined) {
259-
debug('too many parameters')
260-
throw createError(413, 'too many parameters', {
261-
type: 'parameters.too.many'
262-
})
263-
}
264-
265-
debug('parse urlencoding')
266-
return parse(body, undefined, undefined, { maxKeys: parameterLimit })
267-
}
268-
}
269-
270199
/**
271200
* Get the simple type checker.
272201
*

0 commit comments

Comments
 (0)