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(base/config): move the config.js worker parser into its own module
Hristo Terezov67b8a5d3028a
diff --git a/react/features/base/config/parseConfigInWorker.web.ts b/react/features/base/config/parseConfigInWorker.web.tsnew file mode 100644index 0000000000..2f047016c0--- /dev/null+++ b/react/features/base/config/parseConfigInWorker.web.ts@@ -0,0 +1,65 @@+import { safeJsonParse } from '@jitsi/js-utils/json';++const WORKER_SCRIPT = `+self.onmessage = function(e) {+ const configText = e.data;+ try {+ const configObj = eval(+ '(function(){\\n'+ + configText+ + '\\n; return (typeof config !== "undefined" ? config : globalThis.config); })()'+ );++ if (configObj == void 0) {+ self.postMessage({ error: 'config is undefined after eval()' });+ return;+ }++ if (typeof configObj !== 'object') {+ self.postMessage({ error: 'config is not an object' });+ return;+ }++ self.postMessage({ result: JSON.stringify(configObj) });+ } catch (err) {+ self.postMessage({ error: err?.message ?? String(err) });+ }+};+`;++/**+ * Parses config.js text content inside a Web Worker for isolation. Kept free of any other+ * dependency so the preload bundle, which runs before the app bundle, can reuse it as is.+ *+ * @param {string} configText - The raw config.js source text.+ * @returns {Promise<Object>}+ */+export function parseConfigInWorker(configText: string): Promise<Object> {+ return new Promise((resolve, reject) => {+ const blob = new Blob([ WORKER_SCRIPT ]);+ // @ts-ignore -- URL.createObjectURL/revokeObjectURL not typed in this ts-loader context+ const workerUrl = URL.createObjectURL(blob);+ const worker = new Worker(workerUrl);++ worker.onmessage = (e: MessageEvent) => {+ // @ts-ignore+ URL.revokeObjectURL(workerUrl);+ worker.terminate();++ if (e.data.error) {+ reject(new Error(e.data.error));+ } else {+ resolve(safeJsonParse(e.data.result));+ }+ };++ worker.onerror = (err: ErrorEvent) => {+ // @ts-ignore+ URL.revokeObjectURL(workerUrl);+ worker.terminate();+ reject(new Error(err.message));+ };++ worker.postMessage(configText);+ });+}diff --git a/react/features/base/lib-jitsi-meet/functions.web.ts b/react/features/base/lib-jitsi-meet/functions.web.tsindex 23d663ad13..c00a690e28 100644--- a/react/features/base/lib-jitsi-meet/functions.web.ts+++ b/react/features/base/lib-jitsi-meet/functions.web.ts@@ -1,74 +1,10 @@-import { safeJsonParse } from '@jitsi/js-utils/json';-import { IConfig } from '../config/configType';+import { parseConfigInWorker } from '../config/parseConfigInWorker';import logger from './logger';export * from './functions.any';-const WORKER_SCRIPT = `-self.onmessage = function(e) {- const configText = e.data;- try {- const configObj = eval(- '(function(){\\n'- + configText- + '\\n; return (typeof config !== "undefined" ? config : globalThis.config); })()'- );-- if (configObj == void 0) {- self.postMessage({ error: 'config is undefined after eval()' });- return;- }-- if (typeof configObj !== 'object') {- self.postMessage({ error: 'config is not an object' });- return;- }-- self.postMessage({ result: JSON.stringify(configObj) });- } catch (err) {- self.postMessage({ error: err?.message ?? String(err) });- }-};-`;--/**- * Parses config.js text content inside a Web Worker for isolation.- *- * @param {string} configText - The raw config.js source text.- * @returns {Promise<Object>}- */-function parseConfigInWorker(configText: string): Promise<Object> {- return new Promise((resolve, reject) => {- const blob = new Blob([ WORKER_SCRIPT ]);- // @ts-ignore -- URL.createObjectURL/revokeObjectURL not typed in this ts-loader context- const workerUrl = URL.createObjectURL(blob);- const worker = new Worker(workerUrl);-- worker.onmessage = (e: MessageEvent) => {- // @ts-ignore- URL.revokeObjectURL(workerUrl);- worker.terminate();-- if (e.data.error) {- reject(new Error(e.data.error));- } else {- resolve(safeJsonParse(e.data.result));- }- };-- worker.onerror = (err: ErrorEvent) => {- // @ts-ignore- URL.revokeObjectURL(workerUrl);- worker.terminate();- reject(new Error(err.message));- };-- worker.postMessage(configText);- });-}-/*** Loads config.js from a specific URL.*