import assert from "assert" ; import path from "path" ; import { Storage , StorageFactory , sanitisePath } from "@miniflare/shared" ; import type { MemoryStorage } from "@miniflare/storage-memory" ; import type IORedis from "ioredis" ; const redisConnectionStringRegexp = /^rediss?:\/\// ; export class VariedStorageFactory implements StorageFactory { constructor ( private readonly memoryStorages = new Map < string , MemoryStorage > ( ) , private readonly redisConnections = new Map < string , IORedis . Redis > ( ) ) { } async storage ( namespace , persist ? ) { assert ( typeof persist !== "boolean" ) ; if ( persist === undefined ) { let storage = this . memoryStorages . get ( namespace ) ; if ( storage ) return storage ; const { MemoryStorage , } = require ( "@miniflare/storage-memory" ) ; this . memoryStorages . set ( namespace , ( storage = new MemoryStorage ( ) ) ) ; return storage ; } if ( redisConnectionStringRegexp . test ( persist ) ) { const { RedisStorage , } = require ( "@miniflare/storage-redis" ) ; const IORedis = require ( "ioredis" ) ; let connection = this . redisConnections . get ( persist ) ; if ( ! connection ) { this . redisConnections . set ( persist , ( connection = new IORedis ( persist ) ) ) ; } return new RedisStorage ( connection , namespace ) ; } const root = path . join ( persist , sanitisePath ( namespace ) ) ; const { FileStorage , } = require ( "@miniflare/storage-file" ) ; return new FileStorage ( root ) ; } async dispose ( ) { for ( const redisConnection of this . redisConnections . values ( ) ) { redisConnection . disconnect ( ) ; } } }