import { Awaitable } from "./sync" ; export interface StoredMeta < Meta = unknown > { /** Unix timestamp in seconds when this key expires */ expiration ? : number ; /** Arbitrary JSON-serializable object */ metadata ? : Meta ; } export interface StoredValue { value : Uint8Array ; } export interface StoredKey { name : string ; } export type StoredValueMeta < Meta = unknown > = StoredValue & StoredMeta < Meta > ; export type StoredKeyMeta < Meta = unknown > = StoredKey & StoredMeta < Meta > ; export interface StorageListOptions { /** Returned keys must start with this string if defined */ prefix ? : string ; /** Returned keys must be lexicographically >= this string if defined */ start ? : string ; /** Returned keys must be lexicographically < this string if defined */ end ? : string ; /** Return keys in reverse order, MUST be applied before the limit/cursor */ reverse ? : boolean ; /** Cursor for pagination, undefined/empty-string means start at beginning */ cursor ? : string ; /** Maximum number of keys to return if defined */ limit ? : number ; } export interface StorageListResult < Key extends StoredKey = StoredKeyMeta > { keys : Key [ ] ; /** Cursor for next page */ cursor : string ; } /**
 * Common class for key-value storage:
 * - Methods should always return fresh copies of data (safe to mutate returned)
 * - Methods shouldn't return expired keys
 */ export abstract class Storage { abstract has ( key ) ; abstract get < Meta = unknown > ( key , skipMetadata ? ) ; abstract get ( key , skipMetadata ) ; abstract put < Meta = unknown > ( key , value ) ; abstract delete ( key ) ; abstract list < Meta = unknown > ( options ? , skipMetadata ? ) ; abstract list ( options , skipMetadata ) ; async hasMany ( keys ) { const results = keys . map ( this . has . bind ( this ) ) ; let count = 0 ; for ( const result of await Promise . all ( results ) ) if ( result ) count ++ ; return count ; } getMany < Meta = unknown > ( keys , skipMetadata ? ) ; getMany ( keys , skipMetadata ) ; getMany < Meta = unknown > ( keys , skipMetadata ? ) { return Promise . all ( keys . map ( ( key ) => this . get ( key , skipMetadata as any ) ) ) as Promise < ( StoredValueMeta < Meta > | undefined ) [ ] > ; } async putMany < Meta = unknown > ( data ) { await Promise . all ( data . map ( ( [ key , value ] ) => this . put ( key , value ) ) ) ; } async deleteMany ( keys ) { const results = keys . map ( this . delete . bind ( this ) ) ; let count = 0 ; for ( const result of await Promise . all ( results ) ) if ( result ) count ++ ; return count ; } } export interface StorageFactory { storage ( namespace , persist ? ) : Awaitable < Storage > ; dispose ? ( ) : Awaitable < void > ; }