Use URLSearchParams instead of transitive dependency querystring

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2020-04-12 01:45:58 +01:00
parent 6e15684a04
commit 8fdb41412f
5 changed files with 106 additions and 29 deletions

View file

@ -17,7 +17,7 @@ limitations under the License.
// We have to trick webpack into loading our CSS for us.
require("./index.scss");
import * as qs from 'querystring';
import { parseQs, parseQsFromFragment } from "../url_utils";
import { Capability, WidgetApi } from "matrix-react-sdk/src/widgets/WidgetApi";
// Dev note: we use raw JS without many dependencies to reduce bundle size.
@ -40,8 +40,8 @@ let widgetApi: WidgetApi;
try {
// The widget's options are encoded into the fragment to avoid leaking info to the server. The widget
// spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
const widgetQuery = qs.parse(window.location.hash.substring(1));
const query = Object.assign({}, qs.parse(window.location.search.substring(1)), widgetQuery);
const widgetQuery = parseQsFromFragment(window.location);
const query = Object.assign({}, parseQs(window.location), widgetQuery.params);
const qsParam = (name: string, optional = false): string => {
if (!optional && (!query[name] || typeof (query[name]) !== 'string')) {
throw new Error(`Expected singular ${name} in query string`);

View file

@ -14,7 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import * as qs from 'querystring';
function searchParamsToObject(params: URLSearchParams) {
return Object.fromEntries([...params.entries()]);
}
// We want to support some name / value pairs in the fragment
// so we're re-using query string like format
@ -32,15 +34,15 @@ export function parseQsFromFragment(location: Location) {
const result = {
location: decodeURIComponent(hashparts[0]),
params: <qs.ParsedUrlQuery>{},
params: {},
};
if (hashparts.length > 1) {
result.params = qs.parse(hashparts[1]);
result.params = searchParamsToObject(new URLSearchParams(hashparts[1]));
}
return result;
}
export function parseQs(location: Location) {
return qs.parse(location.search.substring(1));
return searchParamsToObject(new URLSearchParams(location.search));
}