/** * In-memory secure storage (for testing and development) */ import type { SecureStorageOptions, StorageResult } from "./types"; import { BaseSecureStorageAdapter, ok } from "./adapter"; /** * Memory-only storage adapter * * Data is NOT persisted and will be lost on app restart. * Use only for testing or ephemeral sessions. */ export class MemorySecureStorage extends BaseSecureStorageAdapter { readonly name = "memory"; readonly available = true; private store = new Map(); constructor(options: SecureStorageOptions = {}) { super(options); } async set(key: string, value: string): Promise> { this.store.set(this.key(key), value); return ok(undefined); } async get(key: string): Promise> { const v = this.store.get(this.key(key)); return ok(v ?? null); } async setItem(key: string, value: string): Promise { await this.set(key, value); } async getItem(key: string): Promise { const res = await this.get(key); return res.ok ? res.value : null; } async delete(key: string): Promise> { this.store.delete(this.key(key)); return ok(undefined); } async has(key: string): Promise> { return ok(this.store.has(this.key(key))); } async keys(): Promise> { const prefix = this.key(""); const keys = Array.from(this.store.keys()) .filter(k => k.startsWith(prefix)) .map(k => k.slice(prefix.length)); return ok(keys); } async clear(): Promise> { const prefix = this.key(""); for (const k of this.store.keys()) { if (k.startsWith(prefix)) { this.store.delete(k); } } return ok(undefined); } }