2020-05-07 22:26:37 +00:00
|
|
|
import { parseURLParams } from './parseURLParams';
|
2022-07-29 13:18:14 +00:00
|
|
|
// eslint-disable-next-line lines-around-comment
|
|
|
|
// @ts-ignore
|
2019-11-06 13:42:49 +00:00
|
|
|
import { normalizeNFKC } from './strings';
|
|
|
|
|
2018-02-14 20:34:33 +00:00
|
|
|
/**
|
|
|
|
* The app linking scheme.
|
|
|
|
* TODO: This should be read from the manifest files later.
|
|
|
|
*/
|
|
|
|
export const APP_LINK_SCHEME = 'org.jitsi.meet:';
|
|
|
|
|
2018-04-10 10:42:48 +00:00
|
|
|
/**
|
|
|
|
* A list of characters to be excluded/removed from the room component/segment
|
|
|
|
* of a conference/meeting URI/URL. The list is based on RFC 3986 and the jxmpp
|
|
|
|
* library utilized by jicofo.
|
|
|
|
*/
|
|
|
|
const _ROOM_EXCLUDE_PATTERN = '[\\:\\?#\\[\\]@!$&\'()*+,;=></"]';
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
/**
|
|
|
|
* The {@link RegExp} pattern of the authority of a URI.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const _URI_AUTHORITY_PATTERN = '(//[^/?#]+)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link RegExp} pattern of the path of a URI.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const _URI_PATH_PATTERN = '([^?#]*)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link RegExp} pattern of the protocol of a URI.
|
|
|
|
*
|
2018-03-14 22:17:49 +00:00
|
|
|
* FIXME: The URL class exposed by JavaScript will not include the colon in
|
|
|
|
* the protocol field. Also in other places (at the time of this writing:
|
2018-04-14 00:00:40 +00:00
|
|
|
* the DeepLinkingMobilePage.js) the APP_LINK_SCHEME does not include
|
2018-03-14 22:17:49 +00:00
|
|
|
* the double dots, so things are inconsistent.
|
|
|
|
*
|
2017-07-21 21:12:02 +00:00
|
|
|
* @type {string}
|
|
|
|
*/
|
2019-01-30 12:16:50 +00:00
|
|
|
export const URI_PROTOCOL_PATTERN = '^([a-z][a-z0-9\\.\\+-]*:)';
|
2017-07-21 21:12:02 +00:00
|
|
|
|
2018-04-10 10:42:48 +00:00
|
|
|
/**
|
2021-07-21 17:48:08 +00:00
|
|
|
* Excludes/removes certain characters from a specific path part which are
|
|
|
|
* incompatible with Jitsi Meet on the client and/or server sides. The main
|
|
|
|
* use case for this method is to clean up the room name and the tenant.
|
2018-04-10 10:42:48 +00:00
|
|
|
*
|
2021-07-21 17:48:08 +00:00
|
|
|
* @param {?string} pathPart - The path part to fix.
|
2018-04-10 10:42:48 +00:00
|
|
|
* @private
|
|
|
|
* @returns {?string}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
function _fixPathPart(pathPart?: string) {
|
2021-07-21 17:48:08 +00:00
|
|
|
return pathPart
|
|
|
|
? pathPart.replace(new RegExp(_ROOM_EXCLUDE_PATTERN, 'g'), '')
|
|
|
|
: pathPart;
|
2018-04-10 10:42:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
/**
|
|
|
|
* Fixes the scheme part of a specific URI (string) so that it contains a
|
|
|
|
* well-known scheme such as HTTP(S). For example, the mobile app implements an
|
|
|
|
* app-specific URI scheme in addition to Universal Links. The app-specific
|
|
|
|
* scheme may precede or replace the well-known scheme. In such a case, dealing
|
|
|
|
* with the app-specific scheme only complicates the logic and it is simpler to
|
|
|
|
* get rid of it (by translating the app-specific scheme into a well-known
|
|
|
|
* scheme).
|
|
|
|
*
|
|
|
|
* @param {string} uri - The URI (string) to fix the scheme of.
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function _fixURIStringScheme(uri: string) {
|
2019-02-06 12:52:03 +00:00
|
|
|
const regex = new RegExp(`${URI_PROTOCOL_PATTERN}+`, 'gi');
|
2017-10-03 19:24:00 +00:00
|
|
|
const match: Array<string> | null = regex.exec(uri);
|
2017-07-21 21:12:02 +00:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
// As an implementation convenience, pick up the last scheme and make
|
|
|
|
// sure that it is a well-known one.
|
|
|
|
let protocol = match[match.length - 1].toLowerCase();
|
|
|
|
|
|
|
|
if (protocol !== 'http:' && protocol !== 'https:') {
|
|
|
|
protocol = 'https:';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
|
|
|
uri = uri.substring(regex.lastIndex);
|
|
|
|
if (uri.startsWith('//')) {
|
|
|
|
// The specified URL was not a room name only, it contained an
|
|
|
|
// authority.
|
|
|
|
uri = protocol + uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:39:48 +00:00
|
|
|
/**
|
|
|
|
* Converts a path to a backend-safe format, by splitting the path '/' processing each part.
|
|
|
|
* Properly lowercased and url encoded.
|
|
|
|
*
|
|
|
|
* @param {string?} path - The path to convert.
|
|
|
|
* @returns {string?}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
export function getBackendSafePath(path?: string): string|undefined {
|
2020-05-28 10:39:48 +00:00
|
|
|
if (!path) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
.split('/')
|
|
|
|
.map(getBackendSafeRoomName)
|
|
|
|
.join('/');
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:31:22 +00:00
|
|
|
/**
|
|
|
|
* Converts a room name to a backend-safe format. Properly lowercased and url encoded.
|
|
|
|
*
|
|
|
|
* @param {string?} room - The room name to convert.
|
|
|
|
* @returns {string?}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
export function getBackendSafeRoomName(room?: string): string|undefined {
|
2019-10-04 07:31:22 +00:00
|
|
|
if (!room) {
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
try {
|
|
|
|
// We do not know if we get an already encoded string at this point
|
|
|
|
// as different platforms do it differently, but we need a decoded one
|
|
|
|
// for sure. However since decoding a non-encoded string is a noop, we're safe
|
|
|
|
// doing it here.
|
|
|
|
room = decodeURIComponent(room);
|
|
|
|
} catch (e) {
|
|
|
|
// This can happen though if we get an unencoded string and it contains
|
|
|
|
// some characters that look like an encoded entity, but it's not.
|
|
|
|
// But in this case we're fine goin on...
|
|
|
|
}
|
|
|
|
|
2019-11-06 13:42:49 +00:00
|
|
|
// Normalize the character set.
|
|
|
|
room = normalizeNFKC(room);
|
2019-10-04 07:31:22 +00:00
|
|
|
|
|
|
|
// Only decoded and normalized strings can be lowercased properly.
|
2022-07-29 13:18:14 +00:00
|
|
|
room = room?.toLowerCase();
|
2019-10-04 07:31:22 +00:00
|
|
|
|
|
|
|
// But we still need to (re)encode it.
|
2022-07-29 13:18:14 +00:00
|
|
|
room = encodeURIComponent(room ?? '');
|
2019-10-04 07:31:22 +00:00
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
|
|
|
|
// Unfortunately we still need to lowercase it, because encoding a string will
|
|
|
|
// add some uppercase characters, but some backend services
|
|
|
|
// expect it to be full lowercase. However lowercasing an encoded string
|
|
|
|
// doesn't change the string value.
|
|
|
|
return room.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
/**
|
|
|
|
* Gets the (Web application) context root defined by a specific location (URI).
|
|
|
|
*
|
|
|
|
* @param {Object} location - The location (URI) which defines the (Web
|
|
|
|
* application) context root.
|
|
|
|
* @public
|
|
|
|
* @returns {string} - The (Web application) context root defined by the
|
|
|
|
* specified {@code location} (URI).
|
|
|
|
*/
|
2017-10-03 19:24:00 +00:00
|
|
|
export function getLocationContextRoot({ pathname }: { pathname: string }) {
|
2017-07-21 21:12:02 +00:00
|
|
|
const contextRootEndIndex = pathname.lastIndexOf('/');
|
|
|
|
|
|
|
|
return (
|
|
|
|
contextRootEndIndex === -1
|
|
|
|
? '/'
|
|
|
|
: pathname.substring(0, contextRootEndIndex + 1));
|
|
|
|
}
|
|
|
|
|
2017-07-27 18:50:09 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new {@code Array} with URL parameter {@code String}s out of a
|
|
|
|
* specific {@code Object}.
|
|
|
|
*
|
|
|
|
* @param {Object} obj - The {@code Object} to turn into URL parameter
|
|
|
|
* {@code String}s.
|
|
|
|
* @returns {Array<string>} The {@code Array} with URL parameter {@code String}s
|
|
|
|
* constructed out of the specified {@code obj}.
|
|
|
|
*/
|
|
|
|
function _objectToURLParamsArray(obj = {}) {
|
|
|
|
const params = [];
|
|
|
|
|
|
|
|
for (const key in obj) { // eslint-disable-line guard-for-in
|
|
|
|
try {
|
|
|
|
params.push(
|
2022-07-29 13:18:14 +00:00
|
|
|
`${key}=${encodeURIComponent(JSON.stringify(obj[key as keyof typeof obj]))}`);
|
2017-07-27 18:50:09 +00:00
|
|
|
} catch (e) {
|
2019-09-06 11:02:39 +00:00
|
|
|
console.warn(`Error encoding ${key}: ${e}`);
|
2017-07-27 18:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
/**
|
|
|
|
* Parses a specific URI string into an object with the well-known properties of
|
|
|
|
* the {@link Location} and/or {@link URL} interfaces implemented by Web
|
|
|
|
* browsers. The parsing attempts to be in accord with IETF's RFC 3986.
|
|
|
|
*
|
|
|
|
* @param {string} str - The URI string to parse.
|
|
|
|
* @public
|
|
|
|
* @returns {{
|
|
|
|
* hash: string,
|
|
|
|
* host: (string|undefined),
|
|
|
|
* hostname: (string|undefined),
|
|
|
|
* pathname: string,
|
|
|
|
* port: (string|undefined),
|
|
|
|
* protocol: (string|undefined),
|
|
|
|
* search: string
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function parseStandardURIString(str: string) {
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
2022-07-29 13:18:14 +00:00
|
|
|
const obj: {[key: string]: any} = {
|
2017-07-27 18:50:09 +00:00
|
|
|
toString: _standardURIToString
|
|
|
|
};
|
2017-07-21 21:12:02 +00:00
|
|
|
|
|
|
|
let regex;
|
2017-10-03 19:24:00 +00:00
|
|
|
let match: Array<string> | null;
|
2017-07-21 21:12:02 +00:00
|
|
|
|
2018-01-19 21:26:13 +00:00
|
|
|
// XXX A URI string as defined by RFC 3986 does not contain any whitespace.
|
|
|
|
// Usually, a browser will have already encoded any whitespace. In order to
|
|
|
|
// avoid potential later problems related to whitespace in URI, strip any
|
|
|
|
// whitespace. Anyway, the Jitsi Meet app is not known to utilize unencoded
|
|
|
|
// whitespace so the stripping is deemed safe.
|
2018-01-15 13:21:50 +00:00
|
|
|
str = str.replace(/\s/g, '');
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
// protocol
|
2019-01-30 12:16:50 +00:00
|
|
|
regex = new RegExp(URI_PROTOCOL_PATTERN, 'gi');
|
2017-07-21 21:12:02 +00:00
|
|
|
match = regex.exec(str);
|
|
|
|
if (match) {
|
|
|
|
obj.protocol = match[1].toLowerCase();
|
|
|
|
str = str.substring(regex.lastIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// authority
|
|
|
|
regex = new RegExp(`^${_URI_AUTHORITY_PATTERN}`, 'gi');
|
|
|
|
match = regex.exec(str);
|
|
|
|
if (match) {
|
2017-10-03 19:24:00 +00:00
|
|
|
let authority: string = match[1].substring(/* // */ 2);
|
2017-07-21 21:12:02 +00:00
|
|
|
|
|
|
|
str = str.substring(regex.lastIndex);
|
|
|
|
|
|
|
|
// userinfo
|
|
|
|
const userinfoEndIndex = authority.indexOf('@');
|
|
|
|
|
|
|
|
if (userinfoEndIndex !== -1) {
|
|
|
|
authority = authority.substring(userinfoEndIndex + 1);
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:18:14 +00:00
|
|
|
// @ts-ignore
|
2017-07-21 21:12:02 +00:00
|
|
|
obj.host = authority;
|
|
|
|
|
|
|
|
// port
|
|
|
|
const portBeginIndex = authority.lastIndexOf(':');
|
|
|
|
|
|
|
|
if (portBeginIndex !== -1) {
|
|
|
|
obj.port = authority.substring(portBeginIndex + 1);
|
|
|
|
authority = authority.substring(0, portBeginIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// hostname
|
|
|
|
obj.hostname = authority;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathname
|
|
|
|
regex = new RegExp(`^${_URI_PATH_PATTERN}`, 'gi');
|
|
|
|
match = regex.exec(str);
|
|
|
|
|
2022-07-29 13:18:14 +00:00
|
|
|
let pathname: string|undefined;
|
2017-07-21 21:12:02 +00:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
pathname = match[1];
|
|
|
|
str = str.substring(regex.lastIndex);
|
|
|
|
}
|
|
|
|
if (pathname) {
|
2017-07-27 18:50:09 +00:00
|
|
|
pathname.startsWith('/') || (pathname = `/${pathname}`);
|
2017-07-21 21:12:02 +00:00
|
|
|
} else {
|
|
|
|
pathname = '/';
|
|
|
|
}
|
|
|
|
obj.pathname = pathname;
|
|
|
|
|
|
|
|
// query
|
|
|
|
if (str.startsWith('?')) {
|
|
|
|
let hashBeginIndex = str.indexOf('#', 1);
|
|
|
|
|
|
|
|
if (hashBeginIndex === -1) {
|
|
|
|
hashBeginIndex = str.length;
|
|
|
|
}
|
|
|
|
obj.search = str.substring(0, hashBeginIndex);
|
|
|
|
str = str.substring(hashBeginIndex);
|
|
|
|
} else {
|
|
|
|
obj.search = ''; // Google Chrome
|
|
|
|
}
|
|
|
|
|
|
|
|
// fragment
|
|
|
|
obj.hash = str.startsWith('#') ? str : '';
|
|
|
|
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a specific URI which (supposedly) references a Jitsi Meet resource
|
|
|
|
* (location).
|
|
|
|
*
|
|
|
|
* @param {(string|undefined)} uri - The URI to parse which (supposedly)
|
|
|
|
* references a Jitsi Meet resource (location).
|
|
|
|
* @public
|
|
|
|
* @returns {{
|
2018-08-17 19:34:41 +00:00
|
|
|
* contextRoot: string,
|
|
|
|
* hash: string,
|
|
|
|
* host: string,
|
|
|
|
* hostname: string,
|
|
|
|
* pathname: string,
|
|
|
|
* port: string,
|
|
|
|
* protocol: string,
|
|
|
|
* room: (string|undefined),
|
|
|
|
* search: string
|
2017-07-21 21:12:02 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
export function parseURIString(uri?: string) {
|
2017-07-21 21:12:02 +00:00
|
|
|
if (typeof uri !== 'string') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-12-18 11:49:19 +00:00
|
|
|
const obj = parseStandardURIString(_fixURIStringScheme(uri));
|
2017-07-21 21:12:02 +00:00
|
|
|
|
2021-07-21 17:48:08 +00:00
|
|
|
// XXX While the components/segments of pathname are URI encoded, Jitsi Meet
|
|
|
|
// on the client and/or server sides still don't support certain characters.
|
2022-07-29 13:18:14 +00:00
|
|
|
obj.pathname = obj.pathname.split('/').map((pathPart: any) => _fixPathPart(pathPart))
|
2021-07-21 17:48:08 +00:00
|
|
|
.join('/');
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
// Add the properties that are specific to a Jitsi Meet resource (location)
|
|
|
|
// such as contextRoot, room:
|
|
|
|
|
|
|
|
// contextRoot
|
2022-07-29 13:18:14 +00:00
|
|
|
// @ts-ignore
|
2017-07-21 21:12:02 +00:00
|
|
|
obj.contextRoot = getLocationContextRoot(obj);
|
|
|
|
|
2018-04-10 10:42:48 +00:00
|
|
|
// The room (name) is the last component/segment of pathname.
|
2017-07-21 21:12:02 +00:00
|
|
|
const { pathname } = obj;
|
|
|
|
|
2018-04-10 10:42:48 +00:00
|
|
|
const contextRootEndIndex = pathname.lastIndexOf('/');
|
|
|
|
|
2021-07-21 17:48:08 +00:00
|
|
|
obj.room = pathname.substring(contextRootEndIndex + 1) || undefined;
|
2017-07-21 21:12:02 +00:00
|
|
|
|
2020-12-09 23:25:55 +00:00
|
|
|
if (contextRootEndIndex > 1) {
|
|
|
|
// The part of the pathname from the beginning to the room name is the tenant.
|
|
|
|
obj.tenant = pathname.substring(1, contextRootEndIndex);
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
return obj;
|
|
|
|
}
|
2017-07-26 19:27:29 +00:00
|
|
|
|
2017-07-27 18:50:09 +00:00
|
|
|
/**
|
|
|
|
* Implements {@code href} and {@code toString} for the {@code Object} returned
|
|
|
|
* by {@link #parseStandardURIString}.
|
|
|
|
*
|
|
|
|
* @param {Object} [thiz] - An {@code Object} returned by
|
|
|
|
* {@code #parseStandardURIString} if any; otherwise, it is presumed that the
|
|
|
|
* function is invoked on such an instance.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
function _standardURIToString(thiz?: Object) {
|
|
|
|
// @ts-ignore
|
2017-07-27 18:50:09 +00:00
|
|
|
// eslint-disable-next-line no-invalid-this
|
|
|
|
const { hash, host, pathname, protocol, search } = thiz || this;
|
|
|
|
let str = '';
|
|
|
|
|
|
|
|
protocol && (str += protocol);
|
|
|
|
|
|
|
|
// TODO userinfo
|
|
|
|
|
|
|
|
host && (str += `//${host}`);
|
|
|
|
str += pathname || '/';
|
|
|
|
search && (str += search);
|
|
|
|
hash && (str += hash);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:09:15 +00:00
|
|
|
/**
|
|
|
|
* Sometimes we receive strings that we don't know if already percent-encoded, or not, due to the
|
|
|
|
* various sources we get URLs or room names. This function encapsulates the decoding in a safe way.
|
|
|
|
*
|
|
|
|
* @param {string} text - The text to decode.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function safeDecodeURIComponent(text: string) {
|
|
|
|
try {
|
|
|
|
return decodeURIComponent(text);
|
|
|
|
} catch (e) {
|
|
|
|
// The text wasn't encoded.
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:27:29 +00:00
|
|
|
/**
|
|
|
|
* Attempts to return a {@code String} representation of a specific
|
|
|
|
* {@code Object} which is supposed to represent a URL. Obviously, if a
|
|
|
|
* {@code String} is specified, it is returned. If a {@code URL} is specified,
|
|
|
|
* its {@code URL#href} is returned. Additionally, an {@code Object} similar to
|
|
|
|
* the one accepted by the constructor of Web's ExternalAPI is supported on both
|
|
|
|
* mobile/React Native and Web/React.
|
|
|
|
*
|
2018-02-26 19:37:12 +00:00
|
|
|
* @param {Object|string} obj - The URL to return a {@code String}
|
2017-07-26 19:27:29 +00:00
|
|
|
* representation of.
|
|
|
|
* @returns {string} - A {@code String} representation of the specified
|
|
|
|
* {@code obj} which is supposed to represent a URL.
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
export function toURLString(obj?: (Object | string)): string|undefined|null {
|
2017-07-26 19:27:29 +00:00
|
|
|
let str;
|
|
|
|
|
|
|
|
switch (typeof obj) {
|
|
|
|
case 'object':
|
|
|
|
if (obj) {
|
|
|
|
if (obj instanceof URL) {
|
|
|
|
str = obj.href;
|
|
|
|
} else {
|
2017-07-27 18:50:09 +00:00
|
|
|
str = urlObjectToString(obj);
|
2017-07-26 19:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'string':
|
|
|
|
str = String(obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to return a {@code String} representation of a specific
|
|
|
|
* {@code Object} similar to the one accepted by the constructor
|
|
|
|
* of Web's ExternalAPI.
|
|
|
|
*
|
2017-07-27 18:50:09 +00:00
|
|
|
* @param {Object} o - The URL to return a {@code String} representation of.
|
2017-07-26 19:27:29 +00:00
|
|
|
* @returns {string} - A {@code String} representation of the specified
|
2017-07-27 18:50:09 +00:00
|
|
|
* {@code Object}.
|
2017-07-26 19:27:29 +00:00
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
export function urlObjectToString(o: {[key: string]: any}): string|undefined {
|
2019-03-06 12:12:54 +00:00
|
|
|
// First normalize the given url. It come as o.url or split into o.serverURL
|
|
|
|
// and o.room.
|
|
|
|
let tmp;
|
|
|
|
|
|
|
|
if (o.serverURL && o.room) {
|
|
|
|
tmp = new URL(o.room, o.serverURL).toString();
|
|
|
|
} else if (o.room) {
|
|
|
|
tmp = o.room;
|
|
|
|
} else {
|
|
|
|
tmp = o.url || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = parseStandardURIString(_fixURIStringScheme(tmp));
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
// protocol
|
|
|
|
if (!url.protocol) {
|
2022-07-29 13:18:14 +00:00
|
|
|
let protocol: string|undefined = o.protocol || o.scheme;
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
if (protocol) {
|
|
|
|
// Protocol is supposed to be the scheme and the final ':'. Anyway,
|
|
|
|
// do not make a fuss if the final ':' is not there.
|
|
|
|
protocol.endsWith(':') || (protocol += ':');
|
|
|
|
url.protocol = protocol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// authority & pathname
|
|
|
|
let { pathname } = url;
|
|
|
|
|
|
|
|
if (!url.host) {
|
|
|
|
// Web's ExternalAPI domain
|
|
|
|
//
|
|
|
|
// It may be host/hostname and pathname with the latter denoting the
|
|
|
|
// tenant.
|
2022-07-29 13:18:14 +00:00
|
|
|
const domain: string|undefined = o.domain || o.host || o.hostname;
|
2017-08-01 00:38:44 +00:00
|
|
|
|
|
|
|
if (domain) {
|
|
|
|
const { host, hostname, pathname: contextRoot, port }
|
2017-09-06 23:26:33 +00:00
|
|
|
= parseStandardURIString(
|
|
|
|
|
|
|
|
// XXX The value of domain in supposed to be host/hostname
|
|
|
|
// and, optionally, pathname. Make sure it is not taken for
|
|
|
|
// a pathname only.
|
2018-02-14 20:34:33 +00:00
|
|
|
_fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
|
2017-07-27 18:50:09 +00:00
|
|
|
|
2017-08-01 00:38:44 +00:00
|
|
|
// authority
|
|
|
|
if (host) {
|
|
|
|
url.host = host;
|
|
|
|
url.hostname = hostname;
|
|
|
|
url.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pathname
|
|
|
|
pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
|
|
|
|
}
|
2017-07-27 18:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pathname
|
|
|
|
|
|
|
|
// Web's ExternalAPI roomName
|
2020-12-10 12:51:51 +00:00
|
|
|
const room = o.roomName || o.room;
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
if (room
|
|
|
|
&& (url.pathname.endsWith('/')
|
|
|
|
|| !url.pathname.endsWith(`/${room}`))) {
|
|
|
|
pathname.endsWith('/') || (pathname += '/');
|
|
|
|
pathname += room;
|
|
|
|
}
|
|
|
|
|
|
|
|
url.pathname = pathname;
|
|
|
|
|
|
|
|
// query/search
|
|
|
|
|
2022-03-08 14:57:53 +00:00
|
|
|
// Web's ExternalAPI jwt and lang
|
2022-06-16 12:27:41 +00:00
|
|
|
const { jwt, lang, release } = o;
|
2022-03-08 14:57:53 +00:00
|
|
|
|
|
|
|
const search = new URLSearchParams(url.search);
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
if (jwt) {
|
2022-03-08 14:57:53 +00:00
|
|
|
search.set('jwt', jwt);
|
|
|
|
}
|
2017-07-27 18:50:09 +00:00
|
|
|
|
2022-03-08 14:57:53 +00:00
|
|
|
const { defaultLanguage } = o.configOverwrite || {};
|
2017-07-27 18:50:09 +00:00
|
|
|
|
2022-03-08 14:57:53 +00:00
|
|
|
if (lang || defaultLanguage) {
|
|
|
|
search.set('lang', lang || defaultLanguage);
|
|
|
|
}
|
|
|
|
|
2022-06-16 12:27:41 +00:00
|
|
|
if (release) {
|
|
|
|
search.set('release', release);
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:57:53 +00:00
|
|
|
const searchString = search.toString();
|
|
|
|
|
|
|
|
if (searchString) {
|
|
|
|
url.search = `?${searchString}`;
|
2017-07-27 18:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fragment/hash
|
|
|
|
|
|
|
|
let { hash } = url;
|
|
|
|
|
2020-09-25 22:51:54 +00:00
|
|
|
for (const urlPrefix of [ 'config', 'interfaceConfig', 'devices', 'userInfo', 'appData' ]) {
|
2017-07-27 18:50:09 +00:00
|
|
|
const urlParamsArray
|
|
|
|
= _objectToURLParamsArray(
|
2019-03-25 11:33:41 +00:00
|
|
|
o[`${urlPrefix}Overwrite`]
|
|
|
|
|| o[urlPrefix]
|
|
|
|
|| o[`${urlPrefix}Override`]);
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
if (urlParamsArray.length) {
|
|
|
|
let urlParamsString
|
2019-03-25 11:33:41 +00:00
|
|
|
= `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
|
2017-07-27 18:50:09 +00:00
|
|
|
|
|
|
|
if (hash.length) {
|
|
|
|
urlParamsString = `&${urlParamsString}`;
|
|
|
|
} else {
|
|
|
|
hash = '#';
|
|
|
|
}
|
|
|
|
hash += urlParamsString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url.hash = hash;
|
|
|
|
|
|
|
|
return url.toString() || undefined;
|
2017-07-26 19:27:29 +00:00
|
|
|
}
|
2020-05-07 22:26:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds hash params to URL.
|
|
|
|
*
|
|
|
|
* @param {URL} url - The URL.
|
|
|
|
* @param {Object} hashParamsToAdd - A map with the parameters to be set.
|
|
|
|
* @returns {URL} - The new URL.
|
|
|
|
*/
|
|
|
|
export function addHashParamsToURL(url: URL, hashParamsToAdd: Object = {}) {
|
|
|
|
const params = parseURLParams(url);
|
|
|
|
const urlParamsArray = _objectToURLParamsArray({
|
|
|
|
...params,
|
|
|
|
...hashParamsToAdd
|
|
|
|
});
|
|
|
|
|
|
|
|
if (urlParamsArray.length) {
|
|
|
|
url.hash = `#${urlParamsArray.join('&')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
2020-07-15 10:07:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the decoded URI.
|
|
|
|
*
|
|
|
|
* @param {string} uri - The URI to decode.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function getDecodedURI(uri: string) {
|
|
|
|
return decodeURI(uri.replace(/^https?:\/\//i, ''));
|
|
|
|
}
|
2022-06-16 12:27:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds new param to a url string. Checks whether to use '?' or '&' as a separator (checks for already existing params).
|
|
|
|
*
|
|
|
|
* @param {string} url - The url to modify.
|
|
|
|
* @param {string} name - The param name to add.
|
|
|
|
* @param {string} value - The value for the param.
|
|
|
|
*
|
|
|
|
* @returns {string} - The modified url.
|
|
|
|
*/
|
|
|
|
export function appendURLParam(url: string, name: string, value: string) {
|
|
|
|
const newUrl = new URL(url);
|
|
|
|
|
|
|
|
newUrl.searchParams.append(name, value);
|
|
|
|
|
|
|
|
return newUrl.toString();
|
|
|
|
}
|