/** * Tiny IndexedDB sidecar for persisting FileSystemDirectoryHandle. * * SQLite can't store DOM objects (they require structured clone), * so we use a minimal IDB store exclusively for the folder handle. */ const DB_NAME = 'FolderSyncDB_handles'; const STORE_NAME = 'handles'; const DB_VERSION = 1; function openHandleDB(): Promise { return new Promise((resolve, reject) => { const req = indexedDB.open(DB_NAME, DB_VERSION); req.onupgradeneeded = () => { req.result.createObjectStore(STORE_NAME, { keyPath: 'key' }); }; req.onsuccess = () => resolve(req.result); req.onerror = () => reject(req.error); }); } export async function storeHandle( key: string, handle: FileSystemDirectoryHandle, ): Promise { const db = await openHandleDB(); try { const tx = db.transaction(STORE_NAME, 'readwrite'); tx.objectStore(STORE_NAME).put({ key, handle }); await new Promise((resolve, reject) => { tx.oncomplete = () => resolve(); tx.onerror = () => reject(tx.error); }); } finally { db.close(); } } export async function loadHandle( key: string, ): Promise { const db = await openHandleDB(); try { const tx = db.transaction(STORE_NAME, 'readonly'); const req = tx.objectStore(STORE_NAME).get(key); return await new Promise( (resolve, reject) => { req.onsuccess = () => resolve(req.result?.handle ?? null); req.onerror = () => reject(req.error); }, ); } finally { db.close(); } }