The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Retry a failed DoH lookup once in safeFetch
Rafiabda9eca8e3e
diff --git a/apps/web/lib/api/safe-fetch.ts b/apps/web/lib/api/safe-fetch.tsindex 8eff98beca..37b1507c7d 100644--- a/apps/web/lib/api/safe-fetch.ts+++ b/apps/web/lib/api/safe-fetch.ts@@ -67,7 +67,14 @@ async function resolveHostnameToIps(}};- const [a, aaaa] = await Promise.all([query("A"), query("AAAA")]);+ // Retry a failed lookup once, so a short DoH error does not reject a valid host.+ const queryWithRetry = async (type: "A" | "AAAA") =>+ (await query(type)) ?? (signal.aborted ? null : query(type));++ const [a, aaaa] = await Promise.all([+ queryWithRetry("A"),+ queryWithRetry("AAAA"),+ ]);if (a === null || aaaa === null) return null;return [...a, ...aaaa];}diff --git a/apps/web/tests/misc/safe-fetch.test.ts b/apps/web/tests/misc/safe-fetch.test.tsindex 05eacf3f9c..52d572604e 100644--- a/apps/web/tests/misc/safe-fetch.test.ts+++ b/apps/web/tests/misc/safe-fetch.test.ts@@ -201,6 +201,36 @@ describe("safeFetch", () => {);});+ it("retries a failed DoH lookup once", async () => {+ let aaaaCalls = 0;+ fetchMock.mockImplementation(async (input, init) => {+ const url = new URL(input.toString());+ if (url.searchParams.get("type") === "AAAA" && aaaaCalls++ === 0) {+ return new Response("error", { status: 503 });+ }+ return defaultFetch(input, init);+ });++ const res = await safeFetch("https://example.com");++ expect(await res.text()).toBe("ok");+ expect(aaaaCalls).toBe(2);+ });++ it("does not retry an NXDOMAIN answer", async () => {+ const lookups: string[] = [];+ fetchMock.mockImplementation(async (input) => {+ const url = new URL(input.toString());+ lookups.push(url.searchParams.get("type")!);+ return Response.json({ Status: 3 });+ });++ await expect(safeFetch("https://missing.example.com")).rejects.toThrow(+ "Could not resolve hostname: missing.example.com",+ );+ expect(lookups.sort()).toEqual(["A", "AAAA"]);+ });+it("fails closed when one DoH lookup fails", async () => {fetchMock.mockImplementation(async (input) => {const url = new URL(input.toString());