import { IStorage } from "./IStorage"; export class JSONStorage implements IStorage { constructor(private storage: IStorage) { } public async get(key: string): Promise { let buffer = await this.storage.get(key); if (buffer === undefined) { return undefined; } try { return JSON.parse(buffer.toString()); } catch (e) { console.warn(`Failed to parse JSON for key: ${key}`, buffer.toString(), e); } } public async set(key: string, value: T): Promise { await this.storage.set(key, Buffer.from(JSON.stringify(value))); } public async remove(key: string): Promise { await this.storage.remove(key); } public async getKeys(): Promise { return await this.storage.getKeys(); } public async getInfo(key: string) { return await this.storage.getInfo(key); } public watchResync(callback: () => void): void { this.storage.watchResync?.(callback); } public async reset() { await this.storage.reset(); } }