main
1// Pull pack scan: bytes in as they arrive, one event out at a time: the header, each object's header, its inflated
2// chunks and its end, the trailer, the end. It keeps one inflate record (the window and one chunk, inflatestream.ts),
3// the unread input, and a 20-byte lag of the trailer hash — never an object (issue #172). Deltas come out as their
4// instruction bytes; resolvePack applies them for a caller that wants objects, from parsePack's buffered records.
5//
6// The same refusals as parsePack, in the same order, with the same caps: maxPackObjects on the header's count,
7// maxObjectSize on each declared size and maxInflatedTotal on the declared sizes plus LIMIT_OBJECT_OVERHEAD each, all
8// before an inflated byte exists. The trailer SHA-1 runs over the bytes as they are fed, 20 bytes behind, so the bytes
9// it has not hashed when the input closes are the trailer exactly when the trailer is where the objects end.
10// test/packstream.test.js holds the scan to parsePack's objects, trailer and error code for every chunking.
11//
12// That is the default budget, for a caller that keeps what it reads. A caller that drops every object can ask for a work
13// budget (workBudget): nothing is retained, so what a pack can cost is CPU, and it is charged as CPU: the bytes actually
14// inflated, against maxInflatedTotal or `ratio` times the pack bytes consumed so far, whichever is larger. A scan's memory
15// is peakBound('pack-scan') under either budget (streambound.ts). The check runs at each chunk's end, so a refusal comes
16// at most one chunk past the budget, and a pack within a chunk of its ratio can pass or refuse depending on how it is fed.
17import type { ErrorOf } from './errors.js';
18import { fail, plyError } from './errors.js';
19import type { Err, Result } from './result.js';
20import { ok, err, assertNever, getOrElse } from './result.js';
21import type { LimitsInput } from './limits.js';
22import { DEFAULT_LIMITS, LIMIT_OBJECT_OVERHEAD, LIMIT_WORK_RATIO, checkLimit, limitExceeded, parseLimits } from './limits.js';
23import type { Input } from './absent.js';
24import { atOr, fromNullish } from './absent.js';
25import { take, shortRead } from './bytes.js';
26import { Sha1, toHex, toOid } from './sha1.js';
27import type { Oid } from './oid.js';
28import type { ObjType } from './format.js';
29import type { InflateStats, InflateStream } from './inflatestream.js';
30export type { InflateStats };
31import { inflateInit, inflateReset, inflateFeed, inflatePull, inflateEnd, inflateUnread, inflateConsumed, declaredBound, zlibWrap } from './inflatestream.js';
32import type { Attrs } from './otel.js';
33import {
34 PACK_SIGNATURE, PACK_VERSION, PACK_HEADER_BYTES, PACK_TRAILER_BYTES, OID_BYTES, UINT32_BYTES, BE32_SHIFTS,
35 OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, OBJ_TAG, OBJ_OFS_DELTA, OBJ_REF_DELTA,
36 OBJ_TYPE_SHIFT, OBJ_TYPE_MASK, OBJ_SIZE_FIRST_MASK, OBJ_SIZE_NEXT_MASK, OBJ_CONTINUE,
37 OBJ_SIZE_NEXT_BASE, VARINT_BITS, VARINT_MAX_SHIFT, varintScale,
38} from './format.js';
40// Indexed reads below are inside a length the code has just checked; noUncheckedIndexedAccess cannot see that.
41/* oxlint-disable typescript/no-unsafe-type-assertion, typescript/non-nullable-type-assertion-style -- in-range indexed reads, see above */
43type Corrupt = ErrorOf<'pack_corrupt'>;
44// Everything a pack can be refused for: corrupt bytes, a limit, or a caller's limit that bounds nothing.
45export type PackError = Corrupt | ErrorOf<'limit_exceeded'> | ErrorOf<'bad_request'>;
46const corrupt = (msg: string): Err<Corrupt> => fail('pack_corrupt', msg);
47const short = (buf: Uint8Array, p: number): Err<Corrupt> => err(shortRead('pack_corrupt', p, 1, buf.length));
48// A read that may run past the end reads atOr(buf, p, NO_BYTE): NO_BYTE there is a short read.
49const NO_BYTE = -1;
51// ---- the header readers (one place: the scan, the single-object parser and the delta header read them) ----
52// Past VARINT_MAX_SHIFT a group used to scale by Infinity, and 0 × Infinity is NaN: every `size > limit` check is false
53// on NaN, so the size reached callers (#13). Checked before the byte is read, so a hostile header costs ≤ 10 bytes.
54const varintLong = (what: string): Err<Corrupt> => corrupt(`${what}: size varint longer than ${VARINT_MAX_SHIFT + 1} bits`);
56// Little-endian 7-bit groups from start, each with a continue bit: the delta header's sizes (from n = 0, shift = 0), and
57// the rest of an object header's size after a first byte that continues (from that byte's four bits, shift = 4).
58// @decoder readVarint prefix=varint
59export function readVarint(buf: Uint8Array, start: number, what: string, n0 = 0, shift0 = 0): Result<readonly [number, number], Corrupt> {
60 let p = start;
61 let n = n0;
62 let shift = shift0;
63 let c: number;
64 do {
65 if (shift > VARINT_MAX_SHIFT) return varintLong(what);
66 c = atOr(buf, p, NO_BYTE);
67 if (c === NO_BYTE) return short(buf, p);
68 p += 1;
69 n += (c & OBJ_SIZE_NEXT_MASK) * varintScale(shift);
70 shift += VARINT_BITS;
71 } while (c & OBJ_CONTINUE);
72 return ok([n, p]);
73}
75// Four bytes at off, big-endian, where the caller has checked they are there.
76const be32 = (b: Uint8Array, off: number): number =>
77 // oxlint-disable-next-line no-magic-numbers -- off + 2 is the third byte of the four BE32_SHIFTS places
78 (((b[off] as number) << (BE32_SHIFTS[0] as number)) | ((b[off + 1] as number) << (BE32_SHIFTS[1] as number)) | ((b[off + 2] as number) << (BE32_SHIFTS[2] as number)) | (b[off + UINT32_BYTES - 1] as number)) >>> 0;
79export function readUint32BE(buf: Uint8Array, off: number): Result<number, Corrupt> {
80 const r = take(buf, off, UINT32_BYTES);
81 return r.ok ? ok(be32(buf, off)) : r;
82}
84// The header's three type bits as one of the six pack types, or null for 0 and 5 (which name nothing).
85function objTypeOf(n: number): ObjType | null {
86 switch (n) {
87 case OBJ_COMMIT:
88 case OBJ_TREE:
89 case OBJ_BLOB:
90 case OBJ_TAG:
91 case OBJ_OFS_DELTA:
92 case OBJ_REF_DELTA:
93 return n;
94 default:
95 return null;
96 }
97}
98// An object's declared inflated size, checked against the reader's cap where the header was read (#156): a reader of
99// an ObjHeader holds a size no larger than the cap it asked for, before any inflated byte exists.
100declare const objSizeBrand: unique symbol;
101export type ObjSize = number & { readonly [objSizeBrand]: typeof objSizeBrand };
102const withinCap = (size: number, max: number): size is ObjSize => size <= max;
103// The size of an empty object, for a record's placeholder (0 is within every cap).
104// Stryker disable next-line all: a placeholder, see above
105const zeroSize = (): ObjSize => { const z = 0; return withinCap(z, z) ? z : zeroSize(); };
106// `type` is parsed here, once: a reader of an ObjHeader sees one of the six pack types and no other number.
107export type ObjHeader = { readonly type: ObjType; readonly size: ObjSize; readonly next: number };
108export type HeaderError = Corrupt | ErrorOf<'limit_exceeded'>;
109// No cap: a streamed blob (edit, read) is bounded by its own reader, not by maxObjectSize.
110export const NO_SIZE_CAP: number = Number.POSITIVE_INFINITY;
111// A header cut by the end of the buffer is a short read (`reason: 'short_read'`): a stream parser waits for more bytes
112// on that reason (fetch.ts spStep, the scan below), and refuses anything else. A whole header whose type is 0 or 5 is
113// `pack: bad object type <n> at <offset>`, after the size (a size varint past 64 bits is refused first, as it always
114// was); `offset` is the header's offset in the pack, which a stream parser's carry buffer does not start at. A size
115// past `maxSize` is limit_exceeded (`pack object size`), after the type.
116// @decoder readObjHeader prefix=obj-header
117export function readObjHeader(buf: Uint8Array, start: number, offset: number = start, maxSize: number = NO_SIZE_CAP): Result<ObjHeader, HeaderError> {
118 const c = atOr(buf, start, NO_BYTE);
119 if (c === NO_BYTE) return short(buf, start);
120 const bits = (c >> OBJ_TYPE_SHIFT) & OBJ_TYPE_MASK;
121 let size = c & OBJ_SIZE_FIRST_MASK;
122 let next = start + 1;
123 if (c & OBJ_CONTINUE) {
124 const rest = readVarint(buf, start + 1, 'pack', size, OBJ_TYPE_SHIFT);
125 if (!rest.ok) return rest;
126 [size, next] = rest.value;
127 }
128 const type = objTypeOf(bits);
129 if (type === null) return corrupt(`pack: bad object type ${bits} at ${offset}`);
130 if (!withinCap(size, maxSize)) return err(limitExceeded('pack object size', size, maxSize));
131 return ok({ type, size, next });
132}
134export type OfsDelta = { readonly off: number; readonly next: number };
135// The same ten-byte cap as the size varints (readObjHeader): git's index-pack refuses an offset that overflows
136// ("offset value overflow for delta base object"); past it the offset could only name a base before the pack start.
137// @decoder readOfsDelta prefix=ofs-delta
138export function readOfsDelta(buf: Uint8Array, start: number): Result<OfsDelta, Corrupt> {
139 let p = start;
140 let c = atOr(buf, p, NO_BYTE);
141 if (c === NO_BYTE) return short(buf, p);
142 p += 1;
143 let off = c & OBJ_SIZE_NEXT_MASK;
144 let shift = VARINT_BITS;
145 while (c & OBJ_CONTINUE) {
146 if (shift > VARINT_MAX_SHIFT) return corrupt(`pack: ofs delta offset longer than ${VARINT_MAX_SHIFT + 1} bits`);
147 c = atOr(buf, p, NO_BYTE);
148 if (c === NO_BYTE) return short(buf, p);
149 p += 1;
150 off = ((off + 1) * OBJ_SIZE_NEXT_BASE) + (c & OBJ_SIZE_NEXT_MASK);
151 shift += VARINT_BITS;
152 }
153 return ok({ off, next: p });
154}
156// ---- the scan ----
157const EMPTY: Uint8Array = new Uint8Array(0);
158const MIN_PACK: number = PACK_HEADER_BYTES + PACK_TRAILER_BYTES;
160// An object's base, parsed where its bytes were read: none (a full object), an offset in this pack (OFS_DELTA, a pack
161// offset past the header and before the object), or an id (REF_DELTA). parsePack's objects and resolvePack's input
162// carry this; nothing reads a base as a number or a string that may be missing.
163export type ObjBase =
164 | { readonly tag: 'none' }
165 | { readonly tag: 'ofs'; readonly offset: number }
166 | { readonly tag: 'ref'; readonly oid: Oid };
167export const NO_BASE: ObjBase = Object.freeze({ tag: 'none' });
168// limits: any subset (a key left out is the default), parsed by parsePackInit.
169// How a scan charges inflation. retain: declared sizes plus LIMIT_OBJECT_OVERHEAD against maxInflatedTotal, before any
170// byte inflates, for a caller that keeps objects (parsePack's refusals). work: see the top of this file; maxObjectSize
171// applies only with capObjects, since a scan that keeps nothing has no reason to refuse a large blob.
172export type ScanBudget =
173 | { readonly tag: 'retain' }
174 | { readonly tag: 'work'; readonly ratio: number; readonly capObjects: boolean };
175export const RETAIN_BUDGET: ScanBudget = Object.freeze({ tag: 'retain' });
176export const workBudget = (ratio: number = LIMIT_WORK_RATIO, capObjects = false): ScanBudget => Object.freeze({ tag: 'work', ratio, capObjects });
177export const WORK_BUDGET: ScanBudget = workBudget();
178export type PackScanOpts = { readonly verify: boolean; readonly limits: LimitsInput; readonly budget: ScanBudget };
179export const packScanOpts = (limits: LimitsInput = DEFAULT_LIMITS, verify = true, budget: ScanBudget = RETAIN_BUDGET): PackScanOpts => ({ verify, limits, budget });
181export type PackPiece =
182 | { readonly kind: 'need' }
183 | { readonly kind: 'header'; readonly version: number; readonly count: number }
184 | { readonly kind: 'object'; readonly index: number; readonly offset: number; readonly headerBytes: number; readonly type: ObjType; readonly size: ObjSize; readonly base: ObjBase }
185 | { readonly kind: 'chunk'; readonly index: number; readonly bytes: Uint8Array<ArrayBuffer> }
186 | { readonly kind: 'object-end'; readonly index: number; readonly compressed: number; readonly stats: InflateStats }
187 | { readonly kind: 'trailer'; readonly trailer: string }
188 | { readonly kind: 'end' };
190export type PackPhase = 'header' | 'object' | 'ofs' | 'ref' | 'gate' | 'body' | 'trailer' | 'end';
191type PackHash = { readonly kind: 'off' } | { readonly kind: 'on'; readonly sha: Sha1 };
192export type PackHalt = { readonly tag: 'running' } | { readonly tag: 'failed'; readonly error: PackError };
193// Stryker disable next-line ObjectLiteral,StringLiteral: halt is read only as tag === 'failed'; any other record runs
194const RUNNING: PackHalt = { tag: 'running' };
196// Single owner, mutable so a pull resumes where the last one stopped.
197export type PackScan = {
198 phase: PackPhase;
199 halt: PackHalt;
200 maxObjects: number;
201 maxInflatedTotal: number;
202 maxObjectSize: number;
203 budget: ScanBudget;
204 ended: boolean;
205 // unread input outside an object body: `carry` from `p`, which is pack offset `at`
206 carry: Uint8Array;
207 p: number;
208 at: number;
209 // every byte fed so far, and the last ≤ 20 of them (not hashed yet)
210 total: number;
211 lag: Uint8Array;
212 hash: PackHash;
213 count: number;
214 index: number;
215 charged: number;
216 // the object being read
217 offset: number;
218 headerBytes: number;
219 type: ObjType;
220 size: ObjSize;
221 base: ObjBase;
222 // one inflate record, reset for every member: the window and chunk buffers are allocated once per scan
223 inflate: InflateStream;
224};
226const NEED_PIECE: PackPiece = { kind: 'need' };
227const END_PIECE: PackPiece = { kind: 'end' };
228// A step's answer: go on, a piece, or stopped (the refusal is on the record).
229type Step = { readonly kind: 'cont' } | { readonly kind: 'piece'; readonly piece: PackPiece } | { readonly kind: 'failed' };
230// Stryker disable next-line ObjectLiteral,StringLiteral: the pull loops on anything that is not a piece or a failure
231const CONT: Step = { kind: 'cont' };
232const NEED: Step = { kind: 'piece', piece: NEED_PIECE };
233// Stryker disable next-line ObjectLiteral,StringLiteral: the pull reads a step only as kind === 'piece'; the refusal is on st.halt
234const FAILED: Step = { kind: 'failed' };
235const piece = (p: PackPiece): Step => ({ kind: 'piece', piece: p });
236function refuse(st: PackScan, error: PackError): Step {
237 st.halt = { tag: 'failed', error };
238 return FAILED;
239}
241// A work-budgeted scan's object cap: the largest size a double holds exactly, so a declared size past it is still refused.
242const UNCAPPED_OBJECT: number = Number.MAX_SAFE_INTEGER;
243// The three caps a scan holds, each already checked (parseLimits, or parsePack's override checked the same way).
244export type PackCaps = { readonly maxObjects: number; readonly maxInflatedTotal: number; readonly maxObjectSize: number };
245// A scan under caps in hand: the limits' check is the caller's (parsePackInit, parsePack).
246export function packScanOpen(caps: PackCaps, verify: boolean, budget: ScanBudget = RETAIN_BUDGET): Result<PackScan, PackError> {
247 // a declared size of 0 under a checked cap: this record always opens (each member resets it)
248 // Stryker disable next-line all: see above
249 const inflate = inflateInit({ bound: declaredBound(0, caps.maxObjectSize), wrap: zlibWrap(true) });
250 // Stryker disable next-line all: see above
251 if (!inflate.ok) return inflate;
252 return ok({
253 phase: 'header', halt: RUNNING, maxObjects: caps.maxObjects, maxInflatedTotal: caps.maxInflatedTotal, maxObjectSize: caps.maxObjectSize, budget,
254 // Stryker disable next-line ObjectLiteral,StringLiteral: the hash is read only as kind 'on'; any other record is off
255 ended: false, carry: EMPTY, p: 0, at: 0, total: 0, lag: EMPTY, hash: verify ? { kind: 'on', sha: new Sha1() } : { kind: 'off' },
256 // Stryker disable next-line all: placeholders until the first object header sets them
257 count: 0, index: 0, charged: 0, offset: 0, headerBytes: 0, type: OBJ_BLOB, size: zeroSize(), base: NO_BASE, inflate: inflate.value,
258 });
259}
260// @decoder parsePackInit prefix=pack
261export function parsePackInit(opts: Omit<PackScanOpts, 'budget'> & Input<Pick<PackScanOpts, 'budget'>>): Result<PackScan, PackError> {
262 const limits = parseLimits(opts.limits);
263 if (!limits.ok) return limits;
264 const budget = getOrElse(fromNullish(opts.budget), RETAIN_BUDGET);
265 if (budget.tag === 'work') {
266 const ratio = checkLimit(budget.ratio, 'workRatio');
267 if (!ratio.ok) return ratio;
268 }
269 const { maxPackObjects: maxObjects, maxInflatedTotal } = limits.value;
270 const maxObjectSize = budget.tag === 'work' && !budget.capObjects ? UNCAPPED_OBJECT : limits.value.maxObjectSize;
271 return packScanOpen({ maxObjects, maxInflatedTotal, maxObjectSize }, opts.verify, budget);
272}
274// Hash everything but the last PACK_TRAILER_BYTES bytes seen so far (spStep's lag, fetch.ts). The lag is kept with
275// verify off too: it is the trailer.
276function hashLagged(st: PackScan, bytes: Uint8Array): void {
277 st.total += bytes.length;
278 const lagged = st.lag.length + bytes.length;
279 // Stryker disable next-line EqualityOperator: at exactly PACK_TRAILER_BYTES the other branch hashes nothing and keeps the same lag
280 if (lagged <= PACK_TRAILER_BYTES) {
281 const lag = new Uint8Array(lagged);
282 lag.set(st.lag, 0);
283 lag.set(bytes, st.lag.length);
284 st.lag = lag;
285 return;
286 }
287 const out = lagged - PACK_TRAILER_BYTES;
288 // what leaves the lag first, then what of `bytes` leaves
289 const fromLag = Math.min(out, st.lag.length);
290 if (st.hash.kind === 'on') {
291 st.hash.sha.update(st.lag.subarray(0, fromLag));
292 st.hash.sha.update(bytes.subarray(0, out - fromLag));
293 }
294 const lag = new Uint8Array(PACK_TRAILER_BYTES);
295 const keepLag = st.lag.length - fromLag;
296 lag.set(st.lag.subarray(fromLag), 0);
297 lag.set(bytes.subarray(out - fromLag), keepLag);
298 st.lag = lag;
299}
301// Append input. Inside an object body it goes straight to the inflate record; elsewhere an exhausted carry is replaced
302// by `bytes` (no copy) and a leftover tail (an object header cut by the chunk) is copied in front of it once.
303// @decoder parsePackFeed prefix=pack
304export function parsePackFeed(st: PackScan, bytes: Uint8Array): void {
305 hashLagged(st, bytes);
306 if (st.phase === 'body') {
307 inflateFeed(st.inflate, bytes);
308 return;
309 }
310 // past the objects only the count matters: the trailer is the hash lag
311 if (st.phase === 'trailer' || st.phase === 'end') return;
312 if (st.p >= st.carry.length) {
313 st.carry = bytes;
314 st.p = 0;
315 return;
316 }
317 const tail = st.carry.subarray(st.p);
318 const joined = new Uint8Array(tail.length + bytes.length);
319 joined.set(tail, 0);
320 joined.set(bytes, tail.length);
321 st.carry = joined;
322 st.p = 0;
323}
324// No more input will come.
325// @decoder parsePackClose prefix=pack
326export function parsePackClose(st: PackScan): void {
327 st.ended = true;
328}
330const available = (st: PackScan): number => st.carry.length - st.p;
331function advance(st: PackScan, n: number): void {
332 st.p += n;
333 st.at += n;
334}
336// parsePack decides the header on a buffer of at least a header and a trailer: so does the scan, or on a closed input.
337function stepHeader(st: PackScan): Step {
338 if (available(st) < MIN_PACK && !st.ended) return NEED;
339 if (st.total < MIN_PACK) return refuse(st, plyError('pack_corrupt', 'pack: bad signature'));
340 for (const [i, b] of PACK_SIGNATURE.entries()) {
341 if (st.carry[st.p + i] !== b) return refuse(st, plyError('pack_corrupt', 'pack: bad signature'));
342 }
343 const version = be32(st.carry, st.p + PACK_SIGNATURE.length);
344 const count = be32(st.carry, st.p + PACK_SIGNATURE.length + UINT32_BYTES);
345 if (version !== PACK_VERSION) return refuse(st, plyError('pack_corrupt', `pack: unsupported version ${version}`));
346 if (count > st.maxObjects) return refuse(st, limitExceeded('pack objects', count, st.maxObjects));
347 advance(st, PACK_HEADER_BYTES);
348 st.count = count;
349 st.phase = count === 0 ? 'trailer' : 'object';
350 return piece({ kind: 'header', version, count });
351}
352// A read cut by the end of what has arrived waits for more; on a closed input it is the refusal parsePack gives.
353function waitOr(st: PackScan, e: HeaderError): Step {
354 // Stryker disable next-line ConditionalExpression: only a pack_corrupt short read carries reason short_read; the code test is for the compiler
355 if (e.code === 'pack_corrupt' && e.reason === 'short_read' && !st.ended) return NEED;
356 return refuse(st, e);
357}
358function stepObject(st: PackScan): Step {
359 // the carry starts mid-pack: a refused type names the pack offset (st.at), as parsePack does
360 const header = readObjHeader(st.carry, st.p, st.at, st.maxObjectSize);
361 if (!header.ok) return waitOr(st, header.error);
362 const type = header.value.type;
363 st.offset = st.at;
364 st.type = type;
365 st.size = header.value.size;
366 st.base = NO_BASE;
367 advance(st, header.value.next - st.p);
368 switch (type) {
369 case OBJ_COMMIT: case OBJ_TREE: case OBJ_BLOB: case OBJ_TAG:
370 st.phase = 'gate';
371 return CONT;
372 case OBJ_OFS_DELTA:
373 st.phase = 'ofs';
374 return CONT;
375 case OBJ_REF_DELTA:
376 st.phase = 'ref';
377 return CONT;
378 }
379 // Stryker disable next-line ConditionalExpression: unreachable, readObjHeader parsed the type into one of the six
380 return assertNever(type);
381}
382function stepOfs(st: PackScan): Step {
383 const ofs = readOfsDelta(st.carry, st.p);
384 if (!ofs.ok) return waitOr(st, ofs.error);
385 advance(st, ofs.value.next - st.p);
386 // git index-pack: "delta base offset is out of bound" unless 0 < base < this object (packresolve.ts parsePack)
387 if (ofs.value.off === 0) return refuse(st, plyError('pack_corrupt', `pack: ofs delta at ${st.offset} is its own base`));
388 const baseOffset = st.offset - ofs.value.off;
389 if (baseOffset < PACK_HEADER_BYTES) return refuse(st, plyError('pack_corrupt', `pack: ofs delta base before pack start at ${st.offset}`));
390 st.base = { tag: 'ofs', offset: baseOffset };
391 st.phase = 'gate';
392 return CONT;
393}
394// parsePack reads a REF_DELTA's base oid as a view: its buffer always holds a trailer past it. A scan that has seen
395// the input close with fewer than 20 bytes here has no trailer either; that is the trailer refusal parsePack gives.
396function stepRef(st: PackScan): Step {
397 if (available(st) < OID_BYTES) {
398 if (!st.ended) return NEED;
399 return refuse(st, plyError('pack_corrupt', `pack: expected trailer at ${st.at}, pack length ${st.total}`));
400 }
401 st.base = { tag: 'ref', oid: toOid(st.carry.subarray(st.p, st.p + OID_BYTES)) };
402 advance(st, OID_BYTES);
403 st.phase = 'gate';
404 return CONT;
405}
406// The declared size against the aggregate cap, before an inflated byte exists (readObjHeader held it to maxObjectSize).
407function stepGate(st: PackScan): Step {
408 if (st.budget.tag === 'retain') {
409 const cost = st.size + LIMIT_OBJECT_OVERHEAD;
410 if (st.charged + cost > st.maxInflatedTotal) return refuse(st, limitExceeded('pack inflated bytes', st.charged + cost, st.maxInflatedTotal));
411 st.charged += cost;
412 }
413 st.headerBytes = st.at - st.offset;
414 // the size passed the cap on the line above, and the cap passed parseLimits: the reset cannot refuse
415 const opened = inflateReset(st.inflate, { bound: declaredBound(st.size, st.maxObjectSize), wrap: zlibWrap(true) });
416 // Stryker disable next-line all: see above
417 if (!opened.ok) return refuse(st, opened.error);
418 // the member's bytes are the inflate record's from here; the scan's carry is empty until the member ends
419 inflateFeed(st.inflate, st.carry.subarray(st.p));
420 st.carry = EMPTY;
421 st.p = 0;
422 st.phase = 'body';
423 return piece({ kind: 'object', index: st.index, offset: st.offset, headerBytes: st.headerBytes, type: st.type, size: st.size, base: st.base });
424}
425// The bytes just inflated against a work budget; false when the scan has refused. A retain budget charged at the gate.
426function chargeWork(st: PackScan, inflated: number): boolean {
427 if (st.budget.tag !== 'work') return true;
428 st.charged += inflated;
429 const allowed = Math.max(st.maxInflatedTotal, st.budget.ratio * (st.at + inflateConsumed(st.inflate)));
430 if (st.charged <= allowed) return true;
431 refuse(st, limitExceeded('pack inflated work', st.charged, allowed));
432 return false;
433}
434function stepBody(st: PackScan): Step {
435 const stream = st.inflate;
436 const pulled = st.ended ? inflateEnd(stream) : inflatePull(stream);
437 if (!pulled.ok) return refuse(st, pulled.error);
438 const got = pulled.value;
439 switch (got.kind) {
440 case 'need': return NEED;
441 case 'chunk':
442 if (!chargeWork(st, got.bytes.length)) return FAILED;
443 return piece({ kind: 'chunk', index: st.index, bytes: got.bytes });
444 case 'end': {
445 // what followed the member is the scan's again
446 st.carry = inflateUnread(stream);
447 st.p = 0;
448 st.at += got.consumed;
449 const index = st.index;
450 st.index += 1;
451 st.phase = st.index < st.count ? 'object' : 'trailer';
452 return piece({ kind: 'object-end', index, compressed: got.consumed, stats: got.stats });
453 }
454 }
455 // Stryker disable next-line ConditionalExpression,BlockStatement,CallExpression: unreachable, the compiler's exhaustiveness check
456 return assertNever(got);
457}
458// The trailer is the 20 bytes after the last object and nothing after them: known when the input closes.
459function stepTrailer(st: PackScan): Step {
460 if (!st.ended) return NEED;
461 if (st.at + PACK_TRAILER_BYTES !== st.total) return refuse(st, plyError('pack_corrupt', `pack: expected trailer at ${st.at}, pack length ${st.total}`));
462 const trailer = toHex(st.lag);
463 if (st.hash.kind === 'on') {
464 const computed = st.hash.sha.hex();
465 if (computed !== trailer) return refuse(st, plyError('pack_corrupt', `pack: trailer sha mismatch ${trailer} != ${computed}`));
466 }
467 st.phase = 'end';
468 return piece({ kind: 'trailer', trailer });
469}
471function step(st: PackScan): Step {
472 switch (st.phase) {
473 case 'header': return stepHeader(st);
474 case 'object': return stepObject(st);
475 case 'ofs': return stepOfs(st);
476 case 'ref': return stepRef(st);
477 case 'gate': return stepGate(st);
478 case 'body': return stepBody(st);
479 case 'trailer': return stepTrailer(st);
480 case 'end': return piece(END_PIECE);
481 }
482 // Stryker disable next-line ConditionalExpression,BlockStatement,CallExpression: unreachable, the compiler's exhaustiveness check
483 return assertNever(st.phase);
484}
485// The next event, or the refusal (every later pull returns it again).
486// @decoder parsePackPull prefix=pack
487export function parsePackPull(st: PackScan): Result<PackPiece, PackError> {
488 for (;;) {
489 if (st.halt.tag === 'failed') return err(st.halt.error);
490 const s = step(st);
491 if (s.kind === 'piece') return ok(s.piece);
492 }
493}
494// Close the input and pull.
495// @decoder parsePackEnd prefix=pack
496export function parsePackEnd(st: PackScan): Result<PackPiece, PackError> {
497 parsePackClose(st);
498 return parsePackPull(st);
499}
501export const streamPackAttrs = (st: PackScan): Attrs => ({
502 'git.pack.phase': st.phase, 'git.pack.objects': st.index, 'git.pack.offset': st.at, 'git.pack.bytes': st.total, 'git.pack.ended': st.ended,
503});
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 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 · hit5 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 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=2 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
fetch2008.2 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=22 git.response.bytes=8411 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=1043b83439f6bf9a39f87976e08864cc4fba505d 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=8411 git.pkt.payload.max=8390 git.sideband.band1.bytes=8389 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
- a5e1f9ba4d8c24ce
- Request
- 837eacbb…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format