import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { fetchWithin, upstream } from "../src/xrpc"; import { routeFetch } from "./helpers"; const url = "https://knot.example.com/xrpc/sh.tangled.x"; function endlessBody(_url: URL, request: Request): Response { return new Response( new ReadableStream({ start(controller) { request.signal.addEventListener("abort", () => { controller.error(request.signal.reason); }); }, }), ); } beforeEach(() => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); }); afterEach(() => { vi.useRealTimers(); }); describe("fetchWithin", () => { it.each([ ["returns", async (response: Response) => await response.text()], ["throws", async () => await Promise.reject(new Error("bad body"))], ])("clears its deadline when the read %s", async (_name, read) => { routeFetch({ [`GET ${url}`]: () => new Response("body") }); let pending = 0; await fetchWithin(url, {}, 60_000, async (response) => { pending = vi.getTimerCount(); return await read(response); }).catch(() => {}); expect(pending).toBe(1); expect(vi.getTimerCount()).toBe(0); }); it("bypasses the Cloudflare cache", async () => { let cache: string | undefined; routeFetch({ [`GET ${url}`]: (_url, request) => { cache = request.cache; return new Response("body"); }, }); await fetchWithin(url, {}, 60_000, async (response) => await response.text()); expect(cache).toBe("no-store"); }); it("fails a read that does not end before the deadline", async () => { routeFetch({ [`GET ${url}`]: endlessBody }); const failed = expect( fetchWithin(url, {}, 60_000, async (response) => await response.text()), ).rejects.toThrow("timed out"); await vi.advanceTimersByTimeAsync(59_999); expect(vi.getTimerCount()).toBe(1); await vi.advanceTimersByTimeAsync(1); await failed; expect(vi.getTimerCount()).toBe(0); }); }); describe("upstream", () => { it("clears its deadline when the caller never reads the body", async () => { routeFetch({ [`GET ${url}`]: () => new Response("body", { status: 404 }) }); const response = await upstream(url, 503); expect(vi.getTimerCount()).toBe(0); expect(response.status).toBe(404); expect(await response.text()).toBe("body"); }); it("clears its deadline after a WebSocket upgrade", async () => { const { 0: client } = new WebSocketPair(); routeFetch({ [`GET ${url}`]: () => new Response(null, { status: 101, webSocket: client }), }); const response = await upstream(url, 503, { headers: { Upgrade: "websocket" } }); expect(response.webSocket).not.toBeNull(); expect(vi.getTimerCount()).toBe(0); }); it("fails when the body does not end before the deadline", async () => { routeFetch({ [`GET ${url}`]: endlessBody }); const failed = expect(upstream(url, 503, undefined, { timeoutMs: 60_000 })).rejects.toThrow( `${url}: TimeoutError`, ); await vi.advanceTimersByTimeAsync(60_000); await failed; expect(vi.getTimerCount()).toBe(0); }); it("fails a body over 1 MiB", async () => { const body = new Uint8Array(1024 * 1024); routeFetch({ [`GET ${url}`]: () => new Response(body) }); expect((await (await upstream(url, 503)).arrayBuffer()).byteLength).toBe(body.length); routeFetch({ [`GET ${url}`]: () => new Response(new Uint8Array(body.length + 1)) }); await expect(upstream(url, 503)).rejects.toThrow(`${url}: ResponseTooLarge`); }); });