Versions 157 to 318
new file, 77 lines
1+import { StoredText } from "../../src/engine/stored-text.ts"; 2+import type { SqliteStorage } from "./engine-project.ts"; 3+import type { PushedCommits } from "./ingest.ts"; 4+import { SqliteWriterState } from "./writer-host.ts"; 6+type Tree = Pick<PushedCommits["commits"][number], "files" | "modes" | "blobs">; 8+/** Keeps earlier Git trees on disk while later commits are decoded. */ 9+export class GitIntakeTrees { 10+ /** Only at a cold Durable Object start, before any intake can be active. */ 11+ static recover(storage: SqliteStorage): void { 12+ SqliteWriterState.initialize(storage); 13+ storage.sql.exec("DELETE FROM writer_state WHERE namespace GLOB 'git-intake:*'"); 15+ readonly namespace = `git-intake:${crypto.randomUUID()}`; 16+ readonly storage: SqliteStorage; 17+ readonly text: StoredText; 19+ private active = true; 20+ constructor(storage: SqliteStorage) { 21+ this.storage = storage; 22+ SqliteWriterState.initialize(storage); 23+ this.text = new StoredText(new SqliteWriterState(storage, this.namespace), "files"); 25+ /** Created outside any scope containing decoded file text or a whole tree. */ 26+ private getter(key: string): () => string { 28+ if (!this.active) throw Error("Git intake trees are closed"); 29+ return this.text.read(key); 32+ retainFile(files: Record<string, string>, path: string, body: string, oid?: string): void { 37+ for (let at = 0; at < body.length; at += StoredText.chunkCharacters) 38+ yield body.slice(at, at + StoredText.chunkCharacters); 43+ retainChunks(files: Record<string, string>, path: string, chunks: Iterable<string>, oid?: string): void { 44+ if (!this.active) throw Error("Git intake trees are closed"); 45+ if (oid !== undefined && !/^[0-9a-f]{40}$/.test(oid)) throw Error("Invalid Git blob id"); 46+ const key = oid === undefined ? String(this.next++) : `blob:${oid}`; 47+ this.storage.transactionSync(() => { 48+ if (this.text.storage.read(this.text.collection, key) === undefined) this.text.writeChunks(key, chunks); 50+ Object.defineProperty(files, path, { 52+ get: this.getter(key), 55+ retain(tree: Tree): Tree { 56+ if (!this.active) throw Error("Git intake trees are closed"); 57+ const files: Record<string, string> = Object.create(null); 58+ this.storage.transactionSync(() => { 59+ for (const path of Object.keys(tree.files)) { 60+ const key = String(this.next++); 61+ this.text.write(key, tree.files[path]!); 62+ Object.defineProperty(files, path, { 64+ get: this.getter(key), 68+ return { files, modes: tree.modes, blobs: tree.blobs }; 70+ [Symbol.dispose](): void { 75+ this.storage.sql.exec("DELETE FROM writer_state WHERE namespace = ?", this.namespace);