Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ The Worker simply calls `fetch(request)` to reach your origin and separately pos

## Runtime Behavior

- Receives each incoming request, proxies to origin with `fetch`, and returns the origin response.
- Receives each incoming request, proxies to origin with `fetch`, and returns the origin response. If configuration is invalid, logs an error and just proxies (no tracking).
- Measures server time (`pf_srv` in seconds), status, and response bytes from `Content-Length` when present.
- Builds a Matomo payload with `idsite`, `rec:1`, `recMode:1`, `url`, `source:'Cloudflare'`, `cdt` (UTC `YYYY-MM-DD HH:mm:ss`), and `ua`.
- Skips tracking when `URL_EXCLUDE_REGEX` matches; detects downloads via `DOCUMENT_REGEX`; disallowed UAs are skipped by `USER_AGENT_ALLOWLIST_REGEX`.
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ export default {
env: Env,
ctx: WorkerContext
): Promise<Response> {
let config: MatomoConfig;
let config: MatomoConfig | null = null;
try {
config = getConfig(env);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return new Response(`Configuration error: ${message}`, { status: 500 });
console.error('Configuration error', { error: message });
return fetch(request);
}
const log = createLogger(config.logLevel);
const start = Date.now();
Expand Down
16 changes: 14 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ describe('Worker fetch handler', () => {
consoleWarn.mockRestore();
});

it('returns 500 when config is invalid', async () => {
it('returns origin response when config is invalid and skips tracking', async () => {
const originResponse = new Response('origin', { status: 200 });
const fetchMock = vi.fn().mockResolvedValue(originResponse);
global.fetch = fetchMock as unknown as typeof fetch;
const sendSpy = vi
.spyOn(http, 'sendMatomoHit')
.mockResolvedValue(undefined);

const response = await worker.fetch(
new Request('https://example.com/path'),
{ MATOMO_SITE_ID: '7' } as never,
{ waitUntil }
);
expect(response.status).toBe(500);
expect(response.status).toBe(200);
expect(waitUntil).not.toHaveBeenCalled();
expect(sendSpy).not.toHaveBeenCalled();
expect(consoleSpies.error).toHaveBeenCalledWith(
'Configuration error',
expect.objectContaining({ error: expect.stringContaining('MATOMO_URL') })
);
});
});