{"draft":null,"from":44,"to":318,"files":[{"path":"server/src/git-pack-spool.ts","added":121,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-pack-spool.ts\n@@ -0,0 +1,121 @@\n+import { createHash } from \"node:crypto\";\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+\n+/** Temporary verified pack bytes. No whole-pack read API is exposed. */\n+export class GitPackSpool {\n+\tstatic readonly chunkBytes = 65536;\n+\t/** Cold Durable Object construction only, before any intake starts. */\n+\tstatic recover(storage: SqliteStorage): void {\n+\t\tGitPackSpool.initialize(storage);\n+\t\tstorage.sql.exec(\"DELETE FROM git_pack_spool\");\n+\t}\n+\tprivate static initialize(storage: SqliteStorage): void {\n+\t\tstorage.sql.exec(\n+\t\t\t\"CREATE TABLE IF NOT EXISTS git_pack_spool (id TEXT NOT NULL, part INTEGER NOT NULL, data BLOB NOT NULL, PRIMARY KEY (id, part))\",\n+\t\t);\n+\t}\n+\treadonly id = crypto.randomUUID();\n+\tprivate size = 0;\n+\tprivate sealed = false;\n+\tprivate closed = false;\n+\tprivate readonly storage: SqliteStorage;\n+\tconstructor(storage: SqliteStorage) {\n+\t\tthis.storage = storage;\n+\t\tGitPackSpool.initialize(storage);\n+\t}\n+\tget byteLength(): number {\n+\t\tthis.assertReadable();\n+\t\treturn this.size;\n+\t}\n+\tprivate assertReadable(): void {\n+\t\tif (!this.sealed || this.closed) throw Error(\"pack spool is not readable\");\n+\t}\n+\tasync ingest(chunks: AsyncIterable<Uint8Array> | Iterable<Uint8Array>, maxBytes: number): Promise<string> {\n+\t\tif (this.size || this.sealed || this.closed) throw Error(\"pack spool already used\");\n+\t\tif (!Number.isSafeInteger(maxBytes) || maxBytes < 32) throw Error(\"invalid pack byte limit\");\n+\t\tconst hash = createHash(\"sha1\");\n+\t\tlet tail = new Uint8Array(0);\n+\t\tlet pending = new Uint8Array(GitPackSpool.chunkBytes),\n+\t\t\tused = 0,\n+\t\t\tpart = 0;\n+\t\tconst save = () => {\n+\t\t\tif (this.closed) throw Error(\"pack spool is closed\");\n+\t\t\tthis.storage.sql.exec(\"INSERT INTO git_pack_spool VALUES (?, ?, ?)\", this.id, part++, pending.slice(0, used));\n+\t\t\tused = 0;\n+\t\t};\n+\t\ttry {\n+\t\t\tfor await (const chunk of chunks) {\n+\t\t\t\tif (this.closed) throw Error(\"pack spool is closed\");\n+\t\t\t\tif (!(chunk instanceof Uint8Array)) throw Error(\"invalid pack chunk\");\n+\t\t\t\tfor (let at = 0; at < chunk.length; ) {\n+\t\t\t\t\tconst count = Math.min(GitPackSpool.chunkBytes - used, chunk.length - at);\n+\t\t\t\t\tif (this.size + count > maxBytes) throw new GitIntakeLimitError(\"pack\", \"pack exceeds byte limit\");\n+\t\t\t\t\tconst piece = chunk.subarray(at, at + count);\n+\t\t\t\t\tconst joined = new Uint8Array(tail.length + piece.length);\n+\t\t\t\t\tjoined.set(tail);\n+\t\t\t\t\tjoined.set(piece, tail.length);\n+\t\t\t\t\tconst end = Math.max(0, joined.length - 20);\n+\t\t\t\t\thash.update(joined.subarray(0, end));\n+\t\t\t\t\ttail = joined.slice(end);\n+\t\t\t\t\tpending.set(piece, used);\n+\t\t\t\t\tused += count;\n+\t\t\t\t\tat += count;\n+\t\t\t\t\tthis.size += count;\n+\t\t\t\t\tif (used === pending.length) save();\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (used) save();\n+\t\t\tif (this.size < 32 || tail.length !== 20) throw Error(\"truncated pack\");\n+\t\t\tconst digest = hash.digest(\"hex\");\n+\t\t\tif (digest !== Buffer.from(tail).toString(\"hex\")) throw Error(\"pack checksum mismatch\");\n+\t\t\tthis.sealed = true;\n+\t\t\tconst header = this.read(0, 12);\n+\t\t\tif (new TextDecoder().decode(header.subarray(0, 4)) !== \"PACK\" || new DataView(header.buffer).getUint32(4) !== 2)\n+\t\t\t\tthrow Error(\"unsupported pack header\");\n+\t\t\treturn digest;\n+\t\t} catch (error) {\n+\t\t\tthis.dispose();\n+\t\t\tthrow error;\n+\t\t}\n+\t}\n+\tread(start: number, length: number): Uint8Array {\n+\t\tthis.assertReadable();\n+\t\tif (\n+\t\t\t!Number.isSafeInteger(start) ||\n+\t\t\t!Number.isSafeInteger(length) ||\n+\t\t\tstart < 0 ||\n+\t\t\tlength < 0 ||\n+\t\t\tlength > GitPackSpool.chunkBytes ||\n+\t\t\tstart + length > this.size\n+\t\t)\n+\t\t\tthrow Error(\"invalid pack byte range\");\n+\t\tconst result = new Uint8Array(length);\n+\t\tlet copied = 0;\n+\t\twhile (copied < length) {\n+\t\t\tconst position = start + copied,\n+\t\t\t\tpart = Math.floor(position / GitPackSpool.chunkBytes);\n+\t\t\tconst rows = this.storage.sql\n+\t\t\t\t.exec(\"SELECT data FROM git_pack_spool WHERE id = ? AND part = ?\", this.id, part)\n+\t\t\t\t.toArray() as { data: ArrayBuffer }[];\n+\t\t\tif (rows.length !== 1) throw Error(\"missing pack chunk\");\n+\t\t\tconst data = new Uint8Array(rows[0]!.data),\n+\t\t\t\toffset = position % GitPackSpool.chunkBytes;\n+\t\t\tconst count = Math.min(length - copied, data.length - offset);\n+\t\t\tif (count <= 0 || data.length > GitPackSpool.chunkBytes) throw Error(\"invalid pack chunk\");\n+\t\t\tresult.set(data.subarray(offset, offset + count), copied);\n+\t\t\tcopied += count;\n+\t\t}\n+\t\treturn result;\n+\t}\n+\t*chunks(): IterableIterator<Uint8Array> {\n+\t\tthis.assertReadable();\n+\t\tfor (let start = 0; start < this.size; start += GitPackSpool.chunkBytes)\n+\t\t\tyield this.read(start, Math.min(GitPackSpool.chunkBytes, this.size - start));\n+\t}\n+\tdispose(): void {\n+\t\tthis.closed = true;\n+\t\tthis.storage.sql.exec(\"DELETE FROM git_pack_spool WHERE id = ?\", this.id);\n+\t}\n+}\n+\n+import { GitIntakeLimitError } from \"./git-intake-limit.ts\";"}],"unmatched":[]}