{"draft":null,"from":39,"to":318,"files":[{"path":"server/src/git-object-bytes.ts","added":158,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-object-bytes.ts\n@@ -0,0 +1,158 @@\n+import { createHash } from \"node:crypto\";\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import type { GitByteRanges } from \"./git-delta-stream.ts\";\n+import { GitIntakeLimitError } from \"./git-intake-limit.ts\";\n+\n+export type StoredGitBytes = GitByteRanges & {\n+\tid: string;\n+\toid: string;\n+\ttype: string;\n+};\n+\n+/** Intake-local object bytes; callers own this store's lifetime. */\n+export class GitObjectBytes {\n+\treadonly scope = crypto.randomUUID();\n+\tprivate closed = false;\n+\tprivate readonly storage: SqliteStorage;\n+\tprivate readonly maxDecodedBytes: number;\n+\tconstructor(storage: SqliteStorage, maxDecodedBytes = Number.MAX_SAFE_INTEGER) {\n+\t\tif (!Number.isSafeInteger(maxDecodedBytes) || maxDecodedBytes < 0) throw Error(\"invalid decoded byte limit\");\n+\t\tthis.storage = storage;\n+\t\tthis.maxDecodedBytes = maxDecodedBytes;\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS git_object_bytes (scope TEXT, id TEXT, part INTEGER, data BLOB NOT NULL, PRIMARY KEY(scope,id,part))\",\n+\t\t);\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS git_object_meta (scope TEXT, id TEXT, type TEXT, bytes INTEGER, oid TEXT, PRIMARY KEY(scope,id))\",\n+\t\t);\n+\t\tstorage.sql.exec(\"CREATE TABLE IF NOT EXISTS git_object_budget (scope TEXT PRIMARY KEY, decoded INTEGER NOT NULL)\");\n+\t\tstorage.sql.exec(\"INSERT INTO git_object_budget VALUES (?, 0)\", this.scope);\n+\t}\n+\tprivate active(): void {\n+\t\tif (this.closed) throw Error(\"Git object store is closed\");\n+\t}\n+\tput(\n+\t\ttype: string,\n+\t\tproduce: (write: (bytes: Uint8Array) => void) => void,\n+\t\tmaxBytes: number,\n+\t\trawDelta = false,\n+\t): StoredGitBytes {\n+\t\tthis.active();\n+\t\tif (![\"blob\", \"tree\", \"commit\", \"tag\"].includes(type) || !Number.isSafeInteger(maxBytes) || maxBytes < 0)\n+\t\t\tthrow Error(\"invalid Git object limits or type\");\n+\t\tconst id = crypto.randomUUID();\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tconst budgetScope = rawDelta ? `${this.scope}:raw` : this.scope;\n+\t\t\tif (rawDelta) this.storage.sql.exec(\"INSERT OR IGNORE INTO git_object_budget VALUES (?, 0)\", budgetScope);\n+\t\t\tconst budget = this.storage.sql\n+\t\t\t\t.exec(\"SELECT decoded FROM git_object_budget WHERE scope = ?\", budgetScope)\n+\t\t\t\t.toArray() as { decoded: number }[];\n+\t\t\tif (budget.length !== 1) throw Error(\"missing Git decoded budget\");\n+\t\t\tconst remaining = this.maxDecodedBytes - budget[0]!.decoded;\n+\t\t\tlet length = 0,\n+\t\t\t\tpart = 0,\n+\t\t\t\tused = 0;\n+\t\t\tconst pending = new Uint8Array(65536);\n+\t\t\tconst flush = () => {\n+\t\t\t\tthis.storage.sql.exec(\n+\t\t\t\t\t\"INSERT INTO git_object_bytes VALUES (?, ?, ?, ?)\",\n+\t\t\t\t\tthis.scope,\n+\t\t\t\t\tid,\n+\t\t\t\t\tpart++,\n+\t\t\t\t\tpending.slice(0, used),\n+\t\t\t\t);\n+\t\t\t\tused = 0;\n+\t\t\t};\n+\t\t\tproduce((bytes) => {\n+\t\t\t\tif (length + bytes.length > maxBytes) throw new GitIntakeLimitError(\"object\", \"Git object exceeds byte limit\");\n+\t\t\t\tif (length + bytes.length > remaining)\n+\t\t\t\t\tthrow new GitIntakeLimitError(\n+\t\t\t\t\t\trawDelta ? \"delta\" : \"decoded\",\n+\t\t\t\t\t\trawDelta ? \"Git pack exceeds total delta byte limit\" : \"Git pack exceeds total decoded byte limit\",\n+\t\t\t\t\t);\n+\t\t\t\tlength += bytes.length;\n+\t\t\t\tfor (let at = 0; at < bytes.length; ) {\n+\t\t\t\t\tconst count = Math.min(pending.length - used, bytes.length - at);\n+\t\t\t\t\tpending.set(bytes.subarray(at, at + count), used);\n+\t\t\t\t\tat += count;\n+\t\t\t\t\tused += count;\n+\t\t\t\t\tif (used === pending.length) flush();\n+\t\t\t\t}\n+\t\t\t});\n+\t\t\tif (used) flush();\n+\t\t\tconst hash = createHash(\"sha1\").update(`${type} ${length}\\0`);\n+\t\t\tfor (let index = 0; index < part; index++) hash.update(this.chunk(id, index));\n+\t\t\tthis.storage.sql.exec(\n+\t\t\t\t\"INSERT INTO git_object_meta VALUES (?, ?, ?, ?, ?)\",\n+\t\t\t\tthis.scope,\n+\t\t\t\tid,\n+\t\t\t\ttype,\n+\t\t\t\tlength,\n+\t\t\t\thash.digest(\"hex\"),\n+\t\t\t);\n+\t\t\tthis.storage.sql.exec(\"UPDATE git_object_budget SET decoded = decoded + ? WHERE scope = ?\", length, budgetScope);\n+\t\t});\n+\t\treturn this.get(id);\n+\t}\n+\tprivate chunk(id: string, part: number): Uint8Array {\n+\t\tthis.active();\n+\t\tconst rows = this.storage.sql\n+\t\t\t.exec(\"SELECT data FROM git_object_bytes WHERE scope = ? AND id = ? AND part = ?\", this.scope, id, part)\n+\t\t\t.toArray() as { data: ArrayBuffer }[];\n+\t\tif (rows.length !== 1) throw Error(\"missing Git object chunk\");\n+\t\tconst bytes = new Uint8Array(rows[0]!.data);\n+\t\tif (!bytes.length || bytes.length > 65536) throw Error(\"invalid Git object chunk\");\n+\t\treturn bytes;\n+\t}\n+\tget(id: string): StoredGitBytes {\n+\t\tthis.active();\n+\t\tconst rows = this.storage.sql\n+\t\t\t.exec(\"SELECT type, bytes, oid FROM git_object_meta WHERE scope = ? AND id = ?\", this.scope, id)\n+\t\t\t.toArray() as { type: string; bytes: number; oid: string }[];\n+\t\tif (rows.length !== 1) throw Error(\"missing Git object\");\n+\t\tconst meta = rows[0]!;\n+\t\treturn {\n+\t\t\tid,\n+\t\t\toid: meta.oid,\n+\t\t\ttype: meta.type,\n+\t\t\tbyteLength: meta.bytes,\n+\t\t\tread: (start, length) => {\n+\t\t\t\tthis.active();\n+\t\t\t\tif (\n+\t\t\t\t\t!Number.isSafeInteger(start) ||\n+\t\t\t\t\t!Number.isSafeInteger(length) ||\n+\t\t\t\t\tstart < 0 ||\n+\t\t\t\t\tlength < 0 ||\n+\t\t\t\t\tlength > 65536 ||\n+\t\t\t\t\tstart + length > meta.bytes\n+\t\t\t\t)\n+\t\t\t\t\tthrow Error(\"invalid Git object range\");\n+\t\t\t\tconst result = new Uint8Array(length);\n+\t\t\t\tfor (let copied = 0; copied < length; ) {\n+\t\t\t\t\tconst position = start + copied,\n+\t\t\t\t\t\tchunk = this.chunk(id, Math.floor(position / 65536));\n+\t\t\t\t\tconst offset = position % 65536,\n+\t\t\t\t\t\tcount = Math.min(length - copied, chunk.length - offset);\n+\t\t\t\t\tif (count <= 0) throw Error(\"truncated Git object chunk\");\n+\t\t\t\t\tresult.set(chunk.subarray(offset, offset + count), copied);\n+\t\t\t\t\tcopied += count;\n+\t\t\t\t}\n+\t\t\t\treturn result;\n+\t\t\t},\n+\t\t};\n+\t}\n+\t/** Remove obsolete delta instructions without refunding their decoded-work budget. */\n+\tremove(id: string): void {\n+\t\tthis.active();\n+\t\tthis.storage.sql.exec(\"DELETE FROM git_object_bytes WHERE scope = ? AND id = ?\", this.scope, id);\n+\t\tthis.storage.sql.exec(\"DELETE FROM git_object_meta WHERE scope = ? AND id = ?\", this.scope, id);\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_object_bytes WHERE scope = ?\", this.scope);\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_object_meta WHERE scope = ?\", this.scope);\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_object_budget WHERE scope IN (?, ?)\", this.scope, `${this.scope}:raw`);\n+\t\t});\n+\t}\n+}"}],"unmatched":[]}