feat(encryption): refactor circular dependencies on services
This commit is contained in:
@@ -1,79 +0,0 @@
|
||||
import { EncryptedBytes } from '@standardnotes/files'
|
||||
|
||||
import { FileMemoryCache } from './FileMemoryCache'
|
||||
|
||||
describe('file memory cache', () => {
|
||||
const createBytes = (size: number): EncryptedBytes => {
|
||||
return { encryptedBytes: new TextEncoder().encode('a'.repeat(size)) }
|
||||
}
|
||||
|
||||
it('should add file', () => {
|
||||
const cache = new FileMemoryCache(5)
|
||||
const file = createBytes(1)
|
||||
cache.add('123', file)
|
||||
|
||||
expect(cache.get('123')).toEqual(file)
|
||||
})
|
||||
|
||||
it('should fail to add file if exceeds maximum', () => {
|
||||
const maxSize = 5
|
||||
const cache = new FileMemoryCache(maxSize)
|
||||
const file = createBytes(maxSize + 1)
|
||||
|
||||
expect(cache.add('123', file)).toEqual(false)
|
||||
})
|
||||
|
||||
it('should allow filling files up to limit', () => {
|
||||
const cache = new FileMemoryCache(5)
|
||||
|
||||
cache.add('1', createBytes(3))
|
||||
cache.add('2', createBytes(2))
|
||||
|
||||
expect(cache.get('1')).toBeTruthy()
|
||||
expect(cache.get('2')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should clear early files when adding new files above limit', () => {
|
||||
const cache = new FileMemoryCache(5)
|
||||
|
||||
cache.add('1', createBytes(3))
|
||||
cache.add('2', createBytes(2))
|
||||
cache.add('3', createBytes(5))
|
||||
|
||||
expect(cache.get('1')).toBeFalsy()
|
||||
expect(cache.get('2')).toBeFalsy()
|
||||
expect(cache.get('3')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should remove single file', () => {
|
||||
const cache = new FileMemoryCache(5)
|
||||
|
||||
cache.add('1', createBytes(3))
|
||||
cache.add('2', createBytes(2))
|
||||
|
||||
cache.remove('1')
|
||||
|
||||
expect(cache.get('1')).toBeFalsy()
|
||||
expect(cache.get('2')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should clear all files', () => {
|
||||
const cache = new FileMemoryCache(5)
|
||||
|
||||
cache.add('1', createBytes(3))
|
||||
cache.add('2', createBytes(2))
|
||||
cache.clear()
|
||||
|
||||
expect(cache.get('1')).toBeFalsy()
|
||||
expect(cache.get('2')).toBeFalsy()
|
||||
})
|
||||
|
||||
it('should return correct size', () => {
|
||||
const cache = new FileMemoryCache(20)
|
||||
|
||||
cache.add('1', createBytes(3))
|
||||
cache.add('2', createBytes(10))
|
||||
|
||||
expect(cache.size).toEqual(13)
|
||||
})
|
||||
})
|
||||
@@ -1,48 +0,0 @@
|
||||
import { removeFromArray } from '@standardnotes/utils'
|
||||
import { Uuid } from '@standardnotes/common'
|
||||
import { EncryptedBytes } from '@standardnotes/files'
|
||||
|
||||
export class FileMemoryCache {
|
||||
private cache: Record<Uuid, EncryptedBytes> = {}
|
||||
private orderedQueue: Uuid[] = []
|
||||
|
||||
constructor(public readonly maxSize: number) {}
|
||||
|
||||
add(uuid: Uuid, data: EncryptedBytes): boolean {
|
||||
if (data.encryptedBytes.length > this.maxSize) {
|
||||
return false
|
||||
}
|
||||
|
||||
while (this.size + data.encryptedBytes.length > this.maxSize) {
|
||||
this.remove(this.orderedQueue[0])
|
||||
}
|
||||
|
||||
this.cache[uuid] = data
|
||||
|
||||
this.orderedQueue.push(uuid)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return Object.values(this.cache)
|
||||
.map((bytes) => bytes.encryptedBytes.length)
|
||||
.reduce((total, fileLength) => total + fileLength, 0)
|
||||
}
|
||||
|
||||
get(uuid: Uuid): EncryptedBytes | undefined {
|
||||
return this.cache[uuid]
|
||||
}
|
||||
|
||||
remove(uuid: Uuid): void {
|
||||
delete this.cache[uuid]
|
||||
|
||||
removeFromArray(this.orderedQueue, uuid)
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.cache = {}
|
||||
|
||||
this.orderedQueue = []
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,3 @@ export * from './Streaming/StreamingReader'
|
||||
export * from './Streaming/StreamingSaver'
|
||||
export * from './Streaming/StreamingApi'
|
||||
export * from './utils'
|
||||
export * from './Cache/FileMemoryCache'
|
||||
|
||||
Reference in New Issue
Block a user