From 92fdbe15df179fe6f1d4015e266b6046ad644319 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Aug 2020 13:24:04 +0200 Subject: [PATCH 1/3] pass olm paths to main fn so build can adjust the file paths, and we can prevent olm from loading by not passing them --- index.html | 6 +++++- scripts/build.mjs | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index cdf0ad4f..b09286a0 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,11 @@ ` + + `` + `` + - ``); + ``); removeOrEnableScript(doc("script#service-worker"), offline); const versionScript = doc("script#version"); @@ -338,7 +346,7 @@ async function copyFolder(srcRoot, dstRoot, filter) { if (dirEnt.isDirectory()) { await fs.mkdir(dstPath); Object.assign(assetPaths, await copyFolder(srcPath, dstPath, filter)); - } else if (dirEnt.isFile() && filter(srcPath)) { + } else if ((dirEnt.isFile() || dirEnt.isSymbolicLink()) && (!filter || filter(srcPath))) { const content = await fs.readFile(srcPath); const hashedDstPath = resource(dstPath, content); await fs.writeFile(hashedDstPath, content); From fe0257bca10b3d1a08c70c143f9c6094a15e04c1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Aug 2020 13:24:55 +0200 Subject: [PATCH 2/3] load olm and pass it to session --- src/main.js | 27 ++++++++++++++++++++++++++- src/matrix/Session.js | 3 ++- src/matrix/SessionContainer.js | 6 ++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main.js b/src/main.js index 2c86b9d9..85274948 100644 --- a/src/main.js +++ b/src/main.js @@ -26,10 +26,34 @@ import {BrawlView} from "./ui/web/BrawlView.js"; import {Clock} from "./ui/web/dom/Clock.js"; import {OnlineStatus} from "./ui/web/dom/OnlineStatus.js"; +function addScript(src) { + return new Promise(function (resolve, reject) { + var s = document.createElement("script"); + s.setAttribute("src", src ); + s.onload=resolve; + s.onerror=reject; + document.body.appendChild(s); + }); +} + +async function loadOlm(olmPaths) { + if (olmPaths) { + if (window.WebAssembly) { + await addScript(olmPaths.wasmBundle); + await window.Olm.init({locateFile: () => olmPaths.wasm}); + } else { + await addScript(olmPaths.legacyBundle); + await window.Olm.init(); + } + return window.Olm; + } + return null; +} + // Don't use a default export here, as we use multiple entries during legacy build, // which does not support default exports, // see https://github.com/rollup/plugins/tree/master/packages/multi-entry -export async function main(container) { +export async function main(container, olmPaths) { try { // to replay: // const fetchLog = await (await fetch("/fetchlogs/constrainterror.json")).json(); @@ -59,6 +83,7 @@ export async function main(container) { sessionInfoStorage, request, clock, + olmPromise: loadOlm(olmPaths), }); }, sessionInfoStorage, diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 7a7dea52..f6db9c97 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -21,7 +21,7 @@ import {User} from "./User.js"; export class Session { // sessionInfo contains deviceId, userId and homeServer - constructor({storage, hsApi, sessionInfo}) { + constructor({storage, hsApi, sessionInfo, olm}) { this._storage = storage; this._hsApi = hsApi; this._session = null; @@ -30,6 +30,7 @@ export class Session { this._sendScheduler = new SendScheduler({hsApi, backoff: new RateLimitingBackoff()}); this._roomUpdateCallback = (room, params) => this._rooms.update(room.id, params); this._user = new User(sessionInfo.userId); + this._olm = olm; } async load() { diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 74184a0a..eb025ec6 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -41,7 +41,7 @@ export const LoginFailure = createEnum( ); export class SessionContainer { - constructor({clock, random, onlineStatus, request, storageFactory, sessionInfoStorage}) { + constructor({clock, random, onlineStatus, request, storageFactory, sessionInfoStorage, olmPromise}) { this._random = random; this._clock = clock; this._onlineStatus = onlineStatus; @@ -57,6 +57,7 @@ export class SessionContainer { this._sync = null; this._sessionId = null; this._storage = null; + this._olmPromise = olmPromise; } createNewSessionId() { @@ -149,7 +150,8 @@ export class SessionContainer { userId: sessionInfo.userId, homeServer: sessionInfo.homeServer, }; - this._session = new Session({storage: this._storage, sessionInfo: filteredSessionInfo, hsApi}); + const olm = await this._olmPromise; + this._session = new Session({storage: this._storage, sessionInfo: filteredSessionInfo, hsApi, olm}); await this._session.load(); this._sync = new Sync({hsApi, storage: this._storage, session: this._session}); From 87aabb30579136593bbce306cdd61aac5edd2621 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Aug 2020 13:31:14 +0200 Subject: [PATCH 3/3] make crypto.getRandomValues available on IE11 without a prefix olm needs this to work on IE11 --- src/main.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main.js b/src/main.js index 85274948..79f5698d 100644 --- a/src/main.js +++ b/src/main.js @@ -37,6 +37,11 @@ function addScript(src) { } async function loadOlm(olmPaths) { + // make crypto.getRandomValues available without + // a prefix on IE11, needed by olm to work + if (window.msCrypto && !window.crypto) { + window.crypto = window.msCrypto; + } if (olmPaths) { if (window.WebAssembly) { await addScript(olmPaths.wasmBundle);