2023-07-29 20:33:28 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var utils = require('./utils');
|
|
|
|
|
|
|
|
var has = Object.prototype.hasOwnProperty;
|
|
|
|
var isArray = Array.isArray;
|
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
allowDots: false,
|
2024-08-26 08:36:01 +02:00
|
|
|
allowEmptyArrays: false,
|
2023-07-29 20:33:28 +02:00
|
|
|
allowPrototypes: false,
|
|
|
|
allowSparse: false,
|
|
|
|
arrayLimit: 20,
|
|
|
|
charset: 'utf-8',
|
|
|
|
charsetSentinel: false,
|
|
|
|
comma: false,
|
2024-08-26 08:36:01 +02:00
|
|
|
decodeDotInKeys: false,
|
2023-07-29 20:33:28 +02:00
|
|
|
decoder: utils.decode,
|
|
|
|
delimiter: '&',
|
|
|
|
depth: 5,
|
2024-08-26 08:36:01 +02:00
|
|
|
duplicates: 'combine',
|
2023-07-29 20:33:28 +02:00
|
|
|
ignoreQueryPrefix: false,
|
|
|
|
interpretNumericEntities: false,
|
|
|
|
parameterLimit: 1000,
|
|
|
|
parseArrays: true,
|
|
|
|
plainObjects: false,
|
2024-08-26 08:36:01 +02:00
|
|
|
strictDepth: false,
|
2023-07-29 20:33:28 +02:00
|
|
|
strictNullHandling: false
|
|
|
|
};
|
|
|
|
|
|
|
|
var interpretNumericEntities = function (str) {
|
|
|
|
return str.replace(/&#(\d+);/g, function ($0, numberStr) {
|
|
|
|
return String.fromCharCode(parseInt(numberStr, 10));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var parseArrayValue = function (val, options) {
|
|
|
|
if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) {
|
|
|
|
return val.split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is what browsers will submit when the ✓ character occurs in an
|
|
|
|
// application/x-www-form-urlencoded body and the encoding of the page containing
|
|
|
|
// the form is iso-8859-1, or when the submitted form has an accept-charset
|
|
|
|
// attribute of iso-8859-1. Presumably also with other charsets that do not contain
|
|
|
|
// the ✓ character, such as us-ascii.
|
|
|
|
var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('✓')
|
|
|
|
|
|
|
|
// These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded.
|
|
|
|
var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
|
|
|
|
|
|
|
|
var parseValues = function parseQueryStringValues(str, options) {
|
2024-08-26 08:36:01 +02:00
|
|
|
var obj = { __proto__: null };
|
|
|
|
|
2023-07-29 20:33:28 +02:00
|
|
|
var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
|
2024-08-26 08:36:01 +02:00
|
|
|
cleanStr = cleanStr.replace(/%5B/gi, '[').replace(/%5D/gi, ']');
|
2023-07-29 20:33:28 +02:00
|
|
|
var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
|
|
|
|
var parts = cleanStr.split(options.delimiter, limit);
|
|
|
|
var skipIndex = -1; // Keep track of where the utf8 sentinel was found
|
|
|
|
var i;
|
|
|
|
|
|
|
|
var charset = options.charset;
|
|
|
|
if (options.charsetSentinel) {
|
|
|
|
for (i = 0; i < parts.length; ++i) {
|
|
|
|
if (parts[i].indexOf('utf8=') === 0) {
|
|
|
|
if (parts[i] === charsetSentinel) {
|
|
|
|
charset = 'utf-8';
|
|
|
|
} else if (parts[i] === isoSentinel) {
|
|
|
|
charset = 'iso-8859-1';
|
|
|
|
}
|
|
|
|
skipIndex = i;
|
|
|
|
i = parts.length; // The eslint settings do not allow break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < parts.length; ++i) {
|
|
|
|
if (i === skipIndex) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var part = parts[i];
|
|
|
|
|
|
|
|
var bracketEqualsPos = part.indexOf(']=');
|
|
|
|
var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1;
|
|
|
|
|
|
|
|
var key, val;
|
|
|
|
if (pos === -1) {
|
|
|
|
key = options.decoder(part, defaults.decoder, charset, 'key');
|
|
|
|
val = options.strictNullHandling ? null : '';
|
|
|
|
} else {
|
|
|
|
key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
|
|
|
|
val = utils.maybeMap(
|
|
|
|
parseArrayValue(part.slice(pos + 1), options),
|
|
|
|
function (encodedVal) {
|
|
|
|
return options.decoder(encodedVal, defaults.decoder, charset, 'value');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
|
|
|
|
val = interpretNumericEntities(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part.indexOf('[]=') > -1) {
|
|
|
|
val = isArray(val) ? [val] : val;
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:36:01 +02:00
|
|
|
var existing = has.call(obj, key);
|
|
|
|
if (existing && options.duplicates === 'combine') {
|
2023-07-29 20:33:28 +02:00
|
|
|
obj[key] = utils.combine(obj[key], val);
|
2024-08-26 08:36:01 +02:00
|
|
|
} else if (!existing || options.duplicates === 'last') {
|
2023-07-29 20:33:28 +02:00
|
|
|
obj[key] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
var parseObject = function (chain, val, options, valuesParsed) {
|
|
|
|
var leaf = valuesParsed ? val : parseArrayValue(val, options);
|
|
|
|
|
|
|
|
for (var i = chain.length - 1; i >= 0; --i) {
|
|
|
|
var obj;
|
|
|
|
var root = chain[i];
|
|
|
|
|
|
|
|
if (root === '[]' && options.parseArrays) {
|
2024-08-26 08:36:01 +02:00
|
|
|
obj = options.allowEmptyArrays && (leaf === '' || (options.strictNullHandling && leaf === null))
|
|
|
|
? []
|
|
|
|
: [].concat(leaf);
|
2023-07-29 20:33:28 +02:00
|
|
|
} else {
|
|
|
|
obj = options.plainObjects ? Object.create(null) : {};
|
|
|
|
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
2024-08-26 08:36:01 +02:00
|
|
|
var decodedRoot = options.decodeDotInKeys ? cleanRoot.replace(/%2E/g, '.') : cleanRoot;
|
|
|
|
var index = parseInt(decodedRoot, 10);
|
|
|
|
if (!options.parseArrays && decodedRoot === '') {
|
2023-07-29 20:33:28 +02:00
|
|
|
obj = { 0: leaf };
|
|
|
|
} else if (
|
|
|
|
!isNaN(index)
|
2024-08-26 08:36:01 +02:00
|
|
|
&& root !== decodedRoot
|
|
|
|
&& String(index) === decodedRoot
|
2023-07-29 20:33:28 +02:00
|
|
|
&& index >= 0
|
|
|
|
&& (options.parseArrays && index <= options.arrayLimit)
|
|
|
|
) {
|
|
|
|
obj = [];
|
|
|
|
obj[index] = leaf;
|
2024-08-26 08:36:01 +02:00
|
|
|
} else if (decodedRoot !== '__proto__') {
|
|
|
|
obj[decodedRoot] = leaf;
|
2023-07-29 20:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leaf = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return leaf;
|
|
|
|
};
|
|
|
|
|
|
|
|
var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
|
|
|
|
if (!givenKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform dot notation to bracket notation
|
|
|
|
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
|
|
|
|
|
|
|
|
// The regex chunks
|
|
|
|
|
|
|
|
var brackets = /(\[[^[\]]*])/;
|
|
|
|
var child = /(\[[^[\]]*])/g;
|
|
|
|
|
|
|
|
// Get the parent
|
|
|
|
|
|
|
|
var segment = options.depth > 0 && brackets.exec(key);
|
|
|
|
var parent = segment ? key.slice(0, segment.index) : key;
|
|
|
|
|
|
|
|
// Stash the parent if it exists
|
|
|
|
|
|
|
|
var keys = [];
|
|
|
|
if (parent) {
|
|
|
|
// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
|
|
|
|
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
|
|
|
if (!options.allowPrototypes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.push(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through children appending to the array until we hit depth
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
while (options.depth > 0 && (segment = child.exec(key)) !== null && i < options.depth) {
|
|
|
|
i += 1;
|
|
|
|
if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
|
|
|
|
if (!options.allowPrototypes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keys.push(segment[1]);
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:36:01 +02:00
|
|
|
// If there's a remainder, check strictDepth option for throw, else just add whatever is left
|
2023-07-29 20:33:28 +02:00
|
|
|
|
|
|
|
if (segment) {
|
2024-08-26 08:36:01 +02:00
|
|
|
if (options.strictDepth === true) {
|
|
|
|
throw new RangeError('Input depth exceeded depth option of ' + options.depth + ' and strictDepth is true');
|
|
|
|
}
|
2023-07-29 20:33:28 +02:00
|
|
|
keys.push('[' + key.slice(segment.index) + ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseObject(keys, val, options, valuesParsed);
|
|
|
|
};
|
|
|
|
|
|
|
|
var normalizeParseOptions = function normalizeParseOptions(opts) {
|
|
|
|
if (!opts) {
|
|
|
|
return defaults;
|
|
|
|
}
|
|
|
|
|
2024-08-26 08:36:01 +02:00
|
|
|
if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
|
|
|
|
throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof opts.decodeDotInKeys !== 'undefined' && typeof opts.decodeDotInKeys !== 'boolean') {
|
|
|
|
throw new TypeError('`decodeDotInKeys` option can only be `true` or `false`, when provided');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.decoder !== null && typeof opts.decoder !== 'undefined' && typeof opts.decoder !== 'function') {
|
2023-07-29 20:33:28 +02:00
|
|
|
throw new TypeError('Decoder has to be a function.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
|
|
|
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
|
|
|
|
}
|
|
|
|
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
|
|
|
|
|
2024-08-26 08:36:01 +02:00
|
|
|
var duplicates = typeof opts.duplicates === 'undefined' ? defaults.duplicates : opts.duplicates;
|
|
|
|
|
|
|
|
if (duplicates !== 'combine' && duplicates !== 'first' && duplicates !== 'last') {
|
|
|
|
throw new TypeError('The duplicates option must be either combine, first, or last');
|
|
|
|
}
|
|
|
|
|
|
|
|
var allowDots = typeof opts.allowDots === 'undefined' ? opts.decodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
|
|
|
|
|
2023-07-29 20:33:28 +02:00
|
|
|
return {
|
2024-08-26 08:36:01 +02:00
|
|
|
allowDots: allowDots,
|
|
|
|
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
|
2023-07-29 20:33:28 +02:00
|
|
|
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
|
|
|
|
allowSparse: typeof opts.allowSparse === 'boolean' ? opts.allowSparse : defaults.allowSparse,
|
|
|
|
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit,
|
|
|
|
charset: charset,
|
|
|
|
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
|
|
|
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
|
2024-08-26 08:36:01 +02:00
|
|
|
decodeDotInKeys: typeof opts.decodeDotInKeys === 'boolean' ? opts.decodeDotInKeys : defaults.decodeDotInKeys,
|
2023-07-29 20:33:28 +02:00
|
|
|
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
|
|
|
|
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
|
|
|
|
// eslint-disable-next-line no-implicit-coercion, no-extra-parens
|
|
|
|
depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth,
|
2024-08-26 08:36:01 +02:00
|
|
|
duplicates: duplicates,
|
2023-07-29 20:33:28 +02:00
|
|
|
ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
|
|
|
|
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
|
|
|
|
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
|
|
|
|
parseArrays: opts.parseArrays !== false,
|
|
|
|
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
|
2024-08-26 08:36:01 +02:00
|
|
|
strictDepth: typeof opts.strictDepth === 'boolean' ? !!opts.strictDepth : defaults.strictDepth,
|
2023-07-29 20:33:28 +02:00
|
|
|
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function (str, opts) {
|
|
|
|
var options = normalizeParseOptions(opts);
|
|
|
|
|
|
|
|
if (str === '' || str === null || typeof str === 'undefined') {
|
|
|
|
return options.plainObjects ? Object.create(null) : {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
|
|
|
|
var obj = options.plainObjects ? Object.create(null) : {};
|
|
|
|
|
|
|
|
// Iterate over the keys and setup the new object
|
|
|
|
|
|
|
|
var keys = Object.keys(tempObj);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
var key = keys[i];
|
|
|
|
var newObj = parseKeys(key, tempObj[key], options, typeof str === 'string');
|
|
|
|
obj = utils.merge(obj, newObj, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.allowSparse === true) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.compact(obj);
|
|
|
|
};
|