main
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);
34}
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));
51}
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 });
128}
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);
133}
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});
Diagnostics4 git calls · 26 ms
CallStatusSizeTime026 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 1020038 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=38434 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=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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2002.8 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2863 git.response.cache=HIT git.response.duration_ms=20 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2863 git.pkt.payload.max=2842 git.sideband.band1.bytes=2841 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.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e323eccda25aae
- Request
- 22273af7…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format