import { defaultClock } from "@miniflare/shared" ; import { StoredKeyMeta , StoredMeta , StoredValueMeta } from "@miniflare/shared" ; import { cloneMetadata } from "./helpers" ; import { LocalStorage } from "./local" ; export class MemoryStorage extends LocalStorage { constructor ( protected map = new Map < string , StoredValueMeta > ( ) , clock = defaultClock ) { super ( clock ) ; } hasMaybeExpired ( key ) { const stored = this . map . get ( key ) ; return ( stored && { expiration : stored . expiration , metadata : cloneMetadata ( stored . metadata ) , } ) ; } getMaybeExpired < Meta > ( key ) { const stored = this . map . get ( key ) ; return ( stored && { value : stored . value . slice ( ) , expiration : stored . expiration , metadata : cloneMetadata ( stored . metadata ) , } ) ; } put < Meta = unknown > ( key , value ) { this . map . set ( key , { value : value . value . slice ( ) , expiration : value . expiration , metadata : cloneMetadata ( value . metadata ) , } ) ; } deleteMaybeExpired ( key ) { return this . map . delete ( key ) ; } private static entryToStoredKey ( [ name , { expiration , metadata } ] ) { return { name , expiration , metadata : cloneMetadata ( metadata ) , } ; } listAllMaybeExpired < Meta > ( ) { return Array . from ( this . map . entries ( ) ) . map ( MemoryStorage . entryToStoredKey ) as StoredKeyMeta < Meta > [ ] ; } }