Skip to content

Commit

Permalink
replace querystring with URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Jul 4, 2024
1 parent ee40a88 commit 38ab38d
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var etag = require('etag');
var mime = require('mime-types')
var proxyaddr = require('proxy-addr');
var qs = require('qs');
var querystring = require('querystring');

/**
* Return strong ETag for `body`.
Expand Down Expand Up @@ -131,36 +130,46 @@ exports.compileETag = function(val) {
return fn;
}

/**
* @typedef {(raw: string) => Record<string, string>} QueryParser
*/

/**
* Compile "query parser" value to function.
*
* @param {String|Function} val
* @return {Function}
* @param {boolean | 'simple' | 'extended' | QueryParser} modeOrFactory \
* `true` | `simple`: use {@link https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams|URLSearchParams} \
* `extended`: use {@link https://www.npmjs.com/package/qs|qs} \
* `false`: disable query parsing \
* a function: provide a custom query parser
*
* @return {QueryParser | undefined}
* @api private
*/

exports.compileQueryParser = function compileQueryParser(val) {
var fn;

if (typeof val === 'function') {
return val;
exports.compileQueryParser = function compileQueryParser(modeOrFactory) {
if (typeof modeOrFactory === 'function') {
return modeOrFactory;
}

switch (val) {
switch (modeOrFactory) {
case true:
case 'simple':
fn = querystring.parse;
break;
case false:
break;
return (raw) => {
const q = new URLSearchParams(raw)
const o = {};

for (const k of q.keys()) o[k] = q.get(k);

return o;
};
case 'extended':
fn = parseExtendedQueryString;
break;
return (raw) => qs.parse(raw, { allowPrototypes: true });
case false:
// Indicate the query parser is disabled
return undefined;
default:
throw new TypeError('unknown value for query parser function: ' + val);
throw new TypeError('unknown value for query parser function: ' + modeOrFactory);
}

return fn;
}

/**
Expand Down Expand Up @@ -235,17 +244,3 @@ function createETagGenerator (options) {
return etag(buf, options)
}
}

/**
* Parse an extended query string with qs.
*
* @param {String} str
* @return {Object}
* @private
*/

function parseExtendedQueryString(str) {
return qs.parse(str, {
allowPrototypes: true
});
}

0 comments on commit 38ab38d

Please sign in to comment.