UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

52 lines (49 loc) 1.36 kB
import { Temporal } from "@js-temporal/polyfill"; import { URLPattern } from "urlpattern-polyfill"; globalThis.addEventListener = () => {}; //#region federation/kv.ts /** * A key–value store that stores values in memory. * Do not use this in production as it does not persist values. * * @since 0.5.0 */ var MemoryKvStore = class { #values = {}; #encodeKey(key) { return JSON.stringify(key); } /** * {@inheritDoc KvStore.get} */ get(key) { const encodedKey = this.#encodeKey(key); const entry = this.#values[encodedKey]; if (entry == null) return Promise.resolve(void 0); const [value, expiration] = entry; if (expiration != null && Temporal.Now.instant().until(expiration).sign < 0) { delete this.#values[encodedKey]; return Promise.resolve(void 0); } return Promise.resolve(value); } /** * {@inheritDoc KvStore.set} */ set(key, value, options) { const encodedKey = this.#encodeKey(key); const expiration = options?.ttl == null ? null : Temporal.Now.instant().add(options.ttl.round({ largestUnit: "hour" })); this.#values[encodedKey] = [value, expiration]; return Promise.resolve(); } /** * {@inheritDoc KvStore.delete} */ delete(key) { const encodedKey = this.#encodeKey(key); delete this.#values[encodedKey]; return Promise.resolve(); } }; //#endregion export { MemoryKvStore };