| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var utils = require('./utils'); |
| 4 | |
| 5 | var has = Object.prototype.hasOwnProperty; |
| 6 | var isArray = Array.isArray; |
| 7 | |
| 8 | var defaults = { |
| 9 | allowDots: false, |
| 10 | allowPrototypes: false, |
| 11 | arrayLimit: 20, |
| 12 | charset: 'utf-8', |
| 13 | charsetSentinel: false, |
| 14 | comma: false, |
| 15 | decoder: utils.decode, |
| 16 | delimiter: '&', |
| 17 | depth: 5, |
| 18 | ignoreQueryPrefix: false, |
| 19 | interpretNumericEntities: false, |
| 20 | parameterLimit: 1000, |
| 21 | parseArrays: true, |
| 22 | plainObjects: false, |
| 23 | strictNullHandling: false |
| 24 | }; |
| 25 | |
| 26 | var interpretNumericEntities = function (str) { |
| 27 | return str.replace(/&#(\d+);/g, function ($0, numberStr) { |
| 28 | return String.fromCharCode(parseInt(numberStr, 10)); |
| 29 | }); |
| 30 | }; |
| 31 | |
| 32 | var parseArrayValue = function (val, options) { |
| 33 | if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) { |
| 34 | return val.split(','); |
| 35 | } |
| 36 | |
| 37 | return val; |
| 38 | }; |
| 39 | |
| 40 | // This is what browsers will submit when the ✓ character occurs in an |
| 41 | // application/x-www-form-urlencoded body and the encoding of the page containing |
| 42 | // the form is iso-8859-1, or when the submitted form has an accept-charset |
| 43 | // attribute of iso-8859-1. Presumably also with other charsets that do not contain |
| 44 | // the ✓ character, such as us-ascii. |
| 45 | var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('✓') |
| 46 | |
| 47 | // These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded. |
| 48 | var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓') |
| 49 | |
| 50 | var parseValues = function parseQueryStringValues(str, options) { |
| 51 | var obj = {}; |
| 52 | var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str; |
| 53 | var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit; |
| 54 | var parts = cleanStr.split(options.delimiter, limit); |
| 55 | var skipIndex = -1; // Keep track of where the utf8 sentinel was found |
| 56 | var i; |
| 57 | |
| 58 | var charset = options.charset; |
| 59 | if (options.charsetSentinel) { |
| 60 | for (i = 0; i < parts.length; ++i) { |
| 61 | if (parts[i].indexOf('utf8=') === 0) { |
| 62 | if (parts[i] === charsetSentinel) { |
| 63 | charset = 'utf-8'; |
| 64 | } else if (parts[i] === isoSentinel) { |
| 65 | charset = 'iso-8859-1'; |
| 66 | } |
| 67 | skipIndex = i; |
| 68 | i = parts.length; // The eslint settings do not allow break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | for (i = 0; i < parts.length; ++i) { |
| 74 | if (i === skipIndex) { |
| 75 | continue; |
| 76 | } |
| 77 | var part = parts[i]; |
| 78 | |
| 79 | var bracketEqualsPos = part.indexOf(']='); |
| 80 | var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1; |
| 81 | |
| 82 | var key, val; |
| 83 | if (pos === -1) { |
| 84 | key = options.decoder(part, defaults.decoder, charset, 'key'); |
| 85 | val = options.strictNullHandling ? null : ''; |
| 86 | } else { |
| 87 | key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key'); |
| 88 | val = utils.maybeMap( |
| 89 | parseArrayValue(part.slice(pos + 1), options), |
| 90 | function (encodedVal) { |
| 91 | return options.decoder(encodedVal, defaults.decoder, charset, 'value'); |
| 92 | } |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | if (val && options.interpretNumericEntities && charset === 'iso-8859-1') { |
| 97 | val = interpretNumericEntities(val); |
| 98 | } |
| 99 | |
| 100 | if (part.indexOf('[]=') > -1) { |
| 101 | val = isArray(val) ? [val] : val; |
| 102 | } |
| 103 | |
| 104 | if (has.call(obj, key)) { |
| 105 | obj[key] = utils.combine(obj[key], val); |
| 106 | } else { |
| 107 | obj[key] = val; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return obj; |
| 112 | }; |
| 113 | |
| 114 | var parseObject = function (chain, val, options, valuesParsed) { |
| 115 | var leaf = valuesParsed ? val : parseArrayValue(val, options); |
| 116 | |
| 117 | for (var i = chain.length - 1; i >= 0; --i) { |
| 118 | var obj; |
| 119 | var root = chain[i]; |
| 120 | |
| 121 | if (root === '[]' && options.parseArrays) { |
| 122 | obj = [].concat(leaf); |
| 123 | } else { |
| 124 | obj = options.plainObjects ? Object.create(null) : {}; |
| 125 | var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root; |
| 126 | var index = parseInt(cleanRoot, 10); |
| 127 | if (!options.parseArrays && cleanRoot === '') { |
| 128 | obj = { 0: leaf }; |
| 129 | } else if ( |
| 130 | !isNaN(index) |
| 131 | && root !== cleanRoot |
| 132 | && String(index) === cleanRoot |
| 133 | && index >= 0 |
| 134 | && (options.parseArrays && index <= options.arrayLimit) |
| 135 | ) { |
| 136 | obj = []; |
| 137 | obj[index] = leaf; |
| 138 | } else { |
| 139 | obj[cleanRoot] = leaf; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | leaf = obj; // eslint-disable-line no-param-reassign |
| 144 | } |
| 145 | |
| 146 | return leaf; |
| 147 | }; |
| 148 | |
| 149 | var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) { |
| 150 | if (!givenKey) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Transform dot notation to bracket notation |
| 155 | var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey; |
| 156 | |
| 157 | // The regex chunks |
| 158 | |
| 159 | var brackets = /(\[[^[\]]*])/; |
| 160 | var child = /(\[[^[\]]*])/g; |
| 161 | |
| 162 | // Get the parent |
| 163 | |
| 164 | var segment = options.depth > 0 && brackets.exec(key); |
| 165 | var parent = segment ? key.slice(0, segment.index) : key; |
| 166 | |
| 167 | // Stash the parent if it exists |
| 168 | |
| 169 | var keys = []; |
| 170 | if (parent) { |
| 171 | // If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties |
| 172 | if (!options.plainObjects && has.call(Object.prototype, parent)) { |
| 173 | if (!options.allowPrototypes) { |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | keys.push(parent); |
| 179 | } |
| 180 | |
| 181 | // Loop through children appending to the array until we hit depth |
| 182 | |
| 183 | var i = 0; |
| 184 | while (options.depth > 0 && (segment = child.exec(key)) !== null && i < options.depth) { |
| 185 | i += 1; |
| 186 | if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) { |
| 187 | if (!options.allowPrototypes) { |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | keys.push(segment[1]); |
| 192 | } |
| 193 | |
| 194 | // If there's a remainder, just add whatever is left |
| 195 | |
| 196 | if (segment) { |
| 197 | keys.push('[' + key.slice(segment.index) + ']'); |
| 198 | } |
| 199 | |
| 200 | return parseObject(keys, val, options, valuesParsed); |
| 201 | }; |
| 202 | |
| 203 | var normalizeParseOptions = function normalizeParseOptions(opts) { |
| 204 | if (!opts) { |
| 205 | return defaults; |
| 206 | } |
| 207 | |
| 208 | if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') { |
| 209 | throw new TypeError('Decoder has to be a function.'); |
| 210 | } |
| 211 | |
| 212 | if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { |
| 213 | throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'); |
| 214 | } |
| 215 | var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset; |
| 216 | |
| 217 | return { |
| 218 | allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, |
| 219 | allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes, |
| 220 | arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit, |
| 221 | charset: charset, |
| 222 | charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, |
| 223 | comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma, |
| 224 | decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder, |
| 225 | delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter, |
| 226 | // eslint-disable-next-line no-implicit-coercion, no-extra-parens |
| 227 | depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth, |
| 228 | ignoreQueryPrefix: opts.ignoreQueryPrefix === true, |
| 229 | interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities, |
| 230 | parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit, |
| 231 | parseArrays: opts.parseArrays !== false, |
| 232 | plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects, |
| 233 | strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling |
| 234 | }; |
| 235 | }; |
| 236 | |
| 237 | module.exports = function (str, opts) { |
| 238 | var options = normalizeParseOptions(opts); |
| 239 | |
| 240 | if (str === '' || str === null || typeof str === 'undefined') { |
| 241 | return options.plainObjects ? Object.create(null) : {}; |
| 242 | } |
| 243 | |
| 244 | var tempObj = typeof str === 'string' ? parseValues(str, options) : str; |
| 245 | var obj = options.plainObjects ? Object.create(null) : {}; |
| 246 | |
| 247 | // Iterate over the keys and setup the new object |
| 248 | |
| 249 | var keys = Object.keys(tempObj); |
| 250 | for (var i = 0; i < keys.length; ++i) { |
| 251 | var key = keys[i]; |
| 252 | var newObj = parseKeys(key, tempObj[key], options, typeof str === 'string'); |
| 253 | obj = utils.merge(obj, newObj, options); |
| 254 | } |
| 255 | |
| 256 | return utils.compact(obj); |
| 257 | }; |