main
1// describePack without git: the choices verify-pack does not spell out (header size, not the resolved object; a thin
2// base is a list, not an error) and every limit the function passes through, at the boundary and one past it.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { describePack, parsePack, resolvePack, buildPack, makeDelta, encodeObjHeader, zlibDeflate } from '../src/pack.js';
6import { hashObject, TYPE } from '../src/objects.js';
7import { Sha1, fromHex } from '../src/sha1.js';
8import { concat, te } from '../src/bytes.js';
9import { DEFAULT_LIMITS, LIMIT_OBJECT_OVERHEAD, withLimits } from '../src/limits.js';
10import {
11 PACK_SIGNATURE, PACK_VERSION, PACK_HEADER_BYTES, PACK_TRAILER_BYTES, UINT32_BYTES,
12 OBJ_CONTINUE, OBJ_SIZE_NEXT_MASK, OBJ_SIZE_NEXT_BASE,
13} from '../src/format.js';
14import { unwrap } from './unwrap.js';
16const refused = (r, code, what) => {
17 assert.equal(r.ok, false, what);
18 if (r.ok) return;
19 assert.equal(r.error.code, code);
20 assert.equal(r.error.what, what);
21};
22const layoutOf = buf => {
23 const r = describePack(buf);
24 assert.equal(r.ok, true);
25 if (!r.ok) throw new Error('unreachable');
26 return r.value;
27};
29function encodeOfs(off) {
30 const out = [off & OBJ_SIZE_NEXT_MASK];
31 while ((off = Math.floor(off / OBJ_SIZE_NEXT_BASE)) > 0) out.unshift(OBJ_CONTINUE | (--off & OBJ_SIZE_NEXT_MASK));
32 return Uint8Array.of(...out);
33}
35// entries: { type, data } full, { data, baseOid } REF_DELTA, { data, baseIndex } OFS_DELTA to an earlier entry.
36async function packOf(entries) {
37 const parts = [Uint8Array.of(...PACK_SIGNATURE, 0, 0, 0, PACK_VERSION), new Uint8Array(UINT32_BYTES)];
38 new DataView(parts[1].buffer).setUint32(0, entries.length);
39 const offsets = [];
40 let at = PACK_HEADER_BYTES;
41 for (const e of entries) {
42 offsets.push(at);
43 const type = e.baseOid ? TYPE.REF_DELTA : e.baseIndex !== undefined ? TYPE.OFS_DELTA : e.type;
44 const entry = concat([
45 encodeObjHeader(type, e.data.length),
46 ...(e.baseOid ? [fromHex(e.baseOid)] : e.baseIndex !== undefined ? [encodeOfs(at - offsets[e.baseIndex])] : []),
47 await zlibDeflate(e.data),
48 ]);
49 parts.push(entry);
50 at += entry.length;
51 }
52 const body = concat(parts);
53 return concat([body, new Sha1().update(body).digest()]);
54}
56test('a pack of one of each object type is four depth-0 rows and no missing base', async () => {
57 const rows = [
58 [TYPE.COMMIT, te.encode('commit-bytes')],
59 [TYPE.TREE, te.encode('tree-bytes')],
60 [TYPE.BLOB, te.encode('blob-bytes')],
61 [TYPE.TAG, te.encode('tag-bytes')],
62 ];
63 const { pack } = await buildPack(rows.map(([type, data]) => ({ type, data })));
64 const layout = layoutOf(pack);
65 assert.equal(layout.version, PACK_VERSION);
66 assert.equal(layout.count, 4);
67 assert.equal(layout.objects.length, 4);
68 assert.deepEqual(layout.missingBases, []);
69 assert.equal(layout.trailer, new Sha1().update(pack.subarray(0, pack.length - PACK_TRAILER_BYTES)).hex());
70 const names = { [TYPE.COMMIT]: 'commit', [TYPE.TREE]: 'tree', [TYPE.BLOB]: 'blob', [TYPE.TAG]: 'tag' };
71 for (const [i, [type, data]] of rows.entries()) {
72 const o = layout.objects[i];
73 assert.equal(o.wireType, type);
74 assert.equal(o.type, type);
75 assert.equal(o.oid, hashObject(names[type], data));
76 assert.equal(o.depth, 0);
77 assert.deepEqual(o.base, { tag: 'none' });
78 assert.equal(o.size, data.length);
79 }
80});
82test('size is the inflated header, and an in-pack REF or OFS base is that object', async () => {
83 const base = te.encode('A'.repeat(64));
84 const target = te.encode(`${'A'.repeat(64)}Z`);
85 const delta = makeDelta(base, target).delta;
86 const baseOid = hashObject('blob', base);
87 const targetOid = hashObject('blob', target);
88 assert.notEqual(delta.length, target.length);
90 const { pack: refPack } = await buildPack([
91 { type: TYPE.BLOB, data: base },
92 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: baseOid } },
93 ]);
94 const ref = layoutOf(refPack);
95 assert.equal(ref.objects[1].wireType, TYPE.REF_DELTA);
96 assert.equal(ref.objects[1].type, TYPE.BLOB);
97 assert.equal(ref.objects[1].oid, targetOid);
98 assert.equal(ref.objects[1].size, delta.length);
99 assert.notEqual(ref.objects[1].size, target.length);
100 assert.equal(ref.objects[1].depth, 1);
101 assert.deepEqual(ref.objects[1].base, { tag: 'ref', oid: baseOid });
102 assert.deepEqual(ref.missingBases, []);
103 const parsed = unwrap(parsePack(refPack));
104 assert.equal(ref.objects[1].headerBytes + ref.objects[1].compressed, parsed.objects[1].headerBytes + parsed.objects[1].compressed);
106 const ofsPack = await packOf([
107 { type: TYPE.BLOB, data: base },
108 { data: delta, baseIndex: 0 },
109 ]);
110 const ofs = layoutOf(ofsPack);
111 assert.equal(ofs.objects[1].wireType, TYPE.OFS_DELTA);
112 assert.equal(ofs.objects[1].type, TYPE.BLOB);
113 assert.equal(ofs.objects[1].oid, targetOid);
114 assert.equal(ofs.objects[1].size, delta.length);
115 assert.equal(ofs.objects[1].depth, 1);
116 assert.deepEqual(ofs.objects[1].base, { tag: 'ofs', offset: ofs.objects[0].offset, oid: baseOid });
117});
119test('a thin REF base is missingBases, in pack order, and resolvePack still refuses it', async () => {
120 const base = te.encode('hello');
121 const target = te.encode('hello!');
122 const other = te.encode('other');
123 const delta = makeDelta(base, target).delta;
124 const deltaB = makeDelta(other, te.encode('other!')).delta;
125 const idA = hashObject('blob', base);
126 const idB = hashObject('blob', other);
127 const { pack } = await buildPack([
128 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: idA } },
129 { type: TYPE.BLOB, data: deltaB, base: { tag: 'ref', oid: idB } },
130 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: idA } },
131 ]);
132 const layout = layoutOf(pack);
133 assert.deepEqual(layout.missingBases, [idA, idB]);
134 for (const o of layout.objects) {
135 assert.equal(o.oid, null);
136 assert.equal(o.type, null);
137 assert.equal(o.depth, 1);
138 assert.equal(o.wireType, TYPE.REF_DELTA);
139 assert.equal(o.size, o === layout.objects[1] ? deltaB.length : delta.length);
140 }
141 assert.deepEqual(layout.objects[0].base, { tag: 'ref', oid: idA });
142 assert.throws(() => unwrap(resolvePack(unwrap(parsePack(pack)))), e => e.code === 'pack_corrupt' && e.message === `pack: ref delta base ${idA} not found`);
144 const chained = await packOf([
145 { data: delta, baseOid: idA },
146 { data: makeDelta(target, te.encode('hello!!')).delta, baseIndex: 0 },
147 ]);
148 const chain = layoutOf(chained);
149 assert.deepEqual(chain.missingBases, [idA]);
150 assert.equal(chain.objects[0].depth, 1);
151 assert.deepEqual(chain.objects[0].base, { tag: 'ref', oid: idA });
152 assert.equal(chain.objects[1].wireType, TYPE.OFS_DELTA);
153 assert.equal(chain.objects[1].depth, 2);
154 assert.equal(chain.objects[1].oid, null);
155 assert.equal(chain.objects[1].type, null);
156 assert.deepEqual(chain.objects[1].base, { tag: 'ofs', offset: chain.objects[0].offset, oid: null });
157});
159test('limits: the boundary accepts and one past it refuses, and a cap that bounds nothing is bad_request', async () => {
160 const base = te.encode('hello');
161 const target = te.encode('hello!');
162 const delta = makeDelta(base, target).delta;
163 const { pack } = await buildPack([
164 { type: TYPE.BLOB, data: base },
165 { type: TYPE.BLOB, data: delta, base: { tag: 'ref', oid: hashObject('blob', base) } },
166 ]);
167 const headerSum = base.length + delta.length;
168 // two objects, each charged LIMIT_OBJECT_OVERHEAD beyond its bytes (#142), and the delta result's bytes
169 const inflated = headerSum + 2 * LIMIT_OBJECT_OVERHEAD + target.length;
170 const maxHeader = Math.max(base.length, delta.length);
171 assert.equal(describePack(pack, withLimits({ maxPackObjects: 2 })).ok, true);
172 refused(describePack(pack, withLimits({ maxPackObjects: 1 })), 'limit_exceeded', 'pack objects');
173 assert.equal(describePack(pack, withLimits({ maxObjectSize: maxHeader })).ok, true);
174 refused(describePack(pack, withLimits({ maxObjectSize: maxHeader - 1 })), 'limit_exceeded', 'pack object size');
175 assert.equal(describePack(pack, withLimits({ maxInflatedTotal: inflated })).ok, true);
176 refused(describePack(pack, withLimits({ maxInflatedTotal: inflated - 1 })), 'limit_exceeded', 'delta result size');
177 assert.equal(describePack(pack, withLimits({ maxDeltaChain: 1 })).ok, true);
178 refused(describePack(pack, withLimits({ maxDeltaChain: 0 })), 'limit_exceeded', 'delta chain depth');
179 assert.equal(describePack(pack, withLimits({ maxDeltaResult: target.length })).ok, true);
180 refused(describePack(pack, withLimits({ maxDeltaResult: target.length - 1 })), 'limit_exceeded', 'delta result size');
181 refused(describePack(pack, withLimits({ maxPackObjects: Number.NaN })), 'bad_request', 'maxPackObjects');
182 // a key left out is the default (#156 parseLimits): the same layout as under DEFAULT_LIMITS; null is a value, refused
183 const missing = { ...DEFAULT_LIMITS };
184 delete missing.maxObjectSize;
185 assert.deepEqual(describePack(pack, missing), describePack(pack, DEFAULT_LIMITS));
186 refused(describePack(pack, { ...DEFAULT_LIMITS, maxObjectSize: null }), 'bad_request', 'maxObjectSize');
188 const badVer = pack.slice();
189 badVer[7] = 3;
190 refused(describePack(badVer), 'pack_corrupt', undefined);
191 const bad = describePack(badVer);
192 assert.equal(bad.ok, false);
193 if (!bad.ok) assert.equal(bad.error.message, 'pack: unsupported version 3');
194});
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 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 · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=6 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.6 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2703 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=b1cf5690484d146fc10420725a71cb825ac1c258 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=2703 git.pkt.payload.max=2682 git.sideband.band1.bytes=2681 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
- a5e1cfc75f5cc6f8
- Request
- 8a9eaafe…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format