Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(preload): bound the config.js and branding requests with a timeout

Hristo Terezove231aeca6c6c
diff --git a/react/features/base/util/fetchWithTimeout.ts b/react/features/base/util/fetchWithTimeout.ts
new file mode 100644
index 0000000000..804a64ca07
--- /dev/null
+++ b/react/features/base/util/fetchWithTimeout.ts
@@ -0,0 +1,18 @@
+/**
+ * Fetches a URL, aborting the request if it has not completed within the given time. Kept free of
+ * any dependency so the preload bundle, which runs before the app bundle, can use it as well.
+ *
+ * @param {string} url - The URL to fetch.
+ * @param {number} timeout - Milliseconds after which the request is aborted.
+ * @returns {Promise<Response>} The response. Rejects on a network failure or when the time is up.
+ */
+export async function fetchWithTimeout(url: string, timeout: number): Promise<Response> {
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), timeout);
+
+ try {
+ return await fetch(url, { signal: controller.signal });
+ } finally {
+ clearTimeout(timer);
+ }
+}
diff --git a/react/features/dynamic-branding/customIconMarkup.ts b/react/features/dynamic-branding/customIconMarkup.ts
index f3507b581d..4ff9c633f8 100644
--- a/react/features/dynamic-branding/customIconMarkup.ts
+++ b/react/features/dynamic-branding/customIconMarkup.ts
@@ -1,3 +1,5 @@
+import { fetchWithTimeout } from '../base/util/fetchWithTimeout';
+
import { CUSTOM_ICON_FETCH_TIMEOUT } from './constants';
/**
@@ -20,18 +22,11 @@ export function isInlineSvg(value: string): boolean {
* @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 });
+ const response = await fetchWithTimeout(url, CUSTOM_ICON_FETCH_TIMEOUT);
- if (!response.ok) {
- throw new Error(`Unexpected status ${response.status}`);
- }
-
- return await response.text();
- } finally {
- clearTimeout(timeout);
+ if (!response.ok) {
+ throw new Error(`Unexpected status ${response.status}`);
}
+
+ return response.text();
}
diff --git a/react/features/preload/constants.ts b/react/features/preload/constants.ts
new file mode 100644
index 0000000000..27bbeaa76f
--- /dev/null
+++ b/react/features/preload/constants.ts
@@ -0,0 +1,8 @@
+/**
+ * Milliseconds after which a config.js or branding request started by the preload script is
+ * abandoned. The app waits on these requests instead of making its own, so they must not be able to
+ * hang forever; once one is abandoned the app requests the resource itself. Generous enough not to
+ * cut off a slow but working backend, short enough that a stalled connection is retried while the
+ * user is still waiting.
+ */
+export const PRELOAD_FETCH_TIMEOUT = 10000;
diff --git a/react/features/preload/preload.web.ts b/react/features/preload/preload.web.ts
index 392e2732c4..0f8ce7e720 100644
--- a/react/features/preload/preload.web.ts
+++ b/react/features/preload/preload.web.ts
@@ -1,10 +1,12 @@
import { buildConfigURL } from '../base/config/buildConfigURL';
import { IConfig } from '../base/config/configType';
import { parseConfigInWorker } from '../base/config/parseConfigInWorker';
+import { fetchWithTimeout } from '../base/util/fetchWithTimeout';
import { parseURIString } from '../base/util/uri';
import { buildDynamicBrandingUrl, extractFqnFromPathname } from '../dynamic-branding/buildDynamicBrandingUrl';
import { fetchCustomIconMarkup, isInlineSvg } from '../dynamic-branding/customIconMarkup';
+import { PRELOAD_FETCH_TIMEOUT } from './constants';
import { IJitsiMeetPreload } from './types';
/**
@@ -26,13 +28,15 @@ const preload: IJitsiMeetPreload = {};
window.JitsiMeetPreload = preload;
/**
- * Fetches a URL, rejecting when the response is not successful.
+ * Fetches a URL, rejecting when the response is not successful or does not arrive within
+ * {@link PRELOAD_FETCH_TIMEOUT}. The app waits on the promises published here, so an unbounded
+ * request would stall the app too; after a timeout it falls back to a request of its own.
*
* @param {string} url - The URL to fetch.
* @returns {Promise<Response>}
*/
async function fetchOk(url: string): Promise<Response> {
- const response = await fetch(url);
+ const response = await fetchWithTimeout(url, PRELOAD_FETCH_TIMEOUT);
if (!response.ok) {
throw new Error(`Unexpected status ${response.status}`);