{"draft":null,"from":39,"to":318,"files":[{"path":"server/src/git-pack-resolver.ts","added":133,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-pack-resolver.ts\n@@ -0,0 +1,133 @@\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import { applyGitDeltaChunks } from \"./git-delta-stream.ts\";\n+import { GitObjectBytes, type StoredGitBytes } from \"./git-object-bytes.ts\";\n+\n+/** Scratch index and decoded objects for one pack. No in-memory object map. */\n+export class GitPackResolver {\n+\treadonly scope = crypto.randomUUID();\n+\treadonly objects: GitObjectBytes;\n+\tprivate readonly storage: SqliteStorage;\n+\tprivate readonly maxObjectBytes: number;\n+\tprivate closed = false;\n+\tconstructor(storage: SqliteStorage, maxObjectBytes: number, maxDecodedBytes = maxObjectBytes) {\n+\t\tif (!Number.isSafeInteger(maxObjectBytes) || maxObjectBytes < 0) throw Error(\"invalid object limit\");\n+\t\tthis.storage = storage;\n+\t\tthis.maxObjectBytes = maxObjectBytes;\n+\t\tthis.objects = new GitObjectBytes(storage, maxDecodedBytes);\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS git_pack_resolve (scope TEXT, offset INTEGER, raw TEXT, base_offset INTEGER, base_oid TEXT, result TEXT, oid TEXT, depth INTEGER, PRIMARY KEY(scope,offset))\",\n+\t\t);\n+\t\tstorage.sql.exec(\"CREATE INDEX IF NOT EXISTS git_pack_resolve_oid ON git_pack_resolve(scope,oid)\");\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE INDEX IF NOT EXISTS git_pack_unresolved ON git_pack_resolve(scope,offset) WHERE result IS NULL\",\n+\t\t);\n+\t}\n+\tprivate active(): void {\n+\t\tif (this.closed) throw Error(\"pack resolver is closed\");\n+\t}\n+\tadd(offset: number, type: string, data: Uint8Array, base?: number | string): void {\n+\t\tthis.addChunks(offset, type, (write) => write(data), base);\n+\t}\n+\t/** The inflater writes bounded output directly into the transactional object store. */\n+\taddChunks(\n+\t\toffset: number,\n+\t\ttype: string,\n+\t\tproduce: (write: (bytes: Uint8Array) => void) => void,\n+\t\tbase?: number | string,\n+\t): void {\n+\t\tthis.active();\n+\t\tif (!Number.isSafeInteger(offset) || offset < 12) throw Error(\"invalid pack offset\");\n+\t\tif (type === \"delta\") {\n+\t\t\tif (\n+\t\t\t\ttypeof base === \"number\"\n+\t\t\t\t\t? !Number.isSafeInteger(base) || base < 12 || base >= offset\n+\t\t\t\t\t: typeof base !== \"string\" || !/^[0-9a-f]{40}$/.test(base)\n+\t\t\t)\n+\t\t\t\tthrow Error(\"invalid delta base reference\");\n+\t\t} else if (base !== undefined) throw Error(\"ordinary object has delta base\");\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tconst stored = this.objects.put(type === \"delta\" ? \"blob\" : type, produce, this.maxObjectBytes, type === \"delta\");\n+\t\t\tthis.storage.sql.exec(\n+\t\t\t\t\"INSERT INTO git_pack_resolve VALUES (?, ?, ?, ?, ?, ?, ?, ?)\",\n+\t\t\t\tthis.scope,\n+\t\t\t\toffset,\n+\t\t\t\tstored.id,\n+\t\t\t\ttypeof base === \"number\" ? base : null,\n+\t\t\t\ttypeof base === \"string\" ? base : null,\n+\t\t\t\ttype === \"delta\" ? null : stored.id,\n+\t\t\t\ttype === \"delta\" ? null : stored.oid,\n+\t\t\t\ttype === \"delta\" ? null : 0,\n+\t\t\t);\n+\t\t});\n+\t}\n+\tresolve(): void {\n+\t\tthis.active();\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tfor (;;) {\n+\t\t\t\tconst rows = this.storage.sql\n+\t\t\t\t\t.exec(\n+\t\t\t\t\t\t`SELECT p.offset, p.raw, COALESCE(b.result, r.result) AS base, COALESCE(b.depth, r.depth) AS depth\n+FROM git_pack_resolve p\n+LEFT JOIN git_pack_resolve b ON b.scope = p.scope AND b.offset = p.base_offset\n+LEFT JOIN git_pack_resolve r ON r.scope = p.scope AND r.oid = p.base_oid\n+WHERE p.scope = ? AND p.result IS NULL AND (b.result IS NOT NULL OR r.result IS NOT NULL)\n+ORDER BY p.offset LIMIT 1`,\n+\t\t\t\t\t\tthis.scope,\n+\t\t\t\t\t)\n+\t\t\t\t\t.toArray() as {\n+\t\t\t\t\toffset: number;\n+\t\t\t\t\traw: string;\n+\t\t\t\t\tbase: string;\n+\t\t\t\t\tdepth: number;\n+\t\t\t\t}[];\n+\t\t\t\tif (!rows.length) break;\n+\t\t\t\tconst row = rows[0]!;\n+\t\t\t\tif (row.depth >= 64) throw Error(\"Git delta chain exceeds depth limit\");\n+\t\t\t\tconst base = this.objects.get(row.base),\n+\t\t\t\t\tdelta = this.objects.get(row.raw);\n+\t\t\t\tconst result = this.objects.put(\n+\t\t\t\t\tbase.type,\n+\t\t\t\t\t(write) => {\n+\t\t\t\t\t\tapplyGitDeltaChunks(delta, base, write, this.maxObjectBytes);\n+\t\t\t\t\t},\n+\t\t\t\t\tthis.maxObjectBytes,\n+\t\t\t\t);\n+\t\t\t\tthis.storage.sql.exec(\n+\t\t\t\t\t\"UPDATE git_pack_resolve SET result = ?, oid = ?, depth = ? WHERE scope = ? AND offset = ?\",\n+\t\t\t\t\tresult.id,\n+\t\t\t\t\tresult.oid,\n+\t\t\t\t\trow.depth + 1,\n+\t\t\t\t\tthis.scope,\n+\t\t\t\t\trow.offset,\n+\t\t\t\t);\n+\t\t\t\tthis.objects.remove(row.raw);\n+\t\t\t}\n+\t\t\tif (\n+\t\t\t\tthis.storage.sql\n+\t\t\t\t\t.exec(\"SELECT offset FROM git_pack_resolve WHERE scope = ? AND result IS NULL LIMIT 1\", this.scope)\n+\t\t\t\t\t.toArray().length\n+\t\t\t)\n+\t\t\t\tthrow Error(\"unresolved or cyclic Git delta base\");\n+\t\t});\n+\t}\n+\tget(oid: string): StoredGitBytes {\n+\t\tconst value = this.find(oid);\n+\t\tif (!value) throw Error(\"missing packed Git object\");\n+\t\treturn value;\n+\t}\n+\tfind(oid: string): StoredGitBytes | undefined {\n+\t\tthis.active();\n+\t\tconst rows = this.storage.sql\n+\t\t\t.exec(\"SELECT result FROM git_pack_resolve WHERE scope = ? AND oid = ? LIMIT 1\", this.scope, oid)\n+\t\t\t.toArray() as { result: string }[];\n+\t\tif (!rows.length) return undefined;\n+\t\treturn this.objects.get(rows[0]!.result);\n+\t}\n+\tdispose(): void {\n+\t\tthis.closed = true;\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_pack_resolve WHERE scope = ?\", this.scope);\n+\t\t\tthis.objects.dispose();\n+\t\t});\n+\t}\n+}"}],"unmatched":[]}