main
ply4 git calls397 ms43 KiB2 cachedprotocol v2
253 lines10 KiBRaw
1import { judgeFlagStep } from './flags-smoke.ts'
2import { asRecord } from './json.ts'
4export type Saved = Readonly<Record<string, string>>
6export type RayNeed = 'required' | 'optional' | 'absent'
8const RAY_ENTRY = /^([a-z-]+)=([0-9a-f]{16}-[A-Z]{3}|-|no-response);dur=\d+$/
9const PHASES = new Set(['ls-refs', 'fetch', 'receive-pack'])
10const REQUEST_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
11const BUILD = /^ply=[0-9a-f]{40} ply-web=[0-9a-f]{40}$/
13export type Expect =
14 | { readonly kind: 'empty-commit' }
15 | { readonly kind: 'refs-empty' }
16 | { readonly kind: 'code'; readonly status: number; readonly code: string; readonly message?: RegExp }
17 | {
18 readonly kind: 'outcome'
19 readonly status: number
20 readonly outcome: string
21 readonly noop?: boolean
22 readonly nullFields?: readonly string[]
23 readonly equals?: readonly { readonly field: string; readonly key: string }[]
24 readonly differs?: readonly { readonly field: string; readonly key: string }[]
25 }
26 | { readonly kind: 'text'; readonly status: number; readonly text: string }
27 | { readonly kind: 'tree'; readonly names: readonly string[] }
28 | { readonly kind: 'parents'; readonly count: number; readonly oidEquals?: string }
29 | { readonly kind: 'born' }
30 | { readonly kind: 'stamps'; readonly authorEmail: string; readonly committerEmail: string }
31 | { readonly kind: 'refs-final'; readonly has: readonly string[]; readonly missing: readonly string[]; readonly head: string }
32 | { readonly kind: 'flags-rules' }
33 | { readonly kind: 'flags-eval' }
34 | { readonly kind: 'usage-off' }
36export type CallHeaders = {
37 readonly requestId: string | null
38 readonly build: string | null
39 readonly version: string | null
40 readonly ray: string | null
41 readonly cacheControl?: string | null
42 readonly contentType?: string | null
45export type CallInput = {
46 readonly status: number
47 readonly bodyText: string
48 readonly headers: CallHeaders
49 readonly ray: RayNeed
50 readonly rayIncludes?: string
51 readonly version: string | null
52 readonly expect: Expect
53 readonly save?: readonly { readonly key: string; readonly field: string }[]
54 readonly secrets: readonly string[]
57export type Judgement =
58 | { readonly kind: 'ok'; readonly detail: string; readonly save: Saved }
59 | { readonly kind: 'fail'; readonly detail: string }
61export function judgeRay(header: string | null, need: RayNeed, includes?: string): string | null {
62 if (header === null || header === '') {
63 if (need === 'required') return 'missing x-artifacts-ray'
64 return null
65 }
66 if (need === 'absent') return 'x-artifacts-ray was set before the Worker talked to upstream'
67 if (header.includes('/')) return 'x-artifacts-ray contains a slash'
68 let overflow = 0
69 for (const part of header.split(', ')) {
70 if (/^overflow=\d+$/.test(part)) {
71 overflow += 1
72 continue
73 }
74 const match = RAY_ENTRY.exec(part)
75 if (!match || !PHASES.has(match[1] ?? '')) return `bad x-artifacts-ray entry ${part}`
76 }
77 if (overflow > 1) return 'x-artifacts-ray has more than one overflow'
78 if (includes && !header.includes(includes)) return `x-artifacts-ray missing ${includes}`
79 return null
82function leak(text: string, secrets: readonly string[]): string | null {
83 for (const secret of secrets) {
84 if (secret.length >= 8 && text.includes(secret)) return 'response contains a secret'
85 }
86 return null
89function jsonBody(text: string): Record<string, unknown> | null {
90 try {
91 return asRecord(JSON.parse(text) as unknown)
92 } catch {
93 return null
94 }
97function sameNames(got: readonly string[], want: readonly string[]): boolean {
98 if (got.length !== want.length) return false
99 const left = [...got].sort()
100 const right = [...want].sort()
101 return left.every((name, i) => name === right[i])
104function emptyCommit(body: Record<string, unknown>): boolean {
105 return body.empty === true
106 && body.ref === 'refs/heads/main'
107 && body.oid === null
108 && body.tree === null
109 && Array.isArray(body.parents)
110 && body.parents.length === 0
111 && body.author === null
112 && body.committer === null
113 && body.message === null
114 && body.signed === false
117function flagHeaders(input: CallInput) {
118 return {
119 cacheControl: input.headers.cacheControl ?? null,
120 contentType: input.headers.contentType ?? null,
121 }
124function checkExpect(input: CallInput, body: Record<string, unknown> | null, saved: Saved): string | null {
125 const expect = input.expect
126 if (expect.kind === 'flags-rules' || expect.kind === 'flags-eval' || expect.kind === 'usage-off') {
127 const judged = judgeFlagStep(expect.kind, input.status, input.bodyText, flagHeaders(input), saved)
128 return judged.kind === 'fail' ? judged.detail : null
129 }
130 if (expect.kind === 'text') {
131 if (input.status !== expect.status) return `status ${input.status}, wanted ${expect.status}`
132 if (input.bodyText !== expect.text) return `body ${JSON.stringify(input.bodyText)}, wanted ${JSON.stringify(expect.text)}`
133 return null
134 }
135 if (!body) return 'response is not a JSON object'
136 if (expect.kind === 'empty-commit') {
137 if (input.status !== 200) return `status ${input.status}, wanted 200`
138 if (!emptyCommit(body)) return `empty commit body ${JSON.stringify(body)}`
139 return null
140 }
141 if (expect.kind === 'refs-empty') {
142 if (input.status !== 200) return `status ${input.status}, wanted 200`
143 if (body.empty !== true || body.head !== null || body.unborn !== 'refs/heads/main') {
144 return `refs body ${JSON.stringify(body)}`
145 }
146 return null
147 }
148 if (expect.kind === 'code') {
149 if (input.status !== expect.status) return `status ${input.status}, wanted ${expect.status}`
150 if (body.code !== expect.code) return `code ${String(body.code)}, wanted ${expect.code}`
151 if (expect.message && (typeof body.message !== 'string' || !expect.message.test(body.message))) {
152 return `message ${JSON.stringify(body.message)}`
153 }
154 return null
155 }
156 if (expect.kind === 'outcome') {
157 if (input.status !== expect.status) return `status ${input.status}, wanted ${expect.status}`
158 if (body.outcome !== expect.outcome) return `outcome ${String(body.outcome)}, wanted ${expect.outcome}`
159 if (expect.noop !== undefined && body.noop !== expect.noop) return `noop ${String(body.noop)}, wanted ${String(expect.noop)}`
160 for (const field of expect.nullFields ?? []) {
161 if (body[field] !== null) return `${field} is ${JSON.stringify(body[field])}, wanted null`
162 }
163 for (const eq of expect.equals ?? []) {
164 const want = saved[eq.key]
165 if (want === undefined) return `saved ${eq.key} is missing`
166 if (body[eq.field] !== want) return `${eq.field} is ${JSON.stringify(body[eq.field])}, wanted ${want}`
167 }
168 for (const diff of expect.differs ?? []) {
169 const other = saved[diff.key]
170 if (other === undefined) return `saved ${diff.key} is missing`
171 if (body[diff.field] === other) return `${diff.field} still equals ${other}`
172 }
173 return null
174 }
175 if (expect.kind === 'tree') {
176 if (input.status !== 200) return `status ${input.status}, wanted 200`
177 if (!Array.isArray(body.entries)) return 'tree has no entries'
178 const names = body.entries.map(entry => {
179 const rec = asRecord(entry)
180 return rec && typeof rec.name === 'string' ? rec.name : ''
181 })
182 if (!sameNames(names, expect.names)) return `tree names ${names.join(', ')}`
183 return null
184 }
185 if (expect.kind === 'parents') {
186 if (input.status !== 200) return `status ${input.status}, wanted 200`
187 if (!Array.isArray(body.parents) || body.parents.length !== expect.count) {
188 return `parents ${JSON.stringify(body.parents)}, wanted ${expect.count}`
189 }
190 if (expect.oidEquals) {
191 const want = saved[expect.oidEquals]
192 if (want === undefined) return `saved ${expect.oidEquals} is missing`
193 if (body.oid !== want) return `oid is ${JSON.stringify(body.oid)}, wanted ${want}`
194 }
195 return null
196 }
197 if (expect.kind === 'born') {
198 if (input.status !== 200) return `status ${input.status}, wanted 200`
199 if (typeof body.oid !== 'string' || !/^[0-9a-f]{40}$/.test(body.oid)) return `oid ${JSON.stringify(body.oid)}`
200 return null
201 }
202 if (expect.kind === 'stamps') {
203 if (input.status !== 200) return `status ${input.status}, wanted 200`
204 const author = asRecord(body.author)
205 const committer = asRecord(body.committer)
206 if (author?.email !== expect.authorEmail) return `author ${JSON.stringify(body.author)}`
207 if (committer?.email !== expect.committerEmail) return `committer ${JSON.stringify(body.committer)}`
208 return null
209 }
210 if (input.status !== 200) return `status ${input.status}, wanted 200`
211 const refs = asRecord(body.refs)
212 if (!refs) return 'refs is not an object'
213 if (body.head !== expect.head) return `head ${JSON.stringify(body.head)}, wanted ${expect.head}`
214 for (const name of expect.has) {
215 if (typeof refs[name] !== 'string') return `missing ${name}`
216 }
217 for (const name of expect.missing) {
218 if (name in refs) return `${name} is still present`
219 }
220 return null
223export function judgeCall(input: CallInput, saved: Saved): Judgement {
224 const leaked = leak(input.bodyText, input.secrets)
225 ?? leak(input.headers.ray ?? '', input.secrets)
226 ?? leak(input.headers.requestId ?? '', input.secrets)
227 if (leaked) return { kind: 'fail', detail: leaked }
228 if (!input.headers.requestId || !REQUEST_ID.test(input.headers.requestId)) {
229 return { kind: 'fail', detail: 'x-ply-request-id is not a uuid' }
230 }
231 if (!input.headers.build || !BUILD.test(input.headers.build)) {
232 return { kind: 'fail', detail: 'x-ply-build is not ply=<sha> ply-web=<sha>' }
233 }
234 if (input.version !== null && input.headers.version !== input.version) {
235 return { kind: 'fail', detail: `x-ply-version ${input.headers.version ?? 'missing'}, wanted ${input.version}` }
236 }
237 const ray = judgeRay(input.headers.ray, input.ray, input.rayIncludes)
238 if (ray) return { kind: 'fail', detail: ray }
239 const body = input.expect.kind === 'text' ? null : jsonBody(input.bodyText)
240 const problem = checkExpect(input, body, saved)
241 if (problem) return { kind: 'fail', detail: problem }
242 const next: Record<string, string> = {}
243 const flag = judgeFlagStep(input.expect.kind, input.status, input.bodyText, flagHeaders(input), saved)
244 if (flag.kind === 'ok') Object.assign(next, flag.save)
245 if (body) {
246 for (const item of input.save ?? []) {
247 const value = body[item.field]
248 if (typeof value !== 'string' || value === '') return { kind: 'fail', detail: `cannot save ${item.key} from ${item.field}` }
249 next[item.key] = value
250 }
251 }
252 return { kind: 'ok', detail: 'ok', save: next }
Diagnostics4 git calls · 397 ms
CallStatusSizeTime0397 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 · 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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.8 KiB391 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=391
git.response.bytes=2857
git.response.cache=MISS
git.response.duration_ms=391
git.fetch.wants=9b2e3e1ac7fe20be5da8a1cd161fa20dcad34d4a
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=2857
git.pkt.payload.max=2836
git.sideband.band1.bytes=2835
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
a5e310d9e9babdc9
Request
0396a0c1…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format