{"draft":null,"from":136,"to":318,"files":[{"path":"server/src/git-pack-decode.ts","added":100,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-pack-decode.ts\n@@ -0,0 +1,100 @@\n+import type { GitPackResolver } from \"./git-pack-resolver.ts\";\n+import type { GitPackSpool } from \"./git-pack-spool.ts\";\n+\n+/** Structural adapter for the pinned Git dependency's streaming inflater. */\n+export interface PackInflater {\n+\tonData: (bytes: Uint8Array) => void;\n+\tpush(bytes: Uint8Array, final: boolean): unknown;\n+\tended: boolean;\n+\terr: number;\n+\tmsg: string;\n+\tstrm: { avail_in: number };\n+}\n+\n+/** Decode verified disk bytes without collecting a pack or decoded object in memory. */\n+export function decodeGitPack(\n+\tspool: GitPackSpool,\n+\tresolver: GitPackResolver,\n+\tcreateInflater: () => PackInflater,\n+): void {\n+\ttry {\n+\t\tdecodeVerifiedPack(spool, resolver, createInflater);\n+\t} catch (error) {\n+\t\t// Intake owns this resolver. Never expose earlier objects from a rejected pack.\n+\t\tresolver.dispose();\n+\t\tthrow error;\n+\t}\n+}\n+\n+function decodeVerifiedPack(spool: GitPackSpool, resolver: GitPackResolver, createInflater: () => PackInflater): void {\n+\tconst end = spool.byteLength - 20;\n+\tconst header = spool.read(0, 12);\n+\tconst count = new DataView(header.buffer, header.byteOffset, header.byteLength).getUint32(8);\n+\tlet position = 12;\n+\tlet cached: Uint8Array = new Uint8Array(0),\n+\t\tcachedAt = 0;\n+\tconst byte = () => {\n+\t\tif (position >= end) throw Error(\"truncated Git pack header\");\n+\t\tif (position < cachedAt || position >= cachedAt + cached.length) {\n+\t\t\tcachedAt = position;\n+\t\t\tcached = spool.read(position, Math.min(65536, end - position));\n+\t\t}\n+\t\treturn cached[position++ - cachedAt]!;\n+\t};\n+\tfor (let index = 0; index < count; index++) {\n+\t\tconst offset = position;\n+\t\tlet current = byte();\n+\t\tconst kind = (current >> 4) & 7;\n+\t\tlet length = current & 15,\n+\t\t\tfactor = 16;\n+\t\twhile (current & 128) {\n+\t\t\tcurrent = byte();\n+\t\t\tlength += (current & 127) * factor;\n+\t\t\tfactor *= 128;\n+\t\t\tif (!Number.isSafeInteger(length) || !Number.isSafeInteger(factor)) throw Error(\"invalid Git object size\");\n+\t\t}\n+\t\tlet base: number | string | undefined;\n+\t\tif (kind === 6) {\n+\t\t\tcurrent = byte();\n+\t\t\tlet distance = current & 127;\n+\t\t\twhile (current & 128) {\n+\t\t\t\tcurrent = byte();\n+\t\t\t\tdistance = (distance + 1) * 128 + (current & 127);\n+\t\t\t\tif (!Number.isSafeInteger(distance)) throw Error(\"invalid Git delta distance\");\n+\t\t\t}\n+\t\t\tbase = offset - distance;\n+\t\t} else if (kind === 7) {\n+\t\t\tlet reference = \"\";\n+\t\t\tfor (let i = 0; i < 20; i++) reference += byte().toString(16).padStart(2, \"0\");\n+\t\t\tbase = reference;\n+\t\t}\n+\t\tconst type = [\"\", \"commit\", \"tree\", \"blob\", \"tag\", \"\", \"delta\", \"delta\"][kind];\n+\t\tif (!type) throw Error(\"unsupported Git object type\");\n+\t\tresolver.addChunks(\n+\t\t\toffset,\n+\t\t\ttype,\n+\t\t\t(write) => {\n+\t\t\t\tconst inflater = createInflater();\n+\t\t\t\tlet decoded = 0;\n+\t\t\t\tinflater.onData = (bytes) => {\n+\t\t\t\t\tdecoded += bytes.length;\n+\t\t\t\t\tif (bytes.length > 65536 || decoded > length) throw Error(\"invalid inflated Git object size\");\n+\t\t\t\t\twrite(bytes);\n+\t\t\t\t};\n+\t\t\t\twhile (!inflater.ended) {\n+\t\t\t\t\tif (position >= end) throw Error(\"truncated compressed Git object\");\n+\t\t\t\t\tconst chunk = spool.read(position, Math.min(65536, end - position));\n+\t\t\t\t\tinflater.push(chunk, false);\n+\t\t\t\t\tif (inflater.err) throw Error(`Git inflate failed: ${inflater.msg}`);\n+\t\t\t\t\tconst consumed = chunk.length - inflater.strm.avail_in;\n+\t\t\t\t\tif (consumed <= 0 || consumed > chunk.length) throw Error(\"Git inflater made no progress\");\n+\t\t\t\t\tposition += consumed;\n+\t\t\t\t}\n+\t\t\t\tif (decoded !== length) throw Error(\"inflated Git object size mismatch\");\n+\t\t\t},\n+\t\t\tbase,\n+\t\t);\n+\t}\n+\tif (position !== end) throw Error(\"unexpected bytes after Git pack objects\");\n+\tresolver.resolve();\n+}"}],"unmatched":[]}