main
ply4 git calls272 ms44 KiB2 cachedprotocol v2
171 lines9.4 KiBRaw
1// Packs the harness writes and ply sends as written (PackForm 'hand' and 'defect' in ply/probes): plain entries, the
2// first blob optionally as a REF_DELTA, then damaged, or a pack with one structural defect, as ply's runner writes them
3// (test/probes/runner.mjs writePack, deltaFor, damagePack, defectPack), entry for entry; the zlib streams come from the platform's CompressionStream (so the bytes
4// may differ from Node's zlib, the objects and the damage do not), the trailer from ply's SHA-1.
5import { Sha1 } from 'ply';
6import type { Damage, PackDefect, PackForm } from 'ply/probes';
7import { type Result, err, ok } from './result.ts';
8import { resolveObjRef, type SeedStore, type StoredObject } from './seed.ts';
9import { mapGet } from '../absent.ts';
11const TYPE_CODE: Readonly<Record<StoredObject['type'], number>> = { commit: 1, tree: 2, blob: 3, tag: 4 };
12const OFS_DELTA = 6;
13const REF_DELTA = 7;
14const BEFORE_START = 4096;
15const TRAILER_BYTES = 20;
16const LITERAL_MAX = 127;
17const PAST_END = 1000;
18const COPY_BYTES = 16;
20// `stated` is an entry whose header is written as given over a payload deflated as is (a type or size that lies).
21export type PackEntry =
22 | { readonly tag: 'whole'; readonly type: number; readonly data: Uint8Array }
23 | { readonly tag: 'ref-delta'; readonly base: Uint8Array; readonly delta: Uint8Array }
24 | { readonly tag: 'stated'; readonly head: Uint8Array; readonly payload: Uint8Array };
26const concat = (parts: readonly Uint8Array[]): Uint8Array => {
27 const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
28 let at = 0;
29 for (const p of parts) { out.set(p, at); at += p.length; }
30 return out;
31};
32const u32 = (n: number): Uint8Array => { const b = new Uint8Array(4); new DataView(b.buffer).setUint32(0, n); return b; };
33export function varint(value: number): Uint8Array {
34 const out: number[] = [];
35 let n = value;
36 do { let b = n & 0x7f; n = Math.floor(n / 128); if (n > 0) b |= 0x80; out.push(b); } while (n > 0);
37 return Uint8Array.from(out);
39export function entryHeader(type: number, size: number): Uint8Array {
40 const out: number[] = [];
41 let b = (type << 4) | (size & 15);
42 let rest = Math.floor(size / 16);
43 while (rest > 0) { out.push(b | 0x80); b = rest & 0x7f; rest = Math.floor(rest / 128); }
44 out.push(b);
45 return Uint8Array.from(out);
47const sha1 = (b: Uint8Array): Uint8Array => new Sha1().update(b).digest();
48async function deflate(data: Uint8Array): Promise<Uint8Array> {
49 const stream = new Blob([data.slice()]).stream().pipeThrough(new CompressionStream('deflate'));
50 return new Uint8Array(await new Response(stream).arrayBuffer());
52const fromHex = (hex: string): Uint8Array => Uint8Array.from(hex.match(/../g) || [], h => parseInt(h, 16));
54export type WrittenPack = { readonly pack: Uint8Array; readonly zlibEnds: readonly number[] };
55export async function writePack(entries: readonly PackEntry[]): Promise<WrittenPack> {
56 const parts: Uint8Array[] = [new TextEncoder().encode('PACK'), u32(2), u32(entries.length)];
57 let at = 12;
58 const zlibEnds: number[] = [];
59 for (const e of entries) {
60 const head = e.tag === 'ref-delta' ? concat([entryHeader(REF_DELTA, e.delta.length), e.base]) : e.tag === 'stated' ? e.head : entryHeader(e.type, e.data.length);
61 const z = await deflate(e.tag === 'ref-delta' ? e.delta : e.tag === 'stated' ? e.payload : e.data);
62 parts.push(head, z);
63 at += head.length + z.length;
64 zlibEnds.push(at);
65 }
66 const body = concat(parts);
67 return { pack: concat([body, sha1(body)]), zlibEnds };
70// A delta from a base of `srcSize` bytes: literal inserts only (valid when the base is there), or one copy reading 16
71// bytes from past the end of the base (never valid).
72export function deltaFor(srcSize: number, target: Uint8Array, kind: 'literal' | 'copy-past-end'): Uint8Array {
73 if (kind === 'copy-past-end') {
74 const off = srcSize + PAST_END;
75 let op = 0x80;
76 const args: number[] = [];
77 for (let i = 0; i < 4; i++) { const b = Math.floor(off / 256 ** i) & 0xff; if (b !== 0) { op |= 1 << i; args.push(b); } }
78 op |= 0x10;
79 args.push(COPY_BYTES);
80 return concat([varint(srcSize), varint(COPY_BYTES), Uint8Array.from([op, ...args])]);
81 }
82 return concat([varint(srcSize), varint(target.length), literalInserts(target)]);
84const literalInserts = (target: Uint8Array): Uint8Array => {
85 const chunks: Uint8Array[] = [];
86 for (let o = 0; o < target.length; o += LITERAL_MAX) { const c = target.subarray(o, o + LITERAL_MAX); chunks.push(Uint8Array.of(c.length), c); }
87 return concat(chunks);
88};
89// git's OFS_DELTA base offset: big-endian 7-bit groups, each continuation adding one (pack-format.txt).
90export function ofsEncode(value: number): Uint8Array {
91 const out = [value & 0x7f];
92 for (let n = Math.floor(value / 128); n > 0; n = Math.floor(n / 128)) { n -= 1; out.unshift(0x80 | (n & 0x7f)); }
93 return Uint8Array.from(out);
96export function damagePack(written: WrittenPack, damage: Damage): Uint8Array {
97 const b = written.pack.slice();
98 switch (damage) {
99 case 'none': return b;
100 case 'bad-trailer': b[b.length - 1] = (b[b.length - 1] || 0) ^ 0xff; return b;
101 case 'truncated': return b.subarray(0, Math.floor(b.length / 2));
102 case 'bad-zlib': {
103 const end = written.zlibEnds[0] || 0;
104 b[end - 1] = (b[end - 1] || 0) ^ 0xff; // the last byte of the first object's Adler-32
105 const body = b.subarray(0, b.length - TRAILER_BYTES);
106 return concat([body, sha1(body)]);
107 }
108 }
111export async function handPack(store: SeedStore, objects: readonly StoredObject[], form: Extract<PackForm, { readonly tag: 'hand' }>): Promise<Result<Uint8Array, string>> {
112 let entries: PackEntry[] = objects.map(o => ({ tag: 'whole', type: TYPE_CODE[o.type], data: o.data }));
113 if (form.deltaBase !== null) {
114 const i = objects.findIndex(o => o.type === 'blob');
115 if (i < 0) return err('hand pack: no blob to send as a delta');
116 const base = resolveObjRef(store, form.deltaBase);
117 if (!base.ok) return base;
118 const baseObj = mapGet(store.objects, base.value);
119 if (baseObj.tag === 'none') return err(`hand pack: delta base ${base.value} is not in the seed`);
120 entries = entries.map((e, k) => (k === i && e.tag === 'whole' ? { tag: 'ref-delta', base: fromHex(base.value), delta: deltaFor(baseObj.value.data.length, e.data, form.delta) } : e));
121 }
122 return ok(damagePack(await writePack(entries), form.damage));
125// The header and body of a written pack with its trailer recomputed after `edit` (the defect is the only wrong thing).
126const retrail = (pack: Uint8Array, edit: (body: DataView, bytes: Uint8Array) => void): Uint8Array => {
127 const body = pack.slice(0, pack.length - TRAILER_BYTES);
128 edit(new DataView(body.buffer), body);
129 return concat([body, sha1(body)]);
130};
132// A pack with one structural defect (ply/probes PackDefect), as test/probes/runner.mjs defectPack writes it.
133export async function defectPack(store: SeedStore, objects: readonly StoredObject[], defect: PackDefect): Promise<Result<Uint8Array, string>> {
134 const plain: PackEntry[] = objects.map(o => ({ tag: 'whole', type: TYPE_CODE[o.type], data: o.data }));
135 const blob = objects.findIndex(o => o.type === 'blob');
136 const firstBlob = (): Result<StoredObject, string> => { const o = objects[blob]; return typeof o === 'object' ? ok(o) : err('defect pack: no blob'); };
137 switch (defect.tag) {
138 case 'signature': return ok(retrail((await writePack(plain)).pack, (_, b) => { b.set(new TextEncoder().encode('PACX'), 0); }));
139 case 'version': return ok(retrail((await writePack(plain)).pack, v => { v.setUint32(4, defect.version); }));
140 case 'count': return ok(retrail((await writePack(plain)).pack, v => { v.setUint32(8, plain.length + defect.by); }));
141 case 'duplicate': return ok((await writePack([...plain, ...plain])).pack);
142 case 'no-objects': return ok((await writePack([])).pack);
143 case 'size-header': case 'bad-type': {
144 const o = firstBlob();
145 if (!o.ok) return o;
146 const head = defect.tag === 'bad-type' ? entryHeader(defect.type, o.value.data.length) : entryHeader(TYPE_CODE.blob, o.value.data.length + defect.by);
147 return ok((await writePack(plain.map((e, k) => (k === blob ? { tag: 'stated', head, payload: o.value.data } : e)))).pack);
148 }
149 case 'ofs-delta-before-start': {
150 const o = firstBlob();
151 if (!o.ok) return o;
152 // the entries before the blob fix its offset; its base is that offset plus 4096 bytes back, before the pack
153 const before = await writePack(plain.slice(0, blob));
154 const at = before.pack.length - TRAILER_BYTES;
155 const delta = deltaFor(o.value.data.length, o.value.data, 'literal');
156 const head = concat([entryHeader(OFS_DELTA, delta.length), ofsEncode(at + BEFORE_START)]);
157 return ok((await writePack(plain.map((e, k) => (k === blob ? { tag: 'stated', head, payload: delta } : e)))).pack);
158 }
159 case 'delta-size-mismatch': {
160 const o = firstBlob();
161 if (!o.ok) return o;
162 const base = resolveObjRef(store, defect.base);
163 if (!base.ok) return base;
164 const baseObj = mapGet(store.objects, base.value);
165 if (baseObj.tag === 'none') return err(`defect pack: delta base ${base.value} is not in the seed`);
166 // the header states one byte more than the inserts write
167 const delta = concat([varint(baseObj.value.data.length), varint(o.value.data.length + 1), literalInserts(o.value.data)]);
168 return ok((await writePack(plain.map((e, k) => (k === blob ? { tag: 'ref-delta', base: fromHex(base.value), delta } : e)))).pack);
169 }
170 }
Diagnostics4 git calls · 272 ms
CallStatusSizeTime0272 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 · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
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=1
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
fetch2003.2 KiB266 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=266
git.response.bytes=3279
git.response.cache=MISS
git.response.duration_ms=266
git.fetch.wants=e32dc2feb8f921d491ad24c30e21fdb7ed3f48f2
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=3279
git.pkt.payload.max=3258
git.sideband.band1.bytes=3257
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
a5e300b2be63f3c9
Request
afe60510…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format