UNPKG

@sunney/flareutils

Version:

Small Utilities and little goodies that make developing with Cloudflare easier and faster.

53 lines (52 loc) 2.54 kB
/// <reference types="@cloudflare/workers-types" /> /** * Stores Durable Object Stubs in Memory, allowing faster recall. Utilizes KV and Unique DO IDs to reduce time to stub return, due to not necessitating global DO availability checks. */ export declare class Stubby { /** * Durable Object Namespace, to which the stubs pertain. */ private readonly ns; /** * KV Namespace used to globally cache DO Stub IDs. */ private readonly kv; /** * Used to ensure Cache Operations are executed while not blocking script execution. */ private readonly waitUntil; /** * Prefix used in KV operations, allows for multiple projects to share one KV namespace. Should be unique. */ private readonly prefix; /** * In memory DO Stub Cache. */ private readonly stubMap; /** * Constructs a Stubby Instance. * @param {DurableObjectNamespace} ns Durable Object Namespace utilized for stubs. * @param {KVNamespace} kv KV Namespace used to store raw DO IDs. * @param {ExecutionContext["waitUntil"]} waitUntil Used to unblock thread, ensuring KV writes are completed without pausing execution of the rest of the script. * @param {string} prefix Used to prefix KV writes, allowing multiple systems to share one KV namespaces without collisions. */ constructor(ns: DurableObjectNamespace, kv: KVNamespace, waitUntil: ExecutionContext["waitUntil"], prefix?: string); /** * Returns a stub from memory or KV, or generates a new stub if not available. Caches returned stubs to ensure quickest recall. * @param {string} key Used to identify stub for retrieval or creation. * @param {string} [locationHint] Used to hint at the location of the stub, allowing for faster stub creation. Available hints are available on the [DO Docs](https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#providing-a-location-hint). * @returns {Promise<DurableObjectStub>} */ get(key: string, locationHint?: DurableObjectLocationHint): Promise<DurableObjectStub>; /** * Removes a singular stub from local and KV storage. Note that this operation is irreversable, and stubs will not be recoverable unless stored elsewhere. * @param {string} key Used to identify stub to remove. * @returns {Promise<void>} */ remove(key: string): Promise<void>; /** * Clears all stubs belonging to this Stubby instance. * @returns {Promise<void>} */ clearAll(): Promise<void>; }