Files
poli0981andClaude Opus 5 8f121a8e9c feat(worker): review queue, admin API, ingest and suggest endpoints (v4 Phase 4)
Adds the server side of the admin and the automatic game discovery. The
catalog stays JSON in this repo; the Worker writes it as a GitHub App
(GraphQL createCommitOnBranch with expectedHeadOid: verified commits,
retried on a race) and keeps only the review queue in D1.

- worker/admin.ts: /api/admin/* (queue list, approve/reject, add, edit the
  three maintainer fields, remove with reason + R2 cover cleanup, restore)
  and the /admin shell with its own CSP. Every request re-verifies the
  Cloudflare Access JWT (aud + maintainer email + same-origin writes).
  Approving commits to scripts/temp_link.json, which starts update.yml.
  The maintainer's email goes to the D1 audit log only, never a commit.
- worker/ingest.ts: POST /api/ingest for the browser extension (Access
  service token, per-client rate limit, Idempotency-Key replay, <= 150
  items / 64 KB), replacing its PAT commits.
- worker/suggest.ts: public POST /api/suggest (same-origin, per-IP rate
  limit, Turnstile siteverify, canonical itch.io URL) into the queue.
- worker/discover.ts + scheduled(): every 4 h, one itch.io RSS feed
  (round-robin, 6 h back-off on 429) into the queue, then reconcile
  (queued -> ingested / failed) and retention (idempotency 7 d, form
  notes 180 d).
- worker/queue.ts: URL classification (catalog, pending, deleted,
  blocklist) and the D1 store; worker/repo.ts builds each admin commit
  from the files at the head of main, byte-compatible with the Python
  pipeline (golden fixtures).
- wrangler.jsonc: D1 (bound by name; migrations applied by hand), rate
  limits, cron, non-secret settings as vars (empty until setup: features
  answer 503 until configured), staging env without the cron.
  `npm run cf:types` ignores .dev.vars so the committed types are stable.
- Tests: 159 passing; the SQL runs against a real local D1 (workerd via
  wrangler's platform proxy).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:55:01 +07:00

35 lines
1.3 KiB
TypeScript

/**
* One canonical form for itch.io game URLs — port of scripts/canonical.py.
* Both implementations are tested against tests/fixtures/url_vectors.json.
*
* Canonical form: https://{creator}.itch.io/{slug}
* - https only, host lower-cased, no port / userinfo / query / fragment
* - exactly one lower-cased path segment, no trailing slash
* - creator must be a *.itch.io subdomain (not itch.io / www.itch.io)
*/
const HOST = /^[a-z0-9][a-z0-9-]*\.itch\.io$/
const SLUG = /^[a-z0-9][a-z0-9_-]*$/
const RESERVED_HOSTS = new Set(['itch.io', 'www.itch.io'])
export function canonicalize(raw: unknown): string | null {
if (typeof raw !== 'string') return null
let text = raw.trim()
if (!text) return null
if (!text.includes('://')) text = `https://${text}`
let url: URL
try {
url = new URL(text)
} catch {
return null
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
if (url.username || url.password) return null
const host = url.hostname.toLowerCase()
if (RESERVED_HOSTS.has(host) || !HOST.test(host)) return null
const segments = url.pathname.split('/').filter(Boolean)
if (segments.length !== 1) return null
const slug = segments[0].toLowerCase()
if (!SLUG.test(slug)) return null
return `https://${host}/${slug}`
}