1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Handle crypto db version upgrades

This commit is contained in:
David Baker
2018-11-16 11:31:08 +00:00
parent 88682e1c3b
commit 2602c155d0
2 changed files with 39 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ import logger from '../../logger';
import LocalStorageCryptoStore from './localStorage-crypto-store'; import LocalStorageCryptoStore from './localStorage-crypto-store';
import MemoryCryptoStore from './memory-crypto-store'; import MemoryCryptoStore from './memory-crypto-store';
import * as IndexedDBCryptoStoreBackend from './indexeddb-crypto-store-backend'; import * as IndexedDBCryptoStoreBackend from './indexeddb-crypto-store-backend';
import {InvalidCryptoStoreError} from '../../errors';
/** /**
* Internal module. indexeddb storage for e2e. * Internal module. indexeddb storage for e2e.
@@ -107,16 +108,25 @@ export default class IndexedDBCryptoStore {
}, },
); );
}).catch((e) => { }).catch((e) => {
if (e.name === 'VersionError') {
logger.warn("Crypto DB is too new for us to use!", e);
// don't fall back to a different store: the user has crypto data
// in this db so we should use it or nothing at all.
throw new InvalidCryptoStoreError(InvalidCryptoStoreError.TOO_NEW);
}
logger.warn( logger.warn(
`unable to connect to indexeddb ${this._dbName}` + `unable to connect to indexeddb ${this._dbName}` +
`: falling back to localStorage store: ${e}`, `: falling back to localStorage store: ${e}`,
); );
return new LocalStorageCryptoStore(global.localStorage);
}).catch((e) => { try {
logger.warn( return new LocalStorageCryptoStore(global.localStorage);
`unable to open localStorage: falling back to in-memory store: ${e}`, } catch (e) {
); logger.warn(
return new MemoryCryptoStore(); `unable to open localStorage: falling back to in-memory store: ${e}`,
);
return new MemoryCryptoStore();
}
}); });
return this._backendPromise; return this._backendPromise;

View File

@@ -1,6 +1,6 @@
// can't just do InvalidStoreError extends Error // can't just do InvalidStoreError extends Error
// because of http://babeljs.io/docs/usage/caveats/#classes // because of http://babeljs.io/docs/usage/caveats/#classes
function InvalidStoreError(reason, value) { export function InvalidStoreError(reason, value) {
const message = `Store is invalid because ${reason}, ` + const message = `Store is invalid because ${reason}, ` +
`please stop the client, delete all data and start the client again`; `please stop the client, delete all data and start the client again`;
const instance = Reflect.construct(Error, [message]); const instance = Reflect.construct(Error, [message]);
@@ -22,4 +22,25 @@ InvalidStoreError.prototype = Object.create(Error.prototype, {
}); });
Reflect.setPrototypeOf(InvalidStoreError, Error); Reflect.setPrototypeOf(InvalidStoreError, Error);
module.exports.InvalidStoreError = InvalidStoreError;
export function InvalidCryptoStoreError(reason) {
const message = `Crypto store is invalid because ${reason}, ` +
`please stop the client, delete all data and start the client again`;
const instance = Reflect.construct(Error, [message]);
Reflect.setPrototypeOf(instance, Reflect.getPrototypeOf(this));
instance.reason = reason;
instance.name = 'InvalidCryptoStoreError';
return instance;
}
InvalidCryptoStoreError.TOO_NEW = "TOO_NEW";
InvalidCryptoStoreError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true,
},
});
Reflect.setPrototypeOf(InvalidCryptoStoreError, Error);