UNPKG

capacitor-shamir

Version:

Provides Shamir's Secret Sharing (SSS) functionality for secure splitting and recovering secrets natively on iOS, Android, and Web.

65 lines 2.18 kB
import { fromBase64, toBase64 } from './base64.utils'; import { IndexedDbFileStorage } from './indexeddb-file-storage'; export class FileSystemMock { constructor() { this.indexedStorage = new IndexedDbFileStorage(); } static getInstance() { if (!FileSystemMock.instance) { FileSystemMock.instance = new FileSystemMock(); } return FileSystemMock.instance; } async read(path, offset, count) { path = this.removeExtraSlashes(path); const foundFile = await this.indexedStorage.getItem(path); if (!foundFile) { throw new Error('[FilesystemMock] File not found'); } let content = fromBase64(foundFile.content); // cut everything up to offset if (offset) { content = content.slice(offset); } // cut everything after count if (count) { content = content.slice(undefined, count); } return content; } async write(path, data, append) { path = this.removeExtraSlashes(path); let file = await this.indexedStorage.getItem(path); if (file) { if (append) { const currentData = fromBase64(file.content); const appended = new Uint8Array(currentData.length + data.length); appended.set(currentData); appended.set(data, currentData.length); file.content = toBase64(appended); } else { file.content = toBase64(data); } } else { file = { path: path, mtime: new Date().getTime(), content: toBase64(data), }; } await this.indexedStorage.setItem(file); } async remove(path) { path = this.removeExtraSlashes(path); await this.indexedStorage.removeItem(path); } updateIndexedDbConfig(config) { this.indexedStorage.updateIndexedDbConfig(config); } removeExtraSlashes(path) { return path.replace(new RegExp('/{2,}', 'g'), '/'); } } //# sourceMappingURL=file-system.mock.js.map