From 914abda7c9362f77e910d3a5ad38bd27b9117cb7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 12 Aug 2021 10:19:09 -0700 Subject: [PATCH] Migrate OlmSessionStore to TypeScript --- src/matrix/storage/idb/Transaction.js | 2 +- ...{OlmSessionStore.js => OlmSessionStore.ts} | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) rename src/matrix/storage/idb/stores/{OlmSessionStore.js => OlmSessionStore.ts} (67%) diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.js index a99dae7a..cc2cde4d 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.js @@ -28,7 +28,7 @@ import {TimelineFragmentStore} from "./stores/TimelineFragmentStore"; import {PendingEventStore} from "./stores/PendingEventStore"; import {UserIdentityStore} from "./stores/UserIdentityStore"; import {DeviceIdentityStore} from "./stores/DeviceIdentityStore"; -import {OlmSessionStore} from "./stores/OlmSessionStore.js"; +import {OlmSessionStore} from "./stores/OlmSessionStore"; import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js"; import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore"; import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore"; diff --git a/src/matrix/storage/idb/stores/OlmSessionStore.js b/src/matrix/storage/idb/stores/OlmSessionStore.ts similarity index 67% rename from src/matrix/storage/idb/stores/OlmSessionStore.js rename to src/matrix/storage/idb/stores/OlmSessionStore.ts index d81cc048..a118b963 100644 --- a/src/matrix/storage/idb/stores/OlmSessionStore.js +++ b/src/matrix/storage/idb/stores/OlmSessionStore.ts @@ -13,26 +13,37 @@ 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 {Store} from "../Store"; -function encodeKey(senderKey, sessionId) { +function encodeKey(senderKey: string, sessionId: string): string { return `${senderKey}|${sessionId}`; } -function decodeKey(key) { +function decodeKey(key: string): { senderKey: string, sessionId: string } { const [senderKey, sessionId] = key.split("|"); return {senderKey, sessionId}; } +interface OlmSession { + session: string; + sessionId: string; + senderKey: string; + lastUsed: number; + key: string; +} + export class OlmSessionStore { - constructor(store) { + private _store: Store; + + constructor(store: Store) { this._store = store; } - async getSessionIds(senderKey) { - const sessionIds = []; + async getSessionIds(senderKey: string): Promise { + const sessionIds: string[] = []; const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, "")); await this._store.iterateKeys(range, key => { - const decodedKey = decodeKey(key); + const decodedKey = decodeKey(key as string); // prevent running into the next room if (decodedKey.senderKey === senderKey) { sessionIds.push(decodedKey.sessionId); @@ -43,23 +54,23 @@ export class OlmSessionStore { return sessionIds; } - getAll(senderKey) { + getAll(senderKey: string): Promise { const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, "")); return this._store.selectWhile(range, session => { return session.senderKey === senderKey; }); } - get(senderKey, sessionId) { + get(senderKey: string, sessionId: string): Promise { return this._store.get(encodeKey(senderKey, sessionId)); } - set(session) { + set(session: OlmSession): Promise { session.key = encodeKey(session.senderKey, session.sessionId); return this._store.put(session); } - remove(senderKey, sessionId) { + remove(senderKey: string, sessionId: string): Promise { return this._store.delete(encodeKey(senderKey, sessionId)); } }