2017-06-13 11:46:49 +00:00
|
|
|
/*
|
2021-06-22 16:22:38 +00:00
|
|
|
Copyright 2017 - 2021 The Matrix.org Foundation C.I.C.
|
2017-06-13 11:46:49 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-07-14 17:05:06 +00:00
|
|
|
// @ts-ignore - `.ts` is needed here to make TS happy
|
|
|
|
import IndexedDBWorker from "../workers/indexeddb.worker.ts";
|
2021-06-22 16:22:38 +00:00
|
|
|
import { createClient, ICreateClientOpts } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { IndexedDBCryptoStore } from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store";
|
|
|
|
import { WebStorageSessionStore } from "matrix-js-sdk/src/store/session/webstorage";
|
|
|
|
import { IndexedDBStore } from "matrix-js-sdk/src/store/indexeddb";
|
2017-06-13 11:46:49 +00:00
|
|
|
|
|
|
|
const localStorage = window.localStorage;
|
|
|
|
|
2017-06-22 14:07:54 +00:00
|
|
|
// just *accessing* indexedDB throws an exception in firefox with
|
|
|
|
// indexeddb disabled.
|
|
|
|
let indexedDB;
|
|
|
|
try {
|
|
|
|
indexedDB = window.indexedDB;
|
2017-11-16 13:19:36 +00:00
|
|
|
} catch (e) {}
|
2017-06-22 14:07:54 +00:00
|
|
|
|
2017-06-13 11:46:49 +00:00
|
|
|
/**
|
|
|
|
* Create a new matrix client, with the persistent stores set up appropriately
|
|
|
|
* (using localstorage/indexeddb, etc)
|
|
|
|
*
|
|
|
|
* @param {Object} opts options to pass to Matrix.createClient. This will be
|
|
|
|
* extended with `sessionStore` and `store` members.
|
|
|
|
*
|
|
|
|
* @returns {MatrixClient} the newly-created MatrixClient
|
|
|
|
*/
|
2021-06-22 16:22:38 +00:00
|
|
|
export default function createMatrixClient(opts: ICreateClientOpts) {
|
|
|
|
const storeOpts: Partial<ICreateClientOpts> = {
|
2017-10-30 10:40:10 +00:00
|
|
|
useAuthorizationHeader: true,
|
|
|
|
};
|
2017-06-13 11:46:49 +00:00
|
|
|
|
2019-03-25 16:22:53 +00:00
|
|
|
if (indexedDB && localStorage) {
|
2021-03-19 02:50:34 +00:00
|
|
|
storeOpts.store = new IndexedDBStore({
|
2017-06-22 14:07:54 +00:00
|
|
|
indexedDB: indexedDB,
|
2017-06-13 11:46:49 +00:00
|
|
|
dbName: "riot-web-sync",
|
|
|
|
localStorage: localStorage,
|
2021-07-12 17:43:20 +00:00
|
|
|
workerFactory: () => new IndexedDBWorker(),
|
2017-06-13 11:46:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:02:26 +00:00
|
|
|
if (localStorage) {
|
2021-03-19 02:50:34 +00:00
|
|
|
storeOpts.sessionStore = new WebStorageSessionStore(localStorage);
|
2019-03-21 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 16:22:53 +00:00
|
|
|
if (indexedDB) {
|
2021-03-19 02:50:34 +00:00
|
|
|
storeOpts.cryptoStore = new IndexedDBCryptoStore(
|
2019-03-21 15:02:26 +00:00
|
|
|
indexedDB, "matrix-js-sdk:crypto",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:22:38 +00:00
|
|
|
return createClient({
|
|
|
|
...storeOpts,
|
|
|
|
...opts,
|
|
|
|
});
|
2017-06-13 11:46:49 +00:00
|
|
|
}
|