{"draft":null,"from":5,"to":318,"files":[{"path":"server/src/git-disk-intake.ts","added":102,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-disk-intake.ts\n@@ -0,0 +1,102 @@\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import type { StoredGitBytes } from \"./git-object-bytes.ts\";\n+import { decodeGitPack, type PackInflater } from \"./git-pack-decode.ts\";\n+import { GitPackResolver } from \"./git-pack-resolver.ts\";\n+import { GitPackSpool } from \"./git-pack-spool.ts\";\n+\n+/** One clone's temporary pack store. The caller must dispose it in finally. */\n+export class GitDiskIntake {\n+\t/** Cold object construction only, before any intake or reader is created. */\n+\tstatic recover(storage: SqliteStorage): void {\n+\t\t// Constructors establish scratch tables only; they do not alter project state.\n+\t\tconst resolver = new GitPackResolver(storage, 0);\n+\t\tstorage.transactionSync(() => {\n+\t\t\tGitPackSpool.recover(storage);\n+\t\t\tstorage.sql.exec(\"DELETE FROM git_pack_resolve\");\n+\t\t\tstorage.sql.exec(\"DELETE FROM git_object_bytes\");\n+\t\t\tstorage.sql.exec(\"DELETE FROM git_object_meta\");\n+\t\t\tstorage.sql.exec(\"DELETE FROM git_object_budget\");\n+\t\t});\n+\t\tresolver.dispose();\n+\t}\n+\tprivate readonly spool: GitPackSpool;\n+\tprivate readonly resolver: GitPackResolver;\n+\tprivate readonly maxPackBytes: number;\n+\tprivate state: \"new\" | \"loading\" | \"ready\" | \"closed\" = \"new\";\n+\tconstructor(storage: SqliteStorage, maxPackBytes: number, maxObjectBytes: number, maxDecodedBytes = maxPackBytes) {\n+\t\tif (!Number.isSafeInteger(maxPackBytes) || maxPackBytes < 32) throw Error(\"invalid pack limit\");\n+\t\tthis.maxPackBytes = maxPackBytes;\n+\t\tthis.resolver = new GitPackResolver(storage, maxObjectBytes, maxDecodedBytes);\n+\t\tthis.spool = new GitPackSpool(storage);\n+\t}\n+\tattach(cache: object): void {\n+\t\tconst key = Symbol.for(\"abso.git.disk-pack\");\n+\t\tif (key in cache) throw Error(\"Git disk intake already attached\");\n+\t\tObject.defineProperty(cache, key, { value: this });\n+\t}\n+\tasync ingest(\n+\t\tchunks: AsyncIterable<Uint8Array> | Iterable<Uint8Array> | AsyncIterator<Uint8Array>,\n+\t\tcreateInflater: () => PackInflater,\n+\t): Promise<void> {\n+\t\tif (this.state !== \"new\") throw Error(\"Git disk intake already used\");\n+\t\tthis.state = \"loading\";\n+\t\tconst iterator =\n+\t\t\tSymbol.asyncIterator in chunks\n+\t\t\t\t? chunks[Symbol.asyncIterator]()\n+\t\t\t\t: Symbol.iterator in chunks\n+\t\t\t\t\t? chunks[Symbol.iterator]()\n+\t\t\t\t\t: chunks;\n+\t\ttry {\n+\t\t\tconst source = {\n+\t\t\t\t[Symbol.asyncIterator]: () => ({\n+\t\t\t\t\tnext: async () => iterator.next(),\n+\t\t\t\t\treturn: async () => (iterator.return ? iterator.return() : { done: true as const, value: undefined }),\n+\t\t\t\t}),\n+\t\t\t};\n+\t\t\tawait this.spool.ingest(source, this.maxPackBytes);\n+\t\t\tif (this.state !== \"loading\") throw Error(\"Git disk intake closed during download\");\n+\t\t\tdecodeGitPack(this.spool, this.resolver, createInflater);\n+\t\t\tthis.spool.dispose();\n+\t\t\tthis.state = \"ready\";\n+\t\t} catch (error) {\n+\t\t\ttry {\n+\t\t\t\tawait iterator.return?.();\n+\t\t\t} catch {\n+\t\t\t\t/* Preserve the intake failure. */\n+\t\t\t}\n+\t\t\tthis.dispose();\n+\t\t\tthrow error;\n+\t\t}\n+\t}\n+\tbytes(oid: string): StoredGitBytes | undefined {\n+\t\tif (this.state === \"closed\") throw Error(\"Git disk intake is closed\");\n+\t\tif (this.state !== \"ready\") return undefined;\n+\t\treturn this.resolver.find(oid);\n+\t}\n+\tread(oid: string): { format: \"content\"; type: string; object: Buffer } | undefined {\n+\t\tconst stored = this.bytes(oid);\n+\t\tif (!stored) return undefined;\n+\t\t// Existing Git consumers require one complete object. This does not claim\n+\t\t// bounded blob consumption; it removes whole-pack retention and caching.\n+\t\tconst object = Buffer.alloc(stored.byteLength);\n+\t\tfor (let at = 0; at < stored.byteLength; at += 65536) {\n+\t\t\tobject.set(stored.read(at, Math.min(65536, stored.byteLength - at)), at);\n+\t\t}\n+\t\treturn { format: \"content\", type: stored.type, object };\n+\t}\n+\tassertReady(): void {\n+\t\tif (this.state !== \"ready\") throw Error(\"Git disk intake was not used by the installed runtime\");\n+\t}\n+\t[Symbol.dispose](): void {\n+\t\tthis.dispose();\n+\t}\n+\tdispose(): void {\n+\t\tif (this.state === \"closed\") return;\n+\t\tthis.state = \"closed\";\n+\t\ttry {\n+\t\t\tthis.resolver.dispose();\n+\t\t} finally {\n+\t\t\tthis.spool.dispose();\n+\t\t}\n+\t}\n+}"}],"unmatched":[]}