@fedify/fedify
Version:
An ActivityPub server framework
111 lines • 3.26 kB
text/typescript
/// <reference lib="esnext.temporal" />
//#region src/federation/kv.d.ts
/**
* A key for a key–value store. An array of one or more strings.
*
* @since 0.5.0
*/
type KvKey = readonly [string] | readonly [string, ...string[]];
/**
* Additional options for setting a value in a key–value store.
*
* @since 0.5.0
*/
interface KvStoreSetOptions {
/**
* The time-to-live (TTL) for the value.
*/
ttl?: Temporal.Duration;
}
/**
* An entry returned by the {@link KvStore.list} method.
*
* @since 1.10.0
*/
interface KvStoreListEntry {
/**
* The key of the entry.
*/
readonly key: KvKey;
/**
* The value of the entry.
*/
readonly value: unknown;
}
/**
* An abstract interface for a key–value store.
*
* @since 0.5.0
*/
interface KvStore {
/**
* Gets the value for the given key.
* @param key The key to get the value for.
* @returns The value for the key, or `undefined` if the key does not exist.
* @template T The type of the value to get.
*/
get<T = unknown>(key: KvKey): Promise<T | undefined>;
/**
* Sets the value for the given key.
* @param key The key to set the value for.
* @param value The value to set.
* @param options Additional options for setting the value.
*/
set(key: KvKey, value: unknown, options?: KvStoreSetOptions): Promise<void>;
/**
* Deletes the value for the given key.
* @param key The key to delete.
*/
delete(key: KvKey): Promise<void>;
/**
* Compare-and-swap (CAS) operation for the key–value store.
* @param key The key to perform the CAS operation on.
* @param expectedValue The expected value for the key.
* @param newValue The new value to set if the expected value matches.
* @param options Additional options for setting the value.
* @return `true` if the CAS operation was successful, `false` otherwise.
* @since 1.8.0
*/
cas?: (key: KvKey, expectedValue: unknown, newValue: unknown, options?: KvStoreSetOptions) => Promise<boolean>;
/**
* Lists all entries in the store that match the given prefix.
* If no prefix is given, all entries are returned.
* @param prefix The prefix to filter keys by. If not specified, all entries
* are returned.
* @returns An async iterable of entries matching the prefix.
* @since 1.10.0
* @since 2.0.0 This method is now required instead of optional.
*/
list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
}
/**
* 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
*/
declare class MemoryKvStore implements KvStore {
#private;
/**
* {@inheritDoc KvStore.get}
*/
get<T = unknown>(key: KvKey): Promise<T | undefined>;
/**
* {@inheritDoc KvStore.set}
*/
set(key: KvKey, value: unknown, options?: KvStoreSetOptions): Promise<void>;
/**
* {@inheritDoc KvStore.delete}
*/
delete(key: KvKey): Promise<void>;
/**
* {@inheritDoc KvStore.cas}
*/
cas(key: KvKey, expectedValue: unknown, newValue: unknown, options?: KvStoreSetOptions): Promise<boolean>;
/**
* {@inheritDoc KvStore.list}
*/
list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
}
//#endregion
export { MemoryKvStore as a, KvStoreSetOptions as i, KvStore as n, KvStoreListEntry as r, KvKey as t };