Skip to content

Commit

Permalink
feat: migrate to picoquery
Browse files Browse the repository at this point in the history
Migrates to using picoquery for query string parsing, a much faster and
lighter library.

Note that we have to decode the stringified query in some cases since we
were previously double encoding.
  • Loading branch information
43081j committed Aug 9, 2024
1 parent f54afc3 commit 2eb5016
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
43 changes: 28 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"node-fetch-commonjs": "^3.3.2",
"openapi-path-templating": "^1.5.1",
"openapi-server-url-templating": "^1.0.0",
"qs": "^6.10.2",
"picoquery": "^1.4.0",
"ramda-adjunct": "^5.0.0",
"neotraverse": "=0.6.15"
},
Expand Down
17 changes: 13 additions & 4 deletions src/http/serializers/request/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import qs from 'qs';
import * as pq from 'picoquery';

import formatKeyValue from './format.js';
import { isFile, isArrayOfFile, FileWithData } from './file.js';

const PQ_OPTIONS = {
arrayRepeat: true,
arrayRepeatSyntax: 'repeat',
};

function stringifyQuery(obj) {
return decodeURIComponent(pq.stringify(obj, PQ_OPTIONS));
}

function buildFormData(reqForm) {
/**
* Build a new FormData instance, support array as field value
Expand Down Expand Up @@ -60,7 +69,7 @@ export function encodeFormOrQuery(data) {
return result;
}, {});

return qs.stringify(encodedQuery, { encode: false, indices: false }) || '';
return stringifyQuery(encodedQuery);
}

// If the request has a `query` object, merge it into the request.url, and delete the object
Expand Down Expand Up @@ -96,10 +105,10 @@ export function serializeRequest(req = {}) {
let newStr = '';

if (oriSearch) {
const oriQuery = qs.parse(oriSearch);
const oriQuery = pq.parse(oriSearch, PQ_OPTIONS);
const keysToRemove = Object.keys(query);
keysToRemove.forEach((key) => delete oriQuery[key]);
newStr = qs.stringify(oriQuery, { encode: true });
newStr = pq.stringify(oriQuery, PQ_OPTIONS);
}

const finalStr = joinSearch(newStr, encodeFormOrQuery(query));
Expand Down

0 comments on commit 2eb5016

Please sign in to comment.