GitHubivarvong

ply

main
ply6 git calls44 ms22 KiB5 cachedprotocol v2
139 lines7.2 KiBRaw
1// pkt-line framing. 4 hex length (inclusive) + payload. 0000 flush, 0001 delim, 0002 response-end.
2// Decoder is a pure step function over an immutable state record. Errors are values: every function that can refuse
3// returns a Result.
4import type { ErrorOf } from './errors.js';
5import { fail } from './errors.js';
6import type { Err, Result } from './result.js';
7import type { Input } from './absent.js';
8import { ok } from './result.js';
9import { te, concat, encodeBytes, HEXV } from './bytes.js';
10import {
11 PKT_LEN_HEX, PKT_HEX_BASE, PKT_FLUSH, PKT_DELIM, PKT_RESPONSE_END, PKT_LEN_INVALID,
12 PKT_MAX_PAYLOAD, PKT_READ_MAX_PAYLOAD, PKT_LEN_SHIFTS,
13} from './format.js';
15type Protocol = ErrorOf<'protocol'>;
16const proto = (msg: string): Err<Protocol> => fail('protocol', msg);
18// The one pkt-line writer. A line longer than git's LARGE_PACKET_MAX is the caller's (a push option, a ref prefix, a
19// want or filter: fields without a length rule of their own), refused before any byte is written, as git's
20// format_packet dies "impossibly long line" (pkt-line.c). bad_request: no retry can make the line shorter.
21export function pkt(payload: string | Uint8Array): Result<Uint8Array<ArrayBuffer>, ErrorOf<'bad_request'>> {
22 // a ref name read from ls-refs may hold escaped bytes (#43): encodeBytes writes them back as the server sent them
23 let p: Uint8Array;
24 if (typeof payload === 'string') {
25 const e = encodeBytes(payload, 'pkt-line');
26 if (!e.ok) return e;
27 p = e.value;
28 } else p = payload;
29 if (p.length > PKT_MAX_PAYLOAD) return fail('bad_request', `pkt-line payload too large: ${p.length} bytes, the most git frames is ${PKT_MAX_PAYLOAD}`, { what: 'pkt-line', got: p.length, max: PKT_MAX_PAYLOAD });
30 const out = new Uint8Array(PKT_LEN_HEX + p.length);
31 out.set(te.encode((p.length + PKT_LEN_HEX).toString(PKT_HEX_BASE).padStart(PKT_LEN_HEX, '0')));
32 out.set(p, PKT_LEN_HEX);
33 return ok(out);
35export const FLUSH: Uint8Array = te.encode('0000');
36export const DELIM: Uint8Array = te.encode('0001');
38// delimAfter: the index of the line a delim packet follows (-1: none); flush: end with a flush packet.
39export type PktsConfig = { readonly delimAfter: number; readonly flush: boolean };
40export type PktsOptions = Input<PktsConfig>;
41export function pkts(lines: ReadonlyArray<string | Uint8Array>, { delimAfter = -1, flush = true }: PktsOptions = {}): Result<Uint8Array<ArrayBuffer>, ErrorOf<'bad_request'>> {
42 const parts: Uint8Array[] = [];
43 for (const [i, l] of lines.entries()) {
44 const framed = pkt(l);
45 if (!framed.ok) return framed;
46 parts.push(framed.value);
47 if (i === delimAfter) parts.push(DELIM);
48 }
49 if (flush) parts.push(FLUSH);
50 return ok(concat(parts));
53export type PktState = {
54 readonly carry: Uint8Array | null;
55 readonly frames: number;
56 readonly data: number;
57 readonly flush: number;
58 readonly delim: number;
59 readonly end: number;
60 readonly bytes: number;
61 readonly maxPayload: number;
62 readonly carryMax: number;
63};
64export type PktFrame =
65 | { readonly kind: 'flush' }
66 | { readonly kind: 'delim' }
67 | { readonly kind: 'end' }
68 | { readonly kind: 'data'; readonly payload: Uint8Array };
70export const pktInit = (): PktState => Object.freeze({
71 carry: null, frames: 0, data: 0, flush: 0, delim: 0, end: 0, bytes: 0, maxPayload: 0, carryMax: 0,
72});
74// (state, chunk) -> { state, frames: [{kind, payload?}] }. Carry is bounded by one max pkt: a length is checked
75// (pktHeader) before the loop waits for its payload. A data payload is a copy, so a caller that keeps a frame keeps one
76// frame and may drop the chunk it came in (peak: one chunk plus one frame, #172). Every slice below is inside a length
77// the loop has already checked: nothing here can be short.
78export type PktStepped = { frames: PktFrame[]; state: PktState };
79// @decoder pktStep prefix=pkt
80// oxlint-disable-next-line eslint/complexity -- one frame loop, a switch on the length
81export function pktStep(st: PktState, chunk: Uint8Array): Result<PktStepped, Protocol> {
82 const frames: PktFrame[] = [];
83 const buf = st.carry ? concat([st.carry, chunk]) : chunk;
84 let p = 0;
85 let { data, flush, delim, end, maxPayload } = st;
86 frame: while (buf.length - p >= PKT_LEN_HEX) {
87 // the four length bytes are in buf (the loop), HEXV covers every byte value and PKT_LEN_SHIFTS has four entries:
88 // every read here is in range, which noUncheckedIndexedAccess cannot see (`as number`, not the `!` src/ forbids)
89 /* oxlint-disable typescript/no-unsafe-type-assertion, typescript/non-nullable-type-assertion-style, no-magic-numbers -- in-range reads of the four length bytes at p, see above */
90 const a = HEXV[buf[p] as number] as number;
91 const b = HEXV[buf[p + 1] as number] as number;
92 const c = HEXV[buf[p + 2] as number] as number;
93 const d = HEXV[buf[p + 3] as number] as number;
94 if ((a | b | c | d) < 0) return proto(`bad pkt-line length at byte ${st.bytes - (buf.length - chunk.length) + p}`);
95 const len = (a << (PKT_LEN_SHIFTS[0] as number)) | (b << (PKT_LEN_SHIFTS[1] as number)) | (c << (PKT_LEN_SHIFTS[2] as number)) | d;
96 /* oxlint-enable typescript/no-unsafe-type-assertion, typescript/non-nullable-type-assertion-style, no-magic-numbers */
97 // git's packet_read_with_status, by length: the three control packets, the invalid 0003, then data
98 switch (len) {
99 case PKT_FLUSH: flush++; frames.push({ kind: 'flush' }); break;
100 case PKT_DELIM: delim++; frames.push({ kind: 'delim' }); break;
101 case PKT_RESPONSE_END: end++; frames.push({ kind: 'end' }); break;
102 case PKT_LEN_INVALID: return proto(`invalid pkt-line length ${len}`);
103 default: {
104 // git's reader refuses a payload that does not fit its LARGE_PACKET_MAX buffer before reading it (pkt-line.c),
105 // so the bound is checked before the loop waits for (and carries) the payload
106 if (len - PKT_LEN_HEX > PKT_READ_MAX_PAYLOAD) return proto(`bad pkt-line length ${len - PKT_LEN_HEX}`);
107 if (buf.length - p < len) break frame;
108 const payload = buf.slice(p + PKT_LEN_HEX, p + len);
109 data++;
110 // Stryker disable next-line EqualityOperator: a payload equal to the current maximum leaves the maximum unchanged
111 if (payload.length > maxPayload) maxPayload = payload.length;
112 frames.push({ kind: 'data', payload });
113 p += len - PKT_LEN_HEX;
114 }
115 }
116 p += PKT_LEN_HEX;
117 }
118 const carry = p < buf.length ? buf.slice(p) : null;
119 return ok({
120 frames,
121 state: Object.freeze({
122 carry, data, flush, delim, end, maxPayload,
123 frames: st.frames + frames.length,
124 bytes: st.bytes + chunk.length,
125 carryMax: Math.max(st.carryMax, carry ? carry.length : 0),
126 }),
127 });
129// @decoder pktFinish prefix=pkt
130export function pktFinish(st: PktState): Result<PktState, Protocol> {
131 // pktStep carries null, never an empty array, when nothing is left
132 return st.carry ? proto(`trailing ${st.carry.length} bytes after last pkt-line`) : ok(st);
135export const pktAttrs = (st: PktState, prefix = 'git.pkt'): Record<string, number> => ({
136 [`${prefix}.frames`]: st.frames, [`${prefix}.data`]: st.data, [`${prefix}.flush`]: st.flush,
137 [`${prefix}.delim`]: st.delim, [`${prefix}.end`]: st.end, [`${prefix}.bytes`]: st.bytes,
138 [`${prefix}.payload.max`]: st.maxPayload, [`${prefix}.carry.max`]: st.carryMax,
139});
Diagnostics6 git calls · 44 ms
CallStatusSizeTime044 ms
discover200191 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=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=11990
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 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=4347
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
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=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · 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=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02002.4 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=2507
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=8f720d29ff85ce682ffdad9c393b04a3834c3203
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2507
git.pkt.payload.max=2480
git.sideband.band1.bytes=2480
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=2480
git.pack.trailer=723b6bf1d4d6021fe5eb940c622b9ff00ee4a381
Open pack map
fetch2002.8 KiB · hit34 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=34
git.response.bytes=2878
git.response.cache=HIT
git.response.duration_ms=34
git.fetch.wants=cabf9533f95c04ea4977c37ec9c8f29c3c95fca0
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2878
git.pkt.payload.max=2851
git.sideband.band1.bytes=2851
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://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e1d0445f15c6f8
Request
22535bd5…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format