diff --git a/packages/suite-storage/package.json b/packages/suite-storage/package.json index dd26dd8af069..73acdba4021e 100644 --- a/packages/suite-storage/package.json +++ b/packages/suite-storage/package.json @@ -17,10 +17,8 @@ "react-native": "./src/native/index.ts", "browser": "./src/web/index.ts", "dependencies": { - "@trezor/env-utils": "workspace:*", "@trezor/eslint": "workspace:*", "@trezor/utils": "workspace:*", - "broadcast-channel": "^7.0.0", "idb": "^8.0.2" } } diff --git a/packages/suite-storage/src/native/index.ts b/packages/suite-storage/src/native/index.ts index a186924b4733..c42ab7e2609a 100644 --- a/packages/suite-storage/src/native/index.ts +++ b/packages/suite-storage/src/native/index.ts @@ -8,8 +8,6 @@ import { StoreValue, } from 'idb'; -import { StorageMessageEvent } from './types'; - export type OnUpgradeFunc = ( db: IDBPDatabase, oldVersion: number, @@ -61,47 +59,21 @@ class CommonDB { CommonDB.instance = this; } - static isDBAvailable = (): Promise => - // Firefox doesn't support indexedDB while in incognito mode, but still returns valid window.indexedDB object. - // https://bugzilla.mozilla.org/show_bug.cgi?id=781982 - // so we need to try accessing the IDB. try/catch around idb.open() does not catch the error (bug in idb?), that's why we use callbacks. - // this solution calls callback function from within onerror/onsuccess event handlers. - // For other browsers checking the window.indexedDB should be enough. - Promise.resolve(false); - // const isFirefox = navigator && navigator.userAgent.toLowerCase().indexOf('firefox') > -1; - // return new Promise((resolve, _reject) => { - // if (isFirefox) { - // const r = indexedDB.open('test'); - // r.onerror = () => resolve(false); - // r.onsuccess = () => resolve(true); - // } else { - // // @ts-ignore - // const idbAvailable = !!indexedDB || !!window.indexedDB || !!global.indexedDB; - // if (idbAvailable) { - // resolve(true); - // } else { - // resolve(false); - // } - // } - // }); - - isSupported = (): Promise => { + static isDBAvailable = () => false; + + isSupported = () => { this.supported = false; - return Promise.resolve(false); + return false; }; - isAccessible = async () => { - const isSupported = await this.isSupported(); + isAccessible = (): Promise => { + const isSupported = this.isSupported(); // if the instance is blocking db upgrade, db connection will be closed - return isSupported && !this.blocking && !this.blocked; + return Promise.resolve(isSupported && !this.blocking && !this.blocked); }; - notify = (_store: StoreNames, _keys: any[]) => {}; - - onChange = (_handler: (event: StorageMessageEvent) => any) => {}; - getDB = (): Promise> => // @ts-expect-error Promise.resolve(); @@ -205,4 +177,3 @@ class CommonDB { } export default CommonDB; -export type { StorageUpdateMessage } from './types'; diff --git a/packages/suite-storage/src/native/types/index.ts b/packages/suite-storage/src/native/types/index.ts deleted file mode 100644 index 8b3fa5c849ea..000000000000 --- a/packages/suite-storage/src/native/types/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { StoreNames } from 'idb'; - -export interface StorageUpdateMessage { - // TODO: only key strings from given store should be allowed - store: StoreNames; - keys: any[]; -} - -export interface StorageMessageEvent extends MessageEvent { - data: StorageUpdateMessage; -} diff --git a/packages/suite-storage/src/web/index.ts b/packages/suite-storage/src/web/index.ts index 8aba318e8fea..0d292bd33bf0 100644 --- a/packages/suite-storage/src/web/index.ts +++ b/packages/suite-storage/src/web/index.ts @@ -1,4 +1,3 @@ -import { BroadcastChannel } from 'broadcast-channel'; import { IDBPDatabase, IDBPTransaction, @@ -12,11 +11,8 @@ import { unwrap, } from 'idb'; -import { isFirefox } from '@trezor/env-utils'; import { createLazy } from '@trezor/utils'; -import { StorageMessageEvent } from './types'; - export type OnUpgradeFunc = ( db: IDBPDatabase, oldVersion: number, @@ -28,7 +24,6 @@ class CommonDB { private static instance: CommonDB; dbName!: string; version!: number; - broadcastChannel!: BroadcastChannel; supported: boolean | undefined; blocking = false; blocked = false; @@ -67,39 +62,14 @@ class CommonDB { this.isSupported(); - // create global instance of broadcast channel - this.broadcastChannel = new BroadcastChannel('storageChangeEvent'); - CommonDB.instance = this; } - static isDBAvailable = () => - // Firefox doesn't support indexedDB while in incognito mode, but still returns valid window.indexedDB object. - // https://bugzilla.mozilla.org/show_bug.cgi?id=781982 - // so we need to try accessing the IDB. try/catch around idb.open() does not catch the error (bug in idb?), that's why we use callbacks. - // this solution calls callback function from within onerror/onsuccess event handlers. - // For other browsers checking the window.indexedDB should be enough. - new Promise(resolve => { - if (isFirefox()) { - const r = indexedDB.open('test'); - r.onerror = () => resolve(false); - r.onsuccess = () => { - indexedDB.deleteDatabase('test'); - resolve(true); - }; - } else { - const idbAvailable = !!indexedDB || !!window.indexedDB || !!global.indexedDB; - if (idbAvailable) { - resolve(true); - } else { - resolve(false); - } - } - }); + static isDBAvailable = () => !!indexedDB || !!window.indexedDB || !!global.indexedDB; - isSupported = async () => { + isSupported = () => { if (this.supported === undefined) { - const isAvailable = await CommonDB.isDBAvailable(); + const isAvailable = CommonDB.isDBAvailable(); this.supported = isAvailable; if (!isAvailable) { console.warn("Couldn't get an access to IndexedDB."); @@ -109,22 +79,11 @@ class CommonDB { return this.supported; }; - isAccessible = async () => { - const isSupported = await this.isSupported(); + isAccessible = (): Promise => { + const isSupported = this.isSupported(); // if the instance is blocking db upgrade, db connection will be closed - return isSupported && !this.blocking && !this.blocked; - }; - - notify = (store: StoreNames, keys: any[]) => { - // sends the message containing store, keys which were updated to other tabs/windows - const message = { store, keys }; - this.broadcastChannel.postMessage(message); - }; - - onChange = (handler: (event: StorageMessageEvent) => any) => { - // listens to the channel. On receiving a message triggers the handler func - this.broadcastChannel.onmessage = handler; + return Promise.resolve(isSupported && !this.blocking && !this.blocked); }; closeAfterTimeout = (timeout = 1000) => { @@ -206,7 +165,6 @@ class CommonDB { reject(req.error); }; req.onsuccess = _event => { - this.notify(store, [req.result]); resolve(req.result); }; } catch (error) { @@ -231,7 +189,7 @@ class CommonDB { const tx = db.transaction(store, 'readwrite'); const keys: StoreKey>[] = []; - const promises = Promise.all( + const promisesOfItems: Promise = Promise.all( items .map(item => { if (upsert) { @@ -245,11 +203,10 @@ class CommonDB { }); }) .concat(tx.done), - ).then(_ => { - this.notify(store, keys); - }); + ); + const aggregatedPromise: Promise = promisesOfItems.then(() => {}); - return promises; + return aggregatedPromise; }; getItemByPK = async < @@ -300,7 +257,6 @@ class CommonDB { const result = await index.get(key); if (result) { Object.assign(result, updateObject); - this.notify(store, [result]); return tx.store.put(result); } @@ -339,7 +295,6 @@ class CommonDB { let cursor = await txIdIndex.openCursor(IDBKeyRange.only(key)); while (cursor) { cursor.delete(); - this.notify(store, cursor.value ? [cursor.value] : []); cursor = await cursor.continue(); } @@ -471,4 +426,3 @@ class CommonDB { } export default CommonDB; -export type { StorageUpdateMessage } from './types'; diff --git a/packages/suite-storage/src/web/types/index.ts b/packages/suite-storage/src/web/types/index.ts deleted file mode 100644 index 8b3fa5c849ea..000000000000 --- a/packages/suite-storage/src/web/types/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { StoreNames } from 'idb'; - -export interface StorageUpdateMessage { - // TODO: only key strings from given store should be allowed - store: StoreNames; - keys: any[]; -} - -export interface StorageMessageEvent extends MessageEvent { - data: StorageUpdateMessage; -} diff --git a/packages/suite-storage/tsconfig.json b/packages/suite-storage/tsconfig.json index f7434c2f17a4..7ab4e4c8c3e6 100644 --- a/packages/suite-storage/tsconfig.json +++ b/packages/suite-storage/tsconfig.json @@ -3,7 +3,6 @@ "compilerOptions": { "outDir": "./libDev" }, "include": [".", "**/*.json"], "references": [ - { "path": "../env-utils" }, { "path": "../eslint" }, { "path": "../utils" } ] diff --git a/packages/suite/src/storage/definitions.ts b/packages/suite/src/storage/definitions.ts index 8adf2c54bb31..a1c82a372883 100644 --- a/packages/suite/src/storage/definitions.ts +++ b/packages/suite/src/storage/definitions.ts @@ -16,7 +16,6 @@ import type { RatesByTimestamps, WalletSettings, } from '@suite-common/wallet-types'; -import type { StorageUpdateMessage } from '@trezor/suite-storage'; import type { SuiteState } from 'src/reducers/suite/suiteReducer'; import type { MetadataState } from 'src/types/suite/metadata'; @@ -146,5 +145,3 @@ export interface SuiteDBSchema extends DBSchema { }; }; } - -export type SuiteStorageUpdateMessage = StorageUpdateMessage; diff --git a/scripts/list-outdated-dependencies/foundation-dependencies.txt b/scripts/list-outdated-dependencies/foundation-dependencies.txt index b0970106997f..60960c7b57e8 100644 --- a/scripts/list-outdated-dependencies/foundation-dependencies.txt +++ b/scripts/list-outdated-dependencies/foundation-dependencies.txt @@ -46,7 +46,6 @@ express # STORAGE idb -broadcast-channel # ESLINT @eslint/js diff --git a/yarn.lock b/yarn.lock index 6eb599d312de..2828d12390ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1622,15 +1622,6 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:7.23.4": - version: 7.23.4 - resolution: "@babel/runtime@npm:7.23.4" - dependencies: - regenerator-runtime: "npm:^0.14.0" - checksum: 10/6ef4f6dcc4ec4d74cb9f6c26a26e92d016b36debd167be48cae293fbd990b3157fb1d8d21c531285da15a5bda9ccb23e651b56234941e03d91c8af69d4c593a9 - languageName: node - linkType: hard - "@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.1.2, @babel/runtime@npm:^7.12.0, @babel/runtime@npm:^7.12.13, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.17.8, @babel/runtime@npm:^7.18.3, @babel/runtime@npm:^7.18.6, @babel/runtime@npm:^7.20.0, @babel/runtime@npm:^7.21.0, @babel/runtime@npm:^7.23.2, @babel/runtime@npm:^7.23.5, @babel/runtime@npm:^7.23.8, @babel/runtime@npm:^7.25.0, @babel/runtime@npm:^7.26.0, @babel/runtime@npm:^7.27.0, @babel/runtime@npm:^7.5.5, @babel/runtime@npm:^7.8.4, @babel/runtime@npm:^7.8.7, @babel/runtime@npm:^7.9.2": version: 7.27.0 resolution: "@babel/runtime@npm:7.27.0" @@ -12140,10 +12131,8 @@ __metadata: version: 0.0.0-use.local resolution: "@trezor/suite-storage@workspace:packages/suite-storage" dependencies: - "@trezor/env-utils": "workspace:*" "@trezor/eslint": "workspace:*" "@trezor/utils": "workspace:*" - broadcast-channel: "npm:^7.0.0" idb: "npm:^8.0.2" languageName: unknown linkType: soft @@ -16460,18 +16449,6 @@ __metadata: languageName: node linkType: hard -"broadcast-channel@npm:^7.0.0": - version: 7.0.0 - resolution: "broadcast-channel@npm:7.0.0" - dependencies: - "@babel/runtime": "npm:7.23.4" - oblivious-set: "npm:1.4.0" - p-queue: "npm:6.6.2" - unload: "npm:2.4.1" - checksum: 10/a7cafdd54df4cafc5c8627205001d0377e77afd7dfb8ae2969f779b1a0ae2085413d021db0ab53ec74c5a43c634687289aafa8f39d8dedfbf0b0324fc670ab18 - languageName: node - linkType: hard - "brorand@npm:^1.0.1, brorand@npm:^1.1.0": version: 1.1.0 resolution: "brorand@npm:1.1.0" @@ -32448,13 +32425,6 @@ __metadata: languageName: node linkType: hard -"oblivious-set@npm:1.4.0": - version: 1.4.0 - resolution: "oblivious-set@npm:1.4.0" - checksum: 10/4503772b19eda65d18afca528a35f73b68956833f88b64c528516159a190f61c5c0c5f7f33a7882aad267ecec67097c11729e497b0bbb34e093a3aa84679cc33 - languageName: node - linkType: hard - "obuf@npm:^1.0.0, obuf@npm:^1.1.2, obuf@npm:~1.1.2": version: 1.1.2 resolution: "obuf@npm:1.1.2" @@ -40735,13 +40705,6 @@ __metadata: languageName: node linkType: hard -"unload@npm:2.4.1": - version: 2.4.1 - resolution: "unload@npm:2.4.1" - checksum: 10/00b1181eac776c7e3bf9ea3ff93e183a926d83ad445ff616e5b5f06c0b4abab19522cf3376a4a4161e4e293f55ab4d58782c48d00a3784c878e13eb7c69d2679 - languageName: node - linkType: hard - "unpipe@npm:1.0.0, unpipe@npm:~1.0.0": version: 1.0.0 resolution: "unpipe@npm:1.0.0"