Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(ingest): return JSON for duplicate tracking requests (#596)

I'm Groot 🌳f7ab810520f4
diff --git a/packages/core/src/modules/ingest/ingest.routes.test.ts b/packages/core/src/modules/ingest/ingest.routes.test.ts
new file mode 100644
index 000000000..4bfc742dd
--- /dev/null
+++ b/packages/core/src/modules/ingest/ingest.routes.test.ts
@@ -0,0 +1,94 @@
+import { beforeAll, beforeEach, expect, mock, test } from 'bun:test';
+import { stubAppDeps } from '../../../test/http-fixtures';
+import type { ClientAuthResult } from '../../http/client-auth';
+
+const IDENTITY = { deviceId: 'device-1', sessionId: 'session-1' };
+const PAYLOAD = { type: 'track', payload: { name: 'screen_view' } };
+const authenticateClient = mock(
+ (): Promise<ClientAuthResult> =>
+ Promise.resolve({
+ ok: true,
+ client: {
+ id: 'client-1',
+ projectId: 'project-1',
+ organizationId: 'organization-1',
+ type: 'write',
+ secretVerified: true,
+ },
+ })
+);
+const isDuplicateIngestRequest = mock(async () => true);
+const ingestTrack = mock(async () => ({ status: 'ok' as const, ...IDENTITY }));
+const ingestLegacyEvent = mock(async () => ({ status: 'ok' as const }));
+const checkIngestBot = mock(async () => undefined);
+const isIngestionWoundDown = mock(async () => false);
+
+mock.module('../../http/client-auth', () => ({ authenticateClient }));
+
+const realService = { ...(await import('./ingest.service')) };
+mock.module('./ingest.service', () => ({
+ ...realService,
+ isDuplicateIngestRequest,
+ ingestTrack,
+ ingestLegacyEvent,
+ checkIngestBot,
+ isIngestionWoundDown,
+}));
+
+let ingestRoutes: typeof import('./ingest.routes').ingestRoutes;
+
+beforeAll(async () => {
+ ({ ingestRoutes } = await import('./ingest.routes'));
+});
+
+beforeEach(() => {
+ isDuplicateIngestRequest.mockReset();
+ isDuplicateIngestRequest.mockResolvedValue(true);
+ authenticateClient.mockClear();
+ ingestTrack.mockClear();
+ ingestLegacyEvent.mockClear();
+ checkIngestBot.mockClear();
+ isIngestionWoundDown.mockClear();
+});
+
+function post(path: string) {
+ const { deps } = stubAppDeps();
+ return ingestRoutes(deps).handle(
+ new Request(`http://localhost${path}`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(PAYLOAD),
+ })
+ );
+}
+
+for (const path of ['/track', '/event']) {
+ test(`duplicate ${path} answers JSON without processing the event again`, async () => {
+ const response = await post(path);
+
+ expect(response.status).toBe(200);
+ expect(response.headers.get('content-type')).toContain('application/json');
+ expect(JSON.parse(await response.text())).toEqual({ duplicate: true });
+ expect(isDuplicateIngestRequest).toHaveBeenCalledTimes(1);
+ expect(authenticateClient).not.toHaveBeenCalled();
+ expect(ingestTrack).not.toHaveBeenCalled();
+ expect(ingestLegacyEvent).not.toHaveBeenCalled();
+ expect(checkIngestBot).not.toHaveBeenCalled();
+ expect(isIngestionWoundDown).not.toHaveBeenCalled();
+ });
+}
+
+test('a non-duplicate track still returns device and session identity', async () => {
+ isDuplicateIngestRequest.mockResolvedValue(false);
+
+ const response = await post('/track');
+
+ expect(response.status).toBe(200);
+ expect(await response.json()).toEqual(IDENTITY);
+ expect(isDuplicateIngestRequest).toHaveBeenCalledTimes(1);
+ expect(authenticateClient).toHaveBeenCalledTimes(1);
+ expect(ingestTrack).toHaveBeenCalledTimes(1);
+ expect(ingestLegacyEvent).not.toHaveBeenCalled();
+ expect(checkIngestBot).toHaveBeenCalledTimes(1);
+ expect(isIngestionWoundDown).toHaveBeenCalledTimes(1);
+});
diff --git a/packages/core/src/modules/ingest/ingest.routes.ts b/packages/core/src/modules/ingest/ingest.routes.ts
index af2b1eacd..b0e3b3c82 100644
--- a/packages/core/src/modules/ingest/ingest.routes.ts
+++ b/packages/core/src/modules/ingest/ingest.routes.ts
@@ -30,7 +30,6 @@ import { produceIncomingEvent } from './src/kafka';
const TAGS = ['Track'];
const LEGACY_EVENT_TAGS = ['Event'];
-const DUPLICATE_BODY = 'Duplicate event';
/** Wind-down answers 202 with `{blocked:true}` rather than 402/403 — see
* `isIngestionWoundDown`. */
const WIND_DOWN_STATUS = 202;
@@ -173,7 +172,7 @@ export const ingestRoutes = defineRoutes((app, deps: AppDeps) => {
body,
})
) {
- return status(200, DUPLICATE_BODY);
+ return status(200, { duplicate: true });
}
},
},