From 2d4137073c0d71a09009ba69af80631c97efe92f Mon Sep 17 00:00:00 2001 From: Yaacov Date: Tue, 8 Sep 2026 19:51:48 +0300 Subject: [PATCH] Fix PostHog transport in Cloudflare Workers --- workers/events/src/readiness.test.ts | 14 ++++++++++++-- workers/events/src/transport.ts | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/workers/events/src/readiness.test.ts b/workers/events/src/readiness.test.ts index af2c42c..975e81d 100644 --- a/workers/events/src/readiness.test.ts +++ b/workers/events/src/readiness.test.ts @@ -5,7 +5,7 @@ import worker, { pruneExpiredAnalyticsEvents, } from "./index"; import { testDatabase } from "./sqlite.testSupport"; -import { posthogEventUuid, readBoundedJson } from "./transport"; +import { posthogEventUuid, posthogRequest, readBoundedJson } from "./transport"; import { withExportLease } from "./exportLease"; const installation = "installation:10000000-0000-4000-8000-000000000001"; @@ -73,6 +73,16 @@ afterEach(() => { for (const db of databases.splice(0)) db.close(); }); +it("does not follow PostHog redirects or turn them into network failures", async () => { + const fetcher = vi.fn().mockResolvedValue(new Response(null, { status: 302 })); + vi.stubGlobal("fetch", fetcher); + + await expect( + posthogRequest("https://eu.i.posthog.com/batch", { method: "POST" }), + ).rejects.toThrow("http"); + expect(fetcher.mock.calls[0]![1].redirect).toBe("manual"); +}); + describe("inactive gateway readiness with real SQL", () => { it("keeps diagnostic events queryable in D1 without exporting them to PostHog", async () => { const f = fixture(); @@ -423,7 +433,7 @@ describe("inactive gateway readiness with real SQL", () => { expect(first.uuid).toMatch( /^[a-f0-9]{8}-[a-f0-9]{4}-8[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/, ); - expect(fetcher.mock.calls[0]![1].redirect).toBe("error"); + expect(fetcher.mock.calls[0]![1].redirect).toBe("manual"); expect(fetcher.mock.calls[0]![1].signal).toBeInstanceOf(AbortSignal); }); diff --git a/workers/events/src/transport.ts b/workers/events/src/transport.ts index 1a88a3b..21fe433 100644 --- a/workers/events/src/transport.ts +++ b/workers/events/src/transport.ts @@ -45,7 +45,11 @@ export async function posthogRequest(url: string, init: RequestInit): Promise