From a0e4de73cc8d1c40c008741053456f6555174fb3 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Tue, 26 Apr 2022 15:20:06 -0700 Subject: [PATCH 1/5] Add support for to-device messages via OLM --- package.json | 1 + public/index.html | 1 + src/IndexedDBWorker.js | 5 +++++ src/matrix-utils.js | 45 ++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 4 ++++ 5 files changed, 56 insertions(+) create mode 100644 src/IndexedDBWorker.js diff --git a/package.json b/package.json index 9471442e..947b255e 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#robertlong/group-call", "mermaid": "^8.13.8", "normalize.css": "^8.0.1", + "olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz", "pako": "^2.0.4", "postcss-preset-env": "^6.7.0", "re-resizable": "^6.9.0", diff --git a/public/index.html b/public/index.html index c6ddea73..50f5fc90 100644 --- a/public/index.html +++ b/public/index.html @@ -15,6 +15,7 @@
+ \ No newline at end of file diff --git a/src/IndexedDBWorker.js b/src/IndexedDBWorker.js new file mode 100644 index 00000000..0e373caf --- /dev/null +++ b/src/IndexedDBWorker.js @@ -0,0 +1,5 @@ +import { IndexedDBStoreWorker } from "matrix-js-sdk/src/indexeddb-worker"; + +const remoteWorker = new IndexedDBStoreWorker(self.postMessage); + +self.onmessage = remoteWorker.onMessage; diff --git a/src/matrix-utils.js b/src/matrix-utils.js index 04ce3dfd..b1201f9f 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -3,6 +3,7 @@ import { GroupCallIntent, GroupCallType, } from "matrix-js-sdk/src/browser-index"; +import IndexedDBWorker from "./IndexedDBWorker?worker"; export const defaultHomeserver = import.meta.env.VITE_DEFAULT_HOMESERVER || @@ -26,11 +27,55 @@ function waitForSync(client) { } export async function initClient(clientOptions) { + let indexedDB; + + try { + indexedDB = window.indexedDB; + } catch (e) {} + + const storeOpts = {}; + + if (indexedDB && localStorage && !import.meta.env.DEV) { + storeOpts.store = new matrix.IndexedDBStore({ + indexedDB: window.indexedDB, + localStorage: window.localStorage, + dbName: "element-call-sync", + workerFactory: () => new IndexedDBWorker(), + }); + } + + if (localStorage) { + storeOpts.sessionStore = new matrix.WebStorageSessionStore(localStorage); + } + + if (indexedDB) { + storeOpts.cryptoStore = new matrix.IndexedDBCryptoStore( + indexedDB, + "matrix-js-sdk:crypto" + ); + } + const client = matrix.createClient({ + ...storeOpts, ...clientOptions, useAuthorizationHeader: true, }); + try { + await client.store.startup(); + } catch (error) { + console.error( + "Error starting matrix client store. Falling back to memory store.", + error + ); + client.store = new matrix.MemoryStore({ localStorage }); + await client.store.startup(); + } + + if (client.initCrypto) { + await client.initCrypto(); + } + await client.startClient({ // dirty hack to reduce chance of gappy syncs // should be fixed by spotting gaps and backpaginating diff --git a/yarn.lock b/yarn.lock index 4c921087..17494759 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8631,6 +8631,10 @@ objectorarray@^1.0.5: resolved "https://registry.yarnpkg.com/objectorarray/-/objectorarray-1.0.5.tgz#2c05248bbefabd8f43ad13b41085951aac5e68a5" integrity sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg== +"olm@https://packages.matrix.org/npm/olm/olm-3.2.1.tgz": + version "3.2.1" + resolved "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz#d623d76f99c3518dde68be8c86618d68bc7b004a" + on-finished@~2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947" From 44486aa62dcfd33f372f6f83b06025319513baeb Mon Sep 17 00:00:00 2001 From: Robert Long Date: Tue, 26 Apr 2022 16:11:32 -0700 Subject: [PATCH 2/5] Fix building olm library in production --- public/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index 50f5fc90..0368b6a1 100644 --- a/public/index.html +++ b/public/index.html @@ -15,7 +15,7 @@
- + \ No newline at end of file From e2aee0be819669ff4bf08313fcaac506eeed42c7 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Tue, 26 Apr 2022 16:28:21 -0700 Subject: [PATCH 3/5] Fix olm import --- src/matrix-utils.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/matrix-utils.js b/src/matrix-utils.js index b1201f9f..a5a0683b 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -4,6 +4,8 @@ import { GroupCallType, } from "matrix-js-sdk/src/browser-index"; import IndexedDBWorker from "./IndexedDBWorker?worker"; +import olmJsPath from "olm/olm.js?url"; +import olmWasmPath from "olm/olm.wasm?url"; export const defaultHomeserver = import.meta.env.VITE_DEFAULT_HOMESERVER || @@ -26,7 +28,20 @@ function waitForSync(client) { }); } +function addScript(src) { + return new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.setAttribute("src", src); + script.onload = resolve; + script.onerror = reject; + document.body.appendChild(script); + }); +} + export async function initClient(clientOptions) { + await addScript(olmJsPath); + await window.Olm.init({ locateFile: () => olmWasmPath }); + let indexedDB; try { From 3d54047f8783590e99c26a6206fbdd5d2b9456e5 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Wed, 27 Apr 2022 13:38:16 -0700 Subject: [PATCH 4/5] Fix Olm import --- package.json | 2 +- public/index.html | 1 - src/matrix-utils.js | 18 ++++-------------- yarn.lock | 8 ++++---- 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 947b255e..11f33c4d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@juggle/resize-observer": "^3.3.1", + "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz", "@react-aria/button": "^3.3.4", "@react-aria/dialog": "^3.1.4", "@react-aria/focus": "^3.5.0", @@ -34,7 +35,6 @@ "matrix-js-sdk": "github:matrix-org/matrix-js-sdk#robertlong/group-call", "mermaid": "^8.13.8", "normalize.css": "^8.0.1", - "olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz", "pako": "^2.0.4", "postcss-preset-env": "^6.7.0", "re-resizable": "^6.9.0", diff --git a/public/index.html b/public/index.html index 0368b6a1..c6ddea73 100644 --- a/public/index.html +++ b/public/index.html @@ -15,7 +15,6 @@
- \ No newline at end of file diff --git a/src/matrix-utils.js b/src/matrix-utils.js index a5a0683b..07e1616e 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -4,8 +4,8 @@ import { GroupCallType, } from "matrix-js-sdk/src/browser-index"; import IndexedDBWorker from "./IndexedDBWorker?worker"; -import olmJsPath from "olm/olm.js?url"; -import olmWasmPath from "olm/olm.wasm?url"; +import Olm from "@matrix-org/olm"; +import olmWasmPath from "@matrix-org/olm/olm.wasm?url"; export const defaultHomeserver = import.meta.env.VITE_DEFAULT_HOMESERVER || @@ -28,19 +28,9 @@ function waitForSync(client) { }); } -function addScript(src) { - return new Promise((resolve, reject) => { - const script = document.createElement("script"); - script.setAttribute("src", src); - script.onload = resolve; - script.onerror = reject; - document.body.appendChild(script); - }); -} - export async function initClient(clientOptions) { - await addScript(olmJsPath); - await window.Olm.init({ locateFile: () => olmWasmPath }); + window.OLM_OPTIONS = {}; + await Olm.init({ locateFile: () => olmWasmPath }); let indexedDB; diff --git a/yarn.lock b/yarn.lock index 17494759..985937a9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1320,6 +1320,10 @@ resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.3.1.tgz#b50a781709c81e10701004214340f25475a171a0" integrity sha512-zMM9Ds+SawiUkakS7y94Ymqx+S0ORzpG3frZirN3l+UlXUmSUR7hF4wxCVqW+ei94JzV5kt0uXBcoOEAuiydrw== +"@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz": + version "3.2.8" + resolved "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz#8d53636d045e1776e2a2ec6613e57330dd9ce856" + "@mdx-js/mdx@^1.6.22": version "1.6.22" resolved "https://registry.yarnpkg.com/@mdx-js/mdx/-/mdx-1.6.22.tgz#8a723157bf90e78f17dc0f27995398e6c731f1ba" @@ -8631,10 +8635,6 @@ objectorarray@^1.0.5: resolved "https://registry.yarnpkg.com/objectorarray/-/objectorarray-1.0.5.tgz#2c05248bbefabd8f43ad13b41085951aac5e68a5" integrity sha512-eJJDYkhJFFbBBAxeh8xW+weHlkI28n2ZdQV/J/DNfWfSKlGEf2xcfAbZTv3riEXHAhL9SVOTs2pRmXiSTf78xg== -"olm@https://packages.matrix.org/npm/olm/olm-3.2.1.tgz": - version "3.2.1" - resolved "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz#d623d76f99c3518dde68be8c86618d68bc7b004a" - on-finished@~2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947" From 7a9ff98550c7b60f9fefd97523b9e53e0a489f80 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Wed, 27 Apr 2022 13:51:08 -0700 Subject: [PATCH 5/5] Add OLM_OPTIONS global TODO --- src/matrix-utils.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/matrix-utils.js b/src/matrix-utils.js index 07e1616e..6132b7d9 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -29,6 +29,7 @@ function waitForSync(client) { } export async function initClient(clientOptions) { + // TODO: https://gitlab.matrix.org/matrix-org/olm/-/issues/10 window.OLM_OPTIONS = {}; await Olm.init({ locateFile: () => olmWasmPath });