- Catalog packs: the build emits /data/pack/manifest-v1.json plus content-hashed deflate-raw packs (columns + dictionaries); the site and the apps read them (about 264 KB on a first visit instead of about 520 KB of JSON). Descriptions live in their own packs and load on demand (a game page: its chunk; a search: all of them). - The IndexedDB cache is stored as compressed bytes by a small binary persister (replaces @tanstack/query-async-storage-persister); fflate is the lazy fallback where the browser has no Compression Streams. - The readable /data/*.json goes only to apps 4.0-4.1.1 (Tauri Origin); everyone else gets a 410 with links to the repository (worker/legacy-data.ts, five positive run_worker_first entries). - /img: exact width strings only, fallback originals and missing images kept at the edge for a day, failed background puts are logged. - Themed scrollbars and a "Hide scrollbars" setting (Settings and the display menu); chart cards no longer block their list scrollbars. - Policies (LEGAL_VERSION 2026-10-03): data reuse goes through the repository. Docs, changelog and version 4.2.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
270 lines
11 KiB
TypeScript
270 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { resetDataCache } from './data'
|
|
import { handleImg, parseImgPath, r2Key, type ImgDeps } from './img'
|
|
|
|
const ORIGIN = 'https://freeitchgames.win'
|
|
const PATH = 'aW1nLzI0OTQ3OTQzLnBuZw==/original/3wlrR0.png'
|
|
const UPSTREAM = `https://img.itch.zone/${PATH}`
|
|
|
|
function makeDeps(overrides: Partial<ImgDeps> = {}) {
|
|
const store = new Map<string, ArrayBuffer>()
|
|
const waits: Promise<unknown>[] = []
|
|
const deps: ImgDeps = {
|
|
assets: {
|
|
fetch: vi.fn(async () => Response.json({ 'https://dev.itch.io/game': UPSTREAM })),
|
|
} as unknown as ImgDeps['assets'],
|
|
bucket: {
|
|
get: vi.fn(async (key: string) =>
|
|
store.has(key) ? ({ body: store.get(key) } as unknown as R2ObjectBody) : null,
|
|
),
|
|
put: vi.fn(async (key: string, value: ArrayBuffer) => {
|
|
store.set(key, value)
|
|
return null
|
|
}),
|
|
} as unknown as ImgDeps['bucket'],
|
|
fetch: vi.fn(async () => new Response(new Uint8Array([1, 2, 3]), { headers: { 'content-type': 'image/webp' } })),
|
|
waitUntil: (p) => void waits.push(p),
|
|
...overrides,
|
|
}
|
|
return { deps, store, settle: () => Promise.all(waits) }
|
|
}
|
|
|
|
/** An edge cache that keeps what it is given (status, headers, body). */
|
|
function memoryCache() {
|
|
const entries = new Map<string, { status: number; headers: [string, string][]; body: ArrayBuffer }>()
|
|
const cache = {
|
|
match: vi.fn(async (req: Request) => {
|
|
const hit = entries.get(req.url)
|
|
if (!hit) return undefined
|
|
return new Response(hit.body.byteLength ? hit.body : null, { status: hit.status, headers: hit.headers })
|
|
}),
|
|
put: vi.fn(async (req: Request, res: Response) => {
|
|
entries.set(req.url, { status: res.status, headers: [...res.headers], body: await res.arrayBuffer() })
|
|
}),
|
|
}
|
|
return cache as typeof cache & ImgDeps['cache']
|
|
}
|
|
|
|
function get(path: string, headers: Record<string, string> = {}) {
|
|
const url = new URL(path, ORIGIN)
|
|
return { request: new Request(url, { headers }), url }
|
|
}
|
|
|
|
beforeEach(() => resetDataCache())
|
|
|
|
describe('parseImgPath', () => {
|
|
it('accepts catalog-shaped paths and keeps encoding', () => {
|
|
expect(parseImgPath(`/img/160/${PATH}`)).toEqual({ width: 160, upstream: UPSTREAM })
|
|
expect(parseImgPath('/img/640/abc%2Fd==/347x500/x%2By.gif')?.upstream).toBe(
|
|
'https://img.itch.zone/abc%2Fd==/347x500/x%2By.gif',
|
|
)
|
|
})
|
|
|
|
it.each([
|
|
'/img/100/a/original/b.png', // width not offered
|
|
'/img/160/a', // too few segments
|
|
'/img/160/../original/b.png',
|
|
'/img/160/a/original/b.png?x',
|
|
'/img/160/a/orig inal/b.png',
|
|
'/img/160/a/b/c/d/e.png', // too many segments
|
|
// Other spellings of an offered width would be separate edge-cache entries.
|
|
'/img/0160/a/original/b.png',
|
|
'/img/160.0/a/original/b.png',
|
|
'/img/0xa0/a/original/b.png',
|
|
'/img/1.6e2/a/original/b.png',
|
|
'/img/+160/a/original/b.png',
|
|
'/img/constructor/a/original/b.png',
|
|
])('rejects %s', (path) => {
|
|
expect(parseImgPath(path)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('r2Key', () => {
|
|
it('is deterministic per width + upstream', async () => {
|
|
const a = await r2Key(160, UPSTREAM)
|
|
expect(a).toMatch(/^w160\/[0-9a-f]{64}\.webp$/)
|
|
expect(await r2Key(160, UPSTREAM)).toBe(a)
|
|
expect(await r2Key(640, UPSTREAM)).not.toBe(a)
|
|
})
|
|
})
|
|
|
|
describe('handleImg', () => {
|
|
it('rejects bad methods, cross-site embeds and bad paths', async () => {
|
|
const { deps } = makeDeps()
|
|
const post = new Request(`${ORIGIN}/img/160/${PATH}`, { method: 'POST' })
|
|
expect((await handleImg(post, new URL(post.url), deps)).status).toBe(405)
|
|
const cross = get(`/img/160/${PATH}`, { 'sec-fetch-site': 'cross-site' })
|
|
expect((await handleImg(cross.request, cross.url, deps)).status).toBe(403)
|
|
const bad = get('/img/999/a/b/c.png')
|
|
expect((await handleImg(bad.request, bad.url, deps)).status).toBe(400)
|
|
})
|
|
|
|
it('only serves images that are in the catalog', async () => {
|
|
const { deps } = makeDeps()
|
|
const other = get('/img/160/other/original/x.png')
|
|
expect((await handleImg(other.request, other.url, deps)).status).toBe(404)
|
|
expect(deps.fetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('transforms once, stores in R2, then serves from R2', async () => {
|
|
const { deps, store, settle } = makeDeps()
|
|
const first = get(`/img/160/${PATH}`)
|
|
const res = await handleImg(first.request, first.url, deps)
|
|
await settle()
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('image/webp')
|
|
expect(res.headers.get('cache-control')).toContain('immutable')
|
|
expect(store.has(await r2Key(160, UPSTREAM))).toBe(true)
|
|
const [, init] = vi.mocked(deps.fetch).mock.calls[0]
|
|
expect((init as { cf: { image: { width: number } } }).cf.image.width).toBe(160)
|
|
|
|
const again = await handleImg(first.request, first.url, deps)
|
|
expect(again.status).toBe(200)
|
|
expect(deps.fetch).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('falls back to the original when the transform fails, without storing it', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response('quota', { status: 403 }))
|
|
.mockResolvedValueOnce(new Response('gif', { headers: { 'content-type': 'image/gif' } }))
|
|
const { deps, store } = makeDeps({ fetch: fetchMock as unknown as typeof fetch })
|
|
const req = get(`/img/640/${PATH}`)
|
|
const res = await handleImg(req.request, req.url, deps)
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('image/gif')
|
|
expect(res.headers.get('cache-control')).toBe('public, max-age=86400')
|
|
expect(store.size).toBe(0)
|
|
})
|
|
|
|
it('maps a 403 from itch.zone (broken original) to a cacheable 404', async () => {
|
|
const forbidden = vi.fn().mockResolvedValue(new Response(null, { status: 403 }))
|
|
const { deps } = makeDeps({ fetch: forbidden as unknown as typeof fetch })
|
|
const req = get(`/img/160/${PATH}`)
|
|
const res = await handleImg(req.request, req.url, deps)
|
|
expect(res.status).toBe(404)
|
|
expect(res.headers.get('cache-control')).toBe('public, max-age=86400')
|
|
})
|
|
|
|
it('passes through upstream 404s and refuses non-images', async () => {
|
|
const notFound = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response(null, { status: 404 }))
|
|
.mockResolvedValueOnce(new Response(null, { status: 404 }))
|
|
const a = makeDeps({ fetch: notFound as unknown as typeof fetch })
|
|
const req = get(`/img/160/${PATH}`)
|
|
expect((await handleImg(req.request, req.url, a.deps)).status).toBe(404)
|
|
|
|
const html = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
|
.mockResolvedValueOnce(new Response('<html>', { headers: { 'content-type': 'text/html' } }))
|
|
const b = makeDeps({ fetch: html as unknown as typeof fetch })
|
|
expect((await handleImg(req.request, req.url, b.deps)).status).toBe(502)
|
|
})
|
|
|
|
it('serves later requests from the edge cache', async () => {
|
|
const cache = memoryCache()
|
|
const { deps, settle } = makeDeps({ cache })
|
|
const req = get(`/img/160/${PATH}`)
|
|
await handleImg(req.request, req.url, deps)
|
|
await settle()
|
|
const again = await handleImg(req.request, req.url, deps)
|
|
expect(again.status).toBe(200)
|
|
expect(again.headers.get('cache-control')).toContain('immutable')
|
|
expect(deps.bucket.get).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('keeps a fallback original at the edge for a day instead of redoing the chain', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response('quota', { status: 403 }))
|
|
.mockResolvedValueOnce(new Response('gif', { headers: { 'content-type': 'image/gif' } }))
|
|
const cache = memoryCache()
|
|
const { deps, settle } = makeDeps({ fetch: fetchMock as unknown as typeof fetch, cache })
|
|
const req = get(`/img/640/${PATH}`)
|
|
expect(await (await handleImg(req.request, req.url, deps)).text()).toBe('gif')
|
|
await settle()
|
|
const again = await handleImg(req.request, req.url, deps)
|
|
expect(await again.text()).toBe('gif')
|
|
expect(again.headers.get('cache-control')).toBe('public, max-age=86400')
|
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
expect(deps.bucket.get).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('remembers a missing upstream image for a day', async () => {
|
|
const missing = vi.fn().mockResolvedValue(new Response(null, { status: 404 }))
|
|
const cache = memoryCache()
|
|
const { deps, settle } = makeDeps({ fetch: missing as unknown as typeof fetch, cache })
|
|
const req = get(`/img/160/${PATH}`)
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(404)
|
|
await settle()
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(404)
|
|
expect(missing).toHaveBeenCalledTimes(2) // the transform and the original, once
|
|
})
|
|
|
|
it('never keeps a bodiless HEAD answer under the cache key', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response('quota', { status: 403 }))
|
|
.mockResolvedValueOnce(new Response('gif', { headers: { 'content-type': 'image/gif' } }))
|
|
const cache = memoryCache()
|
|
const { deps, settle } = makeDeps({ fetch: fetchMock as unknown as typeof fetch, cache })
|
|
const url = new URL(`/img/640/${PATH}`, ORIGIN)
|
|
const res = await handleImg(new Request(url, { method: 'HEAD' }), url, deps)
|
|
expect(res.status).toBe(200)
|
|
await settle()
|
|
expect(cache.put).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('logs failed background writes instead of failing', async () => {
|
|
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
const cache = {
|
|
match: vi.fn(async () => undefined),
|
|
put: vi.fn(async () => {
|
|
throw new Error('cache full')
|
|
}),
|
|
} as unknown as ImgDeps['cache']
|
|
const { deps, settle } = makeDeps({ cache })
|
|
deps.bucket.put = vi.fn(async () => {
|
|
throw new Error('R2 down')
|
|
}) as unknown as ImgDeps['bucket']['put']
|
|
const req = get(`/img/160/${PATH}`)
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(200)
|
|
await settle()
|
|
expect(warn).toHaveBeenCalledTimes(2)
|
|
warn.mockRestore()
|
|
})
|
|
|
|
it('answers upstream network errors with a 502 instead of throwing', async () => {
|
|
const reset = vi.fn().mockRejectedValue(new TypeError('Network connection lost.'))
|
|
const { deps } = makeDeps({ fetch: reset as unknown as typeof fetch })
|
|
const req = get(`/img/160/${PATH}`)
|
|
const res = await handleImg(req.request, req.url, deps)
|
|
expect(res.status).toBe(502)
|
|
expect(res.headers.get('cache-control')).toBe('no-store')
|
|
})
|
|
|
|
it('does not let a stalled first request block the others', async () => {
|
|
const assetsFetch = vi
|
|
.fn()
|
|
.mockReturnValueOnce(new Promise(() => {})) // e.g. its request was cancelled
|
|
.mockResolvedValue(Response.json({ u: UPSTREAM }))
|
|
const { deps } = makeDeps({ assets: { fetch: assetsFetch } as unknown as ImgDeps['assets'] })
|
|
const req = get(`/img/160/${PATH}`)
|
|
void handleImg(req.request, req.url, deps)
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(200)
|
|
})
|
|
|
|
it('returns 503 when the allow-list is unavailable and retries later', async () => {
|
|
const assetsFetch = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
|
.mockResolvedValueOnce(Response.json({ u: UPSTREAM }))
|
|
const { deps } = makeDeps({ assets: { fetch: assetsFetch } as unknown as ImgDeps['assets'] })
|
|
const req = get(`/img/160/${PATH}`)
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(503)
|
|
expect((await handleImg(req.request, req.url, deps)).status).toBe(200)
|
|
})
|
|
})
|