Merge pull request #13138 from vector-im/t3chguy/querystring
This commit is contained in:
commit
ae2e3e8502
4 changed files with 63 additions and 13 deletions
|
@ -153,7 +153,7 @@
|
||||||
"jest": {
|
"jest": {
|
||||||
"testEnvironment": "jest-environment-jsdom-sixteen",
|
"testEnvironment": "jest-environment-jsdom-sixteen",
|
||||||
"testMatch": [
|
"testMatch": [
|
||||||
"<rootDir>/test/**/*-test.js"
|
"<rootDir>/test/**/*-test.[tj]s"
|
||||||
],
|
],
|
||||||
"setupFilesAfterEnv": [
|
"setupFilesAfterEnv": [
|
||||||
"<rootDir>/node_modules/matrix-react-sdk/test/setupTests.js"
|
"<rootDir>/node_modules/matrix-react-sdk/test/setupTests.js"
|
||||||
|
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||||
// We have to trick webpack into loading our CSS for us.
|
// We have to trick webpack into loading our CSS for us.
|
||||||
require("./index.scss");
|
require("./index.scss");
|
||||||
|
|
||||||
import * as qs from 'querystring';
|
|
||||||
import { KJUR } from 'jsrsasign';
|
import { KJUR } from 'jsrsasign';
|
||||||
import {
|
import {
|
||||||
IOpenIDCredentials,
|
IOpenIDCredentials,
|
||||||
|
@ -52,15 +51,16 @@ let meetApi: any; // JitsiMeetExternalAPI
|
||||||
|
|
||||||
(async function() {
|
(async function() {
|
||||||
try {
|
try {
|
||||||
// The widget's options are encoded into the fragment to avoid leaking info to the server. The widget
|
// The widget's options are encoded into the fragment to avoid leaking info to the server.
|
||||||
// spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
|
const widgetQuery = new URLSearchParams(window.location.hash.substring(1));
|
||||||
const widgetQuery = qs.parse(window.location.hash.substring(1));
|
// The widget spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
|
||||||
const query = Object.assign({}, qs.parse(window.location.search.substring(1)), widgetQuery);
|
const realQuery = new URLSearchParams(window.location.search.substring(1));
|
||||||
const qsParam = (name: string, optional = false): string => {
|
const qsParam = (name: string, optional = false): string => {
|
||||||
if (!optional && (!query[name] || typeof (query[name]) !== 'string')) {
|
const vals = widgetQuery.has(name) ? widgetQuery.getAll(name) : realQuery.getAll(name);
|
||||||
|
if (!optional && vals.length !== 1) {
|
||||||
throw new Error(`Expected singular ${name} in query string`);
|
throw new Error(`Expected singular ${name} in query string`);
|
||||||
}
|
}
|
||||||
return <string>query[name];
|
return <string>vals[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
// If we have these params, expect a widget API to be available (ie. to be in an iframe
|
// If we have these params, expect a widget API to be available (ie. to be in an iframe
|
||||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as qs from 'querystring';
|
import { QueryDict, decodeParams } from "matrix-js-sdk/src/utils";
|
||||||
|
|
||||||
// We want to support some name / value pairs in the fragment
|
// We want to support some name / value pairs in the fragment
|
||||||
// so we're re-using query string like format
|
// so we're re-using query string like format
|
||||||
|
@ -32,15 +32,15 @@ export function parseQsFromFragment(location: Location) {
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
location: decodeURIComponent(hashparts[0]),
|
location: decodeURIComponent(hashparts[0]),
|
||||||
params: <qs.ParsedUrlQuery>{},
|
params: <QueryDict>{},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (hashparts.length > 1) {
|
if (hashparts.length > 1) {
|
||||||
result.params = qs.parse(hashparts[1]);
|
result.params = decodeParams(hashparts[1]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseQs(location: Location) {
|
export function parseQs(location: Location): QueryDict {
|
||||||
return qs.parse(location.search.substring(1));
|
return decodeParams(location.search.substring(1));
|
||||||
}
|
}
|
||||||
|
|
50
test/unit-tests/url_utils-test.ts
Normal file
50
test/unit-tests/url_utils-test.ts
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 New Vector Ltd
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { parseQsFromFragment, parseQs } from "../../src/vector/url_utils";
|
||||||
|
|
||||||
|
describe("url_utils.ts", function() {
|
||||||
|
// @ts-ignore
|
||||||
|
const location: Location = {
|
||||||
|
hash: "",
|
||||||
|
search: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
it("parseQsFromFragment", function() {
|
||||||
|
location.hash = "/home?foo=bar";
|
||||||
|
expect(parseQsFromFragment(location)).toEqual({
|
||||||
|
location: "home",
|
||||||
|
params: {
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("parseQs", function() {
|
||||||
|
location.search = "?foo=bar";
|
||||||
|
expect(parseQs(location)).toEqual({
|
||||||
|
"foo": "bar",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("parseQs with arrays", function() {
|
||||||
|
location.search = "?via=s1&via=s2&via=s2&foo=bar";
|
||||||
|
expect(parseQs(location)).toEqual({
|
||||||
|
"via": ["s1", "s2", "s2"],
|
||||||
|
"foo": "bar",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue