@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 80 lines 2.91 KB
Raw
1 /**
2 * Parses an upload-pack result (pkt-line NAK/ACK head, then the pack either
3 * raw or in band-1 sideband frames) and verifies the pack: `PACK` magic,
4 * version 2, and the trailing SHA-1 over everything before it. Shared by
5 * the bench and the unit tests so "bytes received" never stands in for
6 * "a whole pack" (DESIGN §22.4).
7 */
8 import { makeSha1 } from "@alchemy.run/git/Protocol/ObjectCodec";
9
10 export const verifyPackResponse = (
11 raw: Uint8Array,
12 sideband: boolean,
13 ): { readonly objects: number; readonly error?: string } => {
14 const text = (b: Uint8Array) => new TextDecoder().decode(b);
15 let pos = 0;
16 const pieces: Array<Uint8Array> = [];
17 for (;;) {
18 if (pos + 4 > raw.length) break;
19 const four = text(raw.subarray(pos, pos + 4));
20 if (four === "PACK" && !sideband) break; // raw mode: the pack starts here
21 const len = Number.parseInt(four, 16);
22 if (Number.isNaN(len)) return { objects: 0, error: `bad pkt length at ${pos}` };
23 if (len === 0) {
24 pos += 4;
25 if (sideband) break; // trailing flush after the last frame
26 continue;
27 }
28 const payload = raw.subarray(pos + 4, pos + len);
29 pos += len;
30 if (sideband) {
31 if (payload[0] === 1) pieces.push(payload.subarray(1));
32 continue; // band 2/3 progress, or a NAK/ACK line before framing
33 }
34 const line = text(payload);
35 if (line.startsWith("NAK") || line.startsWith("ACK")) continue;
36 return {
37 objects: 0,
38 error: `unexpected pkt-line before pack: ${line.slice(0, 40)}`,
39 };
40 }
41 if (!sideband) pieces.push(raw.subarray(pos));
42 return verifyPack(concat(pieces));
43 };
44
45 /** Verifies a bare pack: magic, version, count, SHA-1 trailer. */
46 export const verifyPack = (
47 pack: Uint8Array,
48 ): { readonly objects: number; readonly error?: string } => {
49 const text = (b: Uint8Array) => new TextDecoder().decode(b);
50 if (pack.length < 32) return { objects: 0, error: `pack too short (${pack.length} bytes)` };
51 if (text(pack.subarray(0, 4)) !== "PACK") return { objects: 0, error: "missing PACK magic" };
52 const view = new DataView(pack.buffer, pack.byteOffset, pack.byteLength);
53 if (view.getUint32(4) !== 2) return { objects: 0, error: `pack version ${view.getUint32(4)}` };
54 const objects = view.getUint32(8);
55 const hash = makeSha1();
56 hash.update(pack.subarray(0, pack.length - 20));
57 const digest = hash.digest();
58 const trailer = pack.subarray(pack.length - 20);
59 for (let i = 0; i < 20; i++) {
60 if (digest[i] !== trailer[i]) {
61 return {
62 objects,
63 error: `trailer SHA-1 mismatch (${pack.length} bytes, ${objects} objects)`,
64 };
65 }
66 }
67 return { objects };
68 };
69
70 export const concat = (pieces: ReadonlyArray<Uint8Array>): Uint8Array => {
71 if (pieces.length === 1) return pieces[0]!;
72 const total = pieces.reduce((n, p) => n + p.length, 0);
73 const out = new Uint8Array(total);
74 let at = 0;
75 for (const p of pieces) {
76 out.set(p, at);
77 at += p.length;
78 }
79 return out;
80 };