Files
poli0981andClaude Opus 5.5 0cdc730461 feat(worker): site-wide rate limit with the branded 429 page (D32) (#49)
* feat(worker): site-wide rate limit with the branded 429 page (D32)
* docs: record how the rate-limit binding really counts (measured on the preview)

Every page, feed and data request runs the Worker first and spends one
unit of SITE_LIMITER (60 per 10 s per IPv4 or IPv6 /64); over it, the
built /errors/429 page (status 429, Retry-After 10, no-store) or JSON
under /api/. What a page loads stays asset-first. Fails open. Measured on
the preview: holds a client on one connection; many-connection floods are
for the zone rule (docs/16 §9.1). Privacy #rate-limit, LEGAL_VERSION
2026-10-26, suite v3.0, v1.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 06:16:14 +07:00

37 lines
1.6 KiB
TypeScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 (SkullMute)
//
// The ONE piece of server code (CLAUDE.md hard rule 1, D25, D32). It runs
// first for the paths in `assets.run_worker_first` (wrangler.jsonc): every
// page, feed and data file, but not the content-hashed assets, icons or the
// service worker. It does two things and serves nothing of its own:
// 1. the site-wide rate limit (worker/limit.ts) — over it, a 429;
// 2. POST /api/verify, the legal gate's Turnstile check (worker/verify.ts).
// Everything else is handed straight to the asset server.
import type { Env } from './env';
import { siteLimit } from './limit';
import { handleVerify } from './verify';
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const log = (entry: Record<string, unknown>) => console.log(JSON.stringify(entry));
const refused = await siteLimit(request, env, log);
if (refused) return refused;
if (new URL(request.url).pathname === '/api/verify') {
return handleVerify(request, env, {
// Wrapped: calling the global fetch detached from globalThis throws
// "Illegal invocation" in workerd.
fetch: (input, init) => fetch(input, init),
now: Date.now,
log,
});
}
// Exactly what the asset server would have answered without the Worker:
// ASSETS.fetch() applies html_handling, not_found_handling (the site's
// 404 page) and public/_headers — checked under `wrangler dev` and on a
// preview version, 2026-10-26.
return env.ASSETS.fetch(request);
},
};