{"draft":null,"from":7,"to":318,"files":[{"path":"server/src/git-file-contents.ts","added":108,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-file-contents.ts\n@@ -0,0 +1,108 @@\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import type { FileContents } from \"./memory-fs.ts\";\n+\n+function initialize(storage: SqliteStorage): void {\n+\tstorage.sql.exec(\n+\t\t\"CREATE TABLE IF NOT EXISTS git_fs_meta (scope TEXT, path TEXT, bytes INTEGER, PRIMARY KEY(scope,path))\",\n+\t);\n+\tstorage.sql.exec(\n+\t\t\"CREATE TABLE IF NOT EXISTS git_fs_chunks (scope TEXT, path TEXT, part INTEGER, data BLOB NOT NULL, PRIMARY KEY(scope,path,part))\",\n+\t);\n+\tstorage.sql.exec(\"CREATE TABLE IF NOT EXISTS git_fs_budget (scope TEXT PRIMARY KEY, bytes INTEGER NOT NULL)\");\n+}\n+\n+/** One Git operation's scratch files. Reads materialize one file, never the whole filesystem. */\n+export class GitFileContents implements FileContents {\n+\tstatic recover(storage: SqliteStorage): void {\n+\t\tinitialize(storage);\n+\t\tstorage.transactionSync(() => {\n+\t\t\tfor (const table of [\"git_fs_chunks\", \"git_fs_meta\", \"git_fs_budget\"]) storage.sql.exec(`DELETE FROM ${table}`);\n+\t\t});\n+\t}\n+\treadonly scope = crypto.randomUUID();\n+\tprivate readonly storage: SqliteStorage;\n+\tprivate readonly maxFileBytes: number;\n+\tprivate readonly maxTotalBytes: number;\n+\tprivate closed = false;\n+\tconstructor(storage: SqliteStorage, maxFileBytes: number, maxTotalBytes: number) {\n+\t\tif (![maxFileBytes, maxTotalBytes].every((n) => Number.isSafeInteger(n) && n >= 0))\n+\t\t\tthrow Error(\"invalid Git scratch limits\");\n+\t\tthis.storage = storage;\n+\t\tthis.maxFileBytes = maxFileBytes;\n+\t\tthis.maxTotalBytes = maxTotalBytes;\n+\t\tinitialize(storage);\n+\t\tstorage.sql.exec(\"INSERT INTO git_fs_budget VALUES (?, 0)\", this.scope);\n+\t}\n+\tprivate active(): void {\n+\t\tif (this.closed) throw Error(\"Git scratch files are closed\");\n+\t}\n+\tprivate size(path: string): number | undefined {\n+\t\tconst rows = this.storage.sql\n+\t\t\t.exec(\"SELECT bytes FROM git_fs_meta WHERE scope = ? AND path = ?\", this.scope, path)\n+\t\t\t.toArray() as { bytes: number }[];\n+\t\treturn rows[0]?.bytes;\n+\t}\n+\twrite(path: string, bytes: Uint8Array): void {\n+\t\tthis.active();\n+\t\tif (bytes.byteLength > this.maxFileBytes) throw Error(\"Git scratch file exceeds byte limit\");\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tconst old = this.size(path) ?? 0;\n+\t\t\tconst rows = this.storage.sql.exec(\"SELECT bytes FROM git_fs_budget WHERE scope = ?\", this.scope).toArray() as {\n+\t\t\t\tbytes: number;\n+\t\t\t}[];\n+\t\t\tif (rows.length !== 1) throw Error(\"missing Git scratch budget\");\n+\t\t\tconst next = rows[0]!.bytes - old + bytes.byteLength;\n+\t\t\tif (!Number.isSafeInteger(next) || next < 0 || next > this.maxTotalBytes)\n+\t\t\t\tthrow Error(\"Git scratch exceeds total byte limit\");\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_fs_chunks WHERE scope = ? AND path = ?\", this.scope, path);\n+\t\t\tfor (let at = 0; at < bytes.byteLength; at += 65536)\n+\t\t\t\tthis.storage.sql.exec(\n+\t\t\t\t\t\"INSERT INTO git_fs_chunks VALUES (?, ?, ?, ?)\",\n+\t\t\t\t\tthis.scope,\n+\t\t\t\t\tpath,\n+\t\t\t\t\tat / 65536,\n+\t\t\t\t\tbytes.slice(at, at + 65536),\n+\t\t\t\t);\n+\t\t\tthis.storage.sql.exec(\"INSERT OR REPLACE INTO git_fs_meta VALUES (?, ?, ?)\", this.scope, path, bytes.byteLength);\n+\t\t\tthis.storage.sql.exec(\"UPDATE git_fs_budget SET bytes = ? WHERE scope = ?\", next, this.scope);\n+\t\t});\n+\t}\n+\tread(path: string): Uint8Array {\n+\t\tthis.active();\n+\t\tconst size = this.size(path);\n+\t\tif (size === undefined || !Number.isSafeInteger(size) || size < 0 || size > this.maxFileBytes)\n+\t\t\tthrow Error(\"invalid Git scratch file\");\n+\t\tconst bytes = new Uint8Array(size);\n+\t\tfor (let at = 0; at < size; at += 65536) {\n+\t\t\tconst rows = this.storage.sql\n+\t\t\t\t.exec(\"SELECT data FROM git_fs_chunks WHERE scope = ? AND path = ? AND part = ?\", this.scope, path, at / 65536)\n+\t\t\t\t.toArray() as { data: ArrayBuffer }[];\n+\t\t\tif (rows.length !== 1) throw Error(\"missing Git scratch chunk\");\n+\t\t\tconst chunk = new Uint8Array(rows[0]!.data);\n+\t\t\tif (chunk.byteLength !== Math.min(65536, size - at)) throw Error(\"invalid Git scratch chunk\");\n+\t\t\tbytes.set(chunk, at);\n+\t\t}\n+\t\treturn bytes;\n+\t}\n+\tremove(path: string): void {\n+\t\tthis.active();\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tconst size = this.size(path);\n+\t\t\tif (size === undefined) throw Error(\"missing Git scratch file\");\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_fs_chunks WHERE scope = ? AND path = ?\", this.scope, path);\n+\t\t\tthis.storage.sql.exec(\"DELETE FROM git_fs_meta WHERE scope = ? AND path = ?\", this.scope, path);\n+\t\t\tthis.storage.sql.exec(\"UPDATE git_fs_budget SET bytes = bytes - ? WHERE scope = ?\", size, this.scope);\n+\t\t});\n+\t}\n+\t[Symbol.dispose](): void {\n+\t\tthis.dispose();\n+\t}\n+\tdispose(): void {\n+\t\tif (this.closed) return;\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tfor (const table of [\"git_fs_chunks\", \"git_fs_meta\", \"git_fs_budget\"])\n+\t\t\t\tthis.storage.sql.exec(`DELETE FROM ${table} WHERE scope = ?`, this.scope);\n+\t\t});\n+\t\tthis.closed = true;\n+\t}\n+}"}],"unmatched":[]}