main
ply4 git calls61 ms42 KiB3 cachedprotocol v2
85 lines3.4 KiBRaw
1// Public viewer entry. No PLY_TOKEN, no git credential, no /v1 handler. Rate limit runs before any git fetch.
2// This module does not import rate.ts: node tests must not load cloudflare:workers. The Worker re-exports the class.
3import { chainGet } from '../absent.ts'
4import { openSpan, type TracingLike } from '../flags/telemetry.ts'
5import { plyBuildValue, BUILD_HEADER } from '../version.ts'
6import { ok, runRequest, type Result, type UnexpectedFailure } from '../http/machine.ts'
7import { viewerApp } from './app.tsx'
8import { viewerCalls, type HandleOpts, type ViewerCall, type ViewerCtx } from './call.ts'
9import { htmlResponse } from './http.ts'
10import type { TraceModel, ViewerEnv } from './model.ts'
11import { renderError } from './render.tsx'
13export type { HandleOpts, ViewerCtx }
15const EMPTY: TraceModel = { bars: [], totalMs: 0, cfRay: '', requestId: '' }
17type EdgeSpan = { setAttribute?(key: string, value: string | number | boolean): void; end?(): void }
19/** A null receiver or a null method skips. A present method keeps its receiver and may throw. */
20function callMethod(receiver: unknown, key: string, args: readonly unknown[]): void {
21 const method = chainGet(receiver, key)
22 if (method.tag === 'none') return
23 const fn = method.value as (...params: unknown[]) => unknown
24 fn.apply(receiver, [...args])
27type ViewerReady = { readonly requestId: string }
29function viewerFailure(failure: UnexpectedFailure, call: ViewerCall): Response {
30 console.log(JSON.stringify({
31 msg: 'ply.viewer',
32 error_kind: failure.kind,
33 stage: failure.stage,
34 error_name: failure.name,
35 }))
36 const trace = { ...EMPTY, cfRay: call.cfRay, requestId: call.requestId }
37 const res = htmlResponse(500, renderError(500, 'internal', 'The viewer hit an internal error.', trace), trace, call.requestId)
38 res.headers.set('x-ply-error-kind', failure.kind)
39 call.fields['http.status_code'] = 500
40 call.fields['error.type'] = failure.kind
41 return res
44export async function handleViewer(req: Request, env: ViewerEnv, ctx: unknown = null, opts: unknown = null): Promise<Response> {
45 const requestIdField = chainGet(opts, 'requestId')
46 const nowField = chainGet(opts, 'now')
47 const ray = req.headers.get('cf-ray')
48 const requestId = requestIdField.tag === 'none' ? crypto.randomUUID() : requestIdField.value as string
49 const call: ViewerCall = {
50 requestId,
51 now: nowField.tag === 'none' ? Date.now() : nowField.value as number,
52 cfRay: ray === null ? '' : ray,
53 opts,
54 ctx,
55 fields: {},
56 route: null,
57 }
58 viewerCalls.set(req, call)
59 let span: EdgeSpan | null = null
60 let spanClosed = false
61 const closeSpan = (): void => {
62 if (spanClosed) return
63 spanClosed = true
64 if (!span) return
65 for (const [key, value] of Object.entries(call.fields)) callMethod(span, 'setAttribute', [key, value])
66 callMethod(span, 'end', [])
67 }
68 return runRequest(
69 {
70 parse: () => {
71 const tracing = chainGet(ctx, 'tracing')
72 span = openSpan(tracing.tag === 'some' ? tracing.value as TracingLike : null, 'ply.viewer')
73 return ok({ requestId } satisfies ViewerReady)
74 },
75 authorize: (parsed: ViewerReady) => ok(parsed),
76 execute: async () => ok(await viewerApp.fetch(req, env)),
77 },
78 (result: Result<Response, UnexpectedFailure>) => {
79 const res = result.ok ? result.value : viewerFailure(result.error, call)
80 closeSpan()
81 res.headers.set(BUILD_HEADER, plyBuildValue())
82 return res
83 },
84 )
Diagnostics4 git calls · 61 ms
CallStatusSizeTime061 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 · hit5 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=5
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=5
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
fetch2001.5 KiB · hit54 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=54
git.response.bytes=1517
git.response.cache=HIT
git.response.duration_ms=54
git.fetch.wants=43c2884c5cbdb0f6639cfecc06c6b6246e6d7b76
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=1517
git.pkt.payload.max=1496
git.sideband.band1.bytes=1495
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
a5e318d94ce93e6e
Request
f534bd6f…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format