import { createServer } from 'node:http'; import { readdir, readFile } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import { RUNS, readRun, runFile } from './records.mjs'; import { publishedTournamentId, readPublishedTournament, readPublishedEffortStudy } from './published-tournament.mjs'; const assets = new Map([ ['/', ['index.html', 'text/html; charset=utf-8']], ['/styles.css', ['styles.css', 'text/css; charset=utf-8']], ['/app.js', ['app.js', 'text/javascript; charset=utf-8']], ]); const eligible = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}\.json$/; const port = Number(process.env.BENCH_PORT ?? 4321); let published; async function publishedTournament() { published ??= await readPublishedTournament(); return published; } function respond(res, status, type, body) { res.writeHead(status, { 'Content-Type': type, 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' }); res.end(body); } const server = createServer(async (req, res) => { const { pathname } = new URL(req.url, 'http://localhost'); try { if (req.method !== 'GET') return respond(res, 405, 'text/plain', 'Method not allowed'); if (pathname === '/api/effort-study') { const study = await readPublishedEffortStudy(); return study ? respond(res, 200, 'application/json', JSON.stringify(study)) : respond(res, 404, 'text/plain', 'Not found'); } if (pathname === '/api/runs') { const tournament = await publishedTournament(); if (tournament) { const { name: id, title, mode, official, createdAt, total } = tournament; return respond(res, 200, 'application/json', JSON.stringify([ { id, title, mode, official, createdAt, games: tournament.games.length, total }, ])); } const files = await readdir(RUNS).catch(error => { if (error.code === 'ENOENT') return []; throw error; }); const runs = []; for (const filename of files.filter(name => eligible.test(name))) { const id = filename.slice(0, -5); const run = await readRun(runFile(id), { includeAnalysis: false }); runs.push({ id, mode: run.mode, official: run.official, createdAt: run.createdAt, games: run.games.length, total: run.total }); } runs.sort((a, b) => Number(b.official) - Number(a.official) || Number(b.mode === 'demo') - Number(a.mode === 'demo') || b.createdAt.localeCompare(a.createdAt)); return respond(res, 200, 'application/json', JSON.stringify(runs)); } const match = /^\/api\/runs\/([a-zA-Z0-9][a-zA-Z0-9_-]{0,63})(\/download)?$/.exec(pathname); if (match) { const [, name, download] = match; const tournament = await publishedTournament(); const run = tournament ? (name === publishedTournamentId ? tournament : null) : await readRun(runFile(name)); if (!run) return respond(res, 404, 'text/plain', 'Not found'); if (download) res.setHeader('Content-Disposition', `attachment; filename="${name}.json"`); return respond(res, 200, 'application/json', JSON.stringify(run)); } const asset = assets.get(pathname); if (asset) { const [file, type] = asset; return respond(res, 200, type, await readFile(fileURLToPath(new URL(`../web/${file}`, import.meta.url)))); } respond(res, 404, 'text/plain', 'Not found'); } catch (error) { if (error.code === 'ENOENT') return respond(res, 404, 'text/plain', 'Not found'); console.error(error.message); respond(res, 500, 'text/plain; charset=utf-8', '赛事数据读取或复核失败'); } }); server.listen(port, '127.0.0.1', () => console.log(`五子棋排行榜 http://127.0.0.1:${port}`));