main
ply4 git calls33 ms46 KiB3 cachedprotocol v2
355 lines18 KiBRaw
1import { Hono } from 'hono'
2import { chainGet, dig, envBinding, recordGet } from '../absent.ts'
3import { createBudget } from './budget.ts'
4import { viewerCalls, type ViewerCall } from './call.ts'
5import { safeFilename } from './escape.ts'
6import { createViewerFetch, type FetchLike } from './fetchguard.ts'
7import { describeFailure } from './failure.ts'
8import { hintFromError } from './hint.ts'
9import { htmlResponse, SECURITY, statusOf, textResponse } from './http.ts'
10import { VIEWER_CAPS, rateLimitFor, type RateBucket } from './limits.ts'
11import type { RateDecision, TraceModel, ViewerEnv } from './model.ts'
12import { isPlyCode } from 'ply'
13import { EDGE_ATTR, cacheStatsOf, plyTracer, routeStamp, spanList } from '../edge-trace.ts'
14import { admitBatched, firstCheckInIsolate, inSpan, rateIdentity, stampCold, versionOf } from '../rate/admit.ts'
15import { describeCounter } from '../rate/stub.ts'
16import { createLoopbackFetch, isLoopback, type LoopbackBinding } from '../gitcache.ts'
17import { applyUpstreamHeaders, createRayLog, observeUpstream, stampUpstreamSpans } from '../rays.ts'
18import { parseViewerRequest, routeOptsFromEnv, type ViewerRoute } from './route.ts'
19import { renderError, renderHome, renderLoaded, renderNamespace } from './render.tsx'
20import { VIEWER_CSS, VIEWER_CSS_HASH } from './styles.ts'
21import { loadRepo } from './session.ts'
22import { buildTrace, traceHeader } from './traceview.ts'
23import { LAYOUT } from './layout.ts'
24import { PUBLIC_NAMESPACE, listPublicRepos, publicRepoExistsMemo, publicRepoShortName, tokenSourceFor, withPublicReadCredential } from '../public.ts'
26function callOf(req: Request): ViewerCall {
27 const call = viewerCalls.get(req)
28 if (!call) throw new Error('viewer call missing')
29 return call
32/**
33 * The viewer's per-client rate limit. /v1 public-namespace reads use it too (index.js). The limiter is the env's
34 * RATE_MODE (docs/rate-limit.md): `memo` (the default: the isolate's copy of the blocked document, refreshed in
35 * waitUntil, + batched counter), `batched` (the cached document read per request + batched counter) or `legacy`
36 * (ViewerRateLimit, one Durable Object call per request). Either way one `ply.rate.check` span records mode, decision and latency.
37 */
38export async function admitClient(req: Request, env: ViewerEnv, bucket: RateBucket, now: number, ctx: unknown = null): Promise<RateDecision | 'unconfigured' | 'unavailable'> {
39 const id = await rateIdentity(req, env, ctx)
40 const limitFor = (b: RateBucket): number => rateLimitFor(b, envBinding(b === 'pack' ? env.VIEWER_PACK_RATE_PER_MIN : env.VIEWER_RATE_PER_MIN))
41 return inSpan(ctx, 'ply.rate.check', { [EDGE_ATTR.version]: versionOf(env), [EDGE_ATTR.route]: 'rate.check' }, async set => {
42 set('rate.mode', id.mode)
43 set('rate.mode_source', id.modeSource)
44 set('rate.bucket', bucket)
45 if (firstCheckInIsolate()) stampCold(set, 'isolate_start')
46 if (id.load) {
47 set('rate.load', true)
48 set('rate.counter', describeCounter(ctx))
49 }
50 const t0 = performance.now()
51 try {
52 if (id.mode === 'batched' || id.mode === 'memo') return await admitBatched({ ctx, env, id, bucket, now, limitFor, set })
53 const ns = env.VIEWER_RATE
54 if (!ns) {
55 set('rate.decision', 'unconfigured')
56 return 'unconfigured'
57 }
58 try {
59 const stub = ns.get(ns.idFromName(id.hex))
60 const decision = await stub.admit(bucket, now, limitFor(bucket), VIEWER_CAPS.rateWindowMs)
61 set('rate.decision', decision.ok ? 'allow' : 'deny')
62 return decision
63 } catch {
64 set('rate.decision', 'unavailable')
65 return 'unavailable'
66 }
67 } finally {
68 set('rate.check_ms', Math.round((performance.now() - t0) * 1000) / 1000)
69 }
70 })
73function isText(bytes: Uint8Array): boolean {
74 for (let i = 0; i < bytes.length; i++) if (bytes[i] === 0) return false
75 try { new TextDecoder('utf-8', { fatal: true }).decode(bytes); return true }
76 catch { return false }
79function headless(req: Request, res: Response): Response {
80 if (req.method !== 'HEAD') return res
81 return new Response(null, { status: res.status, headers: res.headers })
84function traceOf(call: ViewerCall, bars: TraceModel['bars'] = []): TraceModel {
85 return { bars, totalMs: 0, cfRay: call.cfRay, requestId: call.requestId }
88function failureCode(err: unknown, fallback: string): string {
89 const found = chainGet(err, 'code')
90 return found.tag === 'some' && isPlyCode(found.value) ? String(found.value) : fallback
93function failureMessage(err: unknown, code: string): string {
94 if (code === 'internal') return 'The viewer hit an internal error.'
95 const found = chainGet(err, 'message')
96 const text = found.tag === 'some' ? String(found.value) : String(err)
97 return text.slice(0, 500)
100function notify(opts: unknown, extra: Record<string, string | number>): void {
101 const found = chainGet(opts, 'note')
102 if (found.tag === 'none') return
103 const note = found.value as (fields: Record<string, string | number>) => void
104 note(extra)
107function routeError(call: ViewerCall, err: unknown): Response {
108 const code = failureCode(err, 'bad_request')
109 const message = failureMessage(err, code)
110 const status = statusOf({ code })
111 notify(call.opts, { code, status })
112 call.fields['http.status_code'] = status
113 call.fields['error.type'] = code
114 const trace = traceOf(call)
115 return htmlResponse(status, renderError(status, code, message, trace), trace, call.requestId)
118async function rateGate(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract<ViewerRoute, { kind: 'repo' }> | { kind: 'home' } | { kind: 'redirect' } | { kind: 'namespace' }): Promise<Response | null> {
119 const skipRate = chainGet(call.opts, 'skipRate')
120 if (skipRate.tag === 'some' && skipRate.value) return null
121 const bucket: RateBucket = route.kind === 'repo' && route.pack ? 'pack' : 'page'
122 const decision = await admitClient(req, env, bucket, call.now, call.ctx)
123 if (decision === 'unconfigured' || decision === 'unavailable') {
124 const trace = traceOf(call)
125 const message = decision === 'unconfigured' ? 'The viewer rate limiter is not configured.' : 'The viewer rate limiter is unavailable.'
126 notify(call.opts, { code: 'rate_unavailable', status: 503 })
127 call.fields['http.status_code'] = 503
128 return htmlResponse(503, renderError(503, 'unavailable', message, trace), trace, call.requestId)
129 }
130 if (!decision.ok) {
131 const retry = String(Math.max(1, Math.ceil(decision.retryAfterMs / LAYOUT.msPerSecond)))
132 const trace = traceOf(call)
133 notify(call.opts, { code: 'rate_limited', status: 429 })
134 call.fields['http.status_code'] = 429
135 return htmlResponse(429, renderError(429, 'rate_limited', `Too many viewer requests. Retry after ${retry}s.`, trace), trace, call.requestId, { 'retry-after': retry })
136 }
137 return null
140// The hashed URL that matches these bytes is immutable. /view.css (HTML from before hashed hrefs) and a stale hash (HTML
141// rendered by the previous deploy) get the current sheet with an ETag and must revalidate, so they heal on the next load.
142function cssResponse(call: ViewerCall, req: Request): Response {
143 call.fields['http.status_code'] = 200
144 call.fields['ply.viewer.route'] = 'css'
145 const route = call.route
146 const exact = route !== null && route.kind === 'css' && route.hash === VIEWER_CSS_HASH
147 const etag = `"${VIEWER_CSS_HASH}"`
148 const headers = {
149 'content-type': 'text/css; charset=utf-8',
150 'cache-control': exact ? 'public, max-age=31536000, immutable' : 'no-cache',
151 etag,
152 ...SECURITY,
153 'x-ply-request-id': call.requestId,
154 }
155 if (req.headers.get('if-none-match') === etag) {
156 call.fields['http.status_code'] = 304
157 return new Response(null, { status: 304, headers })
158 }
159 return new Response(req.method === 'HEAD' ? null : VIEWER_CSS, { status: 200, headers })
162async function serveRepo(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract<ViewerRoute, { kind: 'repo' }>): Promise<Response> {
163 const limited = await rateGate(req, env, call, route)
164 if (limited) return headless(req, limited)
165 const collected = spanList()
166 const versionId = dig(env, ['VERSION', 'id'])
167 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))
168 const budget = createBudget()
169 const pageSignal = AbortSignal.timeout(VIEWER_CAPS.pageTimeoutMs)
170 // The public namespace's read token is attached below the guard: every hop is validated by createViewerFetch first,
171 // then checked again by withPublicReadCredential, and the pack cache above never sees it in its key.
172 // Public, credential-free exchanges go to the cache-enabled GitCache entrypoint (Workers Caching) under the guard.
173 const fetchField = chainGet(call.opts, 'fetchImpl')
174 const direct = withPublicReadCredential(fetchField.tag === 'some' ? fetchField.value as FetchLike : globalThis.fetch, tokenSourceFor(env.PUBLIC_REPOS))
175 const optsRecord = call.opts !== null && typeof call.opts === 'object' ? call.opts as Record<string, unknown> : null
176 const provided = recordGet(optsRecord, 'loopback')
177 let loopback: (() => LoopbackBinding | null) | null
178 if (provided.tag === 'none') {
179 loopback = env.PACK_CACHE === 'off' ? null : () => {
180 const found = dig(call.ctx, ['exports', 'GitCache'])
181 return found.tag === 'some' ? found.value as LoopbackBinding : null
182 }
183 } else if (provided.value === null) {
184 loopback = null
185 } else {
186 const chosen = provided.value as () => LoopbackBinding | null
187 loopback = () => {
188 const binding = chosen()
189 return isLoopback(binding) ? binding : null
190 }
191 }
192 const rays = createRayLog()
193 const fetchImpl: FetchLike = createViewerFetch(observeUpstream(createLoopbackFetch(direct, loopback, { watchedRefs: env.REFS_POLICY === 'watched' }), rays), route.loc.url, route.loc.allowedHosts, budget, pageSignal)
194 let stamped = false
195 const traced = () => {
196 const report = rays.report()
197 if (!stamped) { stampUpstreamSpans(collected.spans, report); stamped = true }
198 return { ...buildTrace(collected.spans, call.cfRay, call.requestId), expanded: new URL(req.url).searchParams.get('trace') === '1' }
199 }
200 const attach = (res: Response): Response => {
201 applyUpstreamHeaders(res.headers, rays.report(), Date.now() - call.now)
202 // Stamp pack-cache child outcomes onto the response so index.js can put them on the request span.
203 const stats = cacheStatsOf(collected.spans)
204 res.headers.set('x-ply-cache-hits', String(stats.cache_hits))
205 res.headers.set('x-ply-cache-misses', String(stats.cache_misses))
206 res.headers.set('x-ply-spans', String(stats.spans))
207 res.headers.set('x-ply-rtt', String(stats.rtt))
208 return res
209 }
210 try {
211 // The namespace's yes/no runs beside the git reads instead of before them (it was 37 ms of a 105 ms page, trace
212 // 7dc622ba…). A missing repo still answers this 404: its git reads fail at the read-token mint before any upstream
213 // request, and are cancelled here.
214 const short = publicRepoShortName(route.loc.host, route.loc.repoPath)
215 const stop = new AbortController()
216 const pending = loadRepo(route, { tracer, fetchImpl, signal: AbortSignal.any([pageSignal, stop.signal]), budget })
217 if (short) {
218 pending.catch(() => {})
219 const waitUntil = dig(call.ctx, ['waitUntil'])
220 const defer = waitUntil.tag === 'some' && typeof waitUntil.value === 'function' ? (p: Promise<unknown>) => { (waitUntil.value as (p: Promise<unknown>) => void).call(call.ctx, p) } : null
221 const exists = await publicRepoExistsMemo(env.PUBLIC_REPOS, short, call.now, defer)
222 if (exists === false) {
223 stop.abort()
224 throw Object.assign(new Error(`no repository ${short} in ${PUBLIC_NAMESPACE}`), { code: 'not_found' })
225 }
226 }
227 const loaded = await pending
228 const trace = traced()
229 notify(call.opts, { host: route.loc.host, rtt: trace.bars.length, bytes: budget.bytes, code: 'ok' })
230 if (loaded.kind === 'raw') {
231 const text = isText(loaded.bytes)
232 const headers = new Headers({
233 ...SECURITY,
234 'content-type': text ? 'text/plain; charset=utf-8' : 'application/octet-stream',
235 'content-disposition': `${text ? 'inline' : 'attachment'}; filename="${safeFilename(loaded.filename)}"`,
236 'cache-control': 'no-store',
237 'x-content-type-options': 'nosniff',
238 'x-ply-request-id': call.requestId,
239 })
240 const summary = traceHeader(trace)
241 if (summary) headers.set('x-ply-trace', summary)
242 const copy = new ArrayBuffer(loaded.bytes.byteLength)
243 new Uint8Array(copy).set(loaded.bytes)
244 call.fields['http.status_code'] = 200
245 call.fields['ply.viewer.route'] = 'raw'
246 call.fields['server.address'] = route.loc.host
247 return attach(new Response(req.method === 'HEAD' ? null : new Blob([copy]), { status: 200, headers }))
248 }
249 call.fields['http.status_code'] = 200
250 call.fields['ply.viewer.route'] = loaded.kind
251 call.fields['server.address'] = route.loc.host
252 call.fields['ply.viewer.ref'] = loaded.header.ref
253 call.fields['ply.viewer.upstream_spans'] = trace.bars.length
254 return headless(req, attach(htmlResponse(200, renderLoaded(loaded, trace, call.now), trace, call.requestId)))
255 } catch (err) {
256 const code = failureCode(err, 'internal')
257 const upstream = code === 'internal' ? 500 : statusOf(err)
258 const message = failureMessage(err, code)
259 const view = describeFailure({ code, status: upstream, message, host: route.loc.host, repoPath: route.loc.repoPath, ref: route.ref, path: route.path })
260 const trace = traced()
261 notify(call.opts, { host: route.loc.host, code, status: view.status, rtt: trace.bars.length })
262 call.fields['http.status_code'] = view.status
263 call.fields['error.type'] = code
264 call.fields['server.address'] = route.loc.host
265 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)))
266 }
269export const viewerApp = new Hono<{ Bindings: ViewerEnv }>()
271viewerApp.use('*', async (c, next) => {
272 const call = callOf(c.req.raw)
273 if (c.req.method !== 'GET' && c.req.method !== 'HEAD') {
274 call.fields['http.status_code'] = 405
275 return textResponse(405, 'method not allowed', { allow: 'GET, HEAD' })
276 }
277 try {
278 call.route = parseViewerRequest(c.req.url, routeOptsFromEnv(c.env))
279 } catch (err) {
280 return headless(c.req.raw, routeError(call, err))
281 }
282 await next()
283})
285function serveCss(c: { req: { raw: Request } }): Response {
286 const call = callOf(c.req.raw)
287 if (call.route === null || call.route.kind !== 'css') return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' }))
288 return cssResponse(call, c.req.raw)
291viewerApp.on(['GET', 'HEAD'], '/view.css', c => serveCss(c))
292viewerApp.on(['GET', 'HEAD'], '/:sheet{view\\.[0-9a-f]{12}\\.css}', c => serveCss(c))
294viewerApp.on(['GET', 'HEAD'], '/view', c => serveEntry(c.req.raw, c.env))
295viewerApp.on(['GET', 'HEAD'], '/view/', c => serveEntry(c.req.raw, c.env))
297viewerApp.on(['GET', 'HEAD'], '/view/*', c => dispatchViewer(c))
298viewerApp.on(['GET', 'HEAD'], '/r', c => dispatchViewer(c))
299viewerApp.on(['GET', 'HEAD'], '/r/', c => dispatchViewer(c))
300viewerApp.on(['GET', 'HEAD'], '/r/*', c => dispatchViewer(c))
302function dispatchViewer(c: { req: { raw: Request }; env: ViewerEnv }): Promise<Response> | Response {
303 const call = callOf(c.req.raw)
304 const route = call.route
305 if (!route) return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' }))
306 if (route.kind === 'namespace') return serveNamespace(c.req.raw, c.env)
307 if (route.kind === 'redirect') return serveRedirect(c.req.raw, c.env, call, route)
308 if (route.kind !== 'repo') return headless(c.req.raw, routeError(call, { code: 'bad_request', message: 'not a viewer route' }))
309 return serveRepo(c.req.raw, c.env, call, route)
312async function serveRedirect(req: Request, env: ViewerEnv, call: ViewerCall, route: Extract<ViewerRoute, { kind: 'redirect' }>): Promise<Response> {
313 const limited = await rateGate(req, env, call, route)
314 if (limited) return headless(req, limited)
315 call.fields['http.status_code'] = 302
316 return new Response(null, { status: 302, headers: { location: route.location, 'cache-control': 'no-store', ...SECURITY, 'x-ply-request-id': call.requestId } })
319async function serveNamespace(req: Request, env: ViewerEnv): Promise<Response> {
320 const call = callOf(req)
321 const limited = await rateGate(req, env, call, { kind: 'namespace' })
322 if (limited) return headless(req, limited)
323 const trace = traceOf(call)
324 call.fields['http.status_code'] = 200
325 call.fields['ply.viewer.route'] = 'namespace'
326 const publicRepos = await listPublicRepos(env.PUBLIC_REPOS, call.now)
327 return headless(req, htmlResponse(200, renderNamespace(publicRepos, call.now), trace, call.requestId))
330viewerApp.notFound(c => {
331 const call = viewerCalls.get(c.req.raw)
332 if (!call) return textResponse(404, 'not found')
333 call.fields['http.status_code'] = 404
334 const trace = traceOf(call)
335 return headless(c.req.raw, htmlResponse(404, renderError(404, 'not_found', 'not a viewer route', trace), trace, call.requestId))
336})
338async function serveEntry(req: Request, env: ViewerEnv): Promise<Response> {
339 const call = callOf(req)
340 const route = call.route
341 if (!route || (route.kind !== 'home' && route.kind !== 'redirect')) {
342 return headless(req, routeError(call, { code: 'bad_request', message: 'not a viewer route' }))
343 }
344 const limited = await rateGate(req, env, call, route)
345 if (limited) return headless(req, limited)
346 if (route.kind === 'redirect') {
347 call.fields['http.status_code'] = 302
348 return new Response(null, { status: 302, headers: { location: route.location, 'cache-control': 'no-store', ...SECURITY, 'x-ply-request-id': call.requestId } })
349 }
350 const trace = traceOf(call)
351 call.fields['http.status_code'] = 200
352 call.fields['ply.viewer.route'] = 'home'
353 const publicRepos = await listPublicRepos(env.PUBLIC_REPOS, call.now)
354 return headless(req, htmlResponse(200, renderHome(null, call.now, publicRepos), trace, call.requestId))
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2005.5 KiB · hit28 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=28
git.response.bytes=5589
git.response.cache=HIT
git.response.duration_ms=28
git.fetch.wants=cf21a1821e3cf7668e93268fb1056bbed81360c8
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5589
git.pkt.payload.max=5568
git.sideband.band1.bytes=5567
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e338972dae24ce
Request
574c99db…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format