GitHubivarvong
ply
main
1// The packs cluster's errors as values (sem/packs): each cell would have caught the bug it names, and the Result cores
2// are held to the same error the throwing wrapper throws. In the mutation suite (vitest.stryker.config.ts).
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import zlib from 'node:zlib';
6import { readFileSync, writeFileSync } from 'node:fs';
7import { join } from 'node:path';
8import { take, shortRead, encodeBytes, HEXV, te, concat, decodeBytes as decodeBytesOf, decodeChunk as decodeChunkOf } from '../src/bytes.js';
9import { pkt, pktStep, pktFinish, pktInit } from '../src/pktline.js';
10import { inflate, DEFLATE_TABLES } from '../src/inflate.js';
11import { readObjHeader, readOfsDelta, readUint32BE, parsePack, applyDelta, resolvePack, makeDelta } from '../src/packresolve.js';
12import { buildPack, encodeObjHeader, spliceDelta } from '../src/pack.js';
13import { fromHex, toHex, Sha1 } from '../src/sha1.js';
14import { scanPackIndex } from '../src/packscanindex.js';
15import { resolvePackRange, NO_BASE } from '../src/packrangeresolve.js';
16import { rangeFromBytes } from '../src/rangesource.js';
17import { git, gitBuf, tmpRoot } from './server.js';
18import { hashObject, TYPE } from '../src/objects.js';
19import { withLimits, DEFAULT_LIMITS } from '../src/limits.js';
20import { PACK_HEADER_BYTES, OBJ_CONTINUE } from '../src/format.js';
21import { unwrap } from './unwrap.js';
22import { loadTransferPack } from '../src/transferload.js';
23import { createTransport } from '../src/http.js';
24import { some, none } from '../src/result.js';
26const errOf = (r, code, message) => {
27 assert.equal(r.ok, false, 'an Err');
28 assert.equal(r.error.code, code, r.error.message);
29 if (message !== undefined) assert.equal(r.error.message, message);
30 assert.ok(r.error instanceof Error, 'a PlyError is an Error (a stack for whoever logs it)');
31 return r.error;
32};
34// ---- #76: control flow on a typed reason, never on a message ----
36test('#76 a short read carries reason short_read on every bounded reader, and nothing else does', () => {
37 const buf = Uint8Array.of(1, 2, 3);
38 const sr = (r, code, message) => assert.equal(errOf(r, code, message).reason, 'short_read');
39 sr(take(buf, 2, 2), 'pack_corrupt', 'short read: need 2 bytes at 2, have 3');
40 sr(take(buf, 0, 4, 'protocol'), 'protocol', 'short read: need 4 bytes at 0, have 3');
41 sr(take(undefined, 0, 0), 'pack_corrupt', 'short read: need 0 bytes at 0, have 0');
42 sr(take(buf, -1, 1), 'pack_corrupt', 'short read: need 1 bytes at -1, have 3');
43 sr(take(buf, 0.5, 1), 'pack_corrupt', 'short read: need 1 bytes at 0.5, have 3');
44 sr(take(buf, 0, -1), 'pack_corrupt', 'short read: need -1 bytes at 0, have 3');
45 sr(take(buf, 0, 1.5), 'pack_corrupt', 'short read: need 1.5 bytes at 0, have 3');
46 assert.deepEqual(take(buf, 1, 2), { ok: true, value: buf.subarray(1, 3) });
47 assert.equal(take(buf, 3, 0).value.length, 0);
48 assert.throws(() => unwrap(take(buf, 2, 2)), e => e.reason === 'short_read' && e.code === 'pack_corrupt');
49 assert.deepEqual(shortRead('protocol', 7, 2, 5), Object.assign(new Error('short read: need 2 bytes at 7, have 5'), { code: 'protocol', reason: 'short_read' }));
50 // an object header cut anywhere is a short read (fetch.ts spStep waits for more bytes on exactly this reason)
51 const h = encodeObjHeader(TYPE.BLOB, 2 ** 20 + 3);
52 for (let i = 0; i < h.length; i++) sr(readObjHeader(h.subarray(0, i), 0), 'pack_corrupt', `short read: need 1 bytes at ${i}, have ${i}`);
53 assert.deepEqual(readObjHeader(h, 0), { ok: true, value: { type: TYPE.BLOB, size: 2 ** 20 + 3, next: h.length } });
54 sr(readOfsDelta(Uint8Array.of(OBJ_CONTINUE), 0), 'pack_corrupt', 'short read: need 1 bytes at 1, have 1');
55 sr(readUint32BE(buf, 0), 'pack_corrupt', 'short read: need 4 bytes at 0, have 3');
56 sr(inflate(Uint8Array.of(0x78), 0), 'pack_corrupt', 'short read: need 2 bytes at 0, have 1');
57 // a corrupt header is not a short read: no reason
58 const long = new Uint8Array(12).fill(0x80);
59 const e = errOf(readObjHeader(long, 0), 'pack_corrupt', 'pack: size varint longer than 64 bits');
60 assert.equal(e.reason, undefined);
61});
63test('#76 a thin pack resolves through the external phase; a base missing from both is named, typed, and the first', async () => {
64 const v0 = te.encode('base object, long enough to delta against\n');
65 const v1 = te.encode('base object, long enough to delta against\nplus a line\n');
66 const v2 = te.encode('base object, long enough to delta against\nplus a line\nand another\n');
67 const d1 = spliceDelta({ baseLen: v0.length, off: v0.length, oldLen: 0, insert: v1.subarray(v0.length) }).delta;
68 const d2 = spliceDelta({ baseLen: v1.length, off: v1.length, oldLen: 0, insert: v2.subarray(v1.length) }).delta;
69 const id0 = hashObject('blob', v0), id1 = hashObject('blob', v1);
70 // child first: d2 waits on d1 (in the pack) which waits on v0 (external only)
71 const { pack } = await buildPack([{ type: TYPE.BLOB, data: d2, base: { tag: 'ref', oid: id1 } }, { type: TYPE.BLOB, data: d1, base: { tag: 'ref', oid: id0 } }]);
72 const parsed = unwrap(parsePack(pack));
73 let asked = [];
74 const r = resolvePack(parsed, o => { asked.push(o); return o === id0 ? { type: TYPE.BLOB, data: v0 } : undefined; });
75 assert.equal(r.ok, true);
76 assert.deepEqual([...r.value.objects.keys()].sort(), [id1, hashObject('blob', v2)].sort());
77 assert.equal(r.value.stats.externalBases, 1);
78 // the in-pack phase asks nothing; the external phase asks for each base still missing, in pack order (d2's id1 first,
79 // which the pack itself supplies a pass later)
80 assert.deepEqual(asked, [id1, id0]);
81 // nothing external: the error names the base the first waiting object (d2, pack order) is missing, as it always has
82 errOf(resolvePack(parsed), 'pack_corrupt', `pack: ref delta base ${id1} not found`);
83 assert.throws(() => unwrap(resolvePack(parsed)), e => e.code === 'pack_corrupt' && e.message === `pack: ref delta base ${id1} not found`);
84 // an error inside the chain is not mistaken for a missing base: a corrupt delta fails at once, even in the in-pack phase
85 const bad = await buildPack([{ type: TYPE.BLOB, data: d2, base: { tag: 'ref', oid: id1 } }, { type: TYPE.BLOB, data: d1, base: { tag: 'ref', oid: id0 } }, { type: TYPE.BLOB, data: v0 }]);
86 const p2 = unwrap(parsePack(bad.pack));
87 const broken = { ...p2, objects: p2.objects.map((o, i) => (i === 1 ? { ...o, data: Uint8Array.of(v0.length + 1, 1, 1, 0x41) } : o)) };
88 errOf(resolvePack(broken), 'pack_corrupt', `delta: base size ${v0.length + 1} != ${v0.length}`);
89});
91test('#76 no src module in the packs cluster branches on an error message', () => {
92 for (const f of ['bytes', 'pktline', 'pack', 'packresolve', 'inflate', 'sha1']) {
93 const src = readFileSync(new URL(`../src/${f}.ts`, import.meta.url), 'utf8');
94 assert.ok(!/\.message\b/.test(src.replace(/\/\/.*$/gm, '')), `${f}.ts reads an error's message`);
95 assert.ok(!/\bcatch\s*\(/.test(src.replace(/\/\/.*$/gm, '')) || f === 'inflate', `${f}.ts catches (only inflate's STOP slot may)`);
96 }
97});
99// ---- Result cores: Err values, never throws, the same error the wrapper throws ----
101test('Result cores return Err for every refusal their wrappers throw', async () => {
102 errOf(pktStep(pktInit(), te.encode('zzzz')), 'protocol', 'bad pkt-line length at byte 0');
103 errOf(pktStep(pktInit(), te.encode('0003')), 'protocol', 'invalid pkt-line length 3');
104 errOf(pktStep(pktInit(), te.encode('fff4')), 'protocol', 'bad pkt-line length 65520');
105 assert.equal(pktStep(pktInit(), te.encode('fff3')).ok, true, '65523 is the longest frame git reads');
106 const half = unwrap(pktStep(pktInit(), te.encode('0005a000'))).state;
107 errOf(pktFinish(half), 'protocol', 'trailing 3 bytes after last pkt-line');
108 const big = errOf(pkt(new Uint8Array(65517)), 'bad_request', 'pkt-line payload too large: 65517 bytes, the most git frames is 65516');
109 assert.deepEqual([big.what, big.got, big.max], ['pkt-line', 65517, 65516]);
110 errOf(pkt('\ud800'), 'bad_request', 'pkt-line: lone surrogate U+D800 is no byte sequence');
111 errOf(encodeBytes(7, 'ref'), 'bad_request', 'ref must be a string');
112 errOf(encodeBytes('\udcc3\udca9'), 'bad_request', 'string: escaped bytes spell well-formed UTF-8; pass the characters');
113 assert.deepEqual(encodeBytes('a\udce9'), { ok: true, value: Uint8Array.of(0x61, 0xe9) });
114 assert.throws(() => unwrap(encodeBytes(7)), e => e.code === 'bad_request' && e.what === 'string');
116 const z = zlib.deflateSync(te.encode('hello hello hello'));
117 errOf(inflate(z, 0, { maxOutput: Infinity }), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
118 const over = errOf(inflate(z, 0, { expectedSize: 17, maxOutput: 5 }), 'limit_exceeded', 'inflate: expected size 17 exceeds limit 5');
119 assert.deepEqual([over.what, over.got, over.max], ['inflate expected size', 17, 5]);
120 const grow = errOf(inflate(z, 0, { maxOutput: 5 }), 'limit_exceeded', 'inflate: output exceeds limit 5');
121 assert.deepEqual([grow.what, grow.max], ['inflate output', 5]);
122 assert.ok(grow.got > 5);
123 // BTYPE 3 is reserved: the exhaustive block type has no member for it
124 errOf(inflate(Uint8Array.of(0b111), 0, { raw: true }), 'pack_corrupt', 'inflate: invalid block type');
125 assert.throws(() => unwrap(inflate(Uint8Array.of(0b111), 0, { raw: true })), e => e.code === 'pack_corrupt' && e.message === 'inflate: invalid block type');
126 // a bug inside the decoder is not swallowed into an Err: only the STOP sentinel is converted
127 assert.throws(() => inflate(null, 0, { raw: true }), TypeError);
129 const { pack } = await buildPack([{ type: TYPE.BLOB, data: te.encode('x') }]);
130 errOf(parsePack(pack, { maxObjects: -1 }), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
131 errOf(parsePack(pack, { limits: withLimits({ maxObjectSize: NaN }) }), 'bad_request', 'limits.maxObjectSize must be a finite non-negative number');
132 errOf(parsePack(pack, { maxInflatedTotal: -1 }), 'bad_request', 'limits.maxInflatedTotal must be a finite non-negative number');
133 errOf(parsePack(pack, { maxInflatedTotal: 1 }), 'limit_exceeded');
134 errOf(parsePack(pack, { limits: { maxPackObjects: null } }), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
135 const lim = errOf(parsePack(pack, { maxObjects: 0 }), 'limit_exceeded', 'pack objects 1 exceeds limit 0');
136 assert.deepEqual([lim.what, lim.got, lim.max], ['pack objects', 1, 0]);
137 errOf(parsePack(pack.subarray(0, pack.length - 1)), 'pack_corrupt');
138 errOf(applyDelta(new Uint8Array(0), Uint8Array.of(0, 1, 0x81), { maxResult: -1 }), 'bad_request', 'limits.maxDeltaResult must be a finite non-negative number');
139 errOf(resolvePack(unwrap(parsePack(pack)), undefined, withLimits({ maxDeltaChain: null })), 'bad_request', 'limits.maxDeltaChain must be a finite non-negative number');
140 // a key left out (or passed as the missing value) is the default (#156 parseLimits)
141 assert.equal(resolvePack(unwrap(parsePack(pack)), undefined, withLimits({ maxDeltaChain: undefined })).ok, true);
142});
144test('applyDelta: a delta cut inside a copy opcode is a short read at its end, whichever byte is missing', () => {
145 const base = new Uint8Array(300).fill(7);
146 // copy off=0x0102 len=0x0201: opcode, two offset bytes, two length bytes
147 const op = Uint8Array.of(0x80 | 0x01 | 0x02 | 0x10 | 0x20, 0x02, 0x01, 0x01, 0x02);
148 const head = Uint8Array.of(0xac, 0x02, 0x80, 0x04); // base 300, result 512
149 for (let cut = 1; cut < op.length; cut++) {
150 const d = concat([head, op.subarray(0, cut)]);
151 assert.equal(errOf(applyDelta(base, d), 'pack_corrupt', `short read: need 1 bytes at ${d.length}, have ${d.length}`).reason, 'short_read');
152 }
153});
155test('ofs-delta offsets are capped like size varints: an endless continuation is corrupt within ten bytes', () => {
156 const endless = new Uint8Array(4096).fill(0x80);
157 errOf(readOfsDelta(endless, 0), 'pack_corrupt', 'pack: ofs delta offset longer than 64 bits');
158 // ten bytes is the most a cap of 64 bits reads; the tenth may end it
159 const ten = new Uint8Array(10).fill(0x80); ten[9] = 1;
160 assert.equal(readOfsDelta(ten, 0).ok, true);
161 const eleven = new Uint8Array(11).fill(0x80); eleven[10] = 1;
162 errOf(readOfsDelta(eleven, 0), 'pack_corrupt', 'pack: ofs delta offset longer than 64 bits');
163});
165test('parsePack: the six pack types are accepted and 0 and 5 refused, by kind', async () => {
166 const { pack } = await buildPack([{ type: TYPE.BLOB, data: te.encode('x') }]);
167 for (const t of [0, 5]) {
168 const p = Uint8Array.from(pack);
169 p[PACK_HEADER_BYTES] = (p[PACK_HEADER_BYTES] & 0x8f) | (t << 4);
170 errOf(parsePack(p, { verify: false }), 'pack_corrupt', `pack: bad object type ${t} at ${PACK_HEADER_BYTES}`);
171 }
172 const objs = [TYPE.COMMIT, TYPE.TREE, TYPE.BLOB, TYPE.TAG].map(type => ({ type, data: te.encode('') }));
173 const all = await buildPack([...objs, { type: TYPE.BLOB, data: Uint8Array.of(0, 0), base: { tag: 'ref', oid: hashObject('tag', new Uint8Array(0)) } }]);
174 const r = parsePack(all.pack);
175 assert.equal(r.ok, true);
176 assert.deepEqual(r.value.stats.byType, { commit: 1, tree: 1, blob: 1, tag: 1, ref_delta: 1 });
177 assert.deepEqual([r.value.stats.full, r.value.stats.refDelta, r.value.stats.ofsDelta], [4, 1, 0]);
178});
180test('resolvePack parses a hand-built object at its edge: a type, a base, and data, or bad_request before hashing', () => {
181 const data = te.encode('x');
182 const at = PACK_HEADER_BYTES;
183 errOf(resolvePack({ objects: [{ offset: at, type: 5, size: data.length, compressed: 0, data }] }), 'bad_request', `resolvePack: object at ${at} has type 5 and no base`);
184 // a delta type with no base is not a stored object either; a stored type with an explicit none base is one
185 errOf(resolvePack({ objects: [{ offset: at, type: TYPE.OFS_DELTA, data, base: { tag: 'none' } }] }), 'bad_request', `resolvePack: object at ${at} has type 6 and no base`);
186 const one = unwrap(resolvePack({ objects: [{ offset: at, type: TYPE.BLOB, data, base: { tag: 'none' } }] }));
187 const r = one.objects.get(hashObject('blob', data));
188 assert.deepEqual({ type: r.type, size: r.size, compressed: r.compressed, packBase: r.packBase, chain: r.chain }, { type: TYPE.BLOB, size: 1, compressed: 0, packBase: { tag: 'none' }, chain: 0 });
189 errOf(resolvePack({ objects: [{ offset: at, type: TYPE.BLOB, data, base: { tag: 'odd' } }] }), 'bad_request', `resolvePack: object at ${at} has base tag "odd"`);
190 errOf(resolvePack({ objects: [{ offset: at, type: TYPE.BLOB, data, base: {} }] }), 'bad_request', `resolvePack: object at ${at} has base tag undefined`);
191 for (const offset of [-1, 1.5, Number.NaN, 2 ** 53]) {
192 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.OFS_DELTA, data, base: { tag: 'ofs', offset } }] }), 'bad_request', `resolvePack: object at 40 has ofs base ${offset}`);
193 }
194 for (const oid of ['A'.repeat(40), 'a'.repeat(39), 7]) {
195 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.REF_DELTA, data, base: { tag: 'ref', oid } }] }), 'bad_request', `resolvePack: object at 40 has ref base ${JSON.stringify(oid)}, not an object id`);
196 }
197 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.BLOB, data: 'x' }] }), 'bad_request', 'resolvePack: object at 40 has no data');
198 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.BLOB }] }), 'bad_request', 'resolvePack: object at 40 has no data');
199 // the offset 0 and the largest safe one are offsets (a base that is not an object there is the resolver's corrupt)
200 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.OFS_DELTA, data, base: { tag: 'ofs', offset: 0 } }] }), 'pack_corrupt', 'pack: ofs delta base not at object boundary 0');
201 errOf(resolvePack({ objects: [{ offset: 40, type: TYPE.OFS_DELTA, data, base: { tag: 'ofs', offset: 2 ** 53 - 1 } }] }), 'pack_corrupt', `pack: ofs delta base not at object boundary ${2 ** 53 - 1}`);
202 // a left-out size is the data's length, a left-out compressed count 0; given ones are kept
203 const sized = unwrap(resolvePack({ objects: [{ offset: at, type: TYPE.BLOB, data, size: 9, compressed: 4 }] })).objects.get(hashObject('blob', data));
204 assert.deepEqual([sized.size, sized.compressed], [9, 4]);
205 // a REF base as a plain lowercase string is parsed into the Oid the lookup is asked for
206 const asked = [];
207 const base = te.encode('base');
208 const delta = makeDelta(base, te.encode('based')).delta;
209 const id = hashObject('blob', base);
210 const thin = unwrap(resolvePack({ objects: [{ offset: at, type: TYPE.REF_DELTA, data: delta, base: { tag: 'ref', oid: id } }] }, o => { asked.push(o); return { type: TYPE.BLOB, data: base }; }));
211 assert.deepEqual(asked, [id]);
212 assert.deepEqual([...thin.objects.values()].map(o => [o.type, o.packBase]), [[TYPE.BLOB, { tag: 'ref', oid: id }]]);
213});
215test('resolvePack reads each object once at its edge', async () => {
216 const { pack } = await buildPack(Array.from({ length: 5 }, (_, i) => ({ type: TYPE.BLOB, data: te.encode(`blob ${i}`) })));
217 const parsed = unwrap(parsePack(pack));
218 let reads = 0;
219 // count reads of `data` on full objects: hashing reads it once; a second hash would read it again
220 const counted = { ...parsed, objects: parsed.objects.map(o => { let d = o.data; return Object.defineProperty({ ...o }, 'data', { get() { reads++; return d; }, enumerable: true }); }) };
221 const r = unwrap(resolvePack(counted));
222 assert.equal(r.objects.size, 5);
223 assert.equal(reads, 5, 'each object: one read, where the resolver parses its input (the entry holds the bytes from there)');
224});
226// ---- tables and codecs held to their definitions ----
228test('the deflate length/distance tables are RFC 1951 §3.2.5, as printed', () => {
229 assert.deepEqual(DEFLATE_TABLES.LBASE, [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]);
230 assert.deepEqual(DEFLATE_TABLES.LEXT, [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]);
231 assert.deepEqual(DEFLATE_TABLES.DBASE, [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]);
232 assert.deepEqual(DEFLATE_TABLES.DEXT, [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]);
233});
235test('inflate: non-overlapping and overlapping matches, at every distance/length relation, equal zlib', () => {
236 for (const len of [3, 4, 10, 257, 258]) {
237 for (const d of [1, 2, len - 1, len, len + 1, 300]) {
238 if (d < 1) continue;
239 const seed = Uint8Array.from({ length: d }, (_, i) => (i * 37 + 11) & 0xff);
240 const data = concat([seed, new Uint8Array(len).map((_, i) => seed[i % d])]);
241 const z = zlib.deflateSync(data, { level: 9 });
242 const got = unwrap(inflate(z, 0, { expectedSize: data.length })).out;
243 assert.equal(Buffer.compare(Buffer.from(got), Buffer.from(data)), 0, `len ${len} distance ${d}`);
244 }
245 }
246});
248test('fromHex: the table reads every pair parseInt reads, and garbage pairs as parseInt did', () => {
249 const ref = h => { const o = new Uint8Array(h.length >> 1); for (let i = 0; i < o.length; i++) o[i] = Number.parseInt(h.slice(i * 2, i * 2 + 2), 16); return o; };
250 const chars = '0123456789abcdefABCDEFgxz -+ Āİ0';
251 for (const a of chars) for (const b of chars) assert.deepEqual(fromHex(a + b), ref(a + b), JSON.stringify(a + b));
252 for (let n = 0; n < 256; n++) assert.deepEqual(fromHex(n.toString(16).padStart(2, '0')), Uint8Array.of(n));
253 assert.deepEqual(fromHex('abc'), Uint8Array.of(0xab), 'an odd last digit is dropped');
254 const id = 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3';
255 assert.equal(toHex(fromHex(id)), id);
256 assert.equal(toHex(fromHex(id.toUpperCase())), id);
257 for (let c = 0; c < 256; c++) {
258 const want = /[0-9a-f]/i.test(String.fromCharCode(c)) ? Number.parseInt(String.fromCharCode(c), 16) : -1;
259 assert.equal(HEXV[c], want, `HEXV[${c}]`);
260 }
261});
263// ---- #84: bytes.ts survivors, killed by an independent oracle (the platform's strict UTF-8 decoder) ----
265const strictDec = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
266const looseDec = new TextDecoder('utf-8', { ignoreBOM: true });
267const memo = new Map();
268// The one character these bytes are as UTF-8, or null (memoised: the decoder signals ill-formed bytes by throwing).
269function oneChar(bytes) {
270 const k = bytes.join();
271 if (!memo.has(k)) {
272 let v = null;
273 try { const t = strictDec.decode(bytes); if ([...t].length === 1) v = t; } catch { /* ill-formed */ }
274 memo.set(k, v);
275 }
276 return memo.get(k);
277}
278// PEP 383 surrogateescape, by the platform's decoder: at each byte, the shortest prefix that decodes to one code point is
279// a character; a byte that starts none is escaped as U+DC00 + byte.
280function refDecode(b) {
281 let s = '';
282 for (let i = 0; i < b.length;) {
283 let took = 0;
284 for (let n = 1; n <= 4 && i + n <= b.length && !took; n++) {
285 const t = oneChar(b.subarray(i, i + n));
286 if (t !== null) { s += t; took = n; }
287 }
288 if (!took) { s += String.fromCharCode(0xdc00 + b[i]); took = 1; }
289 i += took;
290 }
291 return s;
292}
293const EDGES = [0x00, 0x41, 0x7f, 0x80, 0x8f, 0x90, 0x9f, 0xa0, 0xbf, 0xc0, 0xc1, 0xc2, 0xdf, 0xe0, 0xed, 0xef, 0xf0, 0xf4, 0xf5, 0xff];
295test('#84 decodeBytes is surrogateescape over every lead byte and boundary continuation, and encodeBytes inverts it', () => {
296 for (let lead = 0; lead < 256; lead++) {
297 for (const b1 of EDGES) for (const b2 of [0x7f, 0x80, 0xbf, 0xc0]) for (const b3 of [0x80, 0xbf, 0x41]) {
298 // an ill-formed byte on either side keeps the strict fast path out, so the escape loop decides every byte
299 for (const b of [Uint8Array.of(lead, b1, b2, b3, 0xff), Uint8Array.of(0xff, lead, b1, b2, b3), Uint8Array.of(0xff, lead, b1)]) {
300 const s = decodeBytesOf(b);
301 if (s !== refDecode(b)) assert.fail(`decodeBytes(${[...b].map(x => x.toString(16)).join(' ')}) = ${JSON.stringify(s)}, want ${JSON.stringify(refDecode(b))}`);
302 const back = encodeBytes(s);
303 if (!back.ok || Buffer.compare(Buffer.from(back.value), Buffer.from(b)) !== 0) assert.fail(`encodeBytes does not invert decodeBytes on ${[...b].map(x => x.toString(16)).join(' ')}`);
304 }
305 }
306 }
307});
309test('#84 encodeBytes: surrogate pairs at the edges are characters, lone ones outside the escape range are refused', () => {
310 const ok = (s, bytes) => assert.deepEqual([...encodeBytes(s).value], bytes, JSON.stringify(s));
311 ok('\udce9', [0xf4, 0x8f, 0xbf, 0xbf, 0xe9]); // U+10FFFF, the last pair, beside an escape
312 ok('𐀀\udc80', [0xf0, 0x90, 0x80, 0x80, 0x80]);
313 ok('\udc80\udcff', [0x80, 0xff]);
314 const lone = (s, u) => assert.equal(errOf(encodeBytes(s, 'name'), 'bad_request', `name: lone surrogate U+${u} is no byte sequence`).what, 'name');
315 lone('\ud800', 'D800'); // a high surrogate followed by a high one is no pair
316 lone('\ud800', 'D800'); // nor followed by a code unit past the low surrogates (U+E000)
317 lone('\udc7f', 'DC7F');
318 lone('\udd00', 'DD00');
319 lone('\udbff', 'DBFF');
320 assert.equal(errOf(encodeBytes('\udcc3\udca9', 'name'), 'bad_request', 'name: escaped bytes spell well-formed UTF-8; pass the characters').what, 'name');
321 assert.equal(errOf(encodeBytes(null), 'bad_request', 'string must be a string').what, 'string');
322});
324test('#84 decodeChunk: any split of any byte stream gives the whole stream\'s text, and carries only an unfinished sequence', () => {
325 const need = c => (c >= 0xf0 ? 4 : c >= 0xe0 ? 3 : 2);
326 const streams = [];
327 for (const lead of EDGES) for (let n = 0; n <= 4; n++) for (const cont of [0x80, 0x82, 0xbf, 0x41]) streams.push(Uint8Array.of(0x61, lead, ...new Array(n).fill(cont)));
328 streams.push(te.encode('é€😀a'), Uint8Array.of(0xe2, 0x80, 0x80, 0xf0, 0x9f, 0x98, 0x80));
329 for (const s of streams) {
330 for (let cut = 0; cut <= s.length; cut++) for (let cut2 = cut; cut2 <= s.length; cut2++) {
331 let carry = new Uint8Array(0);
332 let text = '';
333 for (const piece of [s.subarray(0, cut), s.subarray(cut, cut2), s.subarray(cut2)]) {
334 const r = decodeChunkOf(carry, piece);
335 text += r.text; carry = r.carry;
336 if (carry.length) {
337 const where = `${[...s].map(x => x.toString(16)).join(' ')} cut ${cut}/${cut2}`;
338 assert.ok(carry[0] >= 0xc2 && carry.length < need(carry[0]), `carry starts a sequence it has not finished: ${where}`);
339 assert.ok(carry.subarray(1).every(c => c >= 0x80 && c <= 0xbf), `carry holds only continuation bytes after its lead: ${where}`);
340 }
341 }
342 text += looseDec.decode(carry);
343 if (text !== looseDec.decode(s)) assert.fail(`split ${cut}/${cut2} of ${[...s].map(x => x.toString(16)).join(' ')}: ${JSON.stringify(text)}`);
344 }
345 }
346});
348test('pktFinish refuses only a carry; parsePack refuses an ofs delta naming itself', async () => {
349 assert.equal(pktFinish(pktInit()).ok, true);
350 assert.equal(pktFinish(unwrap(pktStep(pktInit(), te.encode('0000'))).state).ok, true);
351 // a one-object pack whose object is an OFS_DELTA with offset 0
352 const body = concat([te.encode('PACK'), Uint8Array.of(0, 0, 0, 2, 0, 0, 0, 1), Uint8Array.of(0x62, 0x00), zlib.deflateSync(Uint8Array.of(0, 0))]);
353 const sha = (await import('node:crypto')).createHash('sha1').update(body).digest();
354 errOf(parsePack(concat([body, sha])), 'pack_corrupt', `pack: ofs delta at ${PACK_HEADER_BYTES} is its own base`);
355});
357test('parsePack passes each part\'s Err through: object header, ofs offset, ref id, zlib body', () => {
358 const crypto = globalThis.process.getBuiltinModule('node:crypto');
359 const packOf = (...obj) => { const body = concat([te.encode('PACK'), Uint8Array.of(0, 0, 0, 2, 0, 0, 0, 1), ...obj]); return concat([body, crypto.createHash('sha1').update(body).digest()]); };
360 // an object header that never ends: the varint cap, not a TypeError on a missing header
361 errOf(parsePack(packOf(new Uint8Array(12).fill(0x80))), 'pack_corrupt', 'pack: size varint longer than 64 bits');
362 // an ofs delta whose offset never ends
363 errOf(parsePack(packOf(Uint8Array.of(0x62), new Uint8Array(12).fill(0x80))), 'pack_corrupt', 'pack: ofs delta offset longer than 64 bits');
364 // a ref delta whose base id runs into the trailer: the id is read (the trailer is 20 bytes, an id's length), and the
365 // object then has no zlib body before the end
366 errOf(parsePack(packOf(Uint8Array.of(0x72), new Uint8Array(4))), 'pack_corrupt');
367 // a blob whose zlib body is not zlib
368 errOf(parsePack(packOf(Uint8Array.of(0x32), Uint8Array.of(0x78, 0x00, 0, 0, 0, 0))), 'pack_corrupt', 'zlib: bad header check');
369});
371test('inflate converts only its own STOP: a bug inside the block loop propagates', () => {
372 // a plain array has no subarray: the stored block's copy throws a TypeError inside the loop, which is not an Err
373 assert.throws(() => inflate([0b001, 1, 0, 0xfe, 0xff, 0x41], 0, { raw: true }), TypeError);
374 // and a refusal is an Err, not a throw, from the same place
375 errOf(inflate(Uint8Array.of(0b001, 1, 0, 0xfe, 0xfe, 0x41), 0, { raw: true }), 'pack_corrupt', 'inflate: stored length check failed');
376 assert.equal(inflate(Uint8Array.of(0b001, 1, 0, 0xfe, 0xff, 0x41), 0, { raw: true }).value.out[0], 0x41);
377});
379test('readOfsDelta: an empty buffer is a short read, not offset 0', () => {
380 assert.equal(errOf(readOfsDelta(new Uint8Array(0), 0), 'pack_corrupt', 'short read: need 1 bytes at 0, have 0').reason, 'short_read');
381 assert.equal(errOf(readOfsDelta(Uint8Array.of(1, 2), 2), 'pack_corrupt', 'short read: need 1 bytes at 2, have 2').reason, 'short_read');
382});
384// The bit reader must not read a byte it does not need: a stored block drops the bits left in the current byte and reads
385// its LEN at the next byte boundary, so one byte read ahead is one byte lost. Every count of bits left over by the block
386// before it (8 here, by 0..7 nine-bit literals), against zlib.
387test('inflate: a stored block after a fixed one, at every leftover bit count, equals zlib', () => {
388 for (let k = 0; k < 8; k++) {
389 const bitsOut = [];
390 const put = (v, n) => { for (let i = 0; i < n; i++) bitsOut.push((v >> i) & 1); };
391 const putCode = (code, n) => { for (let i = n - 1; i >= 0; i--) bitsOut.push((code >> i) & 1); }; // Huffman codes MSB first
392 put(0, 1); put(1, 2); // not final, fixed
393 for (let i = 0; i < k; i++) putCode(0b110010000 + (0x90 - 144), 9); // literal 0x90: 9-bit code 110010000 + (lit - 144)
394 putCode(0, 7); // end of block (256): 7-bit 0000000
395 put(1, 1); put(0, 2); // final, stored
396 while (bitsOut.length % 8) bitsOut.push(0);
397 const head = Uint8Array.from({ length: bitsOut.length / 8 }, (_, i) => bitsOut.slice(i * 8, i * 8 + 8).reduce((a, b, j) => a | (b << j), 0));
398 const raw = concat([head, Uint8Array.of(2, 0, 0xfd, 0xff, 0x5a, 0x59)]);
399 const want = zlib.inflateRawSync(raw);
400 const got = inflate(raw, 0, { raw: true });
401 assert.equal(got.ok, true, `k=${k}: ${got.error?.message}`);
402 assert.equal(Buffer.compare(Buffer.from(got.value.out), want), 0, `k=${k}`);
403 assert.equal(got.value.consumed, raw.length, `k=${k}`);
404 }
405});
407// ---- inflate: a stream cut exactly where each read of the block loop runs out ----
408// Every read in the block loop (the bit reader and the Huffman decoder) is checked where it is made. Without that check
409// the loop goes on with a negative value: the stream may still be refused, but with another message (a bad block type,
410// bad dynamic counts, a repeat with no previous, a repeat overflow, no end-of-block code, a distance too far back), or
411// not at all (a distance symbol that is not there reads as zero bytes). Each stream below is hand-built (stored, fixed
412// and dynamic blocks) and ends at one read with fewer bits left than that read needs, or with bits that match no code.
414// LSB-first bit writer (RFC 1951 §3.1.1): header fields and extra bits in value order, Huffman codes MSB first.
415class DeflateBits {
416 constructor() { this.out = []; }
417 bits(v, n) { for (let i = 0; i < n; i++) this.out.push((v >> i) & 1); return this; }
418 code([c, len]) { for (let i = len - 1; i >= 0; i--) this.out.push((c >> i) & 1); return this; }
419 // the bits left in the last byte after what was written
420 get left() { return (8 - (this.out.length % 8)) % 8; }
421 // as bytes; a partial last byte is padded with `pad`, LSB first
422 done(pad = 0) {
423 const bits = [...this.out];
424 for (let i = 0; bits.length % 8; i++) bits.push((pad >> i) & 1);
425 return Uint8Array.from({ length: bits.length / 8 }, (_, k) => bits.slice(k * 8, k * 8 + 8).reduce((a, b, i) => a | (b << i), 0));
426 }
427}
428// Canonical Huffman codes from code lengths (RFC 1951 §3.2.2): symbol -> [code, length], null for a symbol with none.
429function canonicalCodes(lengths) {
430 const max = Math.max(0, ...lengths);
431 const count = new Array(max + 1).fill(0);
432 for (const l of lengths) if (l) count[l]++;
433 const next = [];
434 for (let code = 0, bits = 1; bits <= max; bits++) { code = (code + count[bits - 1]) << 1; next[bits] = code; }
435 return lengths.map(l => (l ? [next[l]++, l] : null));
436}
437const FIXED_LIT = canonicalCodes(Array.from({ length: 288 }, (_, s) => (s < 144 ? 8 : s < 256 ? 9 : s < 280 ? 7 : 8)));
438const FIXED_DIST = canonicalCodes(new Array(30).fill(5));
439const CLORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
440// A final dynamic block's header through the code length code lengths (HCLEN covers the last one given); returns that code.
441function dynamicHeader(w, { hlit, hdist, clens }) {
442 let hclen = 19;
443 while (hclen > 4 && !clens[CLORDER[hclen - 1]]) hclen--;
444 w.bits(1, 1).bits(2, 2).bits(hlit, 5).bits(hdist, 5).bits(hclen - 4, 4);
445 for (let i = 0; i < hclen; i++) w.bits(clens[CLORDER[i]] ?? 0, 3);
446 return canonicalCodes(Array.from({ length: 19 }, (_, s) => clens[s] ?? 0));
447}
448const fixedBlock = final => new DeflateBits().bits(final, 1).bits(1, 2);
450test('inflate: a stream cut at each read of the block loop is refused with that read\'s message', () => {
451 const END = 'inflate: unexpected end of input';
452 const NO_CODE = 'inflate: invalid huffman code';
453 const refused = (name, w, left, pad, message) => {
454 assert.equal(w.left, left, `${name}: bits left in the last byte`);
455 const bytes = w.done(pad);
456 errOf(inflate(bytes, 0, { raw: true }), 'pack_corrupt', message);
457 assert.throws(() => zlib.inflateRawSync(bytes), Error, `${name}: zlib refuses it too`);
458 };
459 // BFINAL: a non-final empty stored block, then nothing
460 errOf(inflate(Uint8Array.of(0b000, 0, 0, 0xff, 0xff), 0, { raw: true }), 'pack_corrupt', END);
461 // BTYPE: a non-final fixed block of four 9-bit literals (3 + 36 + 7 = 46 bits): BFINAL takes one of the 2 bits left
462 {
463 const w = fixedBlock(0);
464 for (const c of [0x90, 0x91, 0x92, 0x93]) w.code(FIXED_LIT[c]);
465 refused('btype', w.code(FIXED_LIT[256]), 2, 0b01, END);
466 }
467 // HDIST: after a 28-bit fixed block, BFINAL, BTYPE and HLIT (31, a count no stream may give) leave 4 bits: HDIST needs
468 // 5 (HCLEN, 4, would be there)
469 refused('hdist', fixedBlock(0).code(FIXED_LIT[0x90]).code(FIXED_LIT[0x91]).code(FIXED_LIT[256]).bits(1, 1).bits(2, 2).bits(31, 5), 4, 0b1111, END);
470 // HCLEN: BFINAL, BTYPE, HLIT (31) and HDIST end on bit 13 of 16: HCLEN needs 4 of the 3 left
471 refused('hclen', new DeflateBits().bits(1, 1).bits(2, 2).bits(31, 5).bits(0, 5), 3, 0b111, END);
472 // a code length code length: 16 and 17 (one bit each) are read, the third needs 3 bits and 1 is left
473 refused('clen', new DeflateBits().bits(1, 1).bits(2, 2).bits(0, 5).bits(0, 5).bits(0, 4).bits(1, 3).bits(1, 3), 1, 0, END);
474 // a code length symbol: 266 zeros (138 + 128, entry 256 among them) of 271 entries; the 3 padding bits are three more
475 // ('0' is symbol 0), then the next symbol runs out
476 {
477 const w = new DeflateBits();
478 const cl = dynamicHeader(w, { hlit: 13, hdist: 0, clens: { 0: 1, 18: 1 } });
479 refused('code length', w.code(cl[18]).bits(127, 7).code(cl[18]).bits(117, 7), 3, 0, END);
480 }
481 // a code length symbol that matches no code: the code length code has no codes at all, the one incomplete
482 // code-length set zlib builds a table for (inftrees.c: "no symbols, but wait for decoding to report error"; a single
483 // code is `invalid code lengths set` at the header); fifteen 1 bits
484 {
485 const w = new DeflateBits();
486 dynamicHeader(w, { hlit: 0, hdist: 0, clens: {} });
487 refused('code length, no code', w.bits(0x7fff, 15), 4, 0, NO_CODE);
488 }
489 // a repeat's extra bits: 249 zeros (138 + 111) of 258 entries, then an 18 whose 7 extra bits are not there
490 {
491 const w = new DeflateBits();
492 const cl = dynamicHeader(w, { hlit: 0, hdist: 0, clens: { 0: 1, 18: 1 } });
493 refused('repeat', w.code(cl[18]).bits(127, 7).code(cl[18]).bits(100, 7).code(cl[18]), 2, 0, END);
494 }
495 // a length's extra bits: a dynamic block (literal/length 256: '0', 281: '1'; distances 0: '0', 1: '1') whose first
496 // symbol is 281 (5 extra bits); 4 are left, the first of them a distance code
497 {
498 const w = new DeflateBits();
499 const cl = dynamicHeader(w, { hlit: 25, hdist: 1, clens: { 1: 1, 18: 1 } });
500 w.code(cl[18]).bits(127, 7).code(cl[18]).bits(107, 7); // 0..255: 256 zeros
501 w.code(cl[1]); // 256
502 w.code(cl[18]).bits(13, 7); // 257..280: 24 zeros
503 w.code(cl[1]).code(cl[1]).code(cl[1]); // 281, distances 0 and 1
504 const lit = canonicalCodes(Array.from({ length: 282 }, (_, s) => (s === 256 || s === 281 ? 1 : 0)));
505 refused('length extra', w.code(lit[281]), 4, 0, END);
506 }
507 // a distance symbol: fixed, two 9-bit literals and length 3 (3 + 18 + 7 = 28 bits): 4 of its 5 bits are left
508 refused('distance', fixedBlock(1).code(FIXED_LIT[0x90]).code(FIXED_LIT[0x91]).code(FIXED_LIT[257]), 4, 0, END);
509 // a distance symbol that matches no code: fixed distance codes 30 and 31 do not exist (11111 and ten more 1 bits)
510 refused('distance, no code', fixedBlock(1).code(FIXED_LIT[0x41]).code(FIXED_LIT[257]).bits(0x7fff, 15), 7, 0, NO_CODE);
511 // a distance's extra bits: one 9-bit literal, length 3, distance symbol 4 (one extra bit): 3 + 9 + 7 + 5 = 24 bits
512 refused('distance extra', fixedBlock(1).code(FIXED_LIT[0x90]).code(FIXED_LIT[257]).code(FIXED_DIST[4]), 0, 0, END);
513});
515// ---- the 0.2.0 cut-over: what the former throwing wrappers refused is an Err, named for what was encoded or parsed ----
517test('a string with no byte form is refused naming the field: commit, tag, tree entry name, ref, path, message, checkout path', async () => {
518 const { encodeCommit, encodeTag, encodeTree } = await import('../src/objects.js');
519 const { validateRef, validatePath, validateMessage, checkoutPathRefusal } = await import('../src/validate.js');
520 const { utf8ToStr } = await import('../src/attributes.js');
521 const lone = what => `${what}: lone surrogate U+D800 is no byte sequence`;
522 const tree = '1'.repeat(40);
523 errOf(encodeCommit({ tree, author: 'a <a@b> 0 +0000', message: '\ud800' }), 'bad_request', lone('commit'));
524 errOf(encodeTag({ object: tree, type: 'commit', tag: 'v1', tagger: 'a <a@b> 0 +0000', message: '\ud800' }), 'bad_request', lone('tag'));
525 errOf(encodeTree([{ mode: '100644', name: '\ud800', oid: tree }]), 'bad_request', lone('tree entry name'));
526 errOf(validateRef('refs/heads/\ud800'), 'bad_request', lone('ref'));
527 errOf(validatePath('a/\ud800', withLimits()), 'bad_request', lone('path'));
528 errOf(validateMessage('m\ud800'), 'bad_request', lone('message'));
529 errOf(checkoutPathRefusal('a/\ud800', '100644', 'linux'), 'bad_request', lone('checkout path'));
530 // a tree name's escaped bytes are matched as those bytes (one char per byte); a lone surrogate has none
531 assert.deepEqual(utf8ToStr('a\udce9/é'), { ok: true, value: 'a\xe9/\xc3\xa9' });
532 errOf(utf8ToStr('\ud800'), 'bad_request', lone('path'));
533});
535test('treeSort: the first name in the caller\'s order that has no byte form is the refusal, compared or not', async () => {
536 const { treeSort } = await import('../src/objects.js');
537 const e = (name, mode = '100644') => ({ mode, name });
538 assert.deepEqual(treeSort([e('b'), e('a', '40000'), e('a.c')]), { ok: true, value: [e('a.c'), e('a', '40000'), e('b')] });
539 // Deliberate change (perf/treesort-reencodes-keys-per-comparison): treeSort computes each key once, before the sort,
540 // so every name is encoded. One entry used to be returned unencoded because the sort never compared it; it is now
541 // refused like any other name with no byte form, which is what encodeTree does with the same entry.
542 errOf(treeSort([e('\ud800')]), 'bad_request', 'tree entry name: lone surrogate U+D800 is no byte sequence');
543 // the oracle: walk the entries in the order given and stop at the first name that cannot be encoded. It was "the
544 // first name a throwing comparator meets", which depended on the order the engine's sort compares in.
545 const thrownBy = entries => {
546 try {
547 for (const a of entries) unwrap(encodeBytes(a.mode === '40000' ? `${a.name}/` : a.name));
548 } catch (x) { return x.message; }
549 return null;
550 };
551 for (const entries of [[e('\ud800'), e('a')], [e('a'), e('\ud801')], [e('\ud800'), e('\ud801')], [e('\ud801'), e('\ud800')], [e('z'), e('\ud802'), e('a'), e('\ud803', '40000')]]) {
552 const want = thrownBy(entries);
553 assert.match(want, /^string: lone surrogate U\+D80[0-3] is no byte sequence$/);
554 errOf(treeSort(entries), 'bad_request', want.replace(/^string/, 'tree entry name'));
555 }
556});
558test('packParseOpts parses limits and reads the two pack caps out of them; a key left out is the default', async () => {
559 const { packParseOpts } = await import('../src/packresolve.js');
560 const limits = withLimits({ maxPackObjects: 3, maxInflatedTotal: 7 });
561 assert.deepEqual(packParseOpts(limits), { ok: true, value: { limits, maxObjects: 3, maxInflatedTotal: 7 } });
562 assert.deepEqual(unwrap(packParseOpts({})), { limits: { ...DEFAULT_LIMITS }, maxObjects: DEFAULT_LIMITS.maxPackObjects, maxInflatedTotal: DEFAULT_LIMITS.maxInflatedTotal });
563 assert.deepEqual(unwrap(packParseOpts({ maxPackObjects: 1 })).maxObjects, 1);
564 errOf(packParseOpts({ maxPackObjects: -1 }), 'bad_request', 'limits.maxPackObjects must be a finite non-negative number');
565 errOf(packParseOpts({ maxPackObjects: 1, maxInflatedTotal: NaN }), 'bad_request', 'limits.maxInflatedTotal must be a finite non-negative number');
566 // a key the parser does not read is ignored
567 assert.equal(packParseOpts({ maxPackObjects: 1, notALimit: NaN }).ok, true);
568});
570test('parseRefname, parseBranchName, parseTagName, parsePath: the refusal names what was parsed; the validator\'s error is its cause', async () => {
571 const { parseRefname, parseBranchName, parseTagName } = await import('../src/refname.js');
572 const { parsePath } = await import('../src/path.js');
573 const full = 'invalid ref: must be a full ref name starting with refs/';
574 for (const [r, what, message] of [
575 [parseRefname('main'), 'ref', full],
576 [parseBranchName('main'), 'branch', full],
577 // a branch is checked as a ref a push creates: one level after refs/ is receive-pack's funny refname
578 [parseBranchName('refs/heads'), 'branch', 'invalid ref: receive-pack refuses it (funny refname)'],
579 [parseTagName('v1'), 'tag', full],
580 [parsePath(''), 'path', 'invalid path: must be a non-empty string'],
581 ]) {
582 const x = errOf(r, 'bad_request', message);
583 assert.equal(x.what, what, message);
584 assert.equal(x.cause.code, 'bad_request');
585 assert.equal(x.cause.message, message);
586 }
587 // a valid ref outside the namespace is the parser's own refusal, with no cause
588 for (const [r, what, message] of [
589 [parseBranchName('refs/tags/v1.2.3'), 'branch', 'must start with refs/heads/'],
590 [parseTagName('refs/heads/main'), 'tag', 'must start with refs/tags/'],
591 ]) {
592 const x = errOf(r, 'bad_request', message);
593 assert.equal(x.what, what, message);
594 assert.equal(x.cause, undefined);
595 }
596});
598test('planDeltas gives each delta its tagged base and counts them: ofs against an earlier object, ref against a thin base', async () => {
599 const { planDeltas } = await import('../src/delta.js');
600 const v1 = new TextEncoder().encode(Array.from({ length: 40 }, (_, i) => `line ${i}\n`).join(''));
601 const v2 = new TextEncoder().encode(Array.from({ length: 40 }, (_, i) => `line ${i === 7 ? 'x' : i}\n`).join(''));
602 const p = planDeltas([{ type: TYPE.BLOB, data: v1, path: 'a' }, { type: TYPE.BLOB, data: v2, path: 'a' }]);
603 assert.deepEqual([p.ofs, p.ref, p.objects[0].base, p.objects[1].base], [1, 0, undefined, { tag: 'ofs', index: 0 }]);
604 const base = hashObject('blob', v1);
605 const thin = planDeltas([{ type: TYPE.BLOB, data: v2, path: 'a' }], { thinBase: () => ({ oid: base, data: v1 }) });
606 assert.deepEqual([thin.ofs, thin.ref, thin.objects[0].base], [0, 1, { tag: 'ref', oid: base }]);
607 const whole = planDeltas([{ type: TYPE.BLOB, data: v2, path: 'a' }]);
608 assert.deepEqual([whole.ofs, whole.ref, whole.objects[0].base], [0, 0, undefined]);
609});
611// #299 item 5, in a MUTATION_FILES file: decodeSavedIndex's readHeader holds the header count to maxPackObjects
612// (`n <= maxObjects`). A pack of three blobs built here (count 3 by construction) is limit_exceeded under 2 (got 3,
613// max 2) and decodes under 3 (the bound itself); a header claiming 1,000,000 entries is refused under the default,
614// which the README states as maxPackObjects 100_000.
615test('decodeSavedIndex: header count over maxPackObjects is limit_exceeded, at it decodes, default 100_000', async () => {
616 const { scanPackIndex } = await import('../src/packscanindex.js');
617 const { encodeSavedIndex, decodeSavedIndex } = await import('../src/packsaved.js');
618 const { pack } = await buildPack(['a', 'b', 'c'].map(s => ({ type: TYPE.BLOB, data: te.encode(s) })));
619 const scanned = unwrap(await scanPackIndex((async function* () { yield pack; })(), {}));
620 const bytes = unwrap(encodeSavedIndex(scanned, { tag: 'none' }));
621 const id = { trailer: toHex(pack.subarray(pack.length - 20)), bytes: pack.length };
622 assert.deepEqual(decodeSavedIndex(bytes, id, 64, 2), { ok: false, error: { tag: 'limit_exceeded', what: 'pack objects', got: 3, max: 2 } });
623 const at = decodeSavedIndex(bytes, id, 64, 3);
624 assert.equal(at.ok, true);
625 assert.equal(at.value.pack.count, 3);
626 const head = Uint8Array.from(bytes.subarray(0, 84));
627 new DataView(head.buffer).setUint32(16, 1_000_000, true);
628 assert.deepEqual(decodeSavedIndex(head, id), { ok: false, error: { tag: 'limit_exceeded', what: 'pack objects', got: 1_000_000, max: 100_000 } });
629});
631// #310 review (1): loadTransferPack parses scan.limits before anything is read, as the scan parses them, so a limit that
632// does not parse is bad_request (the README contract) with or without a saved index, and never a throw. Mutating the
633// guard away (`if (false)`) reaches scanLimits.value on an Err and throws on the saved path.
634test('loadTransferPack: unparseable scan.limits are bad_request, saved index or not', async () => {
635 const { pack } = await buildPack([{ type: TYPE.BLOB, data: te.encode('x') }]);
636 const t = unwrap(createTransport('http://127.0.0.1:9/none.git', {}));
637 const first = unwrap(await loadTransferPack(t, rangeFromBytes(pack), none));
638 assert.equal(first.loaded.tag, 'scanned');
639 for (const saved of [none, some(first.saved)]) {
640 const bad = await loadTransferPack(t, rangeFromBytes(pack), saved, { scan: { limits: { maxPackObjects: -1 } } });
641 assert.equal(bad.ok, false, `saved ${saved.tag}`);
642 assert.equal(bad.error.code, 'bad_request');
643 assert.match(bad.error.message, /limits\.maxPackObjects/u);
644 }
645 assert.equal(unwrap(await loadTransferPack(t, rangeFromBytes(pack), some(first.saved))).loaded.tag, 'saved');
646});
648// ---- #299: a base id's REF run is applied once, and only a run that exists is marked done ----
649// Packs built by hand (gitformat-pack); identity deltas (source 6, result 6, copy 0..6) so every delta's id is its base's.
650const r299 = {
651 cat: ps => { const o = new Uint8Array(ps.reduce((a, p) => a + p.length, 0)); let at = 0; for (const p of ps) { o.set(p, at); at += p.length; } return o; },
652 hdr(type, size) { const out = []; let b = (type << 4) | (size & 15); size = Math.floor(size / 16); while (size > 0) { out.push(b | 0x80); b = size & 0x7f; size = Math.floor(size / 128); } out.push(b); return Uint8Array.from(out); },
653 ofs(d) { const out = [d & 127]; d = Math.floor(d / 128); while (d > 0) { d -= 1; out.unshift(128 | (d & 127)); d = Math.floor(d / 128); } return Uint8Array.from(out); },
654 // entries: { blob } | { ofs: index } | { ref: oid bytes }
655 build(entries) {
656 const delta = zlib.deflateSync(Uint8Array.of(6, 6, 0x90, 6));
657 const parts = []; const pos = []; let at = PACK_HEADER_BYTES;
658 for (const e of entries) {
659 pos.push(at);
660 const p = e.blob ? r299.cat([r299.hdr(3, e.blob.length), zlib.deflateSync(e.blob)]) : e.ofs !== undefined ? r299.cat([r299.hdr(6, 4), r299.ofs(at - pos[e.ofs]), delta]) : r299.cat([r299.hdr(7, 4), e.ref, delta]);
661 parts.push(p); at += p.length;
662 }
663 const c = entries.length;
664 const body = r299.cat([Uint8Array.of(0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2, (c >>> 24) & 255, (c >>> 16) & 255, (c >>> 8) & 255, c & 255), ...parts]);
665 return r299.cat([body, fromHex(new Sha1().update(body).hex())]);
666 },
667 async resolve(pack, opts) {
668 const idx = await scanPackIndex(new ReadableStream({ start(c) { c.enqueue(pack); c.close(); } }));
669 assert.ok(idx.ok);
670 return resolvePackRange(idx.value, rangeFromBytes(pack), opts);
671 },
672};
674test('#299: an OFS base with no REF run of its own leaves the next id\'s REF run alone (mixed pack, as git index-pack reads it)', async () => {
675 // blob a, an OFS delta on a, blob b, a REF delta on b, with id(a) < id(b): a's REF lookup lands on b's run, which a has
676 // no part in. Marking that run done from a refuses b's delta as unresolved.
677 let a = te.encode('aaaaa\n'); let b = te.encode('bbbbb\n');
678 if (hashObject('blob', a) > hashObject('blob', b)) [a, b] = [b, a];
679 const pack = r299.build([{ blob: a }, { ofs: 0 }, { blob: b }, { ref: fromHex(hashObject('blob', b)) }]);
680 const { root, cleanup } = tmpRoot();
681 try {
682 const repo = join(root, 'r.git');
683 git(root, ['init', '-q', '--bare', repo]);
684 const file = join(repo, 'objects', 'pack', 'pack-mixed.pack');
685 writeFileSync(file, pack);
686 git(repo, ['index-pack', file]);
687 const byOffset = gitBuf(repo, ['show-index'], readFileSync(file.replace(/\.pack$/u, '.idx'))).toString().trim().split('\n').map(l => l.split(' ')).toSorted((x, y) => Number(x[0]) - Number(y[0]));
688 const r = await r299.resolve(pack);
689 assert.ok(r.ok, r.ok ? '' : r.error.message);
690 assert.equal(r.value.count, 4);
691 for (let i = 0; i < 4; i++) assert.equal(r.value.oid(i).value, byOffset[i][1], `entry ${i}`);
692 // by hand: entry 1 rests on entry 0, entry 3 on entry 2, both one deep
693 assert.deepEqual([...r.value.columns.base], [NO_BASE, 0, NO_BASE, 2]);
694 assert.deepEqual([...r.value.columns.depth], [0, 1, 0, 1]);
695 } finally {
696 cleanup();
697 }
698});
700test('#299: a REF run already applied is not applied again from a later holder of its id (work pinned by hand)', async () => {
701 // blob B, k OFS identity deltas on B each with one OFS identity child, then m REF identity deltas on B's id. Every OFS
702 // result has B's id and children, so each is a parent whose REF lookup finds B's run; applying it again would visit the m
703 // REF deltas once per such parent. By hand: B inflated (6); each of the 2k + m deltas inflated (4), applied (6) and
704 // visited once (1).
705 const B = te.encode('hello\n');
706 const k = 100; const m = 100;
707 const es = [{ blob: B }];
708 for (let i = 0; i < k; i++) { es.push({ ofs: 0 }); es.push({ ofs: es.length - 1 }); }
709 for (let i = 0; i < m; i++) es.push({ ref: fromHex(hashObject('blob', B)) });
710 const r = await r299.resolve(r299.build(es));
711 assert.ok(r.ok, r.ok ? '' : r.error.message);
712 assert.equal(r.value.stats.work, 11 * (2 * k + m) + 6);
713});
715test('#299: a second holder of an id whose REF run is applied is not made again (reads and work pinned by hand)', async () => {
716 // blob B, the same blob B again, then one REF identity delta on B's id. The run rests on the first B; the second B has
717 // nothing left to give, so it is never read back. By hand: reads = the first B and the delta (2), no cache hit, work =
718 // B inflated (6) + the delta inflated (4), applied (6) and visited (1) = 17.
719 const B = te.encode('hello\n');
720 const r = await r299.resolve(r299.build([{ blob: B }, { blob: B }, { ref: fromHex(hashObject('blob', B)) }]));
721 assert.ok(r.ok, r.ok ? '' : r.error.message);
722 assert.deepEqual([r.value.stats.reads, r.value.stats.readBytes > 0, r.value.stats.cacheHits, r.value.stats.work], [2, true, 0, 17]);
723 assert.deepEqual([r.value.columns.base[2], r.value.columns.depth[2]], [0, 1]);
724});
726test('#299: the visit charge refuses at the visit, before the delta is read (got pinned by hand)', async () => {
727 // ceiling 6 = the blob: blob inflated (6), then its one child visited (1) = 7 > 6. Were the visit's refusal dropped, the
728 // delta's inflate would refuse instead, at 6 + 1 + 4 = 11.
729 const B = te.encode('hello\n');
730 const r = await r299.resolve(r299.build([{ blob: B }, { ref: fromHex(hashObject('blob', B)) }]), { limits: withLimits({ maxInflatedTotal: 6, maxResolveWork: 6 }) });
731 assert.equal(r.ok, false);
732 assert.deepEqual([r.error.code, r.error.what, r.error.got], ['limit_exceeded', 'resolve work bytes', 7]);
733});
735test('#299: a whole object whose REF lookup is empty is not made again (an id just below another\'s run; reads and work pinned by hand)', async () => {
736 // blob X with nothing resting on it, blob B, one REF identity delta on B, with id(X) < id(B). The scan hashes both
737 // blobs, so each root is made again only if it has kids. X's REF lookup is the empty range [0, 0) just below B's run at
738 // index 0: X has no kids, and reading index 0's done flag there would wrongly give it B's. By hand, head: B made again
739 // (1 read, 1 rebuilt, 6 inflated), the delta read (1 read, 4 inflated), applied (6) and visited (1): reads 2, inflated
740 // 10, rebuilt 1, work 6 + 4 + 6 + 1 = 17. Were X taken to have kids, X is made again too: reads 3, inflated 16,
741 // rebuilt 2, work 23.
742 let X = te.encode('xxxxx\n'); let B = te.encode('hello\n');
743 if (hashObject('blob', X) > hashObject('blob', B)) [X, B] = [B, X];
744 const r = await r299.resolve(r299.build([{ blob: X }, { blob: B }, { ref: fromHex(hashObject('blob', B)) }]));
745 assert.ok(r.ok, r.ok ? '' : r.error.message);
746 const st = r.value.stats;
747 assert.deepEqual([st.reads, st.inflated, st.rebuilt, st.work], [2, 10, 1, 17]);
748 assert.deepEqual([r.value.columns.base[2], r.value.columns.depth[2]], [1, 1]);
749});
Diagnostics6 git calls · 221 ms
CallStatusSizeTime0221 ms
discover200191 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=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 KiB3 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=11990 git.response.cache=UPDATING git.response.duration_ms=3 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=10Open pack map
fetchtree:0200919 B · 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=919 git.response.cache=HIT git.response.duration_ms=3 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · 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=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch20016 KiB210 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=210 git.response.bytes=16423 git.response.cache=MISS git.response.duration_ms=210 git.fetch.wants=7380ddf9f448a55d88116fdc8822ad58b0a73af9 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=5 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=16423 git.pkt.payload.max=8200 git.sideband.band1.bytes=16391 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
- a5e1c71bcfe2c6f8
- Request
- badf46c7…
- 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