Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
refactor(dynamic-branding): split URL and icon download helpers from redux
Hristo Terezovc4923fb5ff06
diff --git a/react/features/dynamic-branding/buildDynamicBrandingUrl.ts b/react/features/dynamic-branding/buildDynamicBrandingUrl.tsnew file mode 100644index 0000000000..45a67d38f4--- /dev/null+++ b/react/features/dynamic-branding/buildDynamicBrandingUrl.ts@@ -0,0 +1,36 @@+import { IConfig } from '../base/config/configType';++/**+ * Extracts the fqn part from a pathname, where fqn represents tenant/roomName.+ *+ * @param {string} pathname - The pathname of the conference URL.+ * @returns {string}+ */+export function extractFqnFromPathname(pathname: string): string {+ const parts = pathname.split('/');+ const len = parts.length;++ return parts.length > 2 ? `${parts[len - 2]}/${parts[len - 1]}` : parts[1];+}++/**+ * Builds the URL used for fetching dynamic branding. Kept free of redux so the preload bundle,+ * which runs before the app bundle, can compute exactly the same URL as the app.+ *+ * @param {Object} config - The config options that decide where the branding comes from.+ * @param {string} fqn - The conference fqn (tenant/roomName).+ * @returns {string | undefined} The branding URL, or undefined when no branding is configured.+ */+export function buildDynamicBrandingUrl(+ { brandingDataUrl, dynamicBrandingUrl }: Pick<IConfig, 'brandingDataUrl' | 'dynamicBrandingUrl'>,+ fqn: string): string | undefined {+ if (dynamicBrandingUrl) {+ return dynamicBrandingUrl;+ }++ if (brandingDataUrl && fqn) {+ return `${brandingDataUrl}?conferenceFqn=${encodeURIComponent(fqn)}`;+ }++ return undefined;+}diff --git a/react/features/dynamic-branding/customIconMarkup.ts b/react/features/dynamic-branding/customIconMarkup.tsnew file mode 100644index 0000000000..f3507b581d--- /dev/null+++ b/react/features/dynamic-branding/customIconMarkup.ts@@ -0,0 +1,37 @@+import { CUSTOM_ICON_FETCH_TIMEOUT } from './constants';++/**+ * Tells whether a custom icon value is inline SVG markup rather than a URL to fetch it from.+ * Inline markup lets the branding payload carry the icons themselves, saving one request per icon.+ *+ * @param {string} value - The custom icon value from the branding data.+ * @returns {boolean}+ */+export function isInlineSvg(value: string): boolean {+ return value.trimStart().startsWith('<');+}++/**+ * Downloads the raw markup of a custom icon, giving up after {@link CUSTOM_ICON_FETCH_TIMEOUT}.+ * Sanitizing is left to the caller so this can also run in the preload bundle, which has to stay+ * small.+ *+ * @param {string} url - The URL of the SVG.+ * @returns {Promise<string>} The raw SVG markup.+ */+export async function fetchCustomIconMarkup(url: string): Promise<string> {+ const controller = new AbortController();+ const timeout = setTimeout(() => controller.abort(), CUSTOM_ICON_FETCH_TIMEOUT);++ try {+ const response = await fetch(url, { signal: controller.signal });++ if (!response.ok) {+ throw new Error(`Unexpected status ${response.status}`);+ }++ return await response.text();+ } finally {+ clearTimeout(timeout);+ }+}diff --git a/react/features/dynamic-branding/functions.any.ts b/react/features/dynamic-branding/functions.any.tsindex 8528408ab0..8f8c0d6828 100644--- a/react/features/dynamic-branding/functions.any.ts+++ b/react/features/dynamic-branding/functions.any.ts@@ -2,7 +2,8 @@ import { IReduxState } from '../app/types';import { IStateful } from '../base/app/types';import { toState } from '../base/redux/functions';-import { CUSTOM_ICON_FETCH_TIMEOUT } from './constants';+import { buildDynamicBrandingUrl, extractFqnFromPathname } from './buildDynamicBrandingUrl';+import { fetchCustomIconMarkup, isInlineSvg } from './customIconMarkup';import { cleanSvg } from './functions';import logger from './logger';@@ -24,10 +25,7 @@ export function extractFqnFromPath(state?: IReduxState) {return '';}- const parts = pathname.split('/');- const len = parts.length;-- return parts.length > 2 ? `${parts[len - 2]}/${parts[len - 1]}` : parts[1];+ return extractFqnFromPathname(pathname);}/**@@ -40,19 +38,7 @@ export function extractFqnFromPath(state?: IReduxState) {export async function getDynamicBrandingUrl(stateful: IStateful) {const state = toState(stateful);- const config = state['features/base/config'];- const { dynamicBrandingUrl } = config;-- if (dynamicBrandingUrl) {- return dynamicBrandingUrl;- }-- const { brandingDataUrl: baseUrl } = config;- const fqn = extractFqnFromPath(state);-- if (baseUrl && fqn) {- return `${baseUrl}?conferenceFqn=${encodeURIComponent(fqn)}`;- }+ return buildDynamicBrandingUrl(state['features/base/config'], extractFqnFromPath(state));}/**@@ -67,9 +53,8 @@ export function isDynamicBrandingDataLoaded(state: IReduxState) {/*** Loads the SVG content of the given branding icons. The icons are requested in parallel and- * each request is bounded by {@link CUSTOM_ICON_FETCH_TIMEOUT}, so a slow or dead URL delays- * neither the other icons nor the rest of the branding. Icons that fail to load are logged- * and left out of the result.+ * each request is bounded by a timeout, so a slow or dead URL delays neither the other icons nor+ * the rest of the branding. Icons that fail to load are logged and left out of the result.** @param {Record<string, string>} customIcons - Map of icon name to SVG URL or inline SVG markup.* @returns {Promise<Record<string, string>>} Map of icon name to sanitized SVG XML.@@ -92,41 +77,13 @@ export async function fetchCustomIcons(customIcons: Record<string, string>): Proreturn localCustomIcons;}-/**- * Tells whether a custom icon value is inline SVG markup rather than a URL to fetch it from.- * Inline markup lets the branding payload carry the icons themselves, saving one request per icon.- *- * @param {string} value - The custom icon value from the branding data.- * @returns {boolean}- */-export function isInlineSvg(value: string): boolean {- return value.trimStart().startsWith('<');-}-/*** Resolves a single custom icon to sanitized SVG XML. Inline markup is used as is; anything else- * is treated as a URL and downloaded, giving up after {@link CUSTOM_ICON_FETCH_TIMEOUT}.+ * is treated as a URL and downloaded.** @param {string} value - The SVG URL or inline SVG markup.* @returns {Promise<string>} The sanitized SVG XML.*/async function loadCustomIcon(value: string): Promise<string> {- if (isInlineSvg(value)) {- return cleanSvg(value);- }-- const controller = new AbortController();- const timeout = setTimeout(() => controller.abort(), CUSTOM_ICON_FETCH_TIMEOUT);-- try {- const response = await fetch(value, { signal: controller.signal });-- if (!response.ok) {- throw new Error(`Unexpected status ${response.status}`);- }-- return cleanSvg(await response.text());- } finally {- clearTimeout(timeout);- }+ return cleanSvg(isInlineSvg(value) ? value : await fetchCustomIconMarkup(value));}