Skip to content

Commit 9b80aa7

Browse files
authored
Merge pull request element-hq#7385 from vector-im/dbkr/wasm
Support WebAssembly version of Olm
2 parents d080a20 + bc93501 commit 9b80aa7

5 files changed

Lines changed: 46 additions & 60 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"webpack-dev-server": "^3.1.9"
147147
},
148148
"optionalDependencies": {
149-
"olm": "https://matrix.org/packages/npm/olm/olm-2.2.1.tgz"
149+
"olm": "https://matrix.org/packages/npm/olm/olm-3.0.0.tgz"
150150
},
151151
"build": {
152152
"appId": "im.riot.app",

‎scripts/copy-res.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const COPY_LIST = [
5757
["res/themes/**", "webapp/themes"],
5858
["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"],
5959
["node_modules/emojione/assets/png/*", "webapp/emojione/png/"],
60+
// XXX: This is tied quite heavily to the matching olm.js so it really should be
61+
// in the bundle dir with the js to avoid caching issues giving us wasm that
62+
// doesn't match our js, but I cannot find any way to get webpack to do this.
63+
["node_modules/olm/olm.wasm", "webapp", { directwatch: 1 }],
64+
["node_modules/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
6065
["./config.json", "webapp", { directwatch: 1 }],
6166
];
6267

‎src/vector/index.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/Setting
5454
import Tinter from 'matrix-react-sdk/lib/Tinter';
5555
import SdkConfig from "matrix-react-sdk/lib/SdkConfig";
5656

57+
import Olm from 'olm';
58+
5759
import rageshake from "matrix-react-sdk/lib/rageshake/rageshake";
5860

5961
import CallHandler from 'matrix-react-sdk/lib/CallHandler';
@@ -229,6 +231,8 @@ async function loadApp() {
229231

230232
window.addEventListener('hashchange', onHashChange);
231233

234+
await loadOlm();
235+
232236
await loadLanguage();
233237

234238
const fragparts = parseQsFromFragment(window.location);
@@ -362,6 +366,42 @@ async function loadApp() {
362366
}
363367
}
364368

369+
function loadOlm() {
370+
/* Load Olm. We try the WebAssembly version first, and then the legacy,
371+
* asm.js version if that fails. For this reason we need to wait for this
372+
* to finish before continuing to load the rest of the app. In future
373+
* we could somehow pass a promise down to react-sdk and have it wait on
374+
* that so olm can be loading in parallel with the rest of the app.
375+
*
376+
* We also need to tell the Olm js to look for its wasm file at the same
377+
* level as index.html. It really should be in the same place as the js,
378+
* ie. in the bundle directory, to avoid caching issues, but as far as I
379+
* can tell this is completely impossible with webpack.
380+
*/
381+
return Olm.init({
382+
locateFile: () => 'olm.wasm',
383+
}).then(() => {
384+
console.log("Using WebAssembly Olm");
385+
}).catch((e) => {
386+
console.log("Failed to load Olm: trying legacy version");
387+
return new Promise((resolve, reject) => {
388+
const s = document.createElement('script');
389+
s.src = 'olm_legacy.js';
390+
s.onload = resolve;
391+
s.onerror = reject;
392+
document.body.appendChild(s);
393+
}).then(() => {
394+
// Init window.Olm, ie. the one just loaded by the script tag,
395+
// not 'Olm' which is still the failed wasm version.
396+
return window.Olm.init();
397+
}).then(() => {
398+
console.log("Using legacy Olm");
399+
}).catch((e) => {
400+
console.log("Both WebAssembly and asm.js Olm failed!", e);
401+
});
402+
});
403+
}
404+
365405
async function loadLanguage() {
366406
const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true);
367407
let langs = [];

‎src/vector/olm-loader.js‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎webpack.config.js‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ module.exports = {
1515

1616
"mobileguide": "./src/vector/mobile_guide/index.js",
1717

18-
// We ship olm.js as a separate lump of javascript. This makes it get
19-
// loaded via a separate <script/> tag in index.html (which loads it
20-
// into the browser global `Olm`, where js-sdk expects to find it).
21-
//
22-
// (we should probably make js-sdk load it asynchronously at some
23-
// point, so that it doesn't block the pageload, but that is a separate
24-
// problem)
25-
"olm": "./src/vector/olm-loader.js",
26-
2718
// CSS themes
2819
"theme-light": "./node_modules/matrix-react-sdk/res/themes/light/css/light.scss",
2920
"theme-dark": "./node_modules/matrix-react-sdk/res/themes/dark/css/dark.scss",
@@ -171,12 +162,3 @@ module.exports = {
171162
inline: false,
172163
},
173164
};
174-
175-
// olm is an optional dependency. Ignore it if it's not installed, to avoid a
176-
// scary-looking error.
177-
try {
178-
require('olm');
179-
} catch (e) {
180-
console.log("Olm is not installed; not shipping it");
181-
delete(module.exports.entry["olm"]);
182-
}

0 commit comments

Comments
 (0)