The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { Miniflare, type MiniflareOptions, type Response } from "miniflare"; |
| 2 | import type { RolldownOutput } from "rolldown"; |
| 3 | import { miniflareModulesFromRolldownOutput } from "./miniflare-module.ts"; |
| 4 | |
| 5 | export interface MiniflareInstance { |
| 6 | url: URL; |
| 7 | fetch(path: string): Promise<Response>; |
| 8 | fetchText(path: string): Promise<string>; |
| 9 | fetchJson<T>(path: string): Promise<T>; |
| 10 | [Symbol.asyncDispose]: () => Promise<void>; |
| 11 | } |
| 12 | |
| 13 | export type Options = Extract<MiniflareOptions, { modules: Array<any> }>; |
| 14 | |
| 15 | export async function createMiniflare(options: Options): Promise<MiniflareInstance> { |
| 16 | const miniflare = new Miniflare(options); |
| 17 | const url = await miniflare.ready; |
| 18 | const fetch = (path: string) => miniflare.dispatchFetch(`http://localhost${path}`); |
| 19 | return { |
| 20 | url, |
| 21 | fetch, |
| 22 | fetchText: (path: string) => fetch(path).then((response) => response.text()), |
| 23 | fetchJson: <T>(path: string) => fetch(path).then((response) => response.json() as Promise<T>), |
| 24 | [Symbol.asyncDispose]: () => miniflare.dispose(), |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | export async function createMiniflareFromRolldown( |
| 29 | output: RolldownOutput, |
| 30 | options: Partial<Options> = {}, |
| 31 | ): Promise<MiniflareInstance> { |
| 32 | return createMiniflare({ |
| 33 | modules: miniflareModulesFromRolldownOutput(output.output), |
| 34 | ...options, |
| 35 | }); |
| 36 | } |