Files
poli0981andClaude Opus 5.5 7920011c70 feat(worker): rate limits for the whole site (4.5.0) (#167)
Every request that runs the Worker is counted per client (IPv4, or IPv6
/64): RL_SITE 120/min (app pages, public /api, the old apps' /data JSON,
misses), RL_IMG 600/min (covers), RL_ADMIN 300/min (/admin + /api/admin);
RL_SUGGEST 5 and RL_GATE 20 stay on top for their POSTs. Verified bots are
not counted. Over a limit: 429 + Retry-After 60 (JSON for the API, covers,
files and /data with CORS; a script-less EN/VI page for app pages). Fails
open.

Every rate-limit namespace moves to 8801-8806 (staging 8901-8906):
namespace ids are account-wide, and other Workers on the account use
1001-1006, so RL_SUGGEST / RL_GATE shared their counters.

docs/operations.md section 1 step 15: the zone's WAF rate-limiting rule as
the first layer (Free plan: one rule, IP, 10 s; 300 requests / 10 s).

[skip-discuss]

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

32 lines
1.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { ApiError, SessionExpired } from './api'
import { ERROR_TEXT, errorText } from './texts'
// The sources of every error the admin API answers with.
const SOURCES = import.meta.glob<string>(['../worker/admin.ts', '../worker/http.ts', '../worker/ratelimit.ts'], {
query: '?raw',
import: 'default',
eager: true,
})
describe('admin error texts', () => {
it('has wording for every error code of the admin API', () => {
const codes = new Set(
Object.values(SOURCES).flatMap((src) =>
[...src.matchAll(/HttpError\(\d{3}, '([a-z_]+)'|json\(\{ error: '([a-z_]+)' \}/g)].map((m) => m[1] ?? m[2]),
),
)
expect(codes).toContain('rate_limited')
expect(codes.size).toBeGreaterThan(15)
expect([...codes].filter((c) => !(c in ERROR_TEXT))).toEqual([])
})
it('adds the server message on a second line, and never loses an unknown code', () => {
expect(errorText(new ApiError(400, 'invalid_edit', 'nsfw must be one of No, Yes'))).toBe(
`${ERROR_TEXT.invalid_edit}\nnsfw must be one of No, Yes`,
)
expect(errorText(new ApiError(418, 'teapot'))).toBe('Error: teapot.')
expect(errorText(new SessionExpired())).toMatch(/expired/)
})
})