Files
jsonPrism/wrangler.jsonc
T
poli0981andClaude Opus 5.5 72cb9a033d feat: 2.0.2 — rate limiting for the whole site (#157)
The Worker now runs first for every request except the content-hashed
bundles under /assets/ (run_worker_first: ["/*", "!/assets/*"]) and counts
it against a per-client limit before serving it:

- SITE_LIMITER: pages and public files, 120 requests / 60 s
- API_LIMITER: every /api/* request, 60 / 60 s
- VERIFY_LIMITER: /api/turnstile/verify on top, 30 / 60 s (unchanged)

Over the limit, pages get a plain-text 429 in the four UI languages and the
API a JSON 429 (rate_limited); both carry Retry-After: 60. Clients are keyed
on their IPv4 address or the /64 of their IPv6 address, never logged. Pages
still go through env.ASSETS.fetch(), so _headers, _redirects and the SPA
fallback apply as before. A failing limiter lets requests through. Previews
get their own counters (namespace_id 4204/4206).

Docs: DEPLOYMENT, PRIVACY (wording only, nothing new collected, no
re-consent), SECURITY, CONTRIBUTING, READMEs, CLAUDE.md, ROADMAP, CHANGELOG.
Version 2.0.2 (versionCode 20002).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 05:36:02 +07:00

96 lines
3.6 KiB
JSON

// Cloudflare Worker for https://jsonprism.poli0981.dev
//
// Workers Builds (configured in the Cloudflare dashboard) runs
// `npx wrangler deploy` on every push to `main`; the "build" section below runs
// `npm run build` first. The Worker serves the Vite build from ./dist as static
// assets (SPA fallback to index.html). Every request except the hashed bundles
// under /assets/ runs the Worker first, which rate limits it per client, then
// hands pages to the ASSETS binding or answers /api/* itself (the Turnstile
// verification endpoint and a health check).
//
// Secrets (set in the dashboard, never committed):
// TURNSTILE_SECRET — Turnstile widget secret for jsonprism.poli0981.dev
// Local development: copy .dev.vars.example to .dev.vars (uses Cloudflare's
// always-pass test secret).
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "jsonprism",
"main": "worker/index.ts",
"compatibility_date": "2026-10-19",
// Build before every deploy, versions upload and dev session, so Workers Builds
// works whether or not a build command is set in the dashboard.
"build": { "command": "npm run build" },
"workers_dev": false,
"routes": [{ "pattern": "jsonprism.poli0981.dev", "custom_domain": true }],
"assets": {
"directory": "./dist",
"binding": "ASSETS",
"not_found_handling": "single-page-application",
// Everything except /assets/* (content-hashed, immutable, the bulk of each
// page load) goes through the Worker so it can be rate limited. Requests
// that reach the Worker are billed as Worker requests; /assets/* stays free.
"run_worker_first": ["/*", "!/assets/*"]
},
"vars": {
// Comma-separated hostnames a Turnstile token may have been issued on.
"ALLOWED_HOSTNAMES": "jsonprism.poli0981.dev"
},
// Per-client throttles (IPv4 address or IPv6 /64), counted per Cloudflare
// data centre. Over the limit → 429 with Retry-After: 60. namespace_id must
// be unique within the account; previews use the even twins below.
"ratelimits": [
{
// Pages and public files (index.html, theme-init.js, favicon…): about
// three per page load, so 40 page loads a minute per client.
"name": "SITE_LIMITER",
"namespace_id": "4205",
"simple": { "limit": 120, "period": 60 }
},
{
// Every /api/* request.
"name": "API_LIMITER",
"namespace_id": "4203",
"simple": { "limit": 60, "period": 60 }
},
{
// /api/turnstile/verify, on top of API_LIMITER.
"name": "VERIFY_LIMITER",
"namespace_id": "4201",
"simple": { "limit": 30, "period": 60 }
}
],
// Workers Builds runs `npx wrangler preview` for non-production branches.
// Previews inherit no vars or bindings, so they are declared again here:
// separate rate-limit counters, and no hostnames for the human check —
// previews get no TURNSTILE_SECRET either, so the check reports
// "unavailable" and lets testers continue. Web Analytics only loads on the
// production hostname.
"previews": {
"vars": {
"ALLOWED_HOSTNAMES": ""
},
"ratelimits": [
{
"name": "SITE_LIMITER",
"namespace_id": "4206",
"simple": { "limit": 120, "period": 60 }
},
{
"name": "API_LIMITER",
"namespace_id": "4204",
"simple": { "limit": 60, "period": 60 }
},
{
"name": "VERIFY_LIMITER",
"namespace_id": "4202",
"simple": { "limit": 30, "period": 60 }
}
]
},
"observability": {
"enabled": true,
"head_sampling_rate": 1
},
"upload_source_maps": false
}