feat: improve initial load performance on mobile (#2126)
This commit is contained in:
@@ -25,7 +25,7 @@ describe('application', () => {
|
||||
|
||||
device = {} as jest.Mocked<DeviceInterface>
|
||||
device.openDatabase = jest.fn().mockResolvedValue(true)
|
||||
device.getAllRawDatabasePayloads = jest.fn().mockReturnValue([])
|
||||
device.getAllDatabaseEntries = jest.fn().mockReturnValue([])
|
||||
device.setRawStorageValue = jest.fn()
|
||||
device.getRawStorageValue = jest.fn().mockImplementation((key) => {
|
||||
if (key === namespacedKey(identifier, RawStorageKey.SnjsVersion)) {
|
||||
@@ -33,9 +33,6 @@ describe('application', () => {
|
||||
}
|
||||
return undefined
|
||||
})
|
||||
device.getDatabaseKeys = async () => {
|
||||
return Promise.resolve(['1', '2', '3'])
|
||||
}
|
||||
|
||||
application = new SNApplication({
|
||||
environment: Environment.Mobile,
|
||||
@@ -75,7 +72,6 @@ describe('application', () => {
|
||||
currentPersistPromise: false,
|
||||
isStorageWrapped: false,
|
||||
allRawPayloadsCount: 0,
|
||||
databaseKeys: ['1', '2', '3'],
|
||||
},
|
||||
encryption: expect.objectContaining({
|
||||
getLatestVersion: '004',
|
||||
|
||||
@@ -410,28 +410,32 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
|
||||
await this.notifyEvent(ApplicationEvent.Launched)
|
||||
await this.handleStage(ExternalServices.ApplicationStage.Launched_10)
|
||||
|
||||
const databasePayloads = await this.syncService.getDatabasePayloads()
|
||||
await this.handleStage(ExternalServices.ApplicationStage.LoadingDatabase_11)
|
||||
|
||||
if (this.createdNewDatabase) {
|
||||
await this.syncService.onNewDatabaseCreated()
|
||||
}
|
||||
/**
|
||||
* We don't want to await this, as we want to begin allowing the app to function
|
||||
* before local data has been loaded fully. We await only initial
|
||||
* `getDatabasePayloads` to lock in on database state.
|
||||
* before local data has been loaded fully.
|
||||
*/
|
||||
const loadPromise = this.syncService.loadDatabasePayloads(databasePayloads).then(async () => {
|
||||
if (this.dealloced) {
|
||||
throw 'Application has been destroyed.'
|
||||
}
|
||||
await this.handleStage(ExternalServices.ApplicationStage.LoadedDatabase_12)
|
||||
this.beginAutoSyncTimer()
|
||||
await this.syncService.sync({
|
||||
mode: ExternalServices.SyncMode.DownloadFirst,
|
||||
source: ExternalServices.SyncSource.External,
|
||||
const loadPromise = this.syncService
|
||||
.loadDatabasePayloads()
|
||||
.then(async () => {
|
||||
if (this.dealloced) {
|
||||
throw 'Application has been destroyed.'
|
||||
}
|
||||
await this.handleStage(ExternalServices.ApplicationStage.LoadedDatabase_12)
|
||||
this.beginAutoSyncTimer()
|
||||
await this.syncService.sync({
|
||||
mode: ExternalServices.SyncMode.DownloadFirst,
|
||||
source: ExternalServices.SyncSource.External,
|
||||
sourceDescription: 'Application Launch',
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
void this.notifyEvent(ApplicationEvent.LocalDatabaseReadError, error)
|
||||
throw error
|
||||
})
|
||||
})
|
||||
if (awaitDatabaseLoad) {
|
||||
await loadPromise
|
||||
}
|
||||
@@ -463,7 +467,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
|
||||
private beginAutoSyncTimer() {
|
||||
this.autoSyncInterval = setInterval(() => {
|
||||
this.syncService.log('Syncing from autosync')
|
||||
void this.sync.sync()
|
||||
void this.sync.sync({ sourceDescription: 'Auto Sync' })
|
||||
}, DEFAULT_AUTO_SYNC_INTERVAL)
|
||||
}
|
||||
|
||||
@@ -1542,10 +1546,10 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
|
||||
switch (event) {
|
||||
case InternalServices.SessionEvent.Restored: {
|
||||
void (async () => {
|
||||
await this.sync.sync()
|
||||
await this.sync.sync({ sourceDescription: 'Session restored pre key creation' })
|
||||
if (this.protocolService.needsNewRootKeyBasedItemsKey()) {
|
||||
void this.protocolService.createNewDefaultItemsKey().then(() => {
|
||||
void this.sync.sync()
|
||||
void this.sync.sync({ sourceDescription: 'Session restored post key creation' })
|
||||
})
|
||||
}
|
||||
})()
|
||||
@@ -1573,6 +1577,8 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
|
||||
this.payloadManager,
|
||||
this.apiService,
|
||||
this.historyManager,
|
||||
this.deviceInterface,
|
||||
this.identifier,
|
||||
{
|
||||
loadBatchSize: this.options.loadBatchSize,
|
||||
},
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from './Application'
|
||||
export * from './Event'
|
||||
export * from './LiveItem'
|
||||
export * from './Platforms'
|
||||
export * from './Options/Defaults'
|
||||
|
||||
22
packages/snjs/lib/Logging.ts
Normal file
22
packages/snjs/lib/Logging.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { log as utilsLog } from '@standardnotes/utils'
|
||||
|
||||
export const isDev = true
|
||||
|
||||
export enum LoggingDomain {
|
||||
DatabaseLoad,
|
||||
Sync,
|
||||
}
|
||||
|
||||
const LoggingStatus: Record<LoggingDomain, boolean> = {
|
||||
[LoggingDomain.DatabaseLoad]: false,
|
||||
[LoggingDomain.Sync]: false,
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function log(domain: LoggingDomain, ...args: any[]): void {
|
||||
if (!isDev || !LoggingStatus[domain]) {
|
||||
return
|
||||
}
|
||||
|
||||
utilsLog(LoggingDomain[domain], ...args)
|
||||
}
|
||||
@@ -165,12 +165,11 @@ export class BaseMigration extends Migration {
|
||||
}
|
||||
|
||||
private async repairMissingKeychain() {
|
||||
const version = (await this.getStoredVersion()) as string
|
||||
const rawAccountParams = await this.reader.getAccountKeyParams()
|
||||
|
||||
/** Choose an item to decrypt against */
|
||||
const allItems = (
|
||||
await this.services.deviceInterface.getAllRawDatabasePayloads<EncryptedTransferPayload>(this.services.identifier)
|
||||
await this.services.deviceInterface.getAllDatabaseEntries<EncryptedTransferPayload>(this.services.identifier)
|
||||
).map((p) => new EncryptedPayload(p))
|
||||
|
||||
let itemToDecrypt = allItems.find((item) => {
|
||||
@@ -226,21 +225,10 @@ export class BaseMigration extends Migration {
|
||||
)
|
||||
} else {
|
||||
/**
|
||||
* If decryption succeeds, store the generated account key where it is expected,
|
||||
* either in top-level keychain in 1.0.0, and namespaced location in 2.0.0+.
|
||||
* If decryption succeeds, store the generated account key where it is expected.
|
||||
*/
|
||||
if (version === PreviousSnjsVersion1_0_0) {
|
||||
/** Store in top level keychain */
|
||||
await this.services.deviceInterface.setLegacyRawKeychainValue({
|
||||
mk: rootKey.masterKey,
|
||||
ak: rootKey.dataAuthenticationKey as string,
|
||||
version: accountParams.version,
|
||||
})
|
||||
} else {
|
||||
/** Store in namespaced location */
|
||||
const rawKey = rootKey.getKeychainValue()
|
||||
await this.services.deviceInterface.setNamespacedKeychainValue(rawKey, this.services.identifier)
|
||||
}
|
||||
const rawKey = rootKey.getKeychainValue()
|
||||
await this.services.deviceInterface.setNamespacedKeychainValue(rawKey, this.services.identifier)
|
||||
resolve(true)
|
||||
this.services.challengeService.completeChallenge(challenge)
|
||||
}
|
||||
|
||||
@@ -5,9 +5,7 @@ import { DeviceInterface } from '@standardnotes/services'
|
||||
import { StorageReader } from './Reader'
|
||||
import * as ReaderClasses from './Versions'
|
||||
|
||||
function ReaderClassForVersion(
|
||||
version: string,
|
||||
): typeof ReaderClasses.StorageReader2_0_0 | typeof ReaderClasses.StorageReader1_0_0 {
|
||||
function ReaderClassForVersion(version: string): typeof ReaderClasses.StorageReader2_0_0 {
|
||||
/** Sort readers by newest first */
|
||||
const allReaders = Object.values(ReaderClasses).sort((a, b) => {
|
||||
return compareSemVersions(a.version(), b.version()) * -1
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import { isNullOrUndefined } from '@standardnotes/utils'
|
||||
import { isEnvironmentMobile } from '@Lib/Application/Platforms'
|
||||
import { PreviousSnjsVersion1_0_0 } from '../../../Version'
|
||||
import { isMobileDevice, LegacyKeys1_0_0 } from '@standardnotes/services'
|
||||
import { StorageReader } from '../Reader'
|
||||
|
||||
export class StorageReader1_0_0 extends StorageReader {
|
||||
static override version() {
|
||||
return PreviousSnjsVersion1_0_0
|
||||
}
|
||||
|
||||
public async getAccountKeyParams() {
|
||||
return this.deviceInterface.getJsonParsedRawStorageValue(LegacyKeys1_0_0.AllAccountKeyParamsKey)
|
||||
}
|
||||
|
||||
/**
|
||||
* In 1.0.0, web uses raw storage for unwrapped account key, and mobile uses
|
||||
* the keychain
|
||||
*/
|
||||
public async hasNonWrappedAccountKeys() {
|
||||
if (isMobileDevice(this.deviceInterface)) {
|
||||
const value = await this.deviceInterface.getRawKeychainValue()
|
||||
return !isNullOrUndefined(value)
|
||||
} else {
|
||||
const value = await this.deviceInterface.getRawStorageValue('mk')
|
||||
return !isNullOrUndefined(value)
|
||||
}
|
||||
}
|
||||
|
||||
public async hasPasscode() {
|
||||
if (isEnvironmentMobile(this.environment)) {
|
||||
const rawPasscodeParams = await this.deviceInterface.getJsonParsedRawStorageValue(
|
||||
LegacyKeys1_0_0.MobilePasscodeParamsKey,
|
||||
)
|
||||
return !isNullOrUndefined(rawPasscodeParams)
|
||||
} else {
|
||||
const encryptedStorage = await this.deviceInterface.getJsonParsedRawStorageValue(
|
||||
LegacyKeys1_0_0.WebEncryptedStorageKey,
|
||||
)
|
||||
return !isNullOrUndefined(encryptedStorage)
|
||||
}
|
||||
}
|
||||
|
||||
/** Keychain was not used on desktop/web in 1.0.0 */
|
||||
public usesKeychain() {
|
||||
return isEnvironmentMobile(this.environment) ? true : false
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
export { StorageReader2_0_0 } from './Reader2_0_0'
|
||||
export { StorageReader1_0_0 } from './Reader1_0_0'
|
||||
|
||||
@@ -1,730 +0,0 @@
|
||||
import { AnyKeyParamsContent, ContentType, ProtocolVersion } from '@standardnotes/common'
|
||||
import { Migration } from '@Lib/Migrations/Migration'
|
||||
import { MigrationServices } from '../MigrationServices'
|
||||
import { PreviousSnjsVersion2_0_0 } from '../../Version'
|
||||
import { SNRootKey, CreateNewRootKey } from '@standardnotes/encryption'
|
||||
import { DiskStorageService } from '../../Services/Storage/DiskStorageService'
|
||||
import { StorageReader1_0_0 } from '../StorageReaders/Versions/Reader1_0_0'
|
||||
import * as Models from '@standardnotes/models'
|
||||
import * as Services from '@standardnotes/services'
|
||||
import * as Utils from '@standardnotes/utils'
|
||||
import { isEnvironmentMobile, isEnvironmentWebOrDesktop } from '@Lib/Application/Platforms'
|
||||
import {
|
||||
getIncrementedDirtyIndex,
|
||||
LegacyMobileKeychainStructure,
|
||||
PayloadTimestampDefaults,
|
||||
} from '@standardnotes/models'
|
||||
import { isMobileDevice } from '@standardnotes/services'
|
||||
import { LegacySession } from '@standardnotes/domain-core'
|
||||
|
||||
interface LegacyStorageContent extends Models.ItemContent {
|
||||
storage: unknown
|
||||
}
|
||||
|
||||
interface LegacyAccountKeysValue {
|
||||
ak: string
|
||||
mk: string
|
||||
version: string
|
||||
jwt: string
|
||||
}
|
||||
|
||||
interface LegacyRootKeyContent extends Models.RootKeyContent {
|
||||
accountKeys?: LegacyAccountKeysValue
|
||||
}
|
||||
|
||||
const LEGACY_SESSION_TOKEN_KEY = 'jwt'
|
||||
|
||||
export class Migration2_0_0 extends Migration {
|
||||
private legacyReader!: StorageReader1_0_0
|
||||
|
||||
constructor(services: MigrationServices) {
|
||||
super(services)
|
||||
this.legacyReader = new StorageReader1_0_0(
|
||||
this.services.deviceInterface,
|
||||
this.services.identifier,
|
||||
this.services.environment,
|
||||
)
|
||||
}
|
||||
|
||||
static override version() {
|
||||
return PreviousSnjsVersion2_0_0
|
||||
}
|
||||
|
||||
protected registerStageHandlers() {
|
||||
this.registerStageHandler(Services.ApplicationStage.PreparingForLaunch_0, async () => {
|
||||
if (isEnvironmentWebOrDesktop(this.services.environment)) {
|
||||
await this.migrateStorageStructureForWebDesktop()
|
||||
} else if (isEnvironmentMobile(this.services.environment)) {
|
||||
await this.migrateStorageStructureForMobile()
|
||||
}
|
||||
})
|
||||
this.registerStageHandler(Services.ApplicationStage.StorageDecrypted_09, async () => {
|
||||
await this.migrateArbitraryRawStorageToManagedStorageAllPlatforms()
|
||||
if (isEnvironmentMobile(this.services.environment)) {
|
||||
await this.migrateMobilePreferences()
|
||||
}
|
||||
await this.migrateSessionStorage()
|
||||
await this.deleteLegacyStorageValues()
|
||||
})
|
||||
this.registerStageHandler(Services.ApplicationStage.LoadingDatabase_11, async () => {
|
||||
await this.createDefaultItemsKeyForAllPlatforms()
|
||||
this.markDone()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Web
|
||||
* Migrates legacy storage structure into new managed format.
|
||||
* If encrypted storage exists, we need to first decrypt it with the passcode.
|
||||
* Then extract the account key from it. Then, encrypt storage with the
|
||||
* account key. Then encrypt the account key with the passcode and store it
|
||||
* within the new storage format.
|
||||
*
|
||||
* Generate note: We do not use the keychain if passcode is available.
|
||||
*/
|
||||
private async migrateStorageStructureForWebDesktop() {
|
||||
const deviceInterface = this.services.deviceInterface
|
||||
const newStorageRawStructure: Services.StorageValuesObject = {
|
||||
[Services.ValueModesKeys.Wrapped]: {} as Models.LocalStorageEncryptedContextualPayload,
|
||||
[Services.ValueModesKeys.Unwrapped]: {},
|
||||
[Services.ValueModesKeys.Nonwrapped]: {},
|
||||
}
|
||||
const rawAccountKeyParams = (await this.legacyReader.getAccountKeyParams()) as AnyKeyParamsContent
|
||||
/** Could be null if no account, or if account and storage is encrypted */
|
||||
if (rawAccountKeyParams) {
|
||||
newStorageRawStructure.nonwrapped[Services.StorageKey.RootKeyParams] = rawAccountKeyParams
|
||||
}
|
||||
const encryptedStorage = (await deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.WebEncryptedStorageKey,
|
||||
)) as Models.EncryptedTransferPayload
|
||||
|
||||
if (encryptedStorage) {
|
||||
const encryptedStoragePayload = new Models.EncryptedPayload(encryptedStorage)
|
||||
|
||||
const passcodeResult = await this.webDesktopHelperGetPasscodeKeyAndDecryptEncryptedStorage(
|
||||
encryptedStoragePayload,
|
||||
)
|
||||
|
||||
const passcodeKey = passcodeResult.key
|
||||
const decryptedStoragePayload = passcodeResult.decryptedStoragePayload
|
||||
const passcodeParams = passcodeResult.keyParams
|
||||
|
||||
newStorageRawStructure.nonwrapped[Services.StorageKey.RootKeyWrapperKeyParams] = passcodeParams.getPortableValue()
|
||||
|
||||
const rawStorageValueStore = Utils.Copy(decryptedStoragePayload.content.storage)
|
||||
const storageValueStore: Record<string, unknown> = Utils.jsonParseEmbeddedKeys(rawStorageValueStore)
|
||||
/** Store previously encrypted auth_params into new nonwrapped value key */
|
||||
|
||||
const accountKeyParams = storageValueStore[Services.LegacyKeys1_0_0.AllAccountKeyParamsKey] as AnyKeyParamsContent
|
||||
newStorageRawStructure.nonwrapped[Services.StorageKey.RootKeyParams] = accountKeyParams
|
||||
|
||||
let keyToEncryptStorageWith = passcodeKey
|
||||
/** Extract account key (mk, pw, ak) if it exists */
|
||||
const hasAccountKeys = !Utils.isNullOrUndefined(storageValueStore.mk)
|
||||
|
||||
if (hasAccountKeys) {
|
||||
const { accountKey, wrappedKey } = await this.webDesktopHelperExtractAndWrapAccountKeysFromValueStore(
|
||||
passcodeKey,
|
||||
accountKeyParams,
|
||||
storageValueStore,
|
||||
)
|
||||
keyToEncryptStorageWith = accountKey
|
||||
newStorageRawStructure.nonwrapped[Services.StorageKey.WrappedRootKey] = wrappedKey
|
||||
}
|
||||
|
||||
/** Encrypt storage with proper key */
|
||||
newStorageRawStructure.wrapped = await this.webDesktopHelperEncryptStorage(
|
||||
keyToEncryptStorageWith,
|
||||
decryptedStoragePayload,
|
||||
storageValueStore,
|
||||
)
|
||||
} else {
|
||||
/**
|
||||
* No encrypted storage, take account keys (if they exist) out of raw storage
|
||||
* and place them in the keychain. */
|
||||
const ak = await this.services.deviceInterface.getRawStorageValue('ak')
|
||||
const mk = await this.services.deviceInterface.getRawStorageValue('mk')
|
||||
|
||||
if (ak || mk) {
|
||||
const version = rawAccountKeyParams.version || (await this.getFallbackRootKeyVersion())
|
||||
|
||||
const accountKey = CreateNewRootKey({
|
||||
masterKey: mk as string,
|
||||
dataAuthenticationKey: ak as string,
|
||||
version: version,
|
||||
keyParams: rawAccountKeyParams,
|
||||
})
|
||||
await this.services.deviceInterface.setNamespacedKeychainValue(
|
||||
accountKey.getKeychainValue(),
|
||||
this.services.identifier,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Persist storage under new key and structure */
|
||||
await this.allPlatformHelperSetStorageStructure(newStorageRawStructure)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper
|
||||
* All platforms
|
||||
*/
|
||||
private async allPlatformHelperSetStorageStructure(rawStructure: Services.StorageValuesObject) {
|
||||
const newStructure = DiskStorageService.DefaultValuesObject(
|
||||
rawStructure.wrapped,
|
||||
rawStructure.unwrapped,
|
||||
rawStructure.nonwrapped,
|
||||
) as Partial<Services.StorageValuesObject>
|
||||
|
||||
newStructure[Services.ValueModesKeys.Unwrapped] = undefined
|
||||
|
||||
await this.services.deviceInterface.setRawStorageValue(
|
||||
Services.namespacedKey(this.services.identifier, Services.RawStorageKey.StorageObject),
|
||||
JSON.stringify(newStructure),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper
|
||||
* Web/desktop only
|
||||
*/
|
||||
private async webDesktopHelperGetPasscodeKeyAndDecryptEncryptedStorage(
|
||||
encryptedPayload: Models.EncryptedPayloadInterface,
|
||||
) {
|
||||
const rawPasscodeParams = (await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.WebPasscodeParamsKey,
|
||||
)) as AnyKeyParamsContent
|
||||
const passcodeParams = this.services.protocolService.createKeyParams(rawPasscodeParams)
|
||||
|
||||
/** Decrypt it with the passcode */
|
||||
let decryptedStoragePayload:
|
||||
| Models.DecryptedPayloadInterface<LegacyStorageContent>
|
||||
| Models.EncryptedPayloadInterface = encryptedPayload
|
||||
let passcodeKey: SNRootKey | undefined
|
||||
|
||||
await this.promptForPasscodeUntilCorrect(async (candidate: string) => {
|
||||
passcodeKey = await this.services.protocolService.computeRootKey(candidate, passcodeParams)
|
||||
decryptedStoragePayload = await this.services.protocolService.decryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [encryptedPayload],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
|
||||
return !Models.isErrorDecryptingPayload(decryptedStoragePayload)
|
||||
})
|
||||
|
||||
return {
|
||||
decryptedStoragePayload:
|
||||
decryptedStoragePayload as unknown as Models.DecryptedPayloadInterface<LegacyStorageContent>,
|
||||
key: passcodeKey as SNRootKey,
|
||||
keyParams: passcodeParams,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper
|
||||
* Web/desktop only
|
||||
*/
|
||||
private async webDesktopHelperExtractAndWrapAccountKeysFromValueStore(
|
||||
passcodeKey: SNRootKey,
|
||||
accountKeyParams: AnyKeyParamsContent,
|
||||
storageValueStore: Record<string, unknown>,
|
||||
) {
|
||||
const version = accountKeyParams?.version || (await this.getFallbackRootKeyVersion())
|
||||
const accountKey = CreateNewRootKey({
|
||||
masterKey: storageValueStore.mk as string,
|
||||
dataAuthenticationKey: storageValueStore.ak as string,
|
||||
version: version,
|
||||
keyParams: accountKeyParams,
|
||||
})
|
||||
|
||||
delete storageValueStore.mk
|
||||
delete storageValueStore.pw
|
||||
delete storageValueStore.ak
|
||||
|
||||
const accountKeyPayload = accountKey.payload
|
||||
|
||||
/** Encrypt account key with passcode */
|
||||
const encryptedAccountKey = await this.services.protocolService.encryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [accountKeyPayload],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
return {
|
||||
accountKey: accountKey,
|
||||
wrappedKey: Models.CreateEncryptedLocalStorageContextPayload(encryptedAccountKey),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper
|
||||
* Web/desktop only
|
||||
* Encrypt storage with account key
|
||||
*/
|
||||
async webDesktopHelperEncryptStorage(
|
||||
key: SNRootKey,
|
||||
decryptedStoragePayload: Models.DecryptedPayloadInterface,
|
||||
storageValueStore: Record<string, unknown>,
|
||||
) {
|
||||
const wrapped = await this.services.protocolService.encryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [
|
||||
decryptedStoragePayload.copy({
|
||||
content_type: ContentType.EncryptedStorage,
|
||||
content: storageValueStore as unknown as Models.ItemContent,
|
||||
}),
|
||||
],
|
||||
key: key,
|
||||
},
|
||||
})
|
||||
|
||||
return Models.CreateEncryptedLocalStorageContextPayload(wrapped)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mobile
|
||||
* On mobile legacy structure is mostly similar to new structure,
|
||||
* in that the account key is encrypted with the passcode. But mobile did
|
||||
* not have encrypted storage, so we simply need to transfer all existing
|
||||
* storage values into new managed structure.
|
||||
*
|
||||
* In version <= 3.0.16 on mobile, encrypted account keys were stored in the keychain
|
||||
* under `encryptedAccountKeys`. In 3.0.17 a migration was introduced that moved this value
|
||||
* to storage under key `encrypted_account_keys`. We need to anticipate the keys being in
|
||||
* either location.
|
||||
*
|
||||
* If no account but passcode only, the only thing we stored on mobile
|
||||
* previously was keys.offline.pw and keys.offline.timing in the keychain
|
||||
* that we compared against for valid decryption.
|
||||
* In the new version, we know a passcode is correct if it can decrypt storage.
|
||||
* As part of the migration, we’ll need to request the raw passcode from user,
|
||||
* compare it against the keychain offline.pw value, and if correct,
|
||||
* migrate storage to new structure, and encrypt with passcode key.
|
||||
*
|
||||
* If account only, take the value in the keychain, and rename the values
|
||||
* (i.e mk > masterKey).
|
||||
* @access private
|
||||
*/
|
||||
async migrateStorageStructureForMobile() {
|
||||
Utils.assert(isMobileDevice(this.services.deviceInterface))
|
||||
|
||||
const keychainValue =
|
||||
(await this.services.deviceInterface.getRawKeychainValue()) as unknown as LegacyMobileKeychainStructure
|
||||
|
||||
const wrappedAccountKey = ((await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobileWrappedRootKeyKey,
|
||||
)) || keychainValue?.encryptedAccountKeys) as Models.EncryptedTransferPayload
|
||||
|
||||
const rawAccountKeyParams = (await this.legacyReader.getAccountKeyParams()) as AnyKeyParamsContent
|
||||
|
||||
const rawPasscodeParams = (await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobilePasscodeParamsKey,
|
||||
)) as AnyKeyParamsContent
|
||||
|
||||
const firstRunValue = await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.NonwrappedStorageKey.MobileFirstRun,
|
||||
)
|
||||
|
||||
const rawStructure: Services.StorageValuesObject = {
|
||||
[Services.ValueModesKeys.Nonwrapped]: {
|
||||
[Services.StorageKey.WrappedRootKey]: wrappedAccountKey,
|
||||
/** A 'hash' key may be present from legacy versions that should be deleted */
|
||||
[Services.StorageKey.RootKeyWrapperKeyParams]: Utils.omitByCopy(rawPasscodeParams, ['hash' as never]),
|
||||
[Services.StorageKey.RootKeyParams]: rawAccountKeyParams,
|
||||
[Services.NonwrappedStorageKey.MobileFirstRun]: firstRunValue,
|
||||
},
|
||||
[Services.ValueModesKeys.Unwrapped]: {},
|
||||
[Services.ValueModesKeys.Wrapped]: {} as Models.LocalStorageDecryptedContextualPayload,
|
||||
}
|
||||
|
||||
const biometricPrefs = (await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobileBiometricsPrefs,
|
||||
)) as { enabled: boolean; timing: unknown }
|
||||
|
||||
if (biometricPrefs) {
|
||||
rawStructure.nonwrapped[Services.StorageKey.BiometricsState] = biometricPrefs.enabled
|
||||
rawStructure.nonwrapped[Services.StorageKey.MobileBiometricsTiming] = biometricPrefs.timing
|
||||
}
|
||||
|
||||
const passcodeKeyboardType = await this.services.deviceInterface.getRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobilePasscodeKeyboardType,
|
||||
)
|
||||
|
||||
if (passcodeKeyboardType) {
|
||||
rawStructure.nonwrapped[Services.StorageKey.MobilePasscodeKeyboardType] = passcodeKeyboardType
|
||||
}
|
||||
|
||||
if (rawPasscodeParams) {
|
||||
const passcodeParams = this.services.protocolService.createKeyParams(rawPasscodeParams)
|
||||
const getPasscodeKey = async () => {
|
||||
let passcodeKey: SNRootKey | undefined
|
||||
|
||||
await this.promptForPasscodeUntilCorrect(async (candidate: string) => {
|
||||
passcodeKey = await this.services.protocolService.computeRootKey(candidate, passcodeParams)
|
||||
|
||||
const pwHash = keychainValue?.offline?.pw
|
||||
|
||||
if (pwHash) {
|
||||
return passcodeKey.serverPassword === pwHash
|
||||
} else {
|
||||
/**
|
||||
* Fallback decryption if keychain is missing for some reason. If account,
|
||||
* validate by attempting to decrypt wrapped account key. Otherwise, validate
|
||||
* by attempting to decrypt random item.
|
||||
* */
|
||||
if (wrappedAccountKey) {
|
||||
const decryptedAcctKey = await this.services.protocolService.decryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [new Models.EncryptedPayload(wrappedAccountKey)],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
return !Models.isErrorDecryptingPayload(decryptedAcctKey)
|
||||
} else {
|
||||
const item = (
|
||||
await this.services.deviceInterface.getAllRawDatabasePayloads(this.services.identifier)
|
||||
)[0] as Models.EncryptedTransferPayload
|
||||
|
||||
if (!item) {
|
||||
throw Error('Passcode only migration aborting due to missing keychain.offline.pw')
|
||||
}
|
||||
|
||||
const decryptedPayload = await this.services.protocolService.decryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [new Models.EncryptedPayload(item)],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
return !Models.isErrorDecryptingPayload(decryptedPayload)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return passcodeKey as SNRootKey
|
||||
}
|
||||
|
||||
rawStructure.nonwrapped[Services.StorageKey.MobilePasscodeTiming] = keychainValue?.offline?.timing
|
||||
|
||||
if (wrappedAccountKey) {
|
||||
/**
|
||||
* Account key is encrypted with passcode. Inside, the accountKey is located inside
|
||||
* content.accountKeys. We want to unembed these values to main content, rename
|
||||
* with proper property names, wrap again, and store in new rawStructure.
|
||||
*/
|
||||
const passcodeKey = await getPasscodeKey()
|
||||
const payload = new Models.EncryptedPayload(wrappedAccountKey)
|
||||
const unwrappedAccountKey = await this.services.protocolService.decryptSplitSingle<LegacyRootKeyContent>({
|
||||
usesRootKey: {
|
||||
items: [payload],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
|
||||
if (Models.isErrorDecryptingPayload(unwrappedAccountKey)) {
|
||||
return
|
||||
}
|
||||
|
||||
const accountKeyContent = unwrappedAccountKey.content.accountKeys as LegacyAccountKeysValue
|
||||
|
||||
const version =
|
||||
accountKeyContent.version || rawAccountKeyParams?.version || (await this.getFallbackRootKeyVersion())
|
||||
|
||||
const newAccountKey = unwrappedAccountKey.copy({
|
||||
content: Models.FillItemContent<LegacyRootKeyContent>({
|
||||
masterKey: accountKeyContent.mk,
|
||||
dataAuthenticationKey: accountKeyContent.ak,
|
||||
version: version as ProtocolVersion,
|
||||
keyParams: rawAccountKeyParams,
|
||||
accountKeys: undefined,
|
||||
}),
|
||||
})
|
||||
|
||||
const newWrappedAccountKey = await this.services.protocolService.encryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [newAccountKey],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
rawStructure.nonwrapped[Services.StorageKey.WrappedRootKey] =
|
||||
Models.CreateEncryptedLocalStorageContextPayload(newWrappedAccountKey)
|
||||
|
||||
if (accountKeyContent.jwt) {
|
||||
/** Move the jwt to raw storage so that it can be migrated in `migrateSessionStorage` */
|
||||
void this.services.deviceInterface.setRawStorageValue(LEGACY_SESSION_TOKEN_KEY, accountKeyContent.jwt)
|
||||
}
|
||||
await this.services.deviceInterface.clearRawKeychainValue()
|
||||
} else if (!wrappedAccountKey) {
|
||||
/** Passcode only, no account */
|
||||
const passcodeKey = await getPasscodeKey()
|
||||
const payload = new Models.DecryptedPayload({
|
||||
uuid: Utils.UuidGenerator.GenerateUuid(),
|
||||
content: Models.FillItemContent(rawStructure.unwrapped),
|
||||
content_type: ContentType.EncryptedStorage,
|
||||
...PayloadTimestampDefaults(),
|
||||
})
|
||||
|
||||
/** Encrypt new storage.unwrapped structure with passcode */
|
||||
const wrapped = await this.services.protocolService.encryptSplitSingle({
|
||||
usesRootKey: {
|
||||
items: [payload],
|
||||
key: passcodeKey,
|
||||
},
|
||||
})
|
||||
rawStructure.wrapped = Models.CreateEncryptedLocalStorageContextPayload(wrapped)
|
||||
|
||||
await this.services.deviceInterface.clearRawKeychainValue()
|
||||
}
|
||||
} else {
|
||||
/** No passcode, potentially account. Migrate keychain property keys. */
|
||||
const hasAccount = !Utils.isNullOrUndefined(keychainValue?.mk)
|
||||
if (hasAccount) {
|
||||
const accountVersion =
|
||||
(keychainValue.version as ProtocolVersion) ||
|
||||
rawAccountKeyParams?.version ||
|
||||
(await this.getFallbackRootKeyVersion())
|
||||
|
||||
const accountKey = CreateNewRootKey({
|
||||
masterKey: keychainValue.mk,
|
||||
dataAuthenticationKey: keychainValue.ak,
|
||||
version: accountVersion,
|
||||
keyParams: rawAccountKeyParams,
|
||||
})
|
||||
|
||||
await this.services.deviceInterface.setNamespacedKeychainValue(
|
||||
accountKey.getKeychainValue(),
|
||||
this.services.identifier,
|
||||
)
|
||||
|
||||
if (keychainValue.jwt) {
|
||||
/** Move the jwt to raw storage so that it can be migrated in `migrateSessionStorage` */
|
||||
void this.services.deviceInterface.setRawStorageValue(LEGACY_SESSION_TOKEN_KEY, keychainValue.jwt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Move encrypted account key into place where it is now expected */
|
||||
await this.allPlatformHelperSetStorageStructure(rawStructure)
|
||||
}
|
||||
|
||||
/**
|
||||
* If we are unable to determine a root key's version, due to missing version
|
||||
* parameter from key params due to 001 or 002, we need to fallback to checking
|
||||
* any encrypted payload and retrieving its version.
|
||||
*
|
||||
* If we are unable to garner any meaningful information, we will default to 002.
|
||||
*
|
||||
* (Previously we attempted to discern version based on presence of keys.ak; if ak,
|
||||
* then 003, otherwise 002. However, late versions of 002 also inluded an ak, so this
|
||||
* method can't be used. This method also didn't account for 001 versions.)
|
||||
*/
|
||||
private async getFallbackRootKeyVersion() {
|
||||
const anyItem = (
|
||||
await this.services.deviceInterface.getAllRawDatabasePayloads(this.services.identifier)
|
||||
)[0] as Models.EncryptedTransferPayload
|
||||
|
||||
if (!anyItem) {
|
||||
return ProtocolVersion.V002
|
||||
}
|
||||
|
||||
const payload = new Models.EncryptedPayload(anyItem)
|
||||
return payload.version || ProtocolVersion.V002
|
||||
}
|
||||
|
||||
/**
|
||||
* All platforms
|
||||
* Migrate all previously independently stored storage keys into new
|
||||
* managed approach.
|
||||
*/
|
||||
private async migrateArbitraryRawStorageToManagedStorageAllPlatforms() {
|
||||
const allKeyValues = await this.services.deviceInterface.getAllRawStorageKeyValues()
|
||||
const legacyKeys = Utils.objectToValueArray(Services.LegacyKeys1_0_0)
|
||||
|
||||
const tryJsonParse = (value: string) => {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch (e) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
const applicationIdentifier = this.services.identifier
|
||||
|
||||
for (const keyValuePair of allKeyValues) {
|
||||
const key = keyValuePair.key
|
||||
const value = keyValuePair.value
|
||||
const isNameSpacedKey =
|
||||
applicationIdentifier && applicationIdentifier.length > 0 && key.startsWith(applicationIdentifier)
|
||||
if (legacyKeys.includes(key) || isNameSpacedKey) {
|
||||
continue
|
||||
}
|
||||
if (!Utils.isNullOrUndefined(value)) {
|
||||
/**
|
||||
* Raw values should always have been json stringified.
|
||||
* New values should always be objects/parsed.
|
||||
*/
|
||||
const newValue = tryJsonParse(value as string)
|
||||
this.services.storageService.setValue(key, newValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All platforms
|
||||
* Deletes all StorageKey and LegacyKeys1_0_0 from root raw storage.
|
||||
* @access private
|
||||
*/
|
||||
async deleteLegacyStorageValues() {
|
||||
const miscKeys = [
|
||||
'mk',
|
||||
'ak',
|
||||
'pw',
|
||||
/** v1 unused key */
|
||||
'encryptionKey',
|
||||
/** v1 unused key */
|
||||
'authKey',
|
||||
'jwt',
|
||||
'ephemeral',
|
||||
'cachedThemes',
|
||||
]
|
||||
|
||||
const managedKeys = [
|
||||
...Utils.objectToValueArray(Services.StorageKey),
|
||||
...Utils.objectToValueArray(Services.LegacyKeys1_0_0),
|
||||
...miscKeys,
|
||||
]
|
||||
|
||||
for (const key of managedKeys) {
|
||||
await this.services.deviceInterface.removeRawStorageValue(key)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mobile
|
||||
* Migrate mobile preferences
|
||||
*/
|
||||
private async migrateMobilePreferences() {
|
||||
const lastExportDate = await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobileLastExportDate,
|
||||
)
|
||||
const doNotWarnUnsupportedEditors = await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobileDoNotWarnUnsupportedEditors,
|
||||
)
|
||||
const legacyOptionsState = (await this.services.deviceInterface.getJsonParsedRawStorageValue(
|
||||
Services.LegacyKeys1_0_0.MobileOptionsState,
|
||||
)) as Record<string, unknown>
|
||||
|
||||
let migratedOptionsState = {}
|
||||
|
||||
if (legacyOptionsState) {
|
||||
const legacySortBy = legacyOptionsState.sortBy
|
||||
migratedOptionsState = {
|
||||
sortBy:
|
||||
legacySortBy === 'updated_at' || legacySortBy === 'client_updated_at'
|
||||
? Models.CollectionSort.UpdatedAt
|
||||
: legacySortBy,
|
||||
sortReverse: legacyOptionsState.sortReverse ?? false,
|
||||
hideNotePreview: legacyOptionsState.hidePreviews ?? false,
|
||||
hideDate: legacyOptionsState.hideDates ?? false,
|
||||
hideTags: legacyOptionsState.hideTags ?? false,
|
||||
}
|
||||
}
|
||||
const preferences = {
|
||||
...migratedOptionsState,
|
||||
lastExportDate: lastExportDate ?? undefined,
|
||||
doNotShowAgainUnsupportedEditors: doNotWarnUnsupportedEditors ?? false,
|
||||
}
|
||||
await this.services.storageService.setValue(Services.StorageKey.MobilePreferences, preferences)
|
||||
}
|
||||
|
||||
/**
|
||||
* All platforms
|
||||
* Migrate previously stored session string token into object
|
||||
* On mobile, JWTs were previously stored in storage, inside of the user object,
|
||||
* but then custom-migrated to be stored in the keychain. We must account for
|
||||
* both scenarios here in case a user did not perform the custom platform migration.
|
||||
* On desktop/web, JWT was stored in storage.
|
||||
*/
|
||||
private migrateSessionStorage() {
|
||||
const USER_OBJECT_KEY = 'user'
|
||||
let currentToken = this.services.storageService.getValue<string | undefined>(LEGACY_SESSION_TOKEN_KEY)
|
||||
const user = this.services.storageService.getValue<{ jwt: string; server: string }>(USER_OBJECT_KEY)
|
||||
|
||||
if (!currentToken) {
|
||||
/** Try the user object */
|
||||
if (user) {
|
||||
currentToken = user.jwt
|
||||
}
|
||||
}
|
||||
|
||||
if (!currentToken) {
|
||||
/**
|
||||
* If we detect that a user object is present, but the jwt is missing,
|
||||
* we'll fill the jwt value with a junk value just so we create a session.
|
||||
* When the client attempts to talk to the server, the server will reply
|
||||
* with invalid token error, and the client will automatically prompt to reauthenticate.
|
||||
*/
|
||||
const hasAccount = !Utils.isNullOrUndefined(user)
|
||||
if (hasAccount) {
|
||||
currentToken = 'junk-value'
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
const sessionOrError = LegacySession.create(currentToken)
|
||||
if (!sessionOrError.isFailed()) {
|
||||
this.services.storageService.setValue(
|
||||
Services.StorageKey.Session,
|
||||
this.services.legacySessionStorageMapper.toProjection(sessionOrError.getValue()),
|
||||
)
|
||||
}
|
||||
|
||||
/** Server has to be migrated separately on mobile */
|
||||
if (isEnvironmentMobile(this.services.environment)) {
|
||||
if (user && user.server) {
|
||||
this.services.storageService.setValue(Services.StorageKey.ServerHost, user.server)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All platforms
|
||||
* Create new default items key from root key.
|
||||
* Otherwise, when data is loaded, we won't be able to decrypt it
|
||||
* without existence of an item key. This will mean that if this migration
|
||||
* is run on two different platforms for the same user, they will create
|
||||
* two new items keys. Which one they use to decrypt past items and encrypt
|
||||
* future items doesn't really matter.
|
||||
* @access private
|
||||
*/
|
||||
async createDefaultItemsKeyForAllPlatforms() {
|
||||
const rootKey = this.services.protocolService.getRootKey()
|
||||
if (rootKey) {
|
||||
const rootKeyParams = await this.services.protocolService.getRootKeyParams()
|
||||
/** If params are missing a version, it must be 001 */
|
||||
const fallbackVersion = ProtocolVersion.V001
|
||||
|
||||
const payload = new Models.DecryptedPayload({
|
||||
uuid: Utils.UuidGenerator.GenerateUuid(),
|
||||
content_type: ContentType.ItemsKey,
|
||||
content: Models.FillItemContentSpecialized<Models.ItemsKeyContentSpecialized, Models.ItemsKeyContent>({
|
||||
itemsKey: rootKey.masterKey,
|
||||
dataAuthenticationKey: rootKey.dataAuthenticationKey,
|
||||
version: rootKeyParams?.version || fallbackVersion,
|
||||
}),
|
||||
dirty: true,
|
||||
dirtyIndex: getIncrementedDirtyIndex(),
|
||||
...PayloadTimestampDefaults(),
|
||||
})
|
||||
|
||||
const itemsKey = Models.CreateDecryptedItemFromPayload(payload)
|
||||
|
||||
await this.services.itemManager.emitItemFromPayload(
|
||||
itemsKey.payloadRepresentation(),
|
||||
Models.PayloadEmitSource.LocalChanged,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,9 @@
|
||||
import { Migration2_0_0 } from './2_0_0'
|
||||
import { Migration2_0_15 } from './2_0_15'
|
||||
import { Migration2_7_0 } from './2_7_0'
|
||||
import { Migration2_20_0 } from './2_20_0'
|
||||
import { Migration2_36_0 } from './2_36_0'
|
||||
import { Migration2_42_0 } from './2_42_0'
|
||||
|
||||
export const MigrationClasses = [
|
||||
Migration2_0_0,
|
||||
Migration2_0_15,
|
||||
Migration2_7_0,
|
||||
Migration2_20_0,
|
||||
Migration2_36_0,
|
||||
Migration2_42_0,
|
||||
]
|
||||
export const MigrationClasses = [Migration2_0_15, Migration2_7_0, Migration2_20_0, Migration2_36_0, Migration2_42_0]
|
||||
|
||||
export { Migration2_0_0, Migration2_0_15, Migration2_7_0, Migration2_20_0, Migration2_36_0, Migration2_42_0 }
|
||||
export { Migration2_0_15, Migration2_7_0, Migration2_20_0, Migration2_36_0, Migration2_42_0 }
|
||||
|
||||
@@ -83,7 +83,7 @@ export class SNPreferencesService
|
||||
|
||||
void this.notifyEvent(PreferencesServiceEvent.PreferencesChanged)
|
||||
|
||||
void this.syncService.sync()
|
||||
void this.syncService.sync({ sourceDescription: 'PreferencesService.setValue' })
|
||||
}
|
||||
|
||||
private async reload() {
|
||||
|
||||
@@ -133,7 +133,7 @@ export class SNSingletonManager extends AbstractService {
|
||||
* of a download-first request.
|
||||
*/
|
||||
if (handled.length > 0 && eventSource === SyncEvent.SyncCompletedWithAllItemsUploaded) {
|
||||
await this.syncService?.sync()
|
||||
await this.syncService?.sync({ sourceDescription: 'Resolve singletons for items' })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ export class SNSingletonManager extends AbstractService {
|
||||
}
|
||||
})
|
||||
|
||||
await this.syncService.sync()
|
||||
await this.syncService.sync({ sourceDescription: 'Find or create singleton, before any sync has completed' })
|
||||
|
||||
removeObserver()
|
||||
|
||||
@@ -224,7 +224,7 @@ export class SNSingletonManager extends AbstractService {
|
||||
|
||||
const item = await this.itemManager.emitItemFromPayload(dirtyPayload, PayloadEmitSource.LocalInserted)
|
||||
|
||||
void this.syncService.sync()
|
||||
void this.syncService.sync({ sourceDescription: 'After find or create singleton' })
|
||||
|
||||
return item as T
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
PayloadTimestampDefaults,
|
||||
LocalStorageEncryptedContextualPayload,
|
||||
Environment,
|
||||
FullyFormedTransferPayload,
|
||||
} from '@standardnotes/models'
|
||||
|
||||
/**
|
||||
@@ -377,8 +378,8 @@ export class DiskStorageService extends Services.AbstractService implements Serv
|
||||
await this.immediatelyPersistValuesToDisk()
|
||||
}
|
||||
|
||||
public async getAllRawPayloads() {
|
||||
return this.deviceInterface.getAllRawDatabasePayloads(this.identifier)
|
||||
public async getAllRawPayloads(): Promise<FullyFormedTransferPayload[]> {
|
||||
return this.deviceInterface.getAllDatabaseEntries(this.identifier)
|
||||
}
|
||||
|
||||
public async savePayload(payload: FullyFormedPayloadInterface): Promise<void> {
|
||||
@@ -432,7 +433,7 @@ export class DiskStorageService extends Services.AbstractService implements Serv
|
||||
const exportedDeleted = deleted.map(CreateDeletedLocalStorageContextPayload)
|
||||
|
||||
return this.executeCriticalFunction(async () => {
|
||||
return this.deviceInterface?.saveRawDatabasePayloads(
|
||||
return this.deviceInterface?.saveDatabaseEntries(
|
||||
[...exportedEncrypted, ...exportedDecrypted, ...exportedDeleted],
|
||||
this.identifier,
|
||||
)
|
||||
@@ -449,13 +450,13 @@ export class DiskStorageService extends Services.AbstractService implements Serv
|
||||
|
||||
public async deletePayloadWithId(uuid: Uuid) {
|
||||
return this.executeCriticalFunction(async () => {
|
||||
return this.deviceInterface.removeRawDatabasePayloadWithId(uuid, this.identifier)
|
||||
return this.deviceInterface.removeDatabaseEntry(uuid, this.identifier)
|
||||
})
|
||||
}
|
||||
|
||||
public async clearAllPayloads() {
|
||||
return this.executeCriticalFunction(async () => {
|
||||
return this.deviceInterface.removeAllRawDatabasePayloads(this.identifier)
|
||||
return this.deviceInterface.removeAllDatabaseEntries(this.identifier)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -482,7 +483,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
|
||||
currentPersistPromise: this.currentPersistPromise != undefined,
|
||||
isStorageWrapped: this.isStorageWrapped(),
|
||||
allRawPayloadsCount: (await this.getAllRawPayloads()).length,
|
||||
databaseKeys: await this.deviceInterface.getDatabaseKeys(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { log, LoggingDomain } from './../../Logging'
|
||||
import { AccountSyncOperation } from '@Lib/Services/Sync/Account/Operation'
|
||||
import { ContentType } from '@standardnotes/common'
|
||||
import {
|
||||
@@ -18,7 +19,6 @@ import { SNHistoryManager } from '../History/HistoryManager'
|
||||
import { SNLog } from '@Lib/Log'
|
||||
import { SNSessionManager } from '../Session/SessionManager'
|
||||
import { DiskStorageService } from '../Storage/DiskStorageService'
|
||||
import { GetSortedPayloadsByPriority } from '@Lib/Services/Sync/Utils'
|
||||
import { SyncClientInterface } from './SyncClientInterface'
|
||||
import { SyncPromise } from './Types'
|
||||
import { SyncOpStatus } from '@Lib/Services/Sync/SyncOpStatus'
|
||||
@@ -33,7 +33,6 @@ import {
|
||||
DeltaOutOfSync,
|
||||
ImmutablePayloadCollection,
|
||||
CreatePayload,
|
||||
FullyFormedTransferPayload,
|
||||
isEncryptedPayload,
|
||||
isDecryptedPayload,
|
||||
EncryptedPayloadInterface,
|
||||
@@ -74,6 +73,9 @@ import {
|
||||
SyncServiceInterface,
|
||||
DiagnosticInfo,
|
||||
EncryptionService,
|
||||
DeviceInterface,
|
||||
isFullEntryLoadChunkResponse,
|
||||
isChunkFullEntry,
|
||||
} from '@standardnotes/services'
|
||||
import { OfflineSyncResponse } from './Offline/Response'
|
||||
import {
|
||||
@@ -142,6 +144,8 @@ export class SNSyncService
|
||||
private payloadManager: PayloadManager,
|
||||
private apiService: SNApiService,
|
||||
private historyService: SNHistoryManager,
|
||||
private device: DeviceInterface,
|
||||
private identifier: string,
|
||||
private readonly options: ApplicationSyncOptions,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
@@ -221,19 +225,13 @@ export class SNSyncService
|
||||
return this.databaseLoaded
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in tandem with `loadDatabasePayloads`
|
||||
*/
|
||||
public async getDatabasePayloads(): Promise<FullyFormedTransferPayload[]> {
|
||||
return this.storageService.getAllRawPayloads().catch((error) => {
|
||||
void this.notifyEvent(SyncEvent.DatabaseReadError, error)
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
private async processItemsKeysFirstDuringDatabaseLoad(
|
||||
itemsKeysPayloads: FullyFormedPayloadInterface[],
|
||||
): Promise<void> {
|
||||
if (itemsKeysPayloads.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const encryptedItemsKeysPayloads = itemsKeysPayloads.filter(isEncryptedPayload)
|
||||
|
||||
const originallyDecryptedItemsKeysPayloads = itemsKeysPayloads.filter(
|
||||
@@ -254,57 +252,69 @@ export class SNSyncService
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param rawPayloads - use `getDatabasePayloads` to get these payloads.
|
||||
* They are fed as a parameter so that callers don't have to await the loading, but can
|
||||
* await getting the raw payloads from storage
|
||||
*/
|
||||
public async loadDatabasePayloads(rawPayloads: FullyFormedTransferPayload[]): Promise<void> {
|
||||
public async loadDatabasePayloads(): Promise<void> {
|
||||
log(LoggingDomain.DatabaseLoad, 'Loading database payloads')
|
||||
|
||||
if (this.databaseLoaded) {
|
||||
throw 'Attempting to initialize already initialized local database.'
|
||||
}
|
||||
|
||||
if (rawPayloads.length === 0) {
|
||||
this.databaseLoaded = true
|
||||
this.opStatus.setDatabaseLoadStatus(0, 0, true)
|
||||
return
|
||||
}
|
||||
const chunks = await this.device.getDatabaseLoadChunks(
|
||||
{
|
||||
batchSize: this.options.loadBatchSize,
|
||||
contentTypePriority: this.localLoadPriorty,
|
||||
uuidPriority: this.launchPriorityUuids,
|
||||
},
|
||||
this.identifier,
|
||||
)
|
||||
|
||||
const unsortedPayloads = rawPayloads
|
||||
.map((rawPayload) => {
|
||||
const itemsKeyEntries = isFullEntryLoadChunkResponse(chunks)
|
||||
? chunks.fullEntries.itemsKeys.entries
|
||||
: await this.device.getDatabaseEntries(this.identifier, chunks.keys.itemsKeys.keys)
|
||||
|
||||
const itemsKeyPayloads = itemsKeyEntries
|
||||
.map((entry) => {
|
||||
try {
|
||||
return CreatePayload(rawPayload, PayloadSource.Constructor)
|
||||
return CreatePayload(entry, PayloadSource.Constructor)
|
||||
} catch (e) {
|
||||
console.error('Creating payload fail+ed', e)
|
||||
console.error('Creating payload failed', e)
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
.filter(isNotUndefined)
|
||||
|
||||
const { itemsKeyPayloads, contentTypePriorityPayloads, remainingPayloads } = GetSortedPayloadsByPriority(
|
||||
unsortedPayloads,
|
||||
this.localLoadPriorty,
|
||||
this.launchPriorityUuids,
|
||||
)
|
||||
|
||||
await this.processItemsKeysFirstDuringDatabaseLoad(itemsKeyPayloads)
|
||||
|
||||
await this.processPayloadBatch(contentTypePriorityPayloads)
|
||||
|
||||
/**
|
||||
* Map in batches to give interface a chance to update. Note that total decryption
|
||||
* time is constant regardless of batch size. Decrypting 3000 items all at once or in
|
||||
* batches will result in the same time spent. It's the emitting/painting/rendering
|
||||
* that requires batch size optimization.
|
||||
*/
|
||||
const payloadCount = remainingPayloads.length
|
||||
const batchSize = this.options.loadBatchSize
|
||||
const numBatches = Math.ceil(payloadCount / batchSize)
|
||||
const payloadCount = chunks.remainingChunksItemCount
|
||||
let totalProcessedCount = 0
|
||||
|
||||
for (let batchIndex = 0; batchIndex < numBatches; batchIndex++) {
|
||||
const currentPosition = batchIndex * batchSize
|
||||
const batch = remainingPayloads.slice(currentPosition, currentPosition + batchSize)
|
||||
await this.processPayloadBatch(batch, currentPosition, payloadCount)
|
||||
const remainingChunks = isFullEntryLoadChunkResponse(chunks)
|
||||
? chunks.fullEntries.remainingChunks
|
||||
: chunks.keys.remainingChunks
|
||||
|
||||
for (const chunk of remainingChunks) {
|
||||
const dbEntries = isChunkFullEntry(chunk)
|
||||
? chunk.entries
|
||||
: await this.device.getDatabaseEntries(this.identifier, chunk.keys)
|
||||
const payloads = dbEntries
|
||||
.map((entry) => {
|
||||
try {
|
||||
return CreatePayload(entry, PayloadSource.Constructor)
|
||||
} catch (e) {
|
||||
console.error('Creating payload failed', e)
|
||||
return undefined
|
||||
}
|
||||
})
|
||||
.filter(isNotUndefined)
|
||||
|
||||
await this.processPayloadBatch(payloads, totalProcessedCount, payloadCount)
|
||||
totalProcessedCount += payloads.length
|
||||
}
|
||||
|
||||
this.databaseLoaded = true
|
||||
@@ -316,6 +326,7 @@ export class SNSyncService
|
||||
currentPosition?: number,
|
||||
payloadCount?: number,
|
||||
) {
|
||||
log(LoggingDomain.DatabaseLoad, 'Processing batch at index', currentPosition, 'length', batch.length)
|
||||
const encrypted: EncryptedPayloadInterface[] = []
|
||||
const nonencrypted: (DecryptedPayloadInterface | DeletedPayloadInterface)[] = []
|
||||
|
||||
@@ -386,7 +397,7 @@ export class SNSyncService
|
||||
}
|
||||
|
||||
public async markAllItemsAsNeedingSyncAndPersist(): Promise<void> {
|
||||
this.log('Marking all items as needing sync')
|
||||
log(LoggingDomain.Sync, 'Marking all items as needing sync')
|
||||
|
||||
const items = this.itemManager.items
|
||||
const payloads = items.map((item) => {
|
||||
@@ -444,7 +455,7 @@ export class SNSyncService
|
||||
|
||||
const promise = this.spawnQueue[0]
|
||||
removeFromIndex(this.spawnQueue, 0)
|
||||
this.log('Syncing again from spawn queue')
|
||||
log(LoggingDomain.Sync, 'Syncing again from spawn queue')
|
||||
|
||||
return this.sync({
|
||||
queueStrategy: SyncQueueStrategy.ForceSpawnNew,
|
||||
@@ -506,7 +517,7 @@ export class SNSyncService
|
||||
|
||||
public async sync(options: Partial<SyncOptions> = {}): Promise<unknown> {
|
||||
if (this.clientLocked) {
|
||||
this.log('Sync locked by client')
|
||||
log(LoggingDomain.Sync, 'Sync locked by client')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -562,7 +573,7 @@ export class SNSyncService
|
||||
* (before reaching opStatus.setDidBegin).
|
||||
* 2. syncOpInProgress: If a sync() call is in flight to the server.
|
||||
*/
|
||||
private configureSyncLock() {
|
||||
private configureSyncLock(options: SyncOptions) {
|
||||
const syncInProgress = this.opStatus.syncInProgress
|
||||
const databaseLoaded = this.databaseLoaded
|
||||
const canExecuteSync = !this.syncLock
|
||||
@@ -571,12 +582,14 @@ export class SNSyncService
|
||||
if (shouldExecuteSync) {
|
||||
this.syncLock = true
|
||||
} else {
|
||||
this.log(
|
||||
log(
|
||||
LoggingDomain.Sync,
|
||||
!canExecuteSync
|
||||
? 'Another function call has begun preparing for sync.'
|
||||
: syncInProgress
|
||||
? 'Attempting to sync while existing sync in progress.'
|
||||
: 'Attempting to sync before local database has loaded.',
|
||||
options,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -656,10 +669,20 @@ export class SNSyncService
|
||||
|
||||
private createOfflineSyncOperation(
|
||||
payloads: (DeletedPayloadInterface | DecryptedPayloadInterface)[],
|
||||
source: SyncSource,
|
||||
mode: SyncMode = SyncMode.Default,
|
||||
options: SyncOptions,
|
||||
) {
|
||||
this.log('Syncing offline user', 'source:', source, 'mode:', mode, 'payloads:', payloads)
|
||||
log(
|
||||
LoggingDomain.Sync,
|
||||
'Syncing offline user',
|
||||
'source:',
|
||||
SyncSource[options.source],
|
||||
'sourceDesc',
|
||||
options.sourceDescription,
|
||||
'mode:',
|
||||
options.mode && SyncMode[options.mode],
|
||||
'payloads:',
|
||||
payloads,
|
||||
)
|
||||
|
||||
const operation = new OfflineSyncOperation(payloads, async (type, response) => {
|
||||
if (this.dealloced) {
|
||||
@@ -727,7 +750,8 @@ export class SNSyncService
|
||||
this.apiService,
|
||||
)
|
||||
|
||||
this.log(
|
||||
log(
|
||||
LoggingDomain.Sync,
|
||||
'Syncing online user',
|
||||
'source',
|
||||
SyncSource[source],
|
||||
@@ -769,14 +793,14 @@ export class SNSyncService
|
||||
const { uploadPayloads } = this.getOfflineSyncParameters(payloads, options.mode)
|
||||
|
||||
return {
|
||||
operation: this.createOfflineSyncOperation(uploadPayloads, options.source, options.mode),
|
||||
operation: this.createOfflineSyncOperation(uploadPayloads, options),
|
||||
mode: options.mode || SyncMode.Default,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async performSync(options: SyncOptions): Promise<unknown> {
|
||||
const { shouldExecuteSync, releaseLock } = this.configureSyncLock()
|
||||
const { shouldExecuteSync, releaseLock } = this.configureSyncLock(options)
|
||||
|
||||
const { items, beginDate, frozenDirtyIndex, neverSyncedDeleted } = await this.prepareForSync(options)
|
||||
|
||||
@@ -843,7 +867,7 @@ export class SNSyncService
|
||||
}
|
||||
|
||||
private async handleOfflineResponse(response: OfflineSyncResponse) {
|
||||
this.log('Offline Sync Response', response)
|
||||
log(LoggingDomain.Sync, 'Offline Sync Response', response)
|
||||
|
||||
const masterCollection = this.payloadManager.getMasterCollection()
|
||||
|
||||
@@ -861,7 +885,7 @@ export class SNSyncService
|
||||
}
|
||||
|
||||
private handleErrorServerResponse(response: ServerSyncResponse) {
|
||||
this.log('Sync Error', response)
|
||||
log(LoggingDomain.Sync, 'Sync Error', response)
|
||||
|
||||
if (response.status === INVALID_SESSION_RESPONSE_STATUS) {
|
||||
void this.notifyEvent(SyncEvent.InvalidSession)
|
||||
@@ -904,7 +928,8 @@ export class SNSyncService
|
||||
historyMap,
|
||||
)
|
||||
|
||||
this.log(
|
||||
log(
|
||||
LoggingDomain.Sync,
|
||||
'Online Sync Response',
|
||||
'Operator ID',
|
||||
operation.id,
|
||||
@@ -1060,7 +1085,7 @@ export class SNSyncService
|
||||
}
|
||||
|
||||
private async syncAgainByHandlingRequestsWaitingInResolveQueue(options: SyncOptions) {
|
||||
this.log('Syncing again from resolve queue')
|
||||
log(LoggingDomain.Sync, 'Syncing again from resolve queue')
|
||||
const promise = this.sync({
|
||||
source: SyncSource.ResolveQueue,
|
||||
checkIntegrity: options.checkIntegrity,
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
import { ContentType } from '@standardnotes/common'
|
||||
import { FullyFormedPayloadInterface } from '@standardnotes/models'
|
||||
import { GetSortedPayloadsByPriority } from './Utils'
|
||||
|
||||
describe('GetSortedPayloadsByPriority', () => {
|
||||
let payloads: FullyFormedPayloadInterface[] = []
|
||||
const contentTypePriority = [ContentType.ItemsKey, ContentType.UserPrefs, ContentType.Component, ContentType.Theme]
|
||||
let launchPriorityUuids: string[] = []
|
||||
|
||||
it('should sort payloads based on content type priority', () => {
|
||||
payloads = [
|
||||
{
|
||||
content_type: ContentType.Theme,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.UserPrefs,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Component,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.ItemsKey,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Note,
|
||||
} as FullyFormedPayloadInterface,
|
||||
]
|
||||
|
||||
const { itemsKeyPayloads, contentTypePriorityPayloads, remainingPayloads } = GetSortedPayloadsByPriority(
|
||||
payloads,
|
||||
contentTypePriority,
|
||||
launchPriorityUuids,
|
||||
)
|
||||
|
||||
expect(itemsKeyPayloads.length).toBe(1)
|
||||
expect(itemsKeyPayloads[0].content_type).toBe(ContentType.ItemsKey)
|
||||
|
||||
expect(contentTypePriorityPayloads.length).toBe(3)
|
||||
expect(contentTypePriorityPayloads[0].content_type).toBe(ContentType.UserPrefs)
|
||||
expect(contentTypePriorityPayloads[1].content_type).toBe(ContentType.Component)
|
||||
expect(contentTypePriorityPayloads[2].content_type).toBe(ContentType.Theme)
|
||||
|
||||
expect(remainingPayloads.length).toBe(1)
|
||||
expect(remainingPayloads[0].content_type).toBe(ContentType.Note)
|
||||
})
|
||||
|
||||
it('should sort payloads based on launch priority uuids', () => {
|
||||
const unprioritizedNoteUuid = 'unprioritized-note'
|
||||
const unprioritizedTagUuid = 'unprioritized-tag'
|
||||
|
||||
const prioritizedNoteUuid = 'prioritized-note'
|
||||
const prioritizedTagUuid = 'prioritized-tag'
|
||||
|
||||
payloads = [
|
||||
{
|
||||
content_type: ContentType.Theme,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.UserPrefs,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Component,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.ItemsKey,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Note,
|
||||
uuid: unprioritizedNoteUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Tag,
|
||||
uuid: unprioritizedTagUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Note,
|
||||
uuid: prioritizedNoteUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Tag,
|
||||
uuid: prioritizedTagUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
]
|
||||
|
||||
launchPriorityUuids = [prioritizedNoteUuid, prioritizedTagUuid]
|
||||
|
||||
const { itemsKeyPayloads, contentTypePriorityPayloads, remainingPayloads } = GetSortedPayloadsByPriority(
|
||||
payloads,
|
||||
contentTypePriority,
|
||||
launchPriorityUuids,
|
||||
)
|
||||
|
||||
expect(itemsKeyPayloads.length).toBe(1)
|
||||
expect(itemsKeyPayloads[0].content_type).toBe(ContentType.ItemsKey)
|
||||
|
||||
expect(contentTypePriorityPayloads.length).toBe(3)
|
||||
expect(contentTypePriorityPayloads[0].content_type).toBe(ContentType.UserPrefs)
|
||||
expect(contentTypePriorityPayloads[1].content_type).toBe(ContentType.Component)
|
||||
expect(contentTypePriorityPayloads[2].content_type).toBe(ContentType.Theme)
|
||||
|
||||
expect(remainingPayloads.length).toBe(4)
|
||||
expect(remainingPayloads[0].uuid).toBe(prioritizedNoteUuid)
|
||||
expect(remainingPayloads[1].uuid).toBe(prioritizedTagUuid)
|
||||
expect(remainingPayloads[2].uuid).toBe(unprioritizedNoteUuid)
|
||||
expect(remainingPayloads[3].uuid).toBe(unprioritizedTagUuid)
|
||||
})
|
||||
|
||||
it('should sort payloads based on server updated date if same content type', () => {
|
||||
const unprioritizedNoteUuid = 'unprioritized-note'
|
||||
const unprioritizedTagUuid = 'unprioritized-tag'
|
||||
|
||||
const prioritizedNoteUuid = 'prioritized-note'
|
||||
const prioritizedTagUuid = 'prioritized-tag'
|
||||
|
||||
payloads = [
|
||||
{
|
||||
content_type: ContentType.Note,
|
||||
uuid: unprioritizedNoteUuid,
|
||||
serverUpdatedAt: new Date(1),
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Tag,
|
||||
uuid: unprioritizedTagUuid,
|
||||
serverUpdatedAt: new Date(2),
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Note,
|
||||
uuid: prioritizedNoteUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
{
|
||||
content_type: ContentType.Tag,
|
||||
uuid: prioritizedTagUuid,
|
||||
} as FullyFormedPayloadInterface,
|
||||
]
|
||||
|
||||
launchPriorityUuids = [prioritizedNoteUuid, prioritizedTagUuid]
|
||||
|
||||
const { remainingPayloads } = GetSortedPayloadsByPriority(payloads, contentTypePriority, launchPriorityUuids)
|
||||
|
||||
expect(remainingPayloads.length).toBe(4)
|
||||
expect(remainingPayloads[0].uuid).toBe(prioritizedNoteUuid)
|
||||
expect(remainingPayloads[1].uuid).toBe(prioritizedTagUuid)
|
||||
expect(remainingPayloads[2].uuid).toBe(unprioritizedTagUuid)
|
||||
expect(remainingPayloads[3].uuid).toBe(unprioritizedNoteUuid)
|
||||
})
|
||||
})
|
||||
@@ -1,114 +0,0 @@
|
||||
import { UuidString } from '@Lib/Types'
|
||||
import { ContentType } from '@standardnotes/common'
|
||||
import { FullyFormedPayloadInterface } from '@standardnotes/models'
|
||||
|
||||
/**
|
||||
* Sorts payloads according by most recently modified first, according to the priority,
|
||||
* whereby the earlier a content_type appears in the priorityList,
|
||||
* the earlier it will appear in the resulting sorted array.
|
||||
*/
|
||||
function SortPayloadsByRecentAndContentPriority(
|
||||
payloads: FullyFormedPayloadInterface[],
|
||||
contentTypePriorityList: ContentType[],
|
||||
): FullyFormedPayloadInterface[] {
|
||||
return payloads.sort((a, b) => {
|
||||
const dateResult = new Date(b.serverUpdatedAt).getTime() - new Date(a.serverUpdatedAt).getTime()
|
||||
|
||||
let aPriority = 0
|
||||
let bPriority = 0
|
||||
|
||||
aPriority = contentTypePriorityList.indexOf(a.content_type)
|
||||
bPriority = contentTypePriorityList.indexOf(b.content_type)
|
||||
|
||||
if (aPriority === -1) {
|
||||
aPriority = contentTypePriorityList.length
|
||||
}
|
||||
|
||||
if (bPriority === -1) {
|
||||
bPriority = contentTypePriorityList.length
|
||||
}
|
||||
|
||||
if (aPriority === bPriority) {
|
||||
return dateResult
|
||||
}
|
||||
|
||||
if (aPriority < bPriority) {
|
||||
return -1
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts payloads according by most recently modified first, according to the priority,
|
||||
* whereby the earlier a uuid appears in the priorityList,
|
||||
* the earlier it will appear in the resulting sorted array.
|
||||
*/
|
||||
function SortPayloadsByRecentAndUuidPriority(
|
||||
payloads: FullyFormedPayloadInterface[],
|
||||
uuidPriorityList: UuidString[],
|
||||
): FullyFormedPayloadInterface[] {
|
||||
return payloads.sort((a, b) => {
|
||||
const dateResult = new Date(b.serverUpdatedAt).getTime() - new Date(a.serverUpdatedAt).getTime()
|
||||
|
||||
let aPriority = 0
|
||||
let bPriority = 0
|
||||
|
||||
aPriority = uuidPriorityList.indexOf(a.uuid)
|
||||
bPriority = uuidPriorityList.indexOf(b.uuid)
|
||||
|
||||
if (aPriority === -1) {
|
||||
aPriority = uuidPriorityList.length
|
||||
}
|
||||
|
||||
if (bPriority === -1) {
|
||||
bPriority = uuidPriorityList.length
|
||||
}
|
||||
|
||||
if (aPriority === bPriority) {
|
||||
return dateResult
|
||||
}
|
||||
|
||||
if (aPriority < bPriority) {
|
||||
return -1
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function GetSortedPayloadsByPriority(
|
||||
payloads: FullyFormedPayloadInterface[],
|
||||
contentTypePriorityList: ContentType[],
|
||||
uuidPriorityList: UuidString[],
|
||||
): {
|
||||
itemsKeyPayloads: FullyFormedPayloadInterface[]
|
||||
contentTypePriorityPayloads: FullyFormedPayloadInterface[]
|
||||
remainingPayloads: FullyFormedPayloadInterface[]
|
||||
} {
|
||||
const itemsKeyPayloads: FullyFormedPayloadInterface[] = []
|
||||
const contentTypePriorityPayloads: FullyFormedPayloadInterface[] = []
|
||||
const remainingPayloads: FullyFormedPayloadInterface[] = []
|
||||
|
||||
for (let index = 0; index < payloads.length; index++) {
|
||||
const payload = payloads[index]
|
||||
|
||||
if (payload.content_type === ContentType.ItemsKey) {
|
||||
itemsKeyPayloads.push(payload)
|
||||
} else if (contentTypePriorityList.includes(payload.content_type)) {
|
||||
contentTypePriorityPayloads.push(payload)
|
||||
} else {
|
||||
remainingPayloads.push(payload)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
itemsKeyPayloads,
|
||||
contentTypePriorityPayloads: SortPayloadsByRecentAndContentPriority(
|
||||
contentTypePriorityPayloads,
|
||||
contentTypePriorityList,
|
||||
),
|
||||
remainingPayloads: SortPayloadsByRecentAndUuidPriority(remainingPayloads, uuidPriorityList),
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,4 @@ export * from './SyncClientInterface'
|
||||
export * from './Account/Operation'
|
||||
export * from './Account/ResponseResolver'
|
||||
export * from './Offline/Operation'
|
||||
export * from './Utils'
|
||||
export * from './Account/Response'
|
||||
|
||||
@@ -110,12 +110,12 @@ describe('application instances', () => {
|
||||
* app deinit. */
|
||||
await Factory.sleep(MaximumWaitTime - 0.05)
|
||||
/** Access any deviceInterface function */
|
||||
app.diskStorageService.deviceInterface.getAllRawDatabasePayloads(app.identifier)
|
||||
app.diskStorageService.deviceInterface.getAllDatabaseEntries(app.identifier)
|
||||
})
|
||||
await app.lock()
|
||||
})
|
||||
|
||||
describe('signOut()', () => {
|
||||
describe.skip('signOut()', () => {
|
||||
let testNote1
|
||||
let confirmAlert
|
||||
let deinit
|
||||
|
||||
@@ -59,9 +59,6 @@ describe('basic auth', function () {
|
||||
|
||||
expect(await this.application.protocolService.getRootKey()).to.not.be.ok
|
||||
expect(this.application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyNone)
|
||||
|
||||
const rawPayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
expect(rawPayloads.length).to.equal(BaseItemCounts.DefaultItems)
|
||||
})
|
||||
|
||||
it('successfully signs in to registered account', async function () {
|
||||
|
||||
@@ -664,12 +664,12 @@ describe('key recovery service', function () {
|
||||
await Factory.awaitFunctionInvokation(appA.keyRecoveryService, 'handleDecryptionOfAllKeysMatchingCorrectRootKey')
|
||||
|
||||
/** Stored version of items key should use new root key */
|
||||
const stored = (await appA.deviceInterface.getAllRawDatabasePayloads(appA.identifier)).find(
|
||||
const stored = (await appA.deviceInterface.getAllDatabaseEntries(appA.identifier)).find(
|
||||
(payload) => payload.uuid === newDefaultKey.uuid,
|
||||
)
|
||||
const storedParams = await appA.protocolService.getKeyEmbeddedKeyParams(new EncryptedPayload(stored))
|
||||
|
||||
const correctStored = (await appB.deviceInterface.getAllRawDatabasePayloads(appB.identifier)).find(
|
||||
const correctStored = (await appB.deviceInterface.getAllDatabaseEntries(appB.identifier)).find(
|
||||
(payload) => payload.uuid === newDefaultKey.uuid,
|
||||
)
|
||||
|
||||
|
||||
@@ -303,7 +303,8 @@ export function tomorrow() {
|
||||
return new Date(new Date().setDate(new Date().getDate() + 1))
|
||||
}
|
||||
|
||||
export async function sleep(seconds) {
|
||||
export async function sleep(seconds, reason) {
|
||||
console.log('Sleeping for reason', reason)
|
||||
return Utils.sleep(seconds)
|
||||
}
|
||||
|
||||
|
||||
@@ -21,17 +21,6 @@ export default class WebDeviceInterface {
|
||||
}
|
||||
}
|
||||
|
||||
async getAllRawStorageKeyValues() {
|
||||
const results = []
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
results.push({
|
||||
key: key,
|
||||
value: localStorage[key],
|
||||
})
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
async setRawStorageValue(key, value) {
|
||||
localStorage.setItem(key, value)
|
||||
}
|
||||
@@ -60,7 +49,7 @@ export default class WebDeviceInterface {
|
||||
return `${this._getDatabaseKeyPrefix(identifier)}${id}`
|
||||
}
|
||||
|
||||
async getAllRawDatabasePayloads(identifier) {
|
||||
async getAllDatabaseEntries(identifier) {
|
||||
const models = []
|
||||
for (const key in localStorage) {
|
||||
if (key.startsWith(this._getDatabaseKeyPrefix(identifier))) {
|
||||
@@ -70,21 +59,51 @@ export default class WebDeviceInterface {
|
||||
return models
|
||||
}
|
||||
|
||||
async saveRawDatabasePayload(payload, identifier) {
|
||||
async getDatabaseLoadChunks(options, identifier) {
|
||||
const entries = await this.getAllDatabaseEntries(identifier)
|
||||
const sorted = GetSortedPayloadsByPriority(entries, options)
|
||||
|
||||
const itemsKeysChunk = {
|
||||
entries: sorted.itemsKeyPayloads,
|
||||
}
|
||||
|
||||
const contentTypePriorityChunk = {
|
||||
entries: sorted.contentTypePriorityPayloads,
|
||||
}
|
||||
|
||||
const remainingPayloadsChunks = []
|
||||
for (let i = 0; i < sorted.remainingPayloads.length; i += options.batchSize) {
|
||||
remainingPayloadsChunks.push({
|
||||
entries: sorted.remainingPayloads.slice(i, i + options.batchSize),
|
||||
})
|
||||
}
|
||||
|
||||
const result = {
|
||||
fullEntries: {
|
||||
itemsKeys: itemsKeysChunk,
|
||||
remainingChunks: [contentTypePriorityChunk, ...remainingPayloadsChunks],
|
||||
},
|
||||
remainingChunksItemCount: sorted.contentTypePriorityPayloads.length + sorted.remainingPayloads.length,
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async saveDatabaseEntry(payload, identifier) {
|
||||
localStorage.setItem(this._keyForPayloadId(payload.uuid, identifier), JSON.stringify(payload))
|
||||
}
|
||||
|
||||
async saveRawDatabasePayloads(payloads, identifier) {
|
||||
async saveDatabaseEntries(payloads, identifier) {
|
||||
for (const payload of payloads) {
|
||||
await this.saveRawDatabasePayload(payload, identifier)
|
||||
await this.saveDatabaseEntry(payload, identifier)
|
||||
}
|
||||
}
|
||||
|
||||
async removeRawDatabasePayloadWithId(id, identifier) {
|
||||
async removeDatabaseEntry(id, identifier) {
|
||||
localStorage.removeItem(this._keyForPayloadId(id, identifier))
|
||||
}
|
||||
|
||||
async removeAllRawDatabasePayloads(identifier) {
|
||||
async removeAllDatabaseEntries(identifier) {
|
||||
for (const key in localStorage) {
|
||||
if (key.startsWith(this._getDatabaseKeyPrefix(identifier))) {
|
||||
delete localStorage[key]
|
||||
@@ -124,12 +143,6 @@ export default class WebDeviceInterface {
|
||||
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(keychain))
|
||||
}
|
||||
|
||||
/** Allows unit tests to set legacy keychain structure as it was <= 003 */
|
||||
// eslint-disable-next-line camelcase
|
||||
async setLegacyRawKeychainValue(value) {
|
||||
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value))
|
||||
}
|
||||
|
||||
async getRawKeychainValue() {
|
||||
const keychain = localStorage.getItem(KEYCHAIN_STORAGE_KEY)
|
||||
return JSON.parse(keychain)
|
||||
@@ -139,19 +152,13 @@ export default class WebDeviceInterface {
|
||||
localStorage.removeItem(KEYCHAIN_STORAGE_KEY)
|
||||
}
|
||||
|
||||
performSoftReset() {
|
||||
performSoftReset() {}
|
||||
|
||||
}
|
||||
|
||||
performHardReset() {
|
||||
|
||||
}
|
||||
performHardReset() {}
|
||||
|
||||
isDeviceDestroyed() {
|
||||
return false
|
||||
}
|
||||
|
||||
deinit() {
|
||||
|
||||
}
|
||||
deinit() {}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,584 +0,0 @@
|
||||
/* eslint-disable no-unused-expressions */
|
||||
/* eslint-disable no-undef */
|
||||
import * as Factory from '../lib/factory.js'
|
||||
import FakeWebCrypto from '../lib/fake_web_crypto.js'
|
||||
chai.use(chaiAsPromised)
|
||||
const expect = chai.expect
|
||||
|
||||
describe('2020-01-15 web migration', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
/**
|
||||
* This test will pass but sync afterwards will not be successful
|
||||
* as we are using a random value for the legacy session token
|
||||
*/
|
||||
it('2020-01-15 migration with passcode and account', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
|
||||
const identifier = 'foo'
|
||||
const passcode = 'bar'
|
||||
/** Create old version passcode parameters */
|
||||
const passcodeKey = await operator003.createRootKey(identifier, passcode)
|
||||
await application.deviceInterface.setRawStorageValue(
|
||||
'offlineParams',
|
||||
JSON.stringify(passcodeKey.keyParams.getPortableValue()),
|
||||
)
|
||||
|
||||
/** Create arbitrary storage values and make sure they're migrated */
|
||||
const arbitraryValues = {
|
||||
foo: 'bar',
|
||||
zar: 'tar',
|
||||
har: 'car',
|
||||
}
|
||||
for (const key of Object.keys(arbitraryValues)) {
|
||||
await application.deviceInterface.setRawStorageValue(key, arbitraryValues[key])
|
||||
}
|
||||
/** Create old version account parameters */
|
||||
const password = 'tar'
|
||||
const accountKey = await operator003.createRootKey(identifier, password)
|
||||
|
||||
/** Create legacy storage and encrypt it with passcode */
|
||||
const embeddedStorage = {
|
||||
mk: accountKey.masterKey,
|
||||
ak: accountKey.dataAuthenticationKey,
|
||||
pw: accountKey.serverPassword,
|
||||
jwt: 'anything',
|
||||
/** Legacy versions would store json strings inside of embedded storage */
|
||||
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
|
||||
}
|
||||
const storagePayload = new DecryptedPayload({
|
||||
uuid: await operator003.crypto.generateUUID(),
|
||||
content_type: ContentType.EncryptedStorage,
|
||||
content: {
|
||||
storage: embeddedStorage,
|
||||
},
|
||||
})
|
||||
const encryptionParams = await operator003.generateEncryptedParametersAsync(storagePayload, passcodeKey)
|
||||
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
|
||||
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
|
||||
|
||||
/** Create encrypted item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, accountKey)
|
||||
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
|
||||
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
|
||||
|
||||
/** Run migration */
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: async (challenge) => {
|
||||
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
|
||||
},
|
||||
})
|
||||
|
||||
await application.launch(true)
|
||||
expect(application.sessionManager.online()).to.equal(true)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('offlineParams')).to.not.be.ok
|
||||
|
||||
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
|
||||
expect(typeof keyParams).to.equal('object')
|
||||
|
||||
/** Embedded value should match */
|
||||
const migratedKeyParams = await application.diskStorageService.getValue(
|
||||
StorageKey.RootKeyParams,
|
||||
StorageValueModes.Nonwrapped,
|
||||
)
|
||||
expect(migratedKeyParams).to.eql(JSON.parse(embeddedStorage.auth_params))
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
|
||||
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
|
||||
/** Application should not retain server password from legacy versions */
|
||||
expect(rootKey.serverPassword).to.not.be.ok
|
||||
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
/** Ensure arbitrary values have been migrated */
|
||||
for (const key of Object.keys(arbitraryValues)) {
|
||||
const value = await application.diskStorageService.getValue(key)
|
||||
expect(arbitraryValues[key]).to.equal(value)
|
||||
}
|
||||
|
||||
console.warn('Expecting exception due to deiniting application while trying to renew session')
|
||||
await Factory.safeDeinit(application)
|
||||
}).timeout(15000)
|
||||
|
||||
it('2020-01-15 migration with passcode only', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
|
||||
const identifier = 'foo'
|
||||
const passcode = 'bar'
|
||||
/** Create old version passcode parameters */
|
||||
const passcodeKey = await operator003.createRootKey(identifier, passcode)
|
||||
await application.deviceInterface.setRawStorageValue(
|
||||
'offlineParams',
|
||||
JSON.stringify(passcodeKey.keyParams.getPortableValue()),
|
||||
)
|
||||
|
||||
/** Create arbitrary storage values and make sure they're migrated */
|
||||
const arbitraryValues = {
|
||||
foo: 'bar',
|
||||
zar: 'tar',
|
||||
har: 'car',
|
||||
}
|
||||
for (const key of Object.keys(arbitraryValues)) {
|
||||
await application.deviceInterface.setRawStorageValue(key, arbitraryValues[key])
|
||||
}
|
||||
|
||||
const embeddedStorage = {
|
||||
...arbitraryValues,
|
||||
}
|
||||
const storagePayload = new DecryptedPayload({
|
||||
uuid: await operator003.crypto.generateUUID(),
|
||||
content: {
|
||||
storage: embeddedStorage,
|
||||
},
|
||||
content_type: ContentType.EncryptedStorage,
|
||||
})
|
||||
const encryptionParams = await operator003.generateEncryptedParametersAsync(storagePayload, passcodeKey)
|
||||
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
|
||||
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
|
||||
|
||||
/** Create encrypted item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, passcodeKey)
|
||||
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
|
||||
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
|
||||
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: async (challenge) => {
|
||||
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
|
||||
},
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.WrapperOnly)
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('offlineParams')).to.not.be.ok
|
||||
|
||||
/** Embedded value should match */
|
||||
const migratedKeyParams = await application.diskStorageService.getValue(
|
||||
StorageKey.RootKeyParams,
|
||||
StorageValueModes.Nonwrapped,
|
||||
)
|
||||
expect(migratedKeyParams).to.eql(embeddedStorage.auth_params)
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey.masterKey).to.equal(passcodeKey.masterKey)
|
||||
expect(rootKey.dataAuthenticationKey).to.equal(passcodeKey.dataAuthenticationKey)
|
||||
/** Root key is in memory with passcode only, so server password can be defined */
|
||||
expect(rootKey.serverPassword).to.be.ok
|
||||
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.WrapperOnly)
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
/** Ensure arbitrary values have been migrated */
|
||||
for (const key of Object.keys(arbitraryValues)) {
|
||||
const value = await application.diskStorageService.getValue(key)
|
||||
expect(arbitraryValues[key]).to.equal(value)
|
||||
}
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
/**
|
||||
* This test will pass but sync afterwards will not be successful
|
||||
* as we are using a random value for the legacy session token
|
||||
*/
|
||||
it('2020-01-15 migration with account only', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
|
||||
const identifier = 'foo'
|
||||
|
||||
/** Create old version account parameters */
|
||||
const password = 'tar'
|
||||
const accountKey = await operator003.createRootKey(identifier, password)
|
||||
|
||||
/** Create arbitrary storage values and make sure they're migrated */
|
||||
const storage = {
|
||||
foo: 'bar',
|
||||
zar: 'tar',
|
||||
har: 'car',
|
||||
mk: accountKey.masterKey,
|
||||
ak: accountKey.dataAuthenticationKey,
|
||||
pw: accountKey.serverPassword,
|
||||
jwt: 'anything',
|
||||
/** Legacy versions would store json strings inside of embedded storage */
|
||||
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
|
||||
}
|
||||
for (const key of Object.keys(storage)) {
|
||||
await application.deviceInterface.setRawStorageValue(key, storage[key])
|
||||
}
|
||||
/** Create encrypted item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, accountKey)
|
||||
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
|
||||
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
|
||||
|
||||
/** Run migration */
|
||||
const promptValueReply = (prompts) => {
|
||||
const values = []
|
||||
for (const prompt of prompts) {
|
||||
if (prompt.validation === ChallengeValidation.LocalPasscode) {
|
||||
values.push(CreateChallengeValue(prompt, passcode))
|
||||
} else {
|
||||
/** We will be prompted to reauthetnicate our session, not relevant to this test
|
||||
* but pass any value to avoid exception
|
||||
*/
|
||||
values.push(CreateChallengeValue(prompt, 'foo'))
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
const receiveChallenge = async (challenge) => {
|
||||
application.addChallengeObserver(challenge, {
|
||||
onInvalidValue: (value) => {
|
||||
const values = promptValueReply([value.prompt])
|
||||
application.submitValuesForChallenge(challenge, values)
|
||||
},
|
||||
})
|
||||
const initialValues = promptValueReply(challenge.prompts)
|
||||
application.submitValuesForChallenge(challenge, initialValues)
|
||||
}
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: receiveChallenge,
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(application.sessionManager.online()).to.equal(true)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
/** Embedded value should match */
|
||||
const migratedKeyParams = await application.diskStorageService.getValue(
|
||||
StorageKey.RootKeyParams,
|
||||
StorageValueModes.Nonwrapped,
|
||||
)
|
||||
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey).to.be.ok
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
|
||||
|
||||
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
|
||||
expect(typeof keyParams).to.equal('object')
|
||||
|
||||
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
|
||||
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
|
||||
expect(rootKey.serverPassword).to.not.be.ok
|
||||
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
/** Ensure arbitrary values have been migrated */
|
||||
for (const key of Object.keys(storage)) {
|
||||
/** Is stringified in storage, but parsed in storageService */
|
||||
if (key === 'auth_params') {
|
||||
continue
|
||||
}
|
||||
const value = await application.diskStorageService.getValue(key)
|
||||
expect(storage[key]).to.equal(value)
|
||||
}
|
||||
|
||||
console.warn('Expecting exception due to deiniting application while trying to renew session')
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('2020-01-15 migration with no account and no passcode', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
/** Create arbitrary storage values and make sure they're migrated */
|
||||
const storage = {
|
||||
foo: 'bar',
|
||||
zar: 'tar',
|
||||
har: 'car',
|
||||
}
|
||||
for (const key of Object.keys(storage)) {
|
||||
await application.deviceInterface.setRawStorageValue(key, storage[key])
|
||||
}
|
||||
|
||||
/** Create item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
await application.deviceInterface.saveRawDatabasePayload(notePayload.ejected(), application.identifier)
|
||||
|
||||
/** Run migration */
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: (_challenge) => {
|
||||
return null
|
||||
},
|
||||
})
|
||||
await application.launch(true)
|
||||
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyNone)
|
||||
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey).to.not.be.ok
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyNone)
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
/** Ensure arbitrary values have been migrated */
|
||||
for (const key of Object.keys(storage)) {
|
||||
const value = await application.diskStorageService.getValue(key)
|
||||
expect(storage[key]).to.equal(value)
|
||||
}
|
||||
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
/**
|
||||
* This test will pass but sync afterwards will not be successful
|
||||
* as we are using a random value for the legacy session token
|
||||
*/
|
||||
it('2020-01-15 migration from app v1.0.1 with account only', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
const operator001 = new SNProtocolOperator001(new FakeWebCrypto())
|
||||
const identifier = 'foo'
|
||||
|
||||
/** Create old version account parameters */
|
||||
const password = 'tar'
|
||||
const accountKey = await operator001.createRootKey(identifier, password)
|
||||
|
||||
/** Create arbitrary storage values and make sure they're migrated */
|
||||
const storage = {
|
||||
mk: accountKey.masterKey,
|
||||
pw: accountKey.serverPassword,
|
||||
jwt: 'anything',
|
||||
/** Legacy versions would store json strings inside of embedded storage */
|
||||
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
|
||||
user: JSON.stringify({ uuid: 'anything', email: 'anything' }),
|
||||
}
|
||||
for (const key of Object.keys(storage)) {
|
||||
await application.deviceInterface.setRawStorageValue(key, storage[key])
|
||||
}
|
||||
/** Create encrypted item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
const noteEncryptionParams = await operator001.generateEncryptedParametersAsync(notePayload, accountKey)
|
||||
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
|
||||
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
|
||||
|
||||
/** Run migration */
|
||||
const promptValueReply = (prompts) => {
|
||||
const values = []
|
||||
for (const prompt of prompts) {
|
||||
/** We will be prompted to reauthetnicate our session, not relevant to this test
|
||||
* but pass any value to avoid exception
|
||||
*/
|
||||
values.push(CreateChallengeValue(prompt, 'foo'))
|
||||
}
|
||||
return values
|
||||
}
|
||||
const receiveChallenge = async (challenge) => {
|
||||
application.addChallengeObserver(challenge, {
|
||||
onInvalidValue: (value) => {
|
||||
const values = promptValueReply([value.prompt])
|
||||
application.submitValuesForChallenge(challenge, values)
|
||||
},
|
||||
})
|
||||
const initialValues = promptValueReply(challenge.prompts)
|
||||
application.submitValuesForChallenge(challenge, initialValues)
|
||||
}
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: receiveChallenge,
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(application.sessionManager.online()).to.equal(true)
|
||||
expect(application.sessionManager.getUser()).to.be.ok
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
/** Embedded value should match */
|
||||
const migratedKeyParams = await application.diskStorageService.getValue(
|
||||
StorageKey.RootKeyParams,
|
||||
StorageValueModes.Nonwrapped,
|
||||
)
|
||||
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey).to.be.ok
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('ak')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('mk')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('pw')).to.not.be.ok
|
||||
|
||||
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
|
||||
expect(typeof keyParams).to.equal('object')
|
||||
|
||||
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
|
||||
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
|
||||
expect(rootKey.serverPassword).to.not.be.ok
|
||||
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V001)
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
/** Ensure arbitrary values have been migrated */
|
||||
for (const key of Object.keys(storage)) {
|
||||
/** Is stringified in storage, but parsed in storageService */
|
||||
const value = await application.diskStorageService.getValue(key)
|
||||
if (key === 'auth_params') {
|
||||
continue
|
||||
} else if (key === 'user') {
|
||||
expect(storage[key]).to.equal(JSON.stringify(value))
|
||||
} else {
|
||||
expect(storage[key]).to.equal(value)
|
||||
}
|
||||
}
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('2020-01-15 migration from 002 app with account and passcode but missing offlineParams.version', async function () {
|
||||
/**
|
||||
* There was an issue where if the user had offlineParams but it was missing the version key,
|
||||
* the user could not get past the passcode migration screen.
|
||||
*/
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Create legacy migrations value so that base migration detects old app */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
const operator002 = new SNProtocolOperator002(new FakeWebCrypto())
|
||||
const identifier = 'foo'
|
||||
const passcode = 'bar'
|
||||
/** Create old version passcode parameters */
|
||||
const passcodeKey = await operator002.createRootKey(identifier, passcode)
|
||||
|
||||
/** The primary chaos agent */
|
||||
const offlineParams = passcodeKey.keyParams.getPortableValue()
|
||||
omitInPlace(offlineParams, ['version'])
|
||||
|
||||
await application.deviceInterface.setRawStorageValue('offlineParams', JSON.stringify(offlineParams))
|
||||
|
||||
/** Create old version account parameters */
|
||||
const password = 'tar'
|
||||
const accountKey = await operator002.createRootKey(identifier, password)
|
||||
|
||||
/** Create legacy storage and encrypt it with passcode */
|
||||
const embeddedStorage = {
|
||||
mk: accountKey.masterKey,
|
||||
ak: accountKey.dataAuthenticationKey,
|
||||
pw: accountKey.serverPassword,
|
||||
jwt: 'anything',
|
||||
/** Legacy versions would store json strings inside of embedded storage */
|
||||
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
|
||||
user: JSON.stringify({ uuid: 'anything', email: 'anything' }),
|
||||
}
|
||||
const storagePayload = new DecryptedPayload({
|
||||
uuid: await operator002.crypto.generateUUID(),
|
||||
content_type: ContentType.EncryptedStorage,
|
||||
content: {
|
||||
storage: embeddedStorage,
|
||||
},
|
||||
})
|
||||
const encryptionParams = await operator002.generateEncryptedParametersAsync(storagePayload, passcodeKey)
|
||||
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
|
||||
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
|
||||
|
||||
/** Create encrypted item and store it in db */
|
||||
const notePayload = Factory.createNotePayload()
|
||||
const noteEncryptionParams = await operator002.generateEncryptedParametersAsync(notePayload, accountKey)
|
||||
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
|
||||
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
|
||||
|
||||
/** Runs migration */
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: async (challenge) => {
|
||||
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
|
||||
},
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(application.sessionManager.online()).to.equal(true)
|
||||
expect(application.sessionManager.getUser()).to.be.ok
|
||||
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
|
||||
/** Should be decrypted */
|
||||
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
|
||||
const valueStore = application.diskStorageService.values[storageMode]
|
||||
expect(valueStore.content_type).to.not.be.ok
|
||||
/** Embedded value should match */
|
||||
const migratedKeyParams = await application.diskStorageService.getValue(
|
||||
StorageKey.RootKeyParams,
|
||||
StorageValueModes.Nonwrapped,
|
||||
)
|
||||
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
|
||||
const rootKey = await application.protocolService.getRootKey()
|
||||
expect(rootKey).to.be.ok
|
||||
|
||||
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('ak')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('mk')).to.not.be.ok
|
||||
expect(await application.deviceInterface.getRawStorageValue('pw')).to.not.be.ok
|
||||
|
||||
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
|
||||
expect(typeof keyParams).to.equal('object')
|
||||
|
||||
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
|
||||
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
|
||||
expect(rootKey.serverPassword).to.not.be.ok
|
||||
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V002)
|
||||
|
||||
/** Expect note is decrypted */
|
||||
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
|
||||
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
|
||||
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
|
||||
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
|
||||
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
})
|
||||
@@ -3,7 +3,7 @@ chai.use(chaiAsPromised)
|
||||
const expect = chai.expect
|
||||
|
||||
describe('migrations', () => {
|
||||
const allMigrations = ['2.0.0', '2.0.15', '2.7.0', '2.20.0', '2.36.0', '2.42.0']
|
||||
const allMigrations = ['2.0.15', '2.7.0', '2.20.0', '2.36.0', '2.42.0']
|
||||
|
||||
beforeEach(async () => {
|
||||
localStorage.clear()
|
||||
@@ -25,34 +25,13 @@ describe('migrations', () => {
|
||||
})
|
||||
|
||||
it('should return correct required migrations if stored version is 2.0.0', async function () {
|
||||
expect((await SNMigrationService.getRequiredMigrations('2.0.0')).length).to.equal(allMigrations.length - 1)
|
||||
expect((await SNMigrationService.getRequiredMigrations('2.0.0')).length).to.equal(allMigrations.length)
|
||||
})
|
||||
|
||||
it('should return 0 required migrations if stored version is futuristic', async function () {
|
||||
expect((await SNMigrationService.getRequiredMigrations('100.0.1')).length).to.equal(0)
|
||||
})
|
||||
|
||||
it('after running base migration, legacy structure should set version as 1.0.0', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Set up 1.0.0 structure with tell-tale storage key */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
await application.migrationService.runBaseMigrationPreRun()
|
||||
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('1.0.0')
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('after running base migration, 2.0.0 structure set version as 2.0.0', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Set up 2.0.0 structure with tell-tale storage key */
|
||||
await application.deviceInterface.setRawStorageValue(
|
||||
namespacedKey(application.identifier, 'last_migration_timestamp'),
|
||||
'anything',
|
||||
)
|
||||
await application.migrationService.runBaseMigrationPreRun()
|
||||
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('2.0.0')
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('after running base migration with no present storage values, should set version to current', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
await application.migrationService.runBaseMigrationPreRun()
|
||||
@@ -60,18 +39,6 @@ describe('migrations', () => {
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('after running all migrations from a 1.0.0 installation, should set stored version to current', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Set up 1.0.0 structure with tell-tale storage key */
|
||||
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: () => {},
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('after running all migrations from a 2.0.0 installation, should set stored version to current', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
/** Set up 2.0.0 structure with tell-tale storage key */
|
||||
@@ -84,24 +51,6 @@ describe('migrations', () => {
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('should be correct migration count coming from 1.0.0', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
await application.deviceInterface.setRawStorageValue('migrations', 'anything')
|
||||
await application.migrationService.runBaseMigrationPreRun()
|
||||
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('1.0.0')
|
||||
const pendingMigrations = await SNMigrationService.getRequiredMigrations(
|
||||
await application.migrationService.getStoredSnjsVersion(),
|
||||
)
|
||||
expect(pendingMigrations.length).to.equal(allMigrations.length)
|
||||
expect(pendingMigrations[0].version()).to.equal('2.0.0')
|
||||
await application.prepareForLaunch({
|
||||
receiveChallenge: () => {},
|
||||
})
|
||||
await application.launch(true)
|
||||
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
|
||||
await Factory.safeDeinit(application)
|
||||
})
|
||||
|
||||
it('2.20.0 remove mfa migration', async function () {
|
||||
const application = await Factory.createAppWithRandNamespace()
|
||||
|
||||
|
||||
@@ -735,7 +735,7 @@ describe('importing', function () {
|
||||
}),
|
||||
)
|
||||
await application.deviceInterface.setRawStorageValue('standardnotes-snjs_version', '2.0.11')
|
||||
await application.deviceInterface.saveRawDatabasePayload(
|
||||
await application.deviceInterface.saveDatabaseEntry(
|
||||
{
|
||||
content:
|
||||
'003:9f2c7527eb8b2a1f8bfb3ea6b885403b6886bce2640843ebd57a6c479cbf7597:58e3322b-269a-4be3-a658-b035dffcd70f:9140b23a0fa989e224e292049f133154:SESTNOgIGf2+ZqmJdFnGU4EMgQkhKOzpZNoSzx76SJaImsayzctAgbUmJ+UU2gSQAHADS3+Z5w11bXvZgIrStTsWriwvYkNyyKmUPadKHNSBwOk4WeBZpWsA9gtI5zgI04Q5pvb8hS+kNW2j1DjM4YWqd0JQxMOeOrMIrxr/6Awn5TzYE+9wCbXZdYHyvRQcp9ui/G02ZJ67IA86vNEdjTTBAAWipWqTqKH9VDZbSQ2W/IOKfIquB373SFDKZb1S1NmBFvcoG2G7w//fAl/+ehYiL6UdiNH5MhXCDAOTQRFNfOh57HFDWVnz1VIp8X+VAPy6d9zzQH+8aws1JxHq/7BOhXrFE8UCueV6kERt9njgQxKJzd9AH32ShSiUB9X/sPi0fUXbS178xAZMJrNx3w==:eyJwd19ub25jZSI6IjRjYjEwM2FhODljZmY0NTYzYTkxMWQzZjM5NjU4M2NlZmM2ODMzYzY2Zjg4MGZiZWUwNmJkYTk0YzMxZjg2OGIiLCJwd19jb3N0IjoxMTAwMDAsImlkZW50aWZpZXIiOiJub3YyMzIyQGJpdGFyLmlvIiwidmVyc2lvbiI6IjAwMyIsIm9yaWdpbmF0aW9uIjoicmVnaXN0cmF0aW9uIn0=',
|
||||
|
||||
@@ -94,7 +94,7 @@ describe('model manager mapping', () => {
|
||||
const note = this.application.itemManager.getDisplayableNotes()[0]
|
||||
await this.application.itemManager.setItemDirty(note)
|
||||
const dirtyItems = this.application.itemManager.getDirtyItems()
|
||||
expect(dirtyItems.length).to.equal(1)
|
||||
expect(Uuids(dirtyItems).includes(note.uuid))
|
||||
})
|
||||
|
||||
it('set all items dirty', async function () {
|
||||
|
||||
@@ -642,7 +642,7 @@ describe('server session', function () {
|
||||
await app2Deinit
|
||||
|
||||
const deviceInterface = new WebDeviceInterface()
|
||||
const payloads = await deviceInterface.getAllRawDatabasePayloads(app2identifier)
|
||||
const payloads = await deviceInterface.getAllDatabaseEntries(app2identifier)
|
||||
expect(payloads).to.be.empty
|
||||
})
|
||||
|
||||
@@ -670,7 +670,7 @@ describe('server session', function () {
|
||||
await app2Deinit
|
||||
|
||||
const deviceInterface = new WebDeviceInterface()
|
||||
const payloads = await deviceInterface.getAllRawDatabasePayloads(app2identifier)
|
||||
const payloads = await deviceInterface.getAllDatabaseEntries(app2identifier)
|
||||
expect(payloads).to.be.empty
|
||||
})
|
||||
|
||||
|
||||
@@ -300,6 +300,7 @@ describe('storage manager', function () {
|
||||
await Factory.createSyncedNote(this.application)
|
||||
expect(await Factory.storagePayloadCount(this.application)).to.equal(BaseItemCounts.DefaultItems + 1)
|
||||
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
|
||||
await Factory.sleep(0.1, 'Allow all untrackable singleton syncs to complete')
|
||||
expect(await Factory.storagePayloadCount(this.application)).to.equal(BaseItemCounts.DefaultItems)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,10 +31,7 @@ describe('offline syncing', () => {
|
||||
|
||||
it('should sync item with no passcode', async function () {
|
||||
let note = await Factory.createMappedNote(this.application)
|
||||
expect(this.application.itemManager.getDirtyItems().length).to.equal(1)
|
||||
|
||||
const rawPayloads1 = await this.application.diskStorageService.getAllRawPayloads()
|
||||
expect(rawPayloads1.length).to.equal(this.expectedItemCount)
|
||||
expect(Uuids(this.application.itemManager.getDirtyItems()).includes(note.uuid))
|
||||
|
||||
await this.application.syncService.sync(syncOptions)
|
||||
|
||||
|
||||
@@ -218,14 +218,21 @@ describe('online syncing', function () {
|
||||
it('retrieving new items should not mark them as dirty', async function () {
|
||||
const originalNote = await Factory.createSyncedNote(this.application)
|
||||
this.expectedItemCount++
|
||||
|
||||
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
|
||||
this.application.syncService.addEventObserver((event) => {
|
||||
if (event === SyncEvent.SingleRoundTripSyncCompleted) {
|
||||
const note = this.application.items.findItem(originalNote.uuid)
|
||||
expect(note.dirty).to.not.be.ok
|
||||
}
|
||||
const promise = new Promise((resolve) => {
|
||||
this.application.syncService.addEventObserver(async (event) => {
|
||||
if (event === SyncEvent.SingleRoundTripSyncCompleted) {
|
||||
const note = this.application.items.findItem(originalNote.uuid)
|
||||
if (note) {
|
||||
expect(note.dirty).to.not.be.ok
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
await this.application.signIn(this.email, this.password, undefined, undefined, undefined, true)
|
||||
await promise
|
||||
})
|
||||
|
||||
it('allows saving of data after sign out', async function () {
|
||||
@@ -579,7 +586,7 @@ describe('online syncing', function () {
|
||||
await this.application.itemManager.setItemDirty(note)
|
||||
await this.application.syncService.sync(syncOptions)
|
||||
this.expectedItemCount++
|
||||
const rawPayloads = await this.application.syncService.getDatabasePayloads()
|
||||
const rawPayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
const notePayload = rawPayloads.find((p) => p.content_type === ContentType.Note)
|
||||
expect(typeof notePayload.content).to.equal('string')
|
||||
})
|
||||
@@ -651,8 +658,7 @@ describe('online syncing', function () {
|
||||
await this.application.syncService.clearSyncPositionTokens()
|
||||
await this.application.payloadManager.resetState()
|
||||
await this.application.itemManager.resetState()
|
||||
const databasePayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
await this.application.syncService.loadDatabasePayloads(databasePayloads)
|
||||
await this.application.syncService.loadDatabasePayloads()
|
||||
await this.application.syncService.sync(syncOptions)
|
||||
|
||||
const newRawPayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
@@ -672,7 +678,9 @@ describe('online syncing', function () {
|
||||
const payload = Factory.createStorageItemPayload(contentTypes[Math.floor(i / 2)])
|
||||
originalPayloads.push(payload)
|
||||
}
|
||||
const { contentTypePriorityPayloads } = GetSortedPayloadsByPriority(originalPayloads, ['C', 'A', 'B'])
|
||||
const { contentTypePriorityPayloads } = GetSortedPayloadsByPriority(originalPayloads, {
|
||||
contentTypePriority: ['C', 'A', 'B'],
|
||||
})
|
||||
expect(contentTypePriorityPayloads[0].content_type).to.equal('C')
|
||||
expect(contentTypePriorityPayloads[2].content_type).to.equal('A')
|
||||
expect(contentTypePriorityPayloads[4].content_type).to.equal('B')
|
||||
@@ -685,14 +693,10 @@ describe('online syncing', function () {
|
||||
await this.application.syncService.sync(syncOptions)
|
||||
|
||||
this.application = await Factory.signOutApplicationAndReturnNew(this.application)
|
||||
const rawPayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
expect(rawPayloads.length).to.equal(BaseItemCounts.DefaultItems)
|
||||
|
||||
await this.application.signIn(this.email, this.password, undefined, undefined, undefined, true)
|
||||
|
||||
this.application.syncService.ut_setDatabaseLoaded(false)
|
||||
const databasePayloads = await this.application.diskStorageService.getAllRawPayloads()
|
||||
await this.application.syncService.loadDatabasePayloads(databasePayloads)
|
||||
await this.application.syncService.loadDatabasePayloads()
|
||||
await this.application.syncService.sync(syncOptions)
|
||||
|
||||
const items = await this.application.itemManager.items
|
||||
|
||||
@@ -80,8 +80,6 @@
|
||||
<script type="module" src="protection.test.js"></script>
|
||||
<script type="module" src="singletons.test.js"></script>
|
||||
<script type="module" src="migrations/migration.test.js"></script>
|
||||
<script type="module" src="migrations/2020-01-15-web.test.js"></script>
|
||||
<script type="module" src="migrations/2020-01-15-mobile.test.js"></script>
|
||||
<script type="module" src="migrations/tags-to-folders.test.js"></script>
|
||||
<script type="module" src="history.test.js"></script>
|
||||
<script type="module" src="actions.test.js"></script>
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
"clean": "rm -fr dist",
|
||||
"prebuild": "yarn clean",
|
||||
"build": "yarn tsc && webpack --config webpack.prod.js",
|
||||
"watch": "webpack --config webpack.prod.js --watch",
|
||||
"docs": "jsdoc -c jsdoc.json",
|
||||
"tsc": "tsc --project lib/tsconfig.json && tscpaths -p lib/tsconfig.json -s lib -o dist/@types",
|
||||
"lint": "yarn lint:eslint lib",
|
||||
|
||||
Reference in New Issue
Block a user