import assert from "assert" ; import { Storage , StorageFactory , StoredValueMeta } from "@miniflare/shared" ; import { MemoryStorage } from "@miniflare/storage-memory" ; export class StackedMemoryStorage extends MemoryStorage { private readonly stack : Map < string , StoredValueMeta > [ ] = [ ] ; push ( ) { this . stack . push ( this . map ) ; this . map = new Map ( this . map ) ; } pop ( ) { this . map = this . stack . pop ( ) ?? new Map ( ) ; } } export class StackedMemoryStorageFactory implements StorageFactory { private readonly storages = new Map < string , StackedMemoryStorage > ( ) ; storage ( namespace , persist ? ) { assert ( ! persist ) ; let storage = this . storages . get ( namespace ) ; if ( storage ) return storage ; this . storages . set ( namespace , ( storage = new StackedMemoryStorage ( ) ) ) ; return storage ; } push ( ) { for ( const storage of this . storages . values ( ) ) storage . push ( ) ; } pop ( ) { for ( const storage of this . storages . values ( ) ) storage . pop ( ) ; } }