UNPKG

@atproto/oauth-client-browser

Version:

ATPROTO OAuth client for the browser (relies on WebCrypto & Indexed DB)

54 lines 1.77 kB
import { DB } from './indexed-db/index.js'; const storeName = 'store'; export class IndexedDBStore { constructor(dbName, maxAge = 600e3) { this.dbName = dbName; this.maxAge = maxAge; } async run(mode, fn) { const db = await DB.open(this.dbName, [ (db) => { const store = db.createObjectStore(storeName); store.createIndex('createdAt', 'createdAt', { unique: false }); }, ], { durability: 'strict' }); try { return await db.transaction([storeName], mode, (tx) => fn(tx.objectStore(storeName))); } finally { await db[Symbol.asyncDispose](); } } async get(key) { const item = await this.run('readonly', (store) => store.get(key)); if (!item) return undefined; const age = Date.now() - item.createdAt.getTime(); if (age > this.maxAge) { await this.del(key); return undefined; } return item?.value; } async set(key, value) { await this.run('readwrite', (store) => { store.put({ value, createdAt: new Date() }, key); }); } async del(key) { await this.run('readwrite', (store) => { store.delete(key); }); } async deleteOutdated() { const upperBound = new Date(Date.now() - this.maxAge); const query = IDBKeyRange.upperBound(upperBound); await this.run('readwrite', async (store) => { const index = store.index('createdAt'); const keys = await index.getAllKeys(query); for (const key of keys) store.delete(key); }); } } //# sourceMappingURL=indexed-db-store.js.map