import http from "http" ; import https from "https" ; import { CachePlugin , CacheStorage } from "@miniflare/cache" ; import { BindingsPlugin , BuildPlugin , CorePlugin , MiniflareCore , MiniflareCoreOptions , } from "@miniflare/core" ; import { DurableObjectId , DurableObjectNamespace , DurableObjectStorage , DurableObjectsPlugin , } from "@miniflare/durable-objects" ; import { HTMLRewriterPlugin } from "@miniflare/html-rewriter" ; import { DEFAULT_PORT , HTTPPlugin , createServer , startServer , } from "@miniflare/http-server" ; import { KVNamespace , KVPlugin } from "@miniflare/kv" ; import { VMScriptRunner } from "@miniflare/runner-vm" ; import { CronScheduler , SchedulerPlugin , startScheduler , } from "@miniflare/scheduler" ; import { Log , NoOpLog } from "@miniflare/shared" ; import { SitesPlugin } from "@miniflare/sites" ; import { WebSocketPlugin } from "@miniflare/web-sockets" ; import sourceMap from "source-map-support" ; import { VariedStorageFactory } from "./storage" ; export const PLUGINS = { CorePlugin , HTTPPlugin , SchedulerPlugin , BuildPlugin , KVPlugin , DurableObjectsPlugin , CachePlugin , SitesPlugin , HTMLRewriterPlugin , WebSocketPlugin , BindingsPlugin , } ; export type Plugins = typeof PLUGINS ; export type MiniflareOptions = Omit < MiniflareCoreOptions < Plugins > , "debug" | "verbose" | "updateCheck" > & { log ? : Log ; sourceMap ? : boolean ; } ; export class Miniflare extends MiniflareCore < Plugins > { #storageFactory : VariedStorageFactory ; constructor ( options ? ) { if ( options ?. sourceMap ) { sourceMap . install ( { emptyCacheBetweenOperations : true } ) ; } const storageFactory = new VariedStorageFactory ( ) ; super ( PLUGINS , { log : options ?. log ?? new NoOpLog ( ) , storageFactory , scriptRunner : new VMScriptRunner ( ) , scriptRequired : true , } , options ) ; this . #storageFactory = storageFactory ; } async dispose ( ) { await super . dispose ( ) ; await this . #storageFactory . dispose ( ) ; } async getKVNamespace ( namespace ) { const plugin = ( await this . getPlugins ( ) ) . KVPlugin ; const storage = this . getPluginStorage ( "KVPlugin" ) ; return plugin . getNamespace ( storage , namespace ) ; } async getCaches ( ) { const plugin = ( await this . getPlugins ( ) ) . CachePlugin ; return plugin . getCaches ( ) ; } async getDurableObjectNamespace ( objectName ) { const plugin = ( await this . getPlugins ( ) ) . DurableObjectsPlugin ; const storage = this . getPluginStorage ( "DurableObjectsPlugin" ) ; return plugin . getNamespace ( storage , objectName ) ; } async getDurableObjectStorage ( id ) { const plugin = ( await this . getPlugins ( ) ) . DurableObjectsPlugin ; const storage = this . getPluginStorage ( "DurableObjectsPlugin" ) ; const state = await plugin . getObject ( storage , id ) ; return state . storage ; } createServer ( options ? ) { return createServer ( this , options ) ; } startServer ( options ? ) { return startServer ( this , options ) ; } startScheduler ( ) { return startScheduler ( this ) ; } async getOpenURL ( ) { const { open , httpsEnabled , host = "localhost" , port = DEFAULT_PORT , } = ( await this . getPlugins ( ) ) . HTTPPlugin ; if ( ! open ) return ; if ( typeof open === "string" ) return open ; const protocol = httpsEnabled ? "https" : "http" ; return `${ protocol }://${ host }:${ port }/` ; } }