The first SonarQube Cloud scan reported 57 issues. 45 are fixed in code, the 11 on the saved itch.io test pages leave the scan via .sonarcloud.properties, and css:S8776 (Tailwind's @utility nesting) is a false positive to mark on SonarQube Cloud. Worker: trailing slashes and the gate cookie's padding are trimmed by trimEnd() instead of /\/+$/ (quadratic on the public 404 path); the feed and collection parsers lose their backtracking regexes; compareCodeUnits keeps the code-unit sorts (the rule's localeCompare is locale-dependent); Number.*, DEFAULT_SIZES, crypto.randomUUID; i18n-edit.mjs reads stdin. Pipeline: the NSFW notices are one selector list, either class (the rule's ".a.b" would need both), each tested; plain if-guards; update_info.main returns None; no plain-http adapter; [[ ]] in cf_build_status.sh. Supply chain: requirements*.txt are hash-locked pip-tools compiles of requirements*.in (on Python 3.14, which Dependabot reads from the header), installed with --require-hashes --only-binary :all: in block scalars; npm ci --ignore-scripts in CI and the release builds (verified by manual release runs on all platforms); npm run cf:check. Release 4.4.2: also ships the toolchain upgrade (#149, #154) and Dependabot #141-#148, #150, #152, #153. CLAUDE.md gotchas 40 and 41. [skip-discuss] Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
4.0 KiB
TypeScript
89 lines
4.0 KiB
TypeScript
/**
|
|
* Requests that match no static asset (wrangler.jsonc `not_found_handling:
|
|
* "none"` sends them here).
|
|
*
|
|
* - Anything that looks like a file (under /assets/ or /data/, or with an
|
|
* extension) is a real 404 — never the app shell, which the browser would
|
|
* otherwise cache under a hashed chunk URL or try to parse as JSON. A
|
|
* missing pack (/data/pack/<hash>.bin from an older deploy) lands here: the
|
|
* client then reloads the manifest. (The readable /data JSON never does: the
|
|
* Worker answers it first, legacy-data.ts.)
|
|
* - App routes get the shell (index.html) with 200; unknown paths and game
|
|
* slugs that are not in the catalog get the same shell with 404, so the SPA
|
|
* still renders its Not Found page but crawlers see a real 404.
|
|
* - With the verification gate on, a visitor who has not passed gets the
|
|
* verification page instead of the shell (gate.ts).
|
|
*/
|
|
import { catalogUrls, type DataSource } from './data'
|
|
import { json } from './http'
|
|
import { trimEnd } from './text'
|
|
|
|
// Keep in sync with the <Routes> in src/App.tsx.
|
|
const ROUTES = new Set(['/', '/games', '/charts', '/removed', '/settings', '/about', '/suggest'])
|
|
// Renamed routes: permanent redirects (the SPA would also redirect, but crawlers get a 301).
|
|
const MOVED: Record<string, string> = { '/deleted': '/removed' }
|
|
const GAME_ROUTE = /^\/games\/([a-z0-9-]+)$/
|
|
const ERROR_PREVIEW_ROUTE = /^\/errors\/[a-z0-9-]+$/
|
|
|
|
// Keep in sync with slugify() in src/lib/utils.ts.
|
|
function slugify(url: string): string {
|
|
return url
|
|
.replace(/^https?:\/\//, '')
|
|
.replace(/[^a-z0-9-]/gi, '-')
|
|
.replace(/-+/g, '-')
|
|
.replace(/^-|-$/g, '')
|
|
.toLowerCase()
|
|
}
|
|
|
|
const slugSets = new WeakMap<Map<string, string>, Set<string>>()
|
|
|
|
async function isGameSlug(slug: string, src: DataSource): Promise<boolean> {
|
|
let urls: Map<string, string>
|
|
try {
|
|
urls = await catalogUrls(src)
|
|
} catch {
|
|
return true // catalog unavailable: don't turn real pages into 404s
|
|
}
|
|
let slugs = slugSets.get(urls)
|
|
if (!slugs) {
|
|
slugs = new Set([...urls.keys()].map(slugify))
|
|
slugSets.set(urls, slugs)
|
|
}
|
|
return slugs.has(slug)
|
|
}
|
|
|
|
async function isAppRoute(pathname: string, src: DataSource): Promise<boolean> {
|
|
const path = pathname.length > 1 ? trimEnd(pathname, '/') : pathname
|
|
if (ROUTES.has(path) || ERROR_PREVIEW_ROUTE.test(path)) return true
|
|
const game = GAME_ROUTE.exec(path)
|
|
return game !== null && (await isGameSlug(game[1], src))
|
|
}
|
|
|
|
/** Answers an app page request that may not see the shell yet; null lets it through (gate.ts). */
|
|
export type PageGate = (request: Request) => Promise<Response | null>
|
|
|
|
export async function handleMiss(request: Request, url: URL, assets: Fetcher, gate?: PageGate): Promise<Response> {
|
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
return json({ error: 'method_not_allowed' }, 405, { Allow: 'GET, HEAD' })
|
|
}
|
|
const last = url.pathname.slice(url.pathname.lastIndexOf('/') + 1)
|
|
if (/^\/(assets|data)\//.test(url.pathname) || last.includes('.')) {
|
|
// CORS on /data misses too, so the desktop/Android apps see a 404, not a CORS error;
|
|
// no-store, so a pack name that comes back later (a rollback) isn't stuck as a 404.
|
|
const cors = url.pathname.startsWith('/data/') ? { 'Access-Control-Allow-Origin': '*' } : undefined
|
|
return json({ error: 'not_found' }, 404, cors)
|
|
}
|
|
const moved = MOVED[trimEnd(url.pathname, '/')]
|
|
if (moved) return Response.redirect(new URL(moved + url.search, url).toString(), 301)
|
|
// Before the shell is fetched, so a conditional request (If-None-Match)
|
|
// without a pass gets the verification page, never a 304 for a cached shell.
|
|
const blocked = await gate?.(request)
|
|
if (blocked) return blocked
|
|
// "/" rather than "/index.html": auto-trailing-slash would redirect the latter.
|
|
const shell = await assets.fetch(new Request(new URL('/', url), request))
|
|
if (shell.status !== 200 || (await isAppRoute(url.pathname, { assets, origin: url.origin }))) {
|
|
return shell
|
|
}
|
|
return new Response(shell.body, { status: 404, headers: shell.headers })
|
|
}
|