From c511902356434b53f7e05617c7195871383e6897 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 25 Sep 2018 17:55:41 +0100 Subject: [PATCH 01/19] Support WebAssembly version of Olm. * Olm no longer supports setting the stack/memory size at runtime, so don't (they're now set to be that in the Olm build). * Copy the wasm file from the Olm library (see multiple comments about it being in the wrong place and webpack being awful). --- scripts/copy-res.js | 4 ++++ src/vector/olm-loader.js | 18 ++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/scripts/copy-res.js b/scripts/copy-res.js index 70a06411d9..43b7768864 100755 --- a/scripts/copy-res.js +++ b/scripts/copy-res.js @@ -57,6 +57,10 @@ const COPY_LIST = [ ["res/themes/**", "webapp/themes"], ["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"], ["node_modules/emojione/assets/png/*", "webapp/emojione/png/"], + // XXX: This is tied quite heavily to the matching olm.js so it really should be + // in the bundle dir with the js to avoid caching issues giving us wasm that + // doesn't match our js, but I cannot find any way to get webpack to do this. + ["node_modules/olm/olm.wasm", "webapp"], ["./config.json", "webapp", { directwatch: 1 }], ]; diff --git a/src/vector/olm-loader.js b/src/vector/olm-loader.js index a62d05b40e..29c83653a4 100644 --- a/src/vector/olm-loader.js +++ b/src/vector/olm-loader.js @@ -1,5 +1,6 @@ /* Copyright 2016 OpenMarket Ltd +Copyright 2018 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,6 +15,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import olm from 'olm/olm.js'; + /* a very thin shim for loading olm.js: just sets the global OLM_OPTIONS and * requires the actual olm.js library. * @@ -26,16 +29,11 @@ limitations under the License. * before olm.js is loaded. */ -/* total_memory must be a power of two, and at least twice the stack. - * - * We don't need a lot of stack, but we do need about 128K of heap to encrypt a - * 64K event (enough to store the ciphertext and the plaintext, bearing in mind - * that the plaintext can only be 48K because base64). We also have about 36K - * of statics. So let's have 256K of memory. +/* 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, + * to avoid caching issues, but as far as I can tell this is completely impossible + * with webpack. */ global.OLM_OPTIONS = { - TOTAL_STACK: 64*1024, - TOTAL_MEMORY: 256*1024, + locateFile: () => 'olm.wasm', }; - -require('olm/olm.js'); From 1f34d2d6440ba8a8b025459e75b7079ec358b2af Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 26 Sep 2018 16:44:52 +0100 Subject: [PATCH 02/19] Kill off olm-loader Now that Olm needs to be inited asynchronously anyway, we can just pass the options to Olm.init(), and as long as we do that before we start the js-sdk, we're all good. This will means the olm js is now part of the main bundle but since it's now just a wrapper around the wasm, this is probably faster. Also add the directwatch flag to olm.wasm because otherwise it doesn't seem to copy the file in watch mode... --- scripts/copy-res.js | 2 +- src/vector/index.js | 16 ++++++++++++++++ src/vector/olm-loader.js | 39 --------------------------------------- webpack.config.js | 18 ------------------ 4 files changed, 17 insertions(+), 58 deletions(-) delete mode 100644 src/vector/olm-loader.js diff --git a/scripts/copy-res.js b/scripts/copy-res.js index 43b7768864..dc55cb1ad8 100755 --- a/scripts/copy-res.js +++ b/scripts/copy-res.js @@ -60,7 +60,7 @@ const COPY_LIST = [ // XXX: This is tied quite heavily to the matching olm.js so it really should be // in the bundle dir with the js to avoid caching issues giving us wasm that // doesn't match our js, but I cannot find any way to get webpack to do this. - ["node_modules/olm/olm.wasm", "webapp"], + ["node_modules/olm/olm.wasm", "webapp", { directwatch: 1 }], ["./config.json", "webapp", { directwatch: 1 }], ]; diff --git a/src/vector/index.js b/src/vector/index.js index ff6b12856c..e9452f65de 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -54,6 +54,8 @@ import SettingsStore, {SettingLevel} from "matrix-react-sdk/lib/settings/Setting import Tinter from 'matrix-react-sdk/lib/Tinter'; import SdkConfig from "matrix-react-sdk/lib/SdkConfig"; +import Olm from 'olm'; + import rageshake from "matrix-react-sdk/lib/rageshake/rageshake"; import CallHandler from 'matrix-react-sdk/lib/CallHandler'; @@ -227,6 +229,20 @@ async function loadApp() { window.addEventListener('hashchange', onHashChange); + /* Start loading Olm. Note that we *don't* wait for this to load: the + * js-sdk will also call this and actually wait on the promise before it + * tries to use the library. It can be loading its wasm while the rest of + * the app loads though. + * + * 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, to avoid caching issues, but as far as I + * can tell this is completely impossible with webpack. + */ + Olm.init({ + locateFile: () => 'olm.wasm', + }); + await loadLanguage(); const fragparts = parseQsFromFragment(window.location); diff --git a/src/vector/olm-loader.js b/src/vector/olm-loader.js deleted file mode 100644 index 29c83653a4..0000000000 --- a/src/vector/olm-loader.js +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright 2016 OpenMarket Ltd -Copyright 2018 New Vector Ltd - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -import olm from 'olm/olm.js'; - -/* a very thin shim for loading olm.js: just sets the global OLM_OPTIONS and - * requires the actual olm.js library. - * - * olm.js reads global.OLM_OPTIONS and defines global.Olm. The latter is fine for us, - * but we need to prepare the former. - * - * We can't use webpack's definePlugin to do this, because we tell webpack not - * to parse olm.js. We also can't put this code in index.js, because olm and - * index.js are loaded in parallel, and we need to make sure OLM_OPTIONS is set - * before olm.js is loaded. - */ - -/* 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, - * to avoid caching issues, but as far as I can tell this is completely impossible - * with webpack. - */ -global.OLM_OPTIONS = { - locateFile: () => 'olm.wasm', -}; diff --git a/webpack.config.js b/webpack.config.js index 122facb24b..e8f20d29b7 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,15 +15,6 @@ module.exports = { "mobileguide": "./src/vector/mobile_guide/index.js", - // We ship olm.js as a separate lump of javascript. This makes it get - // loaded via a separate