70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
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<DurableObjectStub>) {}
|
|
|
|
async #call<T>(name: string, ...args: unknown[]): Promise<T> {
|
|
const response = await this.stub.fetch("http://placeholder/", {
|
|
cf: { miniflare: { controlOp: { name, args } } },
|
|
});
|
|
const result = response.json();
|
|
return (result ?? undefined) as T;
|
|
}
|
|
|
|
sqlQuery<T>(query: string, ...params: unknown[]): Promise<T[]> {
|
|
return this.#call("sqlQuery", query, ...params);
|
|
}
|
|
|
|
async getBlob(id: string): Promise<ReadableStream | null> {
|
|
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<void> {
|
|
return this.#call("enableFakeTimers", timestamp);
|
|
}
|
|
disableFakeTimers(): Promise<void> {
|
|
return this.#call("disableFakeTimers");
|
|
}
|
|
advanceFakeTime(delta: number): Promise<void> {
|
|
return this.#call("advanceFakeTime", delta);
|
|
}
|
|
waitForFakeTasks(): Promise<void> {
|
|
return this.#call("waitForFakeTasks");
|
|
}
|
|
}
|