2017-06-13 14:46:49 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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 Matrix from 'matrix-js-sdk';
|
|
|
|
|
|
|
|
const localStorage = window.localStorage;
|
|
|
|
|
2017-06-22 17:07:54 +03:00
|
|
|
// just *accessing* indexedDB throws an exception in firefox with
|
|
|
|
// indexeddb disabled.
|
|
|
|
let indexedDB;
|
|
|
|
try {
|
|
|
|
indexedDB = window.indexedDB;
|
2017-11-16 16:19:36 +03:00
|
|
|
} catch (e) {}
|
2017-06-22 17:07:54 +03:00
|
|
|
|
2017-06-13 14:46:49 +03: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.
|
|
|
|
*
|
2017-06-21 19:43:34 +03:00
|
|
|
* @property {string} indexedDbWorkerScript Optional URL for a web worker script
|
|
|
|
* for IndexedDB store operations. By default, indexeddb ops are done on
|
2017-06-13 14:46:49 +03:00
|
|
|
* the main thread.
|
|
|
|
*
|
|
|
|
* @returns {MatrixClient} the newly-created MatrixClient
|
|
|
|
*/
|
2019-03-25 19:22:53 +03:00
|
|
|
export default function createMatrixClient(opts) {
|
2017-10-30 13:40:10 +03:00
|
|
|
const storeOpts = {
|
|
|
|
useAuthorizationHeader: true,
|
|
|
|
};
|
2017-06-13 14:46:49 +03:00
|
|
|
|
2019-03-25 19:22:53 +03:00
|
|
|
if (indexedDB && localStorage) {
|
2017-06-13 14:46:49 +03:00
|
|
|
storeOpts.store = new Matrix.IndexedDBStore({
|
2017-06-22 17:07:54 +03:00
|
|
|
indexedDB: indexedDB,
|
2017-06-13 14:46:49 +03:00
|
|
|
dbName: "riot-web-sync",
|
|
|
|
localStorage: localStorage,
|
2017-06-21 19:43:34 +03:00
|
|
|
workerScript: createMatrixClient.indexedDbWorkerScript,
|
2017-06-13 14:46:49 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-21 18:02:26 +03:00
|
|
|
if (localStorage) {
|
|
|
|
storeOpts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
|
|
|
|
}
|
|
|
|
|
2019-03-25 19:22:53 +03:00
|
|
|
if (indexedDB) {
|
2019-03-21 18:02:26 +03:00
|
|
|
storeOpts.cryptoStore = new Matrix.IndexedDBCryptoStore(
|
|
|
|
indexedDB, "matrix-js-sdk:crypto",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-13 14:46:49 +03:00
|
|
|
opts = Object.assign(storeOpts, opts);
|
|
|
|
|
|
|
|
return Matrix.createClient(opts);
|
|
|
|
}
|
2017-06-21 19:43:34 +03:00
|
|
|
|
|
|
|
createMatrixClient.indexedDbWorkerScript = null;
|