import { Hono } from 'hono' import { chainGet, dig, envBinding, recordGet } from '../absent.ts' import { createBudget } from './budget.ts' import { viewerCalls, type ViewerCall } from './call.ts' import { safeFilename } from './escape.ts' import { createViewerFetch, type FetchLike } from './fetchguard.ts' import { describeFailure } from './failure.ts' import { hintFromError } from './hint.ts' import { htmlResponse, SECURITY, statusOf, textResponse } from './http.ts' import { VIEWER_CAPS, rateLimitFor, type RateBucket } from './limits.ts' import type { RateDecision, TraceModel, ViewerEnv } from './model.ts' import { isPlyCode } from 'ply' import { EDGE_ATTR, cacheStatsOf, plyTracer, routeStamp, spanList } from '../edge-trace.ts' import { admitBatched, firstCheckInIsolate, inSpan, rateIdentity, stampCold, versionOf } from '../rate/admit.ts' import { describeCounter } from '../rate/stub.ts' import { createLoopbackFetch, isLoopback, type LoopbackBinding } from '../gitcache.ts' import { applyUpstreamHeaders, createRayLog, observeUpstream, stampUpstreamSpans } from '../rays.ts' import { parseViewerRequest, routeOptsFromEnv, type ViewerRoute } from './route.ts' import { renderError, renderHome, renderLoaded, renderNamespace } from './render.tsx' import { VIEWER_CSS, VIEWER_CSS_HASH } from './styles.ts' import { loadRepo } from './session.ts' import { buildTrace, traceHeader } from './traceview.ts' import { LAYOUT } from './layout.ts' import { PUBLIC_NAMESPACE, listPublicRepos, publicRepoExistsMemo, publicRepoShortName, tokenSourceFor, withPublicReadCredential } from '../public.ts' function callOf(req: Request): ViewerCall { const call = viewerCalls.get(req) if (!call) throw new Error('viewer call missing') return call } /** * The viewer's per-client rate limit. /v1 public-namespace reads use it too (index.js). The limiter is the env's * RATE_MODE (docs/rate-limit.md): `memo` (the default: the isolate's copy of the blocked document, refreshed in * waitUntil, + batched counter), `batched` (the cached document read per request + batched counter) or `legacy` * (ViewerRateLimit, one Durable Object call per request). Either way one `ply.rate.check` span records mode, decision and latency. */ export async function admitClient(req: Request, env: ViewerEnv, bucket: RateBucket, now: number, ctx: unknown = null): Promise { const id = await rateIdentity(req, env, ctx) const limitFor = (b: RateBucket): number => rateLimitFor(b, envBinding(b === 'pack' ? env.VIEWER_PACK_RATE_PER_MIN : env.VIEWER_RATE_PER_MIN)) return inSpan(ctx, 'ply.rate.check', { [EDGE_ATTR.version]: versionOf(env), [EDGE_ATTR.route]: 'rate.check' }, async set => { set('rate.mode', id.mode) set('rate.mode_source', id.modeSource) set('rate.bucket', bucket) if (firstCheckInIsolate()) stampCold(set, 'isolate_start') if (id.load) { set('rate.load', true) set('rate.counter', describeCounter(ctx)) } const t0 = performance.now() try { if (id.mode === 'batched' || id.mode === 'memo') return await admitBatched({ ctx, env, id, bucket, now, limitFor, set }) const ns = env.VIEWER_RATE if (!ns) { set('rate.decision', 'unconfigured') return 'unconfigured' } try { const stub = ns.get(ns.idFromName(id.hex)) const decision = await stub.admit(bucket, now, limitFor(bucket), VIEWER_CAPS.rateWindowMs) set('rate.decision', decision.ok ? 'allow' : 'deny') return decision } catch { set('rate.decision', 'unavailable') return 'unavailable' } } finally { set('rate.check_ms', Math.round((performance.now() - t0) * 1000) / 1000) } }) } function isText(bytes: Uint8Array): boolean { for (let i = 0; i < bytes.length; i++) if (bytes[i] === 0) return false try { new TextDecoder('utf-8', { fatal: true }).decode(bytes); return true } catch { return false } } function headless(req: Request, res: Response): Response { if (req.method !== 'HEAD') return res return new Response(null, { status: res.status, headers: res.headers }) } function traceOf(call: ViewerCall, bars: TraceModel['bars'] = []): TraceModel { return { bars, totalMs: 0, cfRay: call.cfRay, requestId: call.requestId } } function failureCode(err: unknown, fallback: string): string { const found = chainGet(err, 'code') return found.tag === 'some' && isPlyCode(found.value) ? String(found.value) : fallback } function failureMessage(err: unknown, code: string): string { if (code === 'internal') return 'The viewer hit an internal error.' const found = chainGet(err, 'message') const text = found.tag === 'some' ? String(found.value) : String(err) return text.slice(0, 500) } function notify(opts: unknown, extra: Record): void { const found = chainGet(opts, 'note') if (found.tag === 'none') return const note = found.value as (fields: Record) => void note(extra) } function routeError(call: ViewerCall, err: unknown): Response { const code = failureCode(err, 'bad_request') const message = failureMessage(err, code) const status = statusOf({ code }) notify(call.opts, { code, status }) call.fields['http.status_code'] = status call.fields['error.type'] = code const trace = traceOf(call) return htmlResponse(status, renderError(status, code, message, trace), trace, call.requestId) } async function rateGate(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract | { kind: 'home' } | { kind: 'redirect' } | { kind: 'namespace' }): Promise { const skipRate = chainGet(call.opts, 'skipRate') if (skipRate.tag === 'some' && skipRate.value) return null const bucket: RateBucket = route.kind === 'repo' && route.pack ? 'pack' : 'page' const decision = await admitClient(req, env, bucket, call.now, call.ctx) if (decision === 'unconfigured' || decision === 'unavailable') { const trace = traceOf(call) const message = decision === 'unconfigured' ? 'The viewer rate limiter is not configured.' : 'The viewer rate limiter is unavailable.' notify(call.opts, { code: 'rate_unavailable', status: 503 }) call.fields['http.status_code'] = 503 return htmlResponse(503, renderError(503, 'unavailable', message, trace), trace, call.requestId) } if (!decision.ok) { const retry = String(Math.max(1, Math.ceil(decision.retryAfterMs / LAYOUT.msPerSecond))) const trace = traceOf(call) notify(call.opts, { code: 'rate_limited', status: 429 }) call.fields['http.status_code'] = 429 return htmlResponse(429, renderError(429, 'rate_limited', `Too many viewer requests. Retry after ${retry}s.`, trace), trace, call.requestId, { 'retry-after': retry }) } return null } // The hashed URL that matches these bytes is immutable. /view.css (HTML from before hashed hrefs) and a stale hash (HTML // rendered by the previous deploy) get the current sheet with an ETag and must revalidate, so they heal on the next load. function cssResponse(call: ViewerCall, req: Request): Response { call.fields['http.status_code'] = 200 call.fields['ply.viewer.route'] = 'css' const route = call.route const exact = route !== null && route.kind === 'css' && route.hash === VIEWER_CSS_HASH const etag = `"${VIEWER_CSS_HASH}"` const headers = { 'content-type': 'text/css; charset=utf-8', 'cache-control': exact ? 'public, max-age=31536000, immutable' : 'no-cache', etag, ...SECURITY, 'x-ply-request-id': call.requestId, } if (req.headers.get('if-none-match') === etag) { call.fields['http.status_code'] = 304 return new Response(null, { status: 304, headers }) } return new Response(req.method === 'HEAD' ? null : VIEWER_CSS, { status: 200, headers }) } async function serveRepo(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract): Promise { const limited = await rateGate(req, env, call, route) if (limited) return headless(req, limited) const collected = spanList() const versionId = dig(env, ['VERSION', 'id']) const tracer = plyTracer(call.ctx, { onEnd: collected.onEnd, maxEvents: 48, resource: { 'service.name': 'ply-viewer' } }, routeStamp('viewer', versionId.tag === 'some' && typeof versionId.value === 'string' ? versionId.value : null)) const budget = createBudget() const pageSignal = AbortSignal.timeout(VIEWER_CAPS.pageTimeoutMs) // The public namespace's read token is attached below the guard: every hop is validated by createViewerFetch first, // then checked again by withPublicReadCredential, and the pack cache above never sees it in its key. // Public, credential-free exchanges go to the cache-enabled GitCache entrypoint (Workers Caching) under the guard. const fetchField = chainGet(call.opts, 'fetchImpl') const direct = withPublicReadCredential(fetchField.tag === 'some' ? fetchField.value as FetchLike : globalThis.fetch, tokenSourceFor(env.PUBLIC_REPOS)) const optsRecord = call.opts !== null && typeof call.opts === 'object' ? call.opts as Record : null const provided = recordGet(optsRecord, 'loopback') let loopback: (() => LoopbackBinding | null) | null if (provided.tag === 'none') { loopback = env.PACK_CACHE === 'off' ? null : () => { const found = dig(call.ctx, ['exports', 'GitCache']) return found.tag === 'some' ? found.value as LoopbackBinding : null } } else if (provided.value === null) { loopback = null } else { const chosen = provided.value as () => LoopbackBinding | null loopback = () => { const binding = chosen() return isLoopback(binding) ? binding : null } } const rays = createRayLog() const fetchImpl: FetchLike = createViewerFetch(observeUpstream(createLoopbackFetch(direct, loopback, { watchedRefs: env.REFS_POLICY === 'watched' }), rays), route.loc.url, route.loc.allowedHosts, budget, pageSignal) let stamped = false const traced = () => { const report = rays.report() if (!stamped) { stampUpstreamSpans(collected.spans, report); stamped = true } return { ...buildTrace(collected.spans, call.cfRay, call.requestId), expanded: new URL(req.url).searchParams.get('trace') === '1' } } const attach = (res: Response): Response => { applyUpstreamHeaders(res.headers, rays.report(), Date.now() - call.now) // Stamp pack-cache child outcomes onto the response so index.js can put them on the request span. const stats = cacheStatsOf(collected.spans) res.headers.set('x-ply-cache-hits', String(stats.cache_hits)) res.headers.set('x-ply-cache-misses', String(stats.cache_misses)) res.headers.set('x-ply-spans', String(stats.spans)) res.headers.set('x-ply-rtt', String(stats.rtt)) return res } try { // The namespace's yes/no runs beside the git reads instead of before them (it was 37 ms of a 105 ms page, trace // 7dc622ba…). A missing repo still answers this 404: its git reads fail at the read-token mint before any upstream // request, and are cancelled here. const short = publicRepoShortName(route.loc.host, route.loc.repoPath) const stop = new AbortController() const pending = loadRepo(route, { tracer, fetchImpl, signal: AbortSignal.any([pageSignal, stop.signal]), budget }) if (short) { pending.catch(() => {}) const waitUntil = dig(call.ctx, ['waitUntil']) const defer = waitUntil.tag === 'some' && typeof waitUntil.value === 'function' ? (p: Promise) => { (waitUntil.value as (p: Promise) => void).call(call.ctx, p) } : null const exists = await publicRepoExistsMemo(env.PUBLIC_REPOS, short, call.now, defer) if (exists === false) { stop.abort() throw Object.assign(new Error(`no repository ${short} in ${PUBLIC_NAMESPACE}`), { code: 'not_found' }) } } const loaded = await pending const trace = traced() notify(call.opts, { host: route.loc.host, rtt: trace.bars.length, bytes: budget.bytes, code: 'ok' }) if (loaded.kind === 'raw') { const text = isText(loaded.bytes) const headers = new Headers({ ...SECURITY, 'content-type': text ? 'text/plain; charset=utf-8' : 'application/octet-stream', 'content-disposition': `${text ? 'inline' : 'attachment'}; filename="${safeFilename(loaded.filename)}"`, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', 'x-ply-request-id': call.requestId, }) const summary = traceHeader(trace) if (summary) headers.set('x-ply-trace', summary) const copy = new ArrayBuffer(loaded.bytes.byteLength) new Uint8Array(copy).set(loaded.bytes) call.fields['http.status_code'] = 200 call.fields['ply.viewer.route'] = 'raw' call.fields['server.address'] = route.loc.host return attach(new Response(req.method === 'HEAD' ? null : new Blob([copy]), { status: 200, headers })) } call.fields['http.status_code'] = 200 call.fields['ply.viewer.route'] = loaded.kind call.fields['server.address'] = route.loc.host call.fields['ply.viewer.ref'] = loaded.header.ref call.fields['ply.viewer.upstream_spans'] = trace.bars.length return headless(req, attach(htmlResponse(200, renderLoaded(loaded, trace, call.now), trace, call.requestId))) } catch (err) { const code = failureCode(err, 'internal') const upstream = code === 'internal' ? 500 : statusOf(err) const message = failureMessage(err, code) const view = describeFailure({ code, status: upstream, message, host: route.loc.host, repoPath: route.loc.repoPath, ref: route.ref, path: route.path }) const trace = traced() notify(call.opts, { host: route.loc.host, code, status: view.status, rtt: trace.bars.length }) call.fields['http.status_code'] = view.status call.fields['error.type'] = code call.fields['server.address'] = route.loc.host return headless(req, attach(htmlResponse(view.status, renderError(upstream, code, message, trace, route.loc.host, route.loc.repoPath, route.ref, call.now, route.path, hintFromError(err)), trace, call.requestId))) } } export const viewerApp = new Hono<{ Bindings: ViewerEnv }>() viewerApp.use('*', async (c, next) => { const call = callOf(c.req.raw) if (c.req.method !== 'GET' && c.req.method !== 'HEAD') { call.fields['http.status_code'] = 405 return textResponse(405, 'method not allowed', { allow: 'GET, HEAD' }) } try { call.route = parseViewerRequest(c.req.url, routeOptsFromEnv(c.env)) } catch (err) { return headless(c.req.raw, routeError(call, err)) } await next() }) function serveCss(c: { req: { raw: Request } }): Response { const call = callOf(c.req.raw) if (call.route === null || call.route.kind !== 'css') return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' })) return cssResponse(call, c.req.raw) } viewerApp.on(['GET', 'HEAD'], '/view.css', c => serveCss(c)) viewerApp.on(['GET', 'HEAD'], '/:sheet{view\\.[0-9a-f]{12}\\.css}', c => serveCss(c)) viewerApp.on(['GET', 'HEAD'], '/view', c => serveEntry(c.req.raw, c.env)) viewerApp.on(['GET', 'HEAD'], '/view/', c => serveEntry(c.req.raw, c.env)) viewerApp.on(['GET', 'HEAD'], '/view/*', c => dispatchViewer(c)) viewerApp.on(['GET', 'HEAD'], '/r', c => dispatchViewer(c)) viewerApp.on(['GET', 'HEAD'], '/r/', c => dispatchViewer(c)) viewerApp.on(['GET', 'HEAD'], '/r/*', c => dispatchViewer(c)) function dispatchViewer(c: { req: { raw: Request }; env: ViewerEnv }): Promise | Response { const call = callOf(c.req.raw) const route = call.route if (!route) return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' })) if (route.kind === 'namespace') return serveNamespace(c.req.raw, c.env) if (route.kind === 'redirect') return serveRedirect(c.req.raw, c.env, call, route) if (route.kind !== 'repo') return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' })) return serveRepo(c.req.raw, c.env, call, route) } async function serveRedirect(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract): Promise { const limited = await rateGate(req, env, call, route) if (limited) return headless(req, limited) call.fields['http.status_code'] = 302 return new Response(null, { status: 302, headers: { location: route.location, 'cache-control': 'no-store', ...SECURITY, 'x-ply-request-id': call.requestId } }) } async function serveNamespace(req: Request, env: ViewerEnv): Promise { const call = callOf(req) const limited = await rateGate(req, env, call, { kind: 'namespace' }) if (limited) return headless(req, limited) const trace = traceOf(call) call.fields['http.status_code'] = 200 call.fields['ply.viewer.route'] = 'namespace' const publicRepos = await listPublicRepos(env.PUBLIC_REPOS, call.now) return headless(req, htmlResponse(200, renderNamespace(publicRepos, call.now), trace, call.requestId)) } viewerApp.notFound(c => { const call = viewerCalls.get(c.req.raw) if (!call) return textResponse(404, 'not found') call.fields['http.status_code'] = 404 const trace = traceOf(call) return headless(c.req.raw, htmlResponse(404, renderError(404, 'not_found', 'not a viewer route', trace), trace, call.requestId)) }) async function serveEntry(req: Request, env: ViewerEnv): Promise { const call = callOf(req) const route = call.route if (!route || (route.kind !== 'home' && route.kind !== 'redirect')) { return headless(req, routeError(call, { code: 'bad_request', message: 'not a viewer route' })) } const limited = await rateGate(req, env, call, route) if (limited) return headless(req, limited) if (route.kind === 'redirect') { call.fields['http.status_code'] = 302 return new Response(null, { status: 302, headers: { location: route.location, 'cache-control': 'no-store', ...SECURITY, 'x-ply-request-id': call.requestId } }) } const trace = traceOf(call) call.fields['http.status_code'] = 200 call.fields['ply.viewer.route'] = 'home' const publicRepos = await listPublicRepos(env.PUBLIC_REPOS, call.now) return headless(req, htmlResponse(200, renderHome(null, call.now, publicRepos), trace, call.requestId)) }