main
1// Layout of one pack: offsets, types, delta links. Object bytes are not retained; previews are capped text.
2import { atIndex, chainGet, fromAbsent, fromNullable, mapGet } from '../absent.ts';
3import { unwrapOr } from '../option.ts';
4import { limitError } from 'ply';
5import { TYPE, TYPE_NAME, parseCommit, parseTree, hashObject } from 'ply';
6import { parsePack, resolvePack } from 'ply';
7import { unwrap } from '../ply-result.ts';
8import { VIEWER_CAPS, VIEWER_GIT_LIMITS } from './limits.ts';
9import { clampText } from './escape.ts';
11export type ObjectKind = 'commit' | 'tree' | 'blob' | 'tag' | 'ofs_delta' | 'ref_delta';
13export type ObjectPreview =
14 | { kind: 'commit'; summary: string; tree: string; parents: string[] }
15 | { kind: 'tree'; names: string[] }
16 | { kind: 'blob'; binary: boolean; text: string | null }
17 | { kind: 'tag'; text: string }
18 | { kind: 'unresolved' };
20export type PackObjectView = {
21 offset: number;
22 length: number;
23 headerBytes: number;
24 compressed: number;
25 inflated: number;
26 wireType: ObjectKind;
27 resolvedType: ObjectKind | null;
28 oid: string | null;
29 baseOffset: number | null;
30 baseOid: string | null;
31 chain: number;
32 preview: ObjectPreview | null;
33};
35export type DeltaLink = {
36 deltaOffset: number;
37 baseOffset: number | null;
38 baseOid: string | null;
39 depth: number;
40};
42export type PackMap = {
43 bytes: number;
44 version: number;
45 count: number;
46 trailer: string;
47 headerLength: 12;
48 trailerLength: 20;
49 objects: PackObjectView[];
50 links: DeltaLink[];
51 ofsDelta: number;
52 refDelta: number;
53 thin: boolean;
54 maxChain: number;
55 counts: Record<string, number>;
56 unresolved: boolean;
57};
59// ply's own parsed-pack type: resolvePack needs each object's inflate stats, which a hand-written copy left out.
60// The parsed pack itself (ply 0.2.0's parsePack returns it inside a Result).
61export type ParsedPack = Extract<ReturnType<typeof parsePack>, { ok: true }>['value'];
63type ResolvedObject = {
64 oid: string;
65 type: number;
66 data: Uint8Array;
67 chain: number;
68 offset: number;
69 size: number;
70 compressed: number;
71};
73function textOrEmpty(value: unknown): string {
74 const present = fromNullable(value);
75 if (present.tag === 'none') return '';
76 return typeof present.value === 'string' ? present.value : String(present.value);
77}
79function kindOf(type: number): ObjectKind {
80 const found = fromAbsent((TYPE_NAME as Record<number, string>)[type]);
81 const name = found.tag === 'some' ? found.value : '';
82 if (name === 'commit' || name === 'tree' || name === 'blob' || name === 'tag' || name === 'ofs_delta' || name === 'ref_delta') return name;
83 return 'blob';
84}
86function isText(bytes: Uint8Array): boolean {
87 const n = Math.min(bytes.length, 8192);
88 for (let i = 0; i < n; i++) if (bytes[i] === 0) return false;
89 try { new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(0, n)); return true; }
90 catch { return false; }
91}
93function previewOf(type: number, data: Uint8Array, limits: object): ObjectPreview | null {
94 if (data.length > VIEWER_CAPS.maxPreviewChars * 4 && type === TYPE.BLOB) {
95 return { kind: 'blob', binary: !isText(data.subarray(0, 512)), text: null };
96 }
97 try {
98 if (type === TYPE.COMMIT) {
99 const commit = unwrap(parseCommit(data, limits));
100 const message = textOrEmpty(commit.message);
101 const summary = unwrapOr(atIndex(message.split('\n'), 0), '');
102 return { kind: 'commit', summary: clampText(summary, 200), tree: textOrEmpty(commit.tree), parents: commit.parents.slice(0, 8) };
103 }
104 if (type === TYPE.TREE) {
105 const entries = unwrap(parseTree(data, limits));
106 return { kind: 'tree', names: entries.slice(0, 40).map((entry: { name: string }) => entry.name) };
107 }
108 if (type === TYPE.BLOB) {
109 if (!isText(data)) return { kind: 'blob', binary: true, text: null };
110 return { kind: 'blob', binary: false, text: clampText(new TextDecoder().decode(data), VIEWER_CAPS.maxPreviewChars) };
111 }
112 if (type === TYPE.TAG) return { kind: 'tag', text: clampText(new TextDecoder().decode(data), VIEWER_CAPS.maxPreviewChars) };
113 } catch {
114 return null;
115 }
116 return null;
117}
119export function mapPack(buf: Uint8Array, limits = VIEWER_GIT_LIMITS): PackMap {
120 if (buf.length > VIEWER_CAPS.maxPackBytes) throw limitError('pack bytes', buf.length, VIEWER_CAPS.maxPackBytes);
121 const parsed = unwrap(parsePack(buf, { limits, maxObjects: VIEWER_CAPS.maxPackObjects, maxInflatedTotal: VIEWER_CAPS.maxInflatedTotal }));
122 return mapParsed(parsed, limits);
123}
125export function mapParsed(parsed: ParsedPack, limits = VIEWER_GIT_LIMITS): PackMap {
126 let resolved: Map<string, ResolvedObject> | null = null;
127 let unresolved = false;
128 let maxChain = 0;
129 let externalBases = 0;
130 try {
131 const out = unwrap(resolvePack({ objects: parsed.objects }, () => null, limits));
132 resolved = out.objects;
133 maxChain = out.stats.maxChain;
134 externalBases = out.stats.externalBases;
135 // resolvePack's depth guard counts a recursive walk. A base-first ofs chain never recurses that
136 // deep, so the resolved chain length is checked here against the viewer cap.
137 if (maxChain > limits.maxDeltaChain) throw limitError('delta chain depth', maxChain, limits.maxDeltaChain);
138 } catch (err) {
139 const messageField = chainGet(err, 'message');
140 const message = String(messageField.tag === 'some' ? messageField.value : err);
141 const code = chainGet(err, 'code');
142 if (code.tag === 'some' && code.value === 'pack_corrupt' && /ref delta base/.test(message)) unresolved = true;
143 else throw err;
144 }
145 const byOffset = new Map<number, ResolvedObject>();
146 if (resolved) for (const obj of resolved.values()) byOffset.set(obj.offset, obj);
147 const objects: PackObjectView[] = parsed.objects.map((obj) => {
148 const wire = kindOf(obj.type);
149 const full = obj.type >= 1 && obj.type <= 4;
150 const placed = mapGet(byOffset, obj.offset);
151 const placedOid = placed.tag === 'some' ? chainGet(placed.value, 'oid') : fromNullable(null);
152 const typeLabel = fromAbsent((TYPE_NAME as Record<number, string>)[obj.type]);
153 const label = typeLabel.tag === 'some' ? typeLabel.value : 'blob';
154 const oid = placedOid.tag === 'some' ? (typeof placedOid.value === 'string' ? placedOid.value : String(placedOid.value)) : (full ? hashObject(label, obj.data) : null);
155 const resolvedType = placed.tag === 'some' ? kindOf(placed.value.type) : (full ? wire : null);
156 const dataField = placed.tag === 'some' ? chainGet(placed.value, 'data') : fromNullable(null);
157 const data = dataField.tag === 'some' ? dataField.value as Uint8Array : (full ? obj.data : null);
158 const chainField = placed.tag === 'some' ? chainGet(placed.value, 'chain') : fromNullable(null);
159 const chain = chainField.tag === 'some' && typeof chainField.value === 'number' ? chainField.value : 0;
160 const typeField = placed.tag === 'some' ? chainGet(placed.value, 'type') : fromNullable(null);
161 const previewType = typeField.tag === 'some' && typeof typeField.value === 'number' ? typeField.value : obj.type;
162 return {
163 offset: obj.offset,
164 length: obj.headerBytes + obj.compressed,
165 headerBytes: obj.headerBytes,
166 compressed: obj.compressed,
167 inflated: obj.size,
168 wireType: wire,
169 resolvedType,
170 oid,
171 baseOffset: obj.base.tag === 'ofs' ? obj.base.offset : null,
172 baseOid: obj.base.tag === 'ref' ? obj.base.oid : null,
173 chain,
174 preview: data ? previewOf(previewType, data, limits) : (wire === 'ofs_delta' || wire === 'ref_delta' ? { kind: 'unresolved' } : null),
175 };
176 });
177 const links: DeltaLink[] = objects.filter(obj => obj.baseOffset !== null || obj.baseOid !== null).map(obj => ({
178 deltaOffset: obj.offset,
179 baseOffset: obj.baseOffset,
180 baseOid: obj.baseOid,
181 depth: obj.chain,
182 }));
183 const thin = unresolved || externalBases > 0 || objects.some(obj => obj.wireType === 'ref_delta' && !objects.some(other => other.oid === obj.baseOid));
184 const counts: Record<string, number> = {};
185 for (const obj of objects) {
186 const seen = fromAbsent(counts[obj.wireType]);
187 counts[obj.wireType] = (seen.tag === 'some' && typeof seen.value === 'number' ? seen.value : 0) + 1;
188 }
189 return {
190 bytes: parsed.bytes,
191 version: parsed.stats.version,
192 count: parsed.stats.count,
193 trailer: parsed.trailer,
194 headerLength: 12,
195 trailerLength: 20,
196 objects,
197 links,
198 ofsDelta: parsed.stats.ofsDelta,
199 refDelta: parsed.stats.refDelta,
200 thin,
201 maxChain: Math.max(maxChain, ...objects.map(obj => obj.chain), 0),
202 counts,
203 unresolved,
204 };
205}
Diagnostics4 git calls · 703 ms
CallStatusSizeTime0703 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 1020040 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=697 git.response.bytes=41349 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=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2002.7 KiB697 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2739 git.response.cache=MISS git.response.duration_ms=697 git.fetch.wants=019ca37a7e7f052981fe3513d37235a7285b3bab 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=2739 git.pkt.payload.max=2718 git.sideband.band1.bytes=2717 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2d947cc7e14c2
- Request
- ffc2b741…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format