diff --git a/package.json b/package.json index 9471442e8..11f33c4da 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", diff --git a/src/IndexedDBWorker.js b/src/IndexedDBWorker.js new file mode 100644 index 000000000..0e373caf6 --- /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 04ce3dfd8..6132b7d9a 100644 --- a/src/matrix-utils.js +++ b/src/matrix-utils.js @@ -3,6 +3,9 @@ import { GroupCallIntent, GroupCallType, } from "matrix-js-sdk/src/browser-index"; +import IndexedDBWorker from "./IndexedDBWorker?worker"; +import Olm from "@matrix-org/olm"; +import olmWasmPath from "@matrix-org/olm/olm.wasm?url"; export const defaultHomeserver = import.meta.env.VITE_DEFAULT_HOMESERVER || @@ -26,11 +29,59 @@ 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 }); + + 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 4c921087d..985937a99 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"