From d81e2cea14e64805b1d0f84e41abb9f6df4a82bd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 14 Jun 2022 21:29:24 +0100 Subject: [PATCH] Remove unused sessionStorage layer (#8834) * Remove unused sessionStorage layer * Update global.d.ts * Fix tests --- cypress/global.d.ts | 2 -- cypress/support/bot.ts | 2 -- cypress/support/storage.ts | 58 --------------------------------- src/utils/createMatrixClient.ts | 24 +++++++++----- test/test-utils/test-utils.ts | 13 +++----- 5 files changed, 21 insertions(+), 78 deletions(-) delete mode 100644 cypress/support/storage.ts diff --git a/cypress/global.d.ts b/cypress/global.d.ts index de4c10a895..3d36daf951 100644 --- a/cypress/global.d.ts +++ b/cypress/global.d.ts @@ -26,7 +26,6 @@ import type { Visibility, RoomMemberEvent, } from "matrix-js-sdk/src/matrix"; -import type { WebStorageSessionStore } from "matrix-js-sdk/src/store/session/webstorage"; import type { MatrixDispatcher } from "../src/dispatcher/dispatcher"; import type PerformanceMonitor from "../src/performance"; @@ -49,7 +48,6 @@ declare global { MatrixScheduler: typeof MatrixScheduler; MemoryStore: typeof MemoryStore; MemoryCryptoStore: typeof MemoryCryptoStore; - WebStorageSessionStore: typeof WebStorageSessionStore; Visibility: typeof Visibility; Preset: typeof Preset; }; diff --git a/cypress/support/bot.ts b/cypress/support/bot.ts index e3bdf49d31..b75633d27d 100644 --- a/cypress/support/bot.ts +++ b/cypress/support/bot.ts @@ -20,7 +20,6 @@ import request from "browser-request"; import type { MatrixClient } from "matrix-js-sdk/src/client"; import { SynapseInstance } from "../plugins/synapsedocker"; -import { MockStorage } from "./storage"; import Chainable = Cypress.Chainable; declare global { @@ -51,7 +50,6 @@ Cypress.Commands.add("getBot", (synapse: SynapseInstance, displayName?: string): store: new win.matrixcs.MemoryStore(), scheduler: new win.matrixcs.MatrixScheduler(), cryptoStore: new win.matrixcs.MemoryCryptoStore(), - sessionStore: new win.matrixcs.WebStorageSessionStore(new MockStorage()), }); cli.on(win.matrixcs.RoomMemberEvent.Membership, (event, member) => { diff --git a/cypress/support/storage.ts b/cypress/support/storage.ts deleted file mode 100644 index d93e118835..0000000000 --- a/cypress/support/storage.ts +++ /dev/null @@ -1,58 +0,0 @@ -/* -Copyright 2022 The Matrix.org Foundation C.I.C. - -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. -*/ - -export class MockStorage implements Storage { - private data: Record = {}; - private keys: string[] = []; - public length = 0; - - constructor() {} - - public setItem(k: string, v: string) { - this.data[k] = v; - this.recalc(); - } - - public getItem(k: string): string | null { - return this.data[k] || null; - } - - public removeItem(k: string) { - delete this.data[k]; - this.recalc(); - } - - public clear() { - this.data = {}; - this.recalc(); - } - - public key(index: number): string { - return this.keys[index]; - } - - private recalc() { - const keys = []; - for (const k in this.data) { - if (!this.data.hasOwnProperty(k)) { - continue; - } - keys.push(k); - } - this.keys = keys; - this.length = keys.length; - } -} diff --git a/src/utils/createMatrixClient.ts b/src/utils/createMatrixClient.ts index 867c912470..e8b5276880 100644 --- a/src/utils/createMatrixClient.ts +++ b/src/utils/createMatrixClient.ts @@ -13,10 +13,16 @@ 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 { createClient, ICreateClientOpts } from "matrix-js-sdk/src/matrix"; +import { + MatrixClient, + createClient, + ICreateClientOpts, + MemoryCryptoStore, + MemoryStore, +} 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"; +import { LocalStorageCryptoStore } from "matrix-js-sdk/src/crypto/store/localStorage-crypto-store"; // @ts-ignore - `.ts` is needed here to make TS happy import IndexedDBWorker from "../workers/indexeddb.worker.ts"; @@ -39,7 +45,7 @@ try { * * @returns {MatrixClient} the newly-created MatrixClient */ -export default function createMatrixClient(opts: ICreateClientOpts) { +export default function createMatrixClient(opts: ICreateClientOpts): MatrixClient { const storeOpts: Partial = { useAuthorizationHeader: true, }; @@ -48,19 +54,21 @@ export default function createMatrixClient(opts: ICreateClientOpts) { storeOpts.store = new IndexedDBStore({ indexedDB: indexedDB, dbName: "riot-web-sync", - localStorage: localStorage, + localStorage, workerFactory: () => new IndexedDBWorker(), }); - } - - if (localStorage) { - storeOpts.sessionStore = new WebStorageSessionStore(localStorage); + } else if (localStorage) { + storeOpts.store = new MemoryStore({ localStorage }); } if (indexedDB) { storeOpts.cryptoStore = new IndexedDBCryptoStore( indexedDB, "matrix-js-sdk:crypto", ); + } else if (localStorage) { + storeOpts.cryptoStore = new LocalStorageCryptoStore(localStorage); + } else { + storeOpts.cryptoStore = new MemoryCryptoStore(); } return createClient({ diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 63d24e4688..892142b98e 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -82,6 +82,11 @@ export function createTestClient(): MatrixClient { getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), credentials: { userId: "@userId:matrix.rog" }, + store: { + getPendingEvents: jest.fn().mockResolvedValue([]), + setPendingEvents: jest.fn().mockResolvedValue(undefined), + }, + getPushActionsForEvent: jest.fn(), getRoom: jest.fn().mockImplementation(mkStubRoom), getRooms: jest.fn().mockReturnValue([]), @@ -129,14 +134,6 @@ export function createTestClient(): MatrixClient { }), createRoom: jest.fn().mockResolvedValue({ room_id: "!1:example.org" }), setPowerLevel: jest.fn().mockResolvedValue(undefined), - - // Used by various internal bits we aren't concerned with (yet) - sessionStore: { - store: { - getItem: jest.fn(), - setItem: jest.fn(), - }, - }, pushRules: {}, decryptEventIfNeeded: () => Promise.resolve(), isUserIgnored: jest.fn().mockReturnValue(false),