diff --git a/packages/nicolium/src/main.tsx b/packages/nicolium/src/main.tsx index 9ab9cd6fa..1fa9495a4 100644 --- a/packages/nicolium/src/main.tsx +++ b/packages/nicolium/src/main.tsx @@ -10,7 +10,6 @@ declare global { } } -import '@/storage/migrate-legacy-data'; import React from 'react'; import { createRoot } from 'react-dom/client'; diff --git a/packages/nicolium/src/storage/kv-store.ts b/packages/nicolium/src/storage/kv-store.ts index 5f2d8b5f4..c0d3d064a 100644 --- a/packages/nicolium/src/storage/kv-store.ts +++ b/packages/nicolium/src/storage/kv-store.ts @@ -1,7 +1,5 @@ import localforage from 'localforage'; -import { migrationComplete } from './migrate-legacy-data'; - interface IKVStore extends LocalForage { getItemOrError: (key: string) => Promise; } @@ -15,12 +13,6 @@ const KVStore = localforage.createInstance({ storeName: 'keyvaluepairs', }) as IKVStore; -const originalGetItem = KVStore.getItem.bind(KVStore); -KVStore.getItem = async (...args) => { - await migrationComplete; - return originalGetItem(...args); -}; - // localForage returns 'null' when a key isn't found. // In the Redux action flow, we want it to fail harder. KVStore.getItemOrError = (key: string) => diff --git a/packages/nicolium/src/storage/migrate-legacy-data.ts b/packages/nicolium/src/storage/migrate-legacy-data.ts deleted file mode 100644 index 53b341021..000000000 --- a/packages/nicolium/src/storage/migrate-legacy-data.ts +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Migrate data from the legacy `pl-fe` namespaces to the new ones using `nicolium`. - * Includes synchronous migrations for localStorage (ran on import) and async for IndexedDB. - * Made a hack in @/storage/kv-store to delay getItem calls until the async migration is complete to avoid race conditions. - * Will remove this migration handling in like a month or so. - */ - -import localforage from 'localforage'; -import trim from 'lodash/trim'; - -import { FE_SUBDIRECTORY, NODE_ENV } from '@/build-config'; - -// synchronous localStorage migrations - -const migrateLocalStorageKey = (oldKey: string, newKey: string) => { - const value = localStorage.getItem(oldKey); - if (value !== null && localStorage.getItem(newKey) === null) { - localStorage.setItem(newKey, value); - localStorage.removeItem(oldKey); - } -}; - -const migrateAuthStorage = () => { - // subdirectory support, see @/reducers/auth - const subdir = trim(FE_SUBDIRECTORY, '/'); - const oldNamespace = subdir ? `pl-fe@${FE_SUBDIRECTORY}` : 'pl-fe'; - const newNamespace = subdir ? `nicolium@${FE_SUBDIRECTORY}` : 'nicolium'; - - migrateLocalStorageKey(`${oldNamespace}:auth`, `${newNamespace}:auth`); -}; - -const migratePushNotificationSettings = () => { - const keysToMigrate: string[] = []; - - for (let i = 0; i < localStorage.length; i++) { - const key = localStorage.key(i); - if (key?.startsWith('plfe_push_notification_data')) { - keysToMigrate.push(key); - } - } - - for (const oldKey of keysToMigrate) { - const newKey = oldKey.replace( - 'plfe_push_notification_data', - 'nicolium:pushNotificationSettings', - ); - migrateLocalStorageKey(oldKey, newKey); - } -}; - -const shouldMigrateLegacyData = NODE_ENV !== 'test'; - -if (shouldMigrateLegacyData) { - migrateAuthStorage(); - migratePushNotificationSettings(); -} - -// async migrations - -const migrateIndexedDB = async () => { - const oldStore = localforage.createInstance({ - name: 'pl-fe', - driver: localforage.INDEXEDDB, - storeName: 'keyvaluepairs', - }); - - const newStore = localforage.createInstance({ - name: 'nicolium', - driver: localforage.INDEXEDDB, - storeName: 'keyvaluepairs', - }); - - try { - const oldKeys = await oldStore.keys(); - const newKeys = await newStore.keys(); - - if (oldKeys.length === 0 || newKeys.length > 0) { - await oldStore.dropInstance({ name: 'pl-fe' }); - return; - } - - for (const oldKey of oldKeys) { - const value = await oldStore.getItem(oldKey); - - const newKey = oldKey.startsWith('plfe_config:') - ? oldKey.replace('plfe_config:', 'frontendConfig:') - : oldKey; - await newStore.setItem(newKey, value); - } - - await oldStore.dropInstance({ name: 'pl-fe' }); - } catch (e) { - console.error('Failed to migrate IndexedDB data', e); - } -}; - -const migrationComplete = shouldMigrateLegacyData ? migrateIndexedDB() : Promise.resolve(); - -export { migrationComplete };