init
Some checks failed
Close stale issues and PRs / stale (push) Has been cancelled

This commit is contained in:
2025-09-02 14:49:16 +08:00
commit 38ba663466
2885 changed files with 391107 additions and 0 deletions

View File

@@ -0,0 +1 @@
export const SECURITY_URL = 'https://jitsi.org/security/';

View File

@@ -0,0 +1,18 @@
import Clipboard from '@react-native-clipboard/clipboard';
/**
* Tries to copy a given text to the clipboard.
* Returns true if the action succeeds.
*
* @param {string} textToCopy - Text to be copied.
* @returns {Promise<boolean>}
*/
export function copyText(textToCopy: string) {
try {
Clipboard.setString(textToCopy);
return true;
} catch (e) {
return false;
}
}

View File

@@ -0,0 +1,18 @@
import clipboardCopy from 'clipboard-copy';
/**
* Tries to copy a given text to the clipboard.
* Returns true if the action succeeds.
*
* @param {string} textToCopy - Text to be copied.
* @returns {Promise<boolean>}
*/
export async function copyText(textToCopy: string) {
try {
await clipboardCopy(textToCopy);
return true;
} catch (e) {
return false;
}
}

View File

@@ -0,0 +1,35 @@
/**
* Downloads a JSON object.
*
* @param {Object} json - The JSON object to download.
* @param {string} filename - The filename to give to the downloaded file.
* @returns {void}
*/
export function downloadJSON(json: Object, filename: string): void {
const replacer = () => {
const seen = new WeakSet();
return (_: any, value: any) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return '[circular ref]';
}
seen.add(value);
}
return value;
};
};
const data = encodeURIComponent(JSON.stringify(json, replacer(), ' '));
const elem = document.createElement('a');
elem.download = filename;
elem.href = `data:application/json;charset=utf-8,\n${data}`;
elem.dataset.downloadurl = [ 'text/json', elem.download, elem.href ].join(':');
elem.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: false
}));
}

View File

@@ -0,0 +1,26 @@
import { getBundleId } from 'react-native-device-info';
/**
* BUndle ids for the Jitsi Meet apps.
*/
const JITSI_MEET_APPS = [
// iOS app.
'com.atlassian.JitsiMeet.ios',
// Android + iOS (testing) app.
'org.jitsi.meet',
// Android debug app.
'org.jitsi.meet.debug'
];
/**
* Checks whether we are loaded in iframe. In the mobile case we treat SDK
* consumers as the web treats iframes.
*
* @returns {boolean} Whether the current app is a Jitsi Meet app.
*/
export function isEmbedded(): boolean {
return !JITSI_MEET_APPS.includes(getBundleId());
}

View File

@@ -0,0 +1,12 @@
/**
* Checks whether we are loaded in iframe.
*
* @returns {boolean} Whether the current page is loaded in an iframe.
*/
export function isEmbedded(): boolean {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}

View File

@@ -0,0 +1,32 @@
import React from 'react';
import { Text } from 'react-native';
import { IReduxState } from '../../app/types';
import Link from '../react/components/native/Link';
import BaseTheme from '../ui/components/BaseTheme.native';
import { SECURITY_URL } from './contants';
/**
* Gets the unsafe room text for the given context.
*
* @param {IReduxState} state - The redux state.
* @param {Function} t - The translation function.
* @param {'meeting'|'prejoin'|'welcome'} context - The given context of the warning.
* @returns {Text}
*/
export default function getUnsafeRoomText(state: IReduxState, t: Function, context: 'meeting' | 'prejoin' | 'welcome') {
const securityUrl = state['features/base/config'].legalUrls?.security ?? SECURITY_URL;
const link = React.createElement(Link, {
url: securityUrl,
children: 'here',
key: 'support-link',
style: { color: BaseTheme.palette.action01 } });
const options = {
recommendAction: t(`security.unsafeRoomActions.${context}`)
};
return React.createElement(Text, { children: [ t('security.insecureRoomNameWarningNative', options), link, '.' ] });
}

View File

@@ -0,0 +1,20 @@
import { translateToHTML } from '../i18n/functions';
import { SECURITY_URL } from './contants';
/**
* Gets the unsafe room text for the given context.
*
* @param {Function} t - The translation function.
* @param {'meeting'|'prejoin'|'welcome'} context - The given context of the warning.
* @returns {string}
*/
export default function getUnsafeRoomText(t: Function, context: 'meeting' | 'prejoin' | 'welcome') {
const securityUrl = APP.store.getState()['features/base/config'].legalUrls?.security ?? SECURITY_URL;
const options = {
recommendAction: t(`security.unsafeRoomActions.${context}`),
securityUrl
};
return translateToHTML(t, 'security.insecureRoomNameWarningWeb', options);
}

View File

@@ -0,0 +1,182 @@
/**
* A helper function that behaves similar to Object.assign, but only reassigns a
* property in target if it's defined in source.
*
* @param {Object} target - The target object to assign the values into.
* @param {Object} source - The source object.
* @returns {Object}
*/
export function assignIfDefined(target: Object, source: Object) {
const to = Object(target);
for (const nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
const value = source[nextKey as keyof typeof source];
if (typeof value !== 'undefined') {
to[nextKey] = value;
}
}
}
return to;
}
const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
/**
* Escape RegExp special characters.
*
* Based on https://github.com/sindresorhus/escape-string-regexp.
*
* @param {string} s - The regexp string to escape.
* @returns {string}
*/
export function escapeRegexp(s: string) {
if (typeof s !== 'string') {
throw new TypeError('Expected a string');
}
return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
}
/**
* Returns the base URL of the app.
*
* @param {Object} w - Window object to use instead of the built in one.
* @returns {string}
*/
export function getBaseUrl(w: typeof window = window) {
const doc = w.document;
const base = doc.querySelector('base');
if (base?.href) {
return base.href;
}
const { protocol, host } = w.location;
return `${protocol}//${host}`;
}
/**
* Returns the namespace for all global variables, functions, etc that we need.
*
* @returns {Object} The namespace.
*
* NOTE: After React-ifying everything this should be the only global.
*/
export function getJitsiMeetGlobalNS() {
if (!window.JitsiMeetJS) {
window.JitsiMeetJS = {};
}
if (!window.JitsiMeetJS.app) {
window.JitsiMeetJS.app = {};
}
return window.JitsiMeetJS.app;
}
/**
* Returns the object that stores the connection times.
*
* @returns {Object} - The object that stores the connection times.
*/
export function getJitsiMeetGlobalNSConnectionTimes() {
const globalNS = getJitsiMeetGlobalNS();
if (!globalNS.connectionTimes) {
globalNS.connectionTimes = {};
}
return globalNS.connectionTimes;
}
/**
* Prints the error and reports it to the global error handler.
*
* @param {Error} e - The error object.
* @param {string} msg - A custom message to print in addition to the error.
* @returns {void}
*/
export function reportError(e: Error, msg = '') {
console.error(msg, e);
window.onerror?.(msg, undefined, undefined, undefined, e);
}
/**
* Adds alpha to a color css string.
*
* @param {string} color - The color string either in rgb... Or #... Format.
* @param {number} opacity -The opacity(alpha) to apply to the color. Can take a value between 0 and 1, including.
* @returns {string} - The color with applied alpha.
*/
export function setColorAlpha(color: string, opacity: number) {
if (!color) {
return `rgba(0, 0, 0, ${opacity})`;
}
let b, g, r;
try {
if (color.startsWith('rgb')) {
[ r, g, b ] = color.split('(')[1].split(')')[0].split(',').map(c => c.trim());
} else if (color.startsWith('#')) {
if (color.length === 4) {
[ r, g, b ] = parseShorthandColor(color);
} else {
r = parseInt(color.substring(1, 3), 16);
g = parseInt(color.substring(3, 5), 16);
b = parseInt(color.substring(5, 7), 16);
}
} else {
return color;
}
return `rgba(${r}, ${g}, ${b}, ${opacity})`;
} catch {
return color;
}
}
/**
* Gets the hexa rgb values for a shorthand css color.
*
* @param {string} color - The shorthand css color.
* @returns {Array<number>} - Array containing parsed r, g, b values of the color.
*/
function parseShorthandColor(color: string) {
let b, g, r;
r = color.substring(1, 2);
r += r;
r = parseInt(r, 16);
g = color.substring(2, 3);
g += g;
g = parseInt(g, 16);
b = color.substring(3, 4);
b += b;
b = parseInt(b, 16);
return [ r, g, b ];
}
/**
* Sorts an object by a sort function, same functionality as array.sort().
*
* @param {Object} object - The data object.
* @param {Function} callback - The sort function.
* @returns {void}
*/
export function objectSort(object: Object, callback: Function) {
return Object.entries(object)
.sort(([ , a ], [ , b ]) => callback(a, b))
.reduce((row, [ key, value ]) => {
return { ...row,
[key]: value };
}, {});
}

View File

@@ -0,0 +1,12 @@
import { useSelector } from 'react-redux';
/**
* Takes a redux selector and binds it to specific values.
*
* @param {Function} selector - The selector function.
* @param {...any} args - The values to bind to.
* @returns {any}
*/
export function useBoundSelector(selector: Function, ...args: any[]) {
return useSelector(state => selector(state, ...args));
}

View File

@@ -0,0 +1,81 @@
import base64js from 'base64-js';
import { timeoutPromise } from './timeoutPromise';
/**
* The number of milliseconds before deciding that we need retry a fetch request.
*
* @type {number}
*/
const RETRY_TIMEOUT = 3000;
/**
* Wrapper around fetch GET requests to handle json-ifying the response
* and logging errors.
*
* @param {string} url - The URL to perform a GET against.
* @param {?boolean} retry - Whether the request will be retried after short timeout.
* @param {?Object} options - The request options.
* @returns {Promise<Object>} The response body, in JSON format, will be
* through the Promise.
*/
export function doGetJSON(url: string, retry?: boolean, options?: Object) {
const fetchPromise = fetch(url, options)
.then(response => {
const jsonify = response.json();
if (response.ok) {
return jsonify;
}
return jsonify
.then(result => Promise.reject(result));
});
if (retry) {
return timeoutPromise(fetchPromise, RETRY_TIMEOUT)
.catch(response => {
if (response.status >= 400 && response.status < 500) {
return Promise.reject(response);
}
return timeoutPromise(fetchPromise, RETRY_TIMEOUT);
});
}
return fetchPromise;
}
/**
* Encodes strings to Base64URL.
*
* @param {any} data - The byte array to encode.
* @returns {string}
*/
export const encodeToBase64URL = (data: string): string => base64js
.fromByteArray(new window.TextEncoder().encode(data))
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
/**
* Decodes strings from Base64URL.
*
* @param {string} data - The byte array to decode.
* @returns {string}
*/
export const decodeFromBase64URL = (data: string): string => {
let s = data;
// Convert from Base64URL to Base64.
if (s.length % 4 === 2) {
s += '==';
} else if (s.length % 4 === 3) {
s += '=';
}
s = s.replace(/-/g, '+').replace(/_/g, '/');
// Convert Base64 to a byte array.
return new window.TextDecoder().decode(base64js.toByteArray(s));
};

View File

@@ -0,0 +1,56 @@
import { isEqual } from 'lodash-es';
import { NIL, parse as parseUUID } from 'uuid';
import zxcvbn from 'zxcvbn';
// The null UUID.
const NIL_UUID = parseUUID(NIL);
const _zxcvbnCache = new Map();
/**
* Checks if the given string is a valid UUID or not.
*
* @param {string} str - The string to be checked.
* @returns {boolean} - Whether the string is a valid UUID or not.
*/
function isValidUUID(str: string) {
let uuid;
try {
uuid = parseUUID(str);
} catch (e) {
return false;
}
return !isEqual(uuid, NIL_UUID);
}
/**
* Checks a room name and caches the result.
*
* @param {string} roomName - The room name.
* @returns {Object}
*/
function _checkRoomName(roomName = '') {
if (_zxcvbnCache.has(roomName)) {
return _zxcvbnCache.get(roomName);
}
const result = zxcvbn(roomName);
_zxcvbnCache.set(roomName, result);
return result;
}
/**
* Returns true if the room name is considered a weak (insecure) one.
*
* @param {string} roomName - The room name.
* @returns {boolean}
*/
export default function isInsecureRoomName(roomName = ''): boolean {
// room names longer than 200 chars we consider secure
return !isValidUUID(roomName) && (roomName.length < 200 && _checkRoomName(roomName).score < 3);
}

View File

@@ -0,0 +1,62 @@
/**
* Default timeout for loading scripts.
*/
const DEFAULT_TIMEOUT = 5000;
/**
* Loads a script from a specific URL. React Native cannot load a JS
* file/resource/URL via a <script> HTML element, so the implementation
* fetches the specified {@code url} as plain text using {@link fetch()} and
* then evaluates the fetched string as JavaScript code (using {@link eval()}).
*
* @param {string} url - The absolute URL from which the script is to be
* (down)loaded.
* @param {number} [timeout] - The timeout in millisecnods after which the
* loading of the specified {@code url} is to be aborted/rejected (if not
* settled yet).
* @param {boolean} skipEval - Whether we want to skip evaluating the loaded content or not.
* @returns {void}
*/
export async function loadScript(
url: string, timeout: number = DEFAULT_TIMEOUT, skipEval = false): Promise<any> {
// XXX The implementation of fetch on Android will throw an Exception on
// the Java side which will break the app if the URL is invalid (which
// the implementation of fetch on Android calls 'unexpected url'). In
// order to try to prevent the breakage of the app, try to fail on an
// invalid URL as soon as possible.
const { hostname, pathname, protocol } = new URL(url);
// XXX The standard URL implementation should throw an Error if the
// specified URL is relative. Unfortunately, the polyfill used on
// react-native does not.
if (!hostname || !pathname || !protocol) {
throw new Error(`unexpected url: ${url}`);
}
const controller = new AbortController();
const signal = controller.signal;
const timer = setTimeout(() => {
controller.abort();
}, timeout);
const response = await fetch(url, { signal });
// If the timeout hits the above will raise AbortError.
clearTimeout(timer);
switch (response.status) {
case 200: {
const txt = await response.text();
if (skipEval) {
return txt;
}
return eval.call(window, txt); // eslint-disable-line no-eval
}
default:
throw new Error(`loadScript error: ${response.statusText}`);
}
}

View File

@@ -0,0 +1,18 @@
/**
* Loads a script from a specific URL. The script will be interpreted upon load.
*
* @param {string} url - The url to be loaded.
* @returns {Promise} Resolved with no arguments when the script is loaded and
* rejected with the error from JitsiMeetJS.ScriptUtil.loadScript method.
*/
export function loadScript(url: string): Promise<void> {
return new Promise((resolve, reject) =>
JitsiMeetJS.util.ScriptUtil.loadScript(
{ src: url,
async: true,
prepend: false,
relativeURL: false,
loadCallback: resolve,
errorCallback: reject
}));
}

View File

@@ -0,0 +1,3 @@
import { getLogger } from '../logging/functions';
export default getLogger('features/base/util');

View File

@@ -0,0 +1,37 @@
/**
* Compute the greatest common divisor using Euclid's algorithm.
*
* @param {number} num1 - First number.
* @param {number} num2 - Second number.
* @returns {number}
*/
export function greatestCommonDivisor(num1: number, num2: number) {
let number1: number = num1;
let number2: number = num2;
while (number1 !== number2) {
if (number1 > number2) {
number1 = number1 - number2;
} else {
number2 = number2 - number1;
}
}
return number2;
}
/**
* Calculate least common multiple using gcd.
*
* @param {number} num1 - First number.
* @param {number} num2 - Second number.
* @returns {number}
*/
export function leastCommonMultiple(num1: number, num2: number) {
const number1: number = num1;
const number2: number = num2;
const gcd: number = greatestCommonDivisor(number1, number2);
return (number1 * number2) / gcd;
}

View File

@@ -0,0 +1,79 @@
/**
* Interface representing a message that can be grouped.
* Used by both chat messages and subtitles.
*/
export interface IGroupableMessage {
/**
* The ID of the participant who sent the message.
*/
participantId: string;
}
/**
* Interface representing a group of messages from the same sender.
*
* @template T - The type of messages in the group, must extend IGroupableMessage.
*/
export interface IMessageGroup<T extends IGroupableMessage> {
/**
* Array of messages in this group.
*/
messages: T[];
/**
* The ID of the participant who sent all messages in this group.
*/
senderId: string;
}
/**
* Groups an array of messages by sender.
*
* @template T - The type of messages to group, must extend IGroupableMessage.
* @param {T[]} messages - The array of messages to group.
* @returns {IMessageGroup<T>[]} - An array of message groups, where each group contains messages from the same sender.
* @example
* const messages = [
* { participantId: "user1", timestamp: 1000 },
* { participantId: "user1", timestamp: 2000 },
* { participantId: "user2", timestamp: 3000 }
* ];
* const groups = groupMessagesBySender(messages);
* // Returns:
* // [
* // {
* // senderId: "user1",
* // messages: [
* // { participantId: "user1", timestamp: 1000 },
* // { participantId: "user1", timestamp: 2000 }
* // ]
* // },
* // { senderId: "user2", messages: [{ participantId: "user2", timestamp: 3000 }] }
* // ]
*/
export function groupMessagesBySender<T extends IGroupableMessage>(
messages: T[]
): IMessageGroup<T>[] {
if (!messages?.length) {
return [];
}
const groups: IMessageGroup<T>[] = [];
let currentGroup: IMessageGroup<T> | null = null;
for (const message of messages) {
if (!currentGroup || currentGroup.senderId !== message.participantId) {
currentGroup = {
messages: [ message ],
senderId: message.participantId
};
groups.push(currentGroup);
} else {
currentGroup.messages.push(message);
}
}
return groups;
}

View File

@@ -0,0 +1,16 @@
import { Linking } from 'react-native';
import logger from './logger';
/**
* Opens URL in the browser.
*
* @param {string} url - The URL to be opened.
* @param {boolean} _ignore - Ignored.
* @returns {void}
*/
export function openURLInBrowser(url: string, _ignore?: boolean) {
Linking.openURL(url).catch(error => {
logger.error(`An error occurred while trying to open ${url}`, error);
});
}

View File

@@ -0,0 +1,12 @@
/**
* Opens URL in the browser.
*
* @param {string} url - The URL to be opened.
* @param {boolean} openInNewTab - If the link should be opened in a new tab.
* @returns {void}
*/
export function openURLInBrowser(url: string, openInNewTab?: boolean) {
const target = openInNewTab ? '_blank' : '';
window.open(url, target, 'noopener');
}

View File

@@ -0,0 +1,80 @@
// @ts-ignore
import { safeJsonParse } from '@jitsi/js-utils/json';
import { reportError } from './helpers';
/**
* A list if keys to ignore when parsing.
*
* @type {string[]}
*/
const blacklist = [ '__proto__', 'constructor', 'prototype' ];
/**
* Parses the query/search or fragment/hash parameters out of a specific URL and
* returns them as a JS object.
*
* @param {URL} url - The URL to parse.
* @param {boolean} dontParse - If falsy, some transformations (for parsing the
* value as JSON) will be executed.
* @param {string} source - If {@code 'search'}, the parameters will parsed out
* of {@code url.search}; otherwise, out of {@code url.hash}.
* @returns {Object}
*/
export function parseURLParams(
url: URL | string,
dontParse = false,
source = 'hash') {
if (!url) {
return {};
}
if (typeof url === 'string') {
// eslint-disable-next-line no-param-reassign
url = new URL(url);
}
const paramStr = source === 'search' ? url.search : url.hash;
const params: any = {};
const paramParts = paramStr?.substr(1).split('&') || [];
// Detect and ignore hash params for hash routers.
if (source === 'hash' && paramParts.length === 1) {
const firstParam = paramParts[0];
if (firstParam.startsWith('/') && firstParam.split('&').length === 1) {
return params;
}
}
paramParts.forEach((part: string) => {
const param = part.split('=');
const key = param[0];
if (!key || key.split('.').some((k: string) => blacklist.includes(k))) {
return;
}
let value;
try {
value = param[1];
if (!dontParse) {
const decoded = decodeURIComponent(value).replace(/\\&/, '&')
.replace(/[\u2018\u2019]/g, '\'')
.replace(/[\u201C\u201D]/g, '"');
value = decoded === 'undefined' ? undefined : safeJsonParse(decoded);
}
} catch (e: any) {
reportError(
e, `Failed to parse URL parameter value: ${String(value)}`);
return;
}
params[key] = value;
});
return params;
}

View File

@@ -0,0 +1,16 @@
import { IReduxState } from '../../app/types';
/**
* Checks if Jitsi Meet is running on Spot TV.
*
* @param {IReduxState} state - The redux state.
* @returns {boolean} Whether or not Jitsi Meet is running on Spot TV.
*/
export function isSpotTV(state: IReduxState): boolean {
const { defaultLocalDisplayName, iAmSpot } = state['features/base/config'] || {};
return iAmSpot
|| navigator.userAgent.includes('JitsiSpot/') // Jitsi Spot app
|| navigator.userAgent.includes('8x8MeetingRooms/') // 8x8 Meeting Rooms app
|| defaultLocalDisplayName === 'Meeting Room';
}

View File

@@ -0,0 +1,23 @@
import * as unorm from 'unorm';
/**
* Applies NFKC normalization to the given text.
* NOTE: Here we use the unorm package because the JSC version in React Native for Android crashes.
*
* @param {string} text - The text that needs to be normalized.
* @returns {string} - The normalized text.
*/
export function normalizeNFKC(text: string) {
return unorm.nfkc(text);
}
/**
* Replaces accent characters with english alphabet characters.
* NOTE: Here we use the unorm package because the JSC version in React Native for Android crashes.
*
* @param {string} text - The text that needs to be normalized.
* @returns {string} - The normalized text.
*/
export function normalizeAccents(text: string) {
return unorm.nfd(text).replace(/[\u0300-\u036f]/g, '');
}

View File

@@ -0,0 +1,20 @@
/**
* Applies NFKC normalization to the given text.
*
* @param {string} text - The text that needs to be normalized.
* @returns {string} - The normalized text.
*/
export function normalizeNFKC(text: string) {
return text.normalize('NFKC');
}
/**
* Replaces accent characters with english alphabet characters.
* NOTE: Here we use the unorm package because the JSC version in React Native for Android crashes.
*
* @param {string} text - The text that needs to be normalized.
* @returns {string} - The normalized text.
*/
export function normalizeAccents(text: string) {
return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

View File

@@ -0,0 +1,34 @@
/**
* Returns a new {@code Promise} which settles when a specific {@code Promise}
* settles and is automatically rejected if the specified {@code Promise}
* doesn't settle within a specific time interval.
*
* @param {Promise} promise - The {@code Promise} for which automatic rejecting
* after the specified timeout is to be implemented.
* @param {number} timeout - The number of milliseconds to wait the specified
* {@code promise} to settle before automatically rejecting the returned
* {@code Promise}.
* @returns {Promise} - A new {@code Promise} which settles when the specified
* {@code promise} settles and is automatically rejected after {@code timeout}
* milliseconds.
*/
export function timeoutPromise<T>(
promise: Promise<T>,
timeout: number
): Promise<T> {
return new Promise((resolve, reject) => {
const timeoutID
= setTimeout(() => reject(new Error('timeout')), timeout);
promise.then(
/* onFulfilled */ value => {
resolve(value);
clearTimeout(timeoutID);
},
/* onRejected */ reason => {
reject(reason);
clearTimeout(timeoutID);
}
);
});
}

View File

@@ -0,0 +1,710 @@
import { sanitizeUrl as _sanitizeUrl } from '@braintree/sanitize-url';
import { parseURLParams } from './parseURLParams';
import { normalizeNFKC } from './strings';
/**
* Http status codes.
*/
export enum StatusCode {
PaymentRequired = 402
}
/**
* The app linking scheme.
* TODO: This should be read from the manifest files later.
*/
export const APP_LINK_SCHEME = 'org.jitsi.meet:';
/**
* 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 = '[\\:\\?#\\[\\]@!$&\'()*+,;=></"]';
/**
* 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 image data scheme.
*
* @private
* @type {RegExp}
*/
const IMG_DATA_URL: RegExp = /^data:image\/[a-z0-9\-.+]+;base64,/i;
/**
* The {@link RegExp} pattern of the protocol of a URI.
*
* 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:
* the DeepLinkingMobilePage.js) the APP_LINK_SCHEME does not include
* the double dots, so things are inconsistent.
*
* @type {string}
*/
export const URI_PROTOCOL_PATTERN = '^([a-z][a-z0-9\\.\\+-]*:)';
/**
* 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.
*
* @param {?string} pathPart - The path part to fix.
* @private
* @returns {?string}
*/
function _fixPathPart(pathPart?: string) {
return pathPart
? pathPart.replace(new RegExp(_ROOM_EXCLUDE_PATTERN, 'g'), '')
: pathPart;
}
/**
* 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) {
const regex = new RegExp(`${URI_PROTOCOL_PATTERN}+`, 'gi');
const match: Array<string> | null = regex.exec(uri);
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;
}
/**
* 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?}
*/
export function getBackendSafePath(path?: string): string | undefined {
if (!path) {
return path;
}
return path
.split('/')
.map(getBackendSafeRoomName)
.join('/');
}
/**
* Converts a room name to a backend-safe format. Properly lowercased and url encoded.
*
* @param {string?} room - The room name to convert.
* @returns {string?}
*/
export function getBackendSafeRoomName(room?: string): string | undefined {
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 going on...
}
// Normalize the character set.
room = normalizeNFKC(room);
// Only decoded and normalized strings can be lowercased properly.
room = room?.toLowerCase();
// But we still need to (re)encode it.
room = encodeURIComponent(room ?? '');
/* 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();
}
/**
* 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).
*/
export function getLocationContextRoot({ pathname }: { pathname: string; }) {
const contextRootEndIndex = pathname.lastIndexOf('/');
return (
contextRootEndIndex === -1
? '/'
: pathname.substring(0, contextRootEndIndex + 1));
}
/**
* 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(
`${key}=${encodeURIComponent(JSON.stringify(obj[key as keyof typeof obj]))}`);
} catch (e) {
console.warn(`Error encoding ${key}: ${e}`);
}
}
return params;
}
/**
* 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 */
const obj: { [key: string]: any; } = {
toString: _standardURIToString
};
let regex;
let match: Array<string> | null;
// 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.
str = str.replace(/\s/g, '');
// protocol
regex = new RegExp(URI_PROTOCOL_PATTERN, 'gi');
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) {
let authority: string = match[1].substring(/* // */ 2);
str = str.substring(regex.lastIndex);
// userinfo
const userinfoEndIndex = authority.indexOf('@');
if (userinfoEndIndex !== -1) {
authority = authority.substring(userinfoEndIndex + 1);
}
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);
let pathname: string | undefined;
if (match) {
pathname = match[1];
str = str.substring(regex.lastIndex);
}
if (pathname) {
pathname.startsWith('/') || (pathname = `/${pathname}`);
} 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 {{
* contextRoot: string,
* hash: string,
* host: string,
* hostname: string,
* pathname: string,
* port: string,
* protocol: string,
* room: (string|undefined),
* search: string
* }}
*/
export function parseURIString(uri?: string): any {
if (typeof uri !== 'string') {
return undefined;
}
const obj = parseStandardURIString(_fixURIStringScheme(uri));
// 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.
obj.pathname = obj.pathname.split('/').map((pathPart: any) => _fixPathPart(pathPart))
.join('/');
// Add the properties that are specific to a Jitsi Meet resource (location)
// such as contextRoot, room:
// contextRoot
// @ts-ignore
obj.contextRoot = getLocationContextRoot(obj);
// The room (name) is the last component/segment of pathname.
const { pathname } = obj;
const contextRootEndIndex = pathname.lastIndexOf('/');
obj.room = pathname.substring(contextRootEndIndex + 1) || undefined;
if (contextRootEndIndex > 1) {
// The part of the pathname from the beginning to the room name is the tenant.
obj.tenant = pathname.substring(1, contextRootEndIndex);
}
return obj;
}
/**
* 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}
*/
function _standardURIToString(thiz?: Object) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/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;
}
/**
* 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;
}
/**
* 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.
*
* @param {Object|string} obj - The URL to return a {@code String}
* representation of.
* @returns {string} - A {@code String} representation of the specified
* {@code obj} which is supposed to represent a URL.
*/
export function toURLString(obj?: (Object | string)) {
let str;
switch (typeof obj) {
case 'object':
if (obj) {
if (obj instanceof URL) {
str = obj.href;
} else {
str = urlObjectToString(obj);
}
}
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.
*
* @param {Object} o - The URL to return a {@code String} representation of.
* @returns {string} - A {@code String} representation of the specified
* {@code Object}.
*/
export function urlObjectToString(o: { [key: string]: any; }): string | undefined {
// 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));
// protocol
if (!url.protocol) {
let protocol: string | undefined = o.protocol || o.scheme;
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.
const domain: string | undefined = o.domain || o.host || o.hostname;
if (domain) {
const { host, hostname, pathname: contextRoot, port }
= 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.
_fixURIStringScheme(`${APP_LINK_SCHEME}//${domain}`));
// authority
if (host) {
url.host = host;
url.hostname = hostname;
url.port = port;
}
// pathname
pathname === '/' && contextRoot !== '/' && (pathname = contextRoot);
}
}
// pathname
// Web's ExternalAPI roomName
const room = o.roomName || o.room;
if (room
&& (url.pathname.endsWith('/')
|| !url.pathname.endsWith(`/${room}`))) {
pathname.endsWith('/') || (pathname += '/');
pathname += room;
}
url.pathname = pathname;
// query/search
// Web's ExternalAPI jwt and lang
const { jwt, lang, release } = o;
const search = new URLSearchParams(url.search);
// TODO: once all available versions are updated to support the jwt in the hash, remove this
if (jwt) {
search.set('jwt', jwt);
}
const { defaultLanguage } = o.configOverwrite || {};
if (lang || defaultLanguage) {
search.set('lang', lang || defaultLanguage);
}
if (release) {
search.set('release', release);
}
const searchString = search.toString();
if (searchString) {
url.search = `?${searchString}`;
}
// fragment/hash
let { hash } = url;
if (jwt) {
if (hash.length) {
hash = `${hash}&jwt=${JSON.stringify(jwt)}`;
} else {
hash = `#jwt=${JSON.stringify(jwt)}`;
}
}
for (const urlPrefix of [ 'config', 'iceServers', 'interfaceConfig', 'devices', 'userInfo', 'appData' ]) {
const urlParamsArray
= _objectToURLParamsArray(
o[`${urlPrefix}Overwrite`]
|| o[urlPrefix]
|| o[`${urlPrefix}Override`]);
if (urlParamsArray.length) {
let urlParamsString
= `${urlPrefix}.${urlParamsArray.join(`&${urlPrefix}.`)}`;
if (hash.length) {
urlParamsString = `&${urlParamsString}`;
} else {
hash = '#';
}
hash += urlParamsString;
}
}
url.hash = hash;
return url.toString() || undefined;
}
/**
* 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;
}
/**
* Returns the decoded URI.
*
* @param {string} uri - The URI to decode.
* @returns {string}
*/
export function getDecodedURI(uri: string) {
return decodeURI(uri.replace(/^https?:\/\//i, ''));
}
/**
* 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();
}
/**
* Adds new hash 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 appendURLHashParam(url: string, name: string, value: string) {
const newUrl = new URL(url);
const dummyUrl = new URL('https://example.com');
// Copy current hash-parameters without the '#' as search-parameters.
dummyUrl.search = newUrl.hash.substring(1);
// Set or update value with the searchParams-API.
dummyUrl.searchParams.append(name, value);
// Write back as hash parameters.
newUrl.hash = dummyUrl.searchParams.toString();
return newUrl.toString();
}
/**
* Sanitizes the given URL so that it's safe to use. If it's unsafe, null is returned.
*
* @param {string|URL} url - The URL that needs to be sanitized.
*
* @returns {URL?} - The sanitized URL, or null otherwise.
*/
export function sanitizeUrl(url?: string | URL): URL | null {
if (!url) {
return null;
}
const urlStr = url.toString();
const result = _sanitizeUrl(urlStr);
if (result === 'about:blank') {
return null;
}
return new URL(result);
}
/**
* Check whether the given url is a valid image data url.
*
* @param {string} url - The url to check.
* @returns {boolean} True if the url is a valid image data url, false otherwise.
*/
export function isImageDataURL(url: string): boolean {
return IMG_DATA_URL.test(url);
}