import { CorePlugin , MiniflareCore , MiniflareCoreOptions , Request , Response , } from "@miniflare/core" ; import { VMScriptRunner } from "@miniflare/runner-vm" ; import { Awaitable , Context , Log , NoOpLog , Options , PluginSignatures , StorageFactory , } from "@miniflare/shared" ; import { Response as BaseResponse } from "undici" ; import { MemoryStorageFactory } from "./storage" ; import { triggerPromise } from "./sync" ; const scriptRunner = new VMScriptRunner ( ) ; export function useMiniflare < Plugins extends PluginSignatures > ( extraPlugins , options , log = new NoOpLog ( ) , storageFactory = new MemoryStorageFactory ( ) ) { return new MiniflareCore ( { CorePlugin , ... extraPlugins } , { log , storageFactory , scriptRunner } , options ) ; } export function useMiniflareWithHandler < Plugins extends PluginSignatures > ( extraPlugins , options , handler , log = new NoOpLog ( ) ) { return useMiniflare ( extraPlugins , { ... options , script : `addEventListener("fetch", (e) => {
      e.respondWith((${ handler . toString ( ) })(globalThis, e.request));
    })` , } , log ) ; } export function waitForReload ( mf ) { const [ reloadTrigger , reloadPromise ] = triggerPromise < unknown > ( ) ; mf . addEventListener ( "reload" , reloadTrigger , { once : true } ) ; return reloadPromise ; }