From f0703cd30cc4516256494b1a5c751fa3e15de3c0 Mon Sep 17 00:00:00 2001 From: "marc.sirisak" Date: Thu, 1 Aug 2024 15:18:12 +0200 Subject: [PATCH] Revert "Remove code which loads legacy libolm", in Tchap we are still using it for content scanner This reverts commit 7c7cbe655a0cfc0444375665e55c7927719e3326. --- src/vector/index.ts | 3 +++ src/vector/init.tsx | 46 +++++++++++++++++++++++++++++++++++++++++++++ webpack.config.js | 26 ++++++++++++++++++++----- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/vector/index.ts b/src/vector/index.ts index e9d12aa27..3c8a5ad4c 100644 --- a/src/vector/index.ts +++ b/src/vector/index.ts @@ -135,6 +135,7 @@ async function start(): Promise { rageshakePromise, setupLogStorage, preparePlatform, + loadOlm, loadConfig, loadLanguage, loadTheme, @@ -179,6 +180,7 @@ async function start(): Promise { } } + const loadOlmPromise = loadOlm(); // set the platform for react sdk preparePlatform(); // load config requires the platform to be ready @@ -248,6 +250,7 @@ async function start(): Promise { // app load critical path starts here // assert things started successfully // ################################## + await loadOlmPromise; await loadModulesPromise; await loadThemePromise; await loadLanguagePromise; diff --git a/src/vector/init.tsx b/src/vector/init.tsx index 76c238d0e..e83c336bb 100644 --- a/src/vector/init.tsx +++ b/src/vector/init.tsx @@ -17,6 +17,10 @@ See the License for the specific language governing permissions and limitations under the License. */ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +import olmWasmPath from "@matrix-org/olm/olm.wasm"; +import Olm from "@matrix-org/olm"; import * as ReactDOM from "react-dom"; import * as React from "react"; import * as languageHandler from "matrix-react-sdk/src/languageHandler"; @@ -72,6 +76,48 @@ export async function loadConfig(): Promise { } } +export function loadOlm(): Promise { + /* Load Olm. We try the WebAssembly version first, and then the legacy, + * asm.js version if that fails. For this reason we need to wait for this + * to finish before continuing to load the rest of the app. In future + * we could somehow pass a promise down to react-sdk and have it wait on + * that so olm can be loading in parallel with the rest of the app. + * + * We also need to tell the Olm js to look for its wasm file at the same + * level as index.html. It really should be in the same place as the js, + * ie. in the bundle directory, but as far as I can tell this is + * completely impossible with webpack. We do, however, use a hashed + * filename to avoid caching issues. + */ + return Olm.init({ + locateFile: () => olmWasmPath, + }) + .then(() => { + logger.log("Using WebAssembly Olm"); + }) + .catch((wasmLoadError) => { + logger.log("Failed to load Olm: trying legacy version", wasmLoadError); + return new Promise((resolve, reject) => { + const s = document.createElement("script"); + s.src = "olm_legacy.js"; // XXX: This should be cache-busted too + s.onload = resolve; + s.onerror = reject; + document.body.appendChild(s); + }) + .then(() => { + // Init window.Olm, ie. the one just loaded by the script tag, + // not 'Olm' which is still the failed wasm version. + return window.Olm.init(); + }) + .then(() => { + logger.log("Using legacy Olm"); + }) + .catch((legacyLoadError) => { + logger.log("Both WebAssembly and asm.js Olm failed!", legacyLoadError); + }); + }); +} + export async function loadLanguage(): Promise { const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/ true); let langs: string[] = []; diff --git a/webpack.config.js b/webpack.config.js index 97f9b1980..d18c45e9f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -287,6 +287,10 @@ module.exports = (env, argv) => { // there is no need for webpack to parse them - they can just be // included as-is. /highlight\.js[\\/]lib[\\/]languages/, + + // olm takes ages for webpack to process, and it's already heavily + // optimised, so there is little to gain by us uglifying it. + /olm[\\/](javascript[\\/])?olm\.js$/, ], rules: [ useHMR && { @@ -452,6 +456,20 @@ module.exports = (env, argv) => { }, ], }, + { + // the olm library wants to load its own wasm, rather than have webpack do it. + // We therefore use the `file-loader` to tell webpack to dump the contents to + // a separate file and return the name, and override the default `type` for `.wasm` files + // (which is `webassembly/experimental` under webpack 4) to stop webpack trying to interpret + // the filename as webassembly. (see also https://github.com/webpack/webpack/issues/6725) + test: /olm\.wasm$/, + loader: "file-loader", + type: "javascript/auto", + options: { + name: "[name].[hash:7].[ext]", + outputPath: ".", + }, + }, { // Fix up the name of the opus-recorder worker (react-sdk dependency). // We more or less just want it to be clear it's for opus and not something else. @@ -493,11 +511,8 @@ module.exports = (env, argv) => { }, }, { - // The decoderWorker wants to load its own wasm, rather than have webpack do it. - // We therefore use the `file-loader` to tell webpack to dump the contents to - // a separate file and return the name, and override the default `type` for `.wasm` files - // (which is `webassembly/experimental` under webpack 4) to stop webpack trying to interpret - // the filename as webassembly. (see also https://github.com/webpack/webpack/issues/6725) + // Same deal as olm.wasm: the decoderWorker wants to load the wasm artifact + // itself. test: /decoderWorker\.min\.wasm$/, loader: "file-loader", type: "javascript/auto", @@ -748,6 +763,7 @@ module.exports = (env, argv) => { { from: "vector-icons/**", context: path.resolve(__dirname, "res") }, { from: "decoder-ring/**", context: path.resolve(__dirname, "res") }, { from: "media/**", context: path.resolve(__dirname, "node_modules/matrix-react-sdk/res/") }, + "node_modules/@matrix-org/olm/olm_legacy.js", { from: "config.json", noErrorOnMissing: true }, "contribute.json", ],