diff --git a/apps/web/src/lib/server/metrics/metrics.test.ts b/apps/web/src/lib/server/metrics/metrics.test.ts index 3fa6005..cfa9cb6 100644 --- a/apps/web/src/lib/server/metrics/metrics.test.ts +++ b/apps/web/src/lib/server/metrics/metrics.test.ts @@ -302,6 +302,21 @@ describe('siteMetrics', () => { expect(empty.byDay).toEqual([]); }); + it('never calls fetch with the api object as `this` — workerd rejects that', async () => { + const thisValues: unknown[] = []; + const api = { + accountId: 'acct', + token: 'tok', + fetch: function (this: unknown) { + thisValues.push(this); + return Promise.resolve(new Response(JSON.stringify({ data: [] }))); + } as typeof fetch + }; + await siteMetrics(api, DID, 7); + expect(thisValues.length).toBeGreaterThan(0); + for (const t of thisValues) expect(t).toBe(globalThis); + }); + it('surfaces an API failure with the status', async () => { const api = { accountId: 'acct', diff --git a/apps/web/src/lib/server/metrics/query.ts b/apps/web/src/lib/server/metrics/query.ts index 5b022ef..d1b68ff 100644 --- a/apps/web/src/lib/server/metrics/query.ts +++ b/apps/web/src/lib/server/metrics/query.ts @@ -49,7 +49,9 @@ export function didLiteral(did: string): string { } async function sql(api: MetricsApi, query: string): Promise[]> { - const res = await api.fetch( + // fetch rejects a foreign `this` (the api object a method call would pass). + const res = await api.fetch.call( + globalThis, `https://api.cloudflare.com/client/v4/accounts/${api.accountId}/analytics_engine/sql`, { method: 'POST',