Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace node:querystring with URLSearchParams #5731

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
}