fix: extract encryption type split functions

This commit is contained in:
Karol Sójko
2022-07-05 12:22:38 +02:00
parent d026b659fb
commit 2ec1a4dbb4
4 changed files with 25 additions and 22 deletions

View File

@@ -40,7 +40,7 @@ import {
isEncryptedPayload,
RootKeyInterface,
} from '@standardnotes/models'
import { SplitPayloadsByEncryptionType } from '../../Split/EncryptionTypeSplit'
import { SplitPayloadsByEncryptionType } from '../../Split/Functions'
import { ClientDisplayableError } from '@standardnotes/responses'
import { isNotUndefined } from '@standardnotes/utils'
import { DiagnosticInfo } from '@standardnotes/services'

View File

@@ -1,27 +1,6 @@
import { DecryptedPayloadInterface, EncryptedPayloadInterface } from '@standardnotes/models'
import { ItemContentTypeUsesRootKeyEncryption } from '../Keys/RootKey/Functions'
export interface EncryptionTypeSplit<T = EncryptedPayloadInterface | DecryptedPayloadInterface> {
rootKeyEncryption?: T[]
itemsKeyEncryption?: T[]
}
export function SplitPayloadsByEncryptionType<T extends EncryptedPayloadInterface | DecryptedPayloadInterface>(
payloads: T[],
): EncryptionTypeSplit<T> {
const usesRootKey: T[] = []
const usesItemsKey: T[] = []
for (const item of payloads) {
if (ItemContentTypeUsesRootKeyEncryption(item.content_type)) {
usesRootKey.push(item)
} else {
usesItemsKey.push(item)
}
}
return {
rootKeyEncryption: usesRootKey.length > 0 ? usesRootKey : undefined,
itemsKeyEncryption: usesItemsKey.length > 0 ? usesItemsKey : undefined,
}
}

View File

@@ -0,0 +1,23 @@
import { DecryptedPayloadInterface, EncryptedPayloadInterface } from '@standardnotes/models'
import { ItemContentTypeUsesRootKeyEncryption } from '../Keys/RootKey/Functions'
import { EncryptionTypeSplit } from './EncryptionTypeSplit'
export function SplitPayloadsByEncryptionType<T extends EncryptedPayloadInterface | DecryptedPayloadInterface>(
payloads: T[],
): EncryptionTypeSplit<T> {
const usesRootKey: T[] = []
const usesItemsKey: T[] = []
for (const item of payloads) {
if (ItemContentTypeUsesRootKeyEncryption(item.content_type)) {
usesRootKey.push(item)
} else {
usesItemsKey.push(item)
}
}
return {
rootKeyEncryption: usesRootKey.length > 0 ? usesRootKey : undefined,
itemsKeyEncryption: usesItemsKey.length > 0 ? usesItemsKey : undefined,
}
}

View File

@@ -28,6 +28,7 @@ export * from './Service/RootKey/KeyMode'
export * from './Service/RootKey/RootKeyEncryption'
export * from './Split/EncryptionSplit'
export * from './Split/EncryptionTypeSplit'
export * from './Split/Functions'
export * from './Types/EncryptedParameters'
export * from './Types/ItemAuthenticatedData'
export * from './Types/LegacyAttachedData'