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 | 29x 29x 29x 29x 119x 119x 29x 29x 29x 29x 29x 29x 29x | import { API_DOCS } from '@chattra/shared'
import { Hono } from 'hono'
import { secureHeaders } from 'hono/secure-headers'
import { ipCeiling, requireActor, requireOrigin } from '../server/auth/middleware'
import { notFound, onError, requestId, type AppEnv } from '../server/http'
import { channelRoutes } from './channel-routes'
import { docsRoutes } from './docs-routes'
import { sessionRoutes } from './session-routes'
import { systemRoutes } from './system-routes'
export const app = new Hono<AppEnv>()
app.use('*', requestId)
// The Worker answers JSON only, so nothing may load or frame it. The SPA's own policy is in
// apps/web/public/_headers: Workers Assets serves those files without running this code.
const jsonOnlyHeaders = secureHeaders({
xFrameOptions: 'DENY',
contentSecurityPolicy: { defaultSrc: ["'none'"], frameAncestors: ["'none'"] },
})
// Not on /__/auth/*: those are Firebase's sign-in pages, which run scripts and load in an iframe
// (4.1), so they keep the headers Firebase sends and get none of ours. /api/docs is the one HTML
// page the Worker builds; docs-routes.ts gives it its own policy.
const hasOwnHeaders = (path: string): boolean =>
path.startsWith('/__/auth/') || path === API_DOCS.page
app.use('*', (c, next) => (hasOwnHeaders(c.req.path) ? next() : jsonOnlyHeaders(c, next)))
// Design 2.2 order, after the request id: Origin → rate limit → ID token, then each route's Zod.
app.use('/api/*', requireOrigin, ipCeiling, requireActor)
app.onError(onError)
app.notFound(notFound)
// One sub-app per aggregate, mounted here.
app.route('/', systemRoutes)
app.route('/', sessionRoutes)
app.route('/', channelRoutes)
app.route('/', docsRoutes)
|