2023-08-30 19:57:20 +02:00
|
|
|
// eslint-disable-next-line import/no-restricted-paths -- Needed to print log messages
|
|
|
|
import { LogService } from "../platform/abstractions/log.service";
|
|
|
|
// eslint-disable-next-line import/no-restricted-paths -- Needed to interface with storage locations
|
|
|
|
import { AbstractStorageService } from "../platform/abstractions/storage.service";
|
|
|
|
|
|
|
|
import { MigrationBuilder } from "./migration-builder";
|
2024-01-10 17:51:45 +01:00
|
|
|
import { EverHadUserKeyMigrator } from "./migrations/10-move-ever-had-user-key-to-state-providers";
|
2024-01-23 22:01:49 +01:00
|
|
|
import { OrganizationKeyMigrator } from "./migrations/11-move-org-keys-to-state-providers";
|
2024-01-24 20:21:50 +01:00
|
|
|
import { MoveEnvironmentStateToProviders } from "./migrations/12-move-environment-state-to-providers";
|
2024-01-29 22:53:01 +01:00
|
|
|
import { ProviderKeyMigrator } from "./migrations/13-move-provider-keys-to-state-providers";
|
2024-02-05 19:02:28 +01:00
|
|
|
import { MoveBiometricClientKeyHalfToStateProviders } from "./migrations/14-move-biometric-client-key-half-state-to-providers";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { FolderMigrator } from "./migrations/15-move-folder-state-to-state-provider";
|
2024-02-06 21:00:41 +01:00
|
|
|
import { LastSyncMigrator } from "./migrations/16-move-last-sync-to-state-provider";
|
2024-02-06 21:15:22 +01:00
|
|
|
import { EnablePasskeysMigrator } from "./migrations/17-move-enable-passkeys-to-state-providers";
|
2024-02-12 23:11:04 +01:00
|
|
|
import { AutofillSettingsKeyMigrator } from "./migrations/18-move-autofill-settings-to-state-providers";
|
2024-02-14 20:25:08 +01:00
|
|
|
import { RequirePasswordOnStartMigrator } from "./migrations/19-migrate-require-password-on-start";
|
2024-02-14 21:04:08 +01:00
|
|
|
import { PrivateKeyMigrator } from "./migrations/20-move-private-key-to-state-providers";
|
2024-02-14 23:03:03 +01:00
|
|
|
import { CollectionMigrator } from "./migrations/21-move-collections-state-to-state-provider";
|
2024-02-16 18:53:24 +01:00
|
|
|
import { CollapsedGroupingsMigrator } from "./migrations/22-move-collapsed-groupings-to-state-provider";
|
2024-02-23 15:21:18 +01:00
|
|
|
import { MoveBiometricPromptsToStateProviders } from "./migrations/23-move-biometric-prompts-to-state-providers";
|
2024-02-23 19:16:42 +01:00
|
|
|
import { SmOnboardingTasksMigrator } from "./migrations/24-move-sm-onboarding-key-to-state-providers";
|
2024-02-23 19:52:13 +01:00
|
|
|
import { ClearClipboardDelayMigrator } from "./migrations/25-move-clear-clipboard-to-autofill-settings-state-provider";
|
2024-03-04 15:34:22 +01:00
|
|
|
import { RevertLastSyncMigrator } from "./migrations/26-revert-move-last-sync-to-state-provider";
|
|
|
|
import { BadgeSettingsMigrator } from "./migrations/27-move-badge-settings-to-state-providers";
|
|
|
|
import { MoveBiometricUnlockToStateProviders } from "./migrations/28-move-biometric-unlock-to-state-providers";
|
2024-03-04 20:12:23 +01:00
|
|
|
import { UserNotificationSettingsKeyMigrator } from "./migrations/29-move-user-notification-settings-to-state-provider";
|
2023-08-30 19:57:20 +02:00
|
|
|
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
|
2024-03-08 01:26:00 +01:00
|
|
|
import { PolicyMigrator } from "./migrations/30-move-policy-state-to-state-provider";
|
2024-03-08 16:34:07 +01:00
|
|
|
import { EnableContextMenuMigrator } from "./migrations/31-move-enable-context-menu-to-autofill-settings-state-provider";
|
2024-03-11 18:59:19 +01:00
|
|
|
import { PreferredLanguageMigrator } from "./migrations/32-move-preferred-language";
|
2024-03-12 16:12:40 +01:00
|
|
|
import { AppIdMigrator } from "./migrations/33-move-app-id-to-state-providers";
|
2024-03-12 20:07:14 +01:00
|
|
|
import { DomainSettingsMigrator } from "./migrations/34-move-domain-settings-to-state-providers";
|
2024-03-13 16:25:39 +01:00
|
|
|
import { MoveThemeToStateProviderMigrator } from "./migrations/35-move-theme-to-state-providers";
|
2024-03-14 16:37:57 +01:00
|
|
|
import { VaultSettingsKeyMigrator } from "./migrations/36-move-show-card-and-identity-to-state-provider";
|
2024-03-14 17:56:48 +01:00
|
|
|
import { AvatarColorMigrator } from "./migrations/37-move-avatar-color-to-state-providers";
|
2024-03-15 14:07:22 +01:00
|
|
|
import { DeleteBiometricPromptCancelledData } from "./migrations/38-delete-orphaned-biometric-prompt-data";
|
2023-08-30 19:57:20 +02:00
|
|
|
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
|
|
|
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
|
|
|
import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key";
|
|
|
|
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
|
|
|
|
import { MoveStateVersionMigrator } from "./migrations/8-move-state-version";
|
2023-11-17 15:20:42 +01:00
|
|
|
import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-settings-to-global";
|
2023-08-30 19:57:20 +02:00
|
|
|
import { MinVersionMigrator } from "./migrations/min-version";
|
|
|
|
|
|
|
|
export const MIN_VERSION = 2;
|
2024-03-15 14:07:22 +01:00
|
|
|
export const CURRENT_VERSION = 38;
|
2023-08-30 19:57:20 +02:00
|
|
|
export type MinVersion = typeof MIN_VERSION;
|
|
|
|
|
2024-02-14 14:52:13 +01:00
|
|
|
export function createMigrationBuilder() {
|
|
|
|
return MigrationBuilder.create()
|
2023-08-30 19:57:20 +02:00
|
|
|
.with(MinVersionMigrator)
|
|
|
|
.with(FixPremiumMigrator, 2, 3)
|
|
|
|
.with(RemoveEverBeenUnlockedMigrator, 3, 4)
|
|
|
|
.with(AddKeyTypeToOrgKeysMigrator, 4, 5)
|
|
|
|
.with(RemoveLegacyEtmKeyMigrator, 5, 6)
|
|
|
|
.with(MoveBiometricAutoPromptToAccount, 6, 7)
|
2023-11-17 15:20:42 +01:00
|
|
|
.with(MoveStateVersionMigrator, 7, 8)
|
2024-01-10 17:51:45 +01:00
|
|
|
.with(MoveBrowserSettingsToGlobal, 8, 9)
|
2024-01-23 22:01:49 +01:00
|
|
|
.with(EverHadUserKeyMigrator, 9, 10)
|
2024-01-24 20:21:50 +01:00
|
|
|
.with(OrganizationKeyMigrator, 10, 11)
|
2024-01-29 22:53:01 +01:00
|
|
|
.with(MoveEnvironmentStateToProviders, 11, 12)
|
2024-02-05 19:02:28 +01:00
|
|
|
.with(ProviderKeyMigrator, 12, 13)
|
2024-02-06 20:51:02 +01:00
|
|
|
.with(MoveBiometricClientKeyHalfToStateProviders, 13, 14)
|
2024-02-06 21:00:41 +01:00
|
|
|
.with(FolderMigrator, 14, 15)
|
2024-02-06 21:15:22 +01:00
|
|
|
.with(LastSyncMigrator, 15, 16)
|
2024-02-12 23:11:04 +01:00
|
|
|
.with(EnablePasskeysMigrator, 16, 17)
|
2024-02-14 20:25:08 +01:00
|
|
|
.with(AutofillSettingsKeyMigrator, 17, 18)
|
2024-02-14 21:04:08 +01:00
|
|
|
.with(RequirePasswordOnStartMigrator, 18, 19)
|
2024-02-14 23:03:03 +01:00
|
|
|
.with(PrivateKeyMigrator, 19, 20)
|
2024-02-16 18:53:24 +01:00
|
|
|
.with(CollectionMigrator, 20, 21)
|
2024-02-23 15:21:18 +01:00
|
|
|
.with(CollapsedGroupingsMigrator, 21, 22)
|
2024-02-23 19:16:42 +01:00
|
|
|
.with(MoveBiometricPromptsToStateProviders, 22, 23)
|
2024-02-23 19:52:13 +01:00
|
|
|
.with(SmOnboardingTasksMigrator, 23, 24)
|
2024-02-27 22:03:12 +01:00
|
|
|
.with(ClearClipboardDelayMigrator, 24, 25)
|
2024-03-04 15:34:22 +01:00
|
|
|
.with(RevertLastSyncMigrator, 25, 26)
|
|
|
|
.with(BadgeSettingsMigrator, 26, 27)
|
2024-03-04 20:12:23 +01:00
|
|
|
.with(MoveBiometricUnlockToStateProviders, 27, 28)
|
2024-03-08 01:26:00 +01:00
|
|
|
.with(UserNotificationSettingsKeyMigrator, 28, 29)
|
2024-03-08 16:34:07 +01:00
|
|
|
.with(PolicyMigrator, 29, 30)
|
2024-03-11 18:59:19 +01:00
|
|
|
.with(EnableContextMenuMigrator, 30, 31)
|
2024-03-12 16:12:40 +01:00
|
|
|
.with(PreferredLanguageMigrator, 31, 32)
|
2024-03-12 20:07:14 +01:00
|
|
|
.with(AppIdMigrator, 32, 33)
|
2024-03-13 16:25:39 +01:00
|
|
|
.with(DomainSettingsMigrator, 33, 34)
|
2024-03-14 16:37:57 +01:00
|
|
|
.with(MoveThemeToStateProviderMigrator, 34, 35)
|
2024-03-14 17:56:48 +01:00
|
|
|
.with(VaultSettingsKeyMigrator, 35, 36)
|
2024-03-15 14:07:22 +01:00
|
|
|
.with(AvatarColorMigrator, 36, 37)
|
|
|
|
.with(DeleteBiometricPromptCancelledData, 37, CURRENT_VERSION);
|
2023-08-30 19:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function currentVersion(
|
|
|
|
storageService: AbstractStorageService,
|
|
|
|
logService: LogService,
|
|
|
|
) {
|
|
|
|
let state = await storageService.get<number>("stateVersion");
|
|
|
|
if (state == null) {
|
|
|
|
// Pre v8
|
|
|
|
state = (await storageService.get<{ stateVersion: number }>("global"))?.stateVersion;
|
|
|
|
}
|
|
|
|
if (state == null) {
|
|
|
|
logService.info("No state version found, assuming empty state.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
logService.info(`State version: ${state}`);
|
|
|
|
return state;
|
|
|
|
}
|
2024-02-06 18:01:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for migrations to have a chance to run and will resolve the promise once they are.
|
|
|
|
*
|
|
|
|
* @param storageService Disk storage where the `stateVersion` will or is already saved in.
|
|
|
|
* @param logService Log service
|
|
|
|
*/
|
|
|
|
export async function waitForMigrations(
|
|
|
|
storageService: AbstractStorageService,
|
|
|
|
logService: LogService,
|
|
|
|
) {
|
|
|
|
const isReady = async () => {
|
|
|
|
const version = await currentVersion(storageService, logService);
|
|
|
|
// The saved version is what we consider the latest
|
2024-03-08 16:54:12 +01:00
|
|
|
// migrations should be complete, the state version
|
|
|
|
// shouldn't become larger than `CURRENT_VERSION` in
|
|
|
|
// any normal usage of the application but it is common
|
|
|
|
// enough in dev scenarios where we want to consider that
|
|
|
|
// ready as well and return true in that scenario.
|
|
|
|
return version >= CURRENT_VERSION;
|
2024-02-06 18:01:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const wait = async (time: number) => {
|
|
|
|
// Wait exponentially
|
|
|
|
const nextTime = time * 2;
|
|
|
|
if (nextTime > 8192) {
|
|
|
|
// Don't wait longer than ~8 seconds in a single wait,
|
|
|
|
// if the migrations still haven't happened. They aren't
|
|
|
|
// likely to.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (!(await isReady())) {
|
|
|
|
logService.info(`Waiting for migrations to finish, waiting for ${nextTime}ms`);
|
|
|
|
await wait(nextTime);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
}, time);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!(await isReady())) {
|
|
|
|
// Wait for 2ms to start with
|
|
|
|
await wait(2);
|
|
|
|
}
|
|
|
|
}
|