UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

43 lines (42 loc) 1.29 kB
import { CryptographicKey, Multikey } from "../vocab/vocab.js"; export class KvKeyCache { kv; prefix; options; nullKeys; constructor(kv, prefix, options = {}) { this.kv = kv; this.prefix = prefix; this.nullKeys = new Set(); this.options = options; } async get(keyId) { if (this.nullKeys.has(keyId.href)) return null; const serialized = await this.kv.get([...this.prefix, keyId.href]); if (serialized == null) return undefined; try { return await CryptographicKey.fromJsonLd(serialized, this.options); } catch { try { return await Multikey.fromJsonLd(serialized, this.options); } catch { await this.kv.delete([...this.prefix, keyId.href]); return undefined; } } } async set(keyId, key) { if (key == null) { this.nullKeys.add(keyId.href); await this.kv.delete([...this.prefix, keyId.href]); return; } this.nullKeys.delete(keyId.href); const serialized = await key.toJsonLd(this.options); await this.kv.set([...this.prefix, keyId.href], serialized); } }