Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 11x 172x 11x 11x 11x 11x 544x 544x 11x 99x 99x 99x 99x 99x 99x 11x 329x 329x 11x 11x 11x 11x | import { Hono } from 'hono';
import { claimAccount } from './accounts.ts';
import { createV0API } from './api/v0.ts';
import { createAuth, oidcAuth } from './auth.ts';
import { bmsSearchLookup, type ChartsetLookup } from './chartsets/refresh.ts';
import { openDatabase, type Database } from './db/client.ts';
import type { Env } from './env.ts';
import { serverTiming } from './http/server-timing.ts';
import { mountPages } from './pages/routes.ts';
import type { Viewer } from './pages/viewer.ts';
interface Variables {
db: Database;
userId: string | undefined;
/** The signed-in player on the pages (src/pages/viewer.ts); null when signed out. */
viewer: Viewer | null;
}
export type AppEnv = { Bindings: Env; Variables: Variables };
/** The Leaderboard API (docs/leaderboard.md section 8). */
export function createApp(options: { now?: () => number; newId?: () => string; chartsetLookup?: ChartsetLookup } = {}) {
const now = options.now ?? Date.now;
const newId = options.newId ?? (() => crypto.randomUUID());
const chartsetLookup = options.chartsetLookup ?? bmsSearchLookup();
const app = new Hono<AppEnv>();
// Every response says how long it took and how much of that the database did.
app.use('*', serverTiming<AppEnv>());
app.use('*', async (c, next) => {
Iif (c.env.AUTH_ISSUER && !c.req.path.startsWith('/api/')) {
const response = await oidcAuth(c.req.raw, c.env);
if (response) return response;
}
await next();
});
app.on(['GET', 'POST'], '/api/auth/*', async (c) => {
Iif (c.env.AUTH_ISSUER) return (await oidcAuth(c.req.raw, c.env)) ?? c.json({ error: 'not-found' }, 404);
Iif (!['localhost', '127.0.0.1', '[::1]'].includes(new URL(c.req.url).hostname))
return c.json({ error: 'oidc-not-configured' }, 503);
const auth = createAuth(c.env, new URL(c.req.url).origin);
const response = await auth.handler(c.req.raw);
// A registered passkey is a way back into the account, so it stops being a guest (docs/leaderboard.md section 3).
Iif (c.req.path === '/api/auth/passkey/verify-registration' && response.ok) {
const session = await auth.api.getSession({ headers: c.req.raw.headers });
if (session !== null) await claimAccount(openDatabase(c.env.DB), session.user.id);
}
return response;
});
app.use('/api/v0/*', async (c, next) => {
Iif (
c.env.AUTH_ISSUER &&
!['GET', 'HEAD', 'OPTIONS'].includes(c.req.method) &&
!c.req.header('authorization') &&
c.req.header('origin') !== c.env.PUBLIC_ORIGIN
)
return c.json({ error: 'invalid-origin' }, 403);
await next();
});
app.route('/api/v0', createV0API({ now, newId, chartsetLookup }));
// Anything else under `/api`, an unknown version included, answers in JSON like the API does.
app.all('/api/*', (c) => c.json({ error: 'not-found' }, 404));
mountPages(app);
return app;
}
export type AppType = ReturnType<typeof createApp>;
|