Skip to content

Commit

Permalink
replace querystring with URLSearchParams
Browse files Browse the repository at this point in the history
The "multiple keys" scenario isn't defined by the URL spec so the JSONP test is failing.
Read more: https://www.rfc-editor.org/rfc/rfc3986#section-3.4
  • Loading branch information
TheDevMinerTV committed Jun 26, 2024
1 parent ee40a88 commit df011ff
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 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,39 @@ 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) => Object.fromEntries(new URLSearchParams(raw).entries());
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 +237,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 df011ff

Please sign in to comment.