import type { DurableObjectStub } from "@cloudflare/workers-types/experimental"; import type { ReplaceWorkersTypes } from "miniflare"; import type { ReadableStream } from "node:stream/web"; export class MiniflareDurableObjectControlStub { constructor(private readonly stub: ReplaceWorkersTypes) {} async #call(name: string, ...args: unknown[]): Promise { const response = await this.stub.fetch("http://placeholder/", { cf: { miniflare: { controlOp: { name, args } } }, }); const result = response.json(); return (result ?? undefined) as T; } sqlQuery(query: string, ...params: unknown[]): Promise { return this.#call("sqlQuery", query, ...params); } async getBlob(id: string): Promise { const response = await this.stub.fetch("http://placeholder/", { cf: { miniflare: { controlOp: { name: "getBlob", args: [id] } } }, }); if (response.status === 404) { await response.arrayBuffer(); return null; } return response.body; } enableFakeTimers(timestamp: number): Promise { return this.#call("enableFakeTimers", timestamp); } disableFakeTimers(): Promise { return this.#call("disableFakeTimers"); } advanceFakeTime(delta: number): Promise { return this.#call("advanceFakeTime", delta); } waitForFakeTasks(): Promise { return this.#call("waitForFakeTasks"); } }