* Get the extended query parser. * * @param {Object} options * @returns {Function} * @private
(options)
| 60 | * @private |
| 61 | */ |
| 62 | function createQueryParser (options) { |
| 63 | const extended = Boolean(options?.extended) |
| 64 | let parameterLimit = options?.parameterLimit !== undefined |
| 65 | ? options?.parameterLimit |
| 66 | : 1000 |
| 67 | const charsetSentinel = options?.charsetSentinel |
| 68 | const interpretNumericEntities = options?.interpretNumericEntities |
| 69 | const depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0 |
| 70 | |
| 71 | if (isNaN(parameterLimit) || parameterLimit < 1) { |
| 72 | throw new TypeError('option parameterLimit must be a positive number') |
| 73 | } |
| 74 | |
| 75 | if (isNaN(depth) || depth < 0) { |
| 76 | throw new TypeError('option depth must be a zero or a positive number') |
| 77 | } |
| 78 | |
| 79 | if (isFinite(parameterLimit)) { |
| 80 | parameterLimit = parameterLimit | 0 |
| 81 | } |
| 82 | |
| 83 | return function parse (body, encoding) { |
| 84 | if (!body.length) return {} |
| 85 | |
| 86 | const paramCount = parameterCount(body, parameterLimit) |
| 87 | |
| 88 | if (paramCount === undefined) { |
| 89 | debug('too many parameters') |
| 90 | throw createError(413, 'too many parameters', { |
| 91 | type: 'parameters.too.many' |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | const arrayLimit = extended ? Math.max(100, paramCount) : paramCount |
| 96 | |
| 97 | debug('parse ' + (extended ? 'extended ' : '') + 'urlencoding') |
| 98 | try { |
| 99 | return qs.parse(body, { |
| 100 | allowPrototypes: true, |
| 101 | arrayLimit: arrayLimit, |
| 102 | depth: depth, |
| 103 | charsetSentinel: charsetSentinel, |
| 104 | interpretNumericEntities: interpretNumericEntities, |
| 105 | charset: encoding, |
| 106 | parameterLimit: parameterLimit, |
| 107 | strictDepth: true |
| 108 | }) |
| 109 | } catch (err) { |
| 110 | if (err instanceof RangeError) { |
| 111 | throw createError(400, 'The input exceeded the depth', { |
| 112 | type: 'querystring.parse.rangeError' |
| 113 | }) |
| 114 | } else { |
| 115 | throw err |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
no test coverage detected
searching dependent graphs…