Versions 28 to 318
new file, 102 lines
1+import type { SqliteStorage } from "./engine-project.ts"; 2+import type { StoredGitBytes } from "./git-object-bytes.ts"; 3+import { decodeGitPack, type PackInflater } from "./git-pack-decode.ts"; 4+import { GitPackResolver } from "./git-pack-resolver.ts"; 5+import { GitPackSpool } from "./git-pack-spool.ts"; 7+/** One clone's temporary pack store. The caller must dispose it in finally. */ 8+export class GitDiskIntake { 9+ /** Cold object construction only, before any intake or reader is created. */ 10+ static recover(storage: SqliteStorage): void { 11+ // Constructors establish scratch tables only; they do not alter project state. 12+ const resolver = new GitPackResolver(storage, 0); 13+ storage.transactionSync(() => { 14+ GitPackSpool.recover(storage); 15+ storage.sql.exec("DELETE FROM git_pack_resolve"); 16+ storage.sql.exec("DELETE FROM git_object_bytes"); 17+ storage.sql.exec("DELETE FROM git_object_meta"); 18+ storage.sql.exec("DELETE FROM git_object_budget"); 22+ private readonly spool: GitPackSpool; 23+ private readonly resolver: GitPackResolver; 24+ private readonly maxPackBytes: number; 25+ private state: "new" | "loading" | "ready" | "closed" = "new"; 26+ constructor(storage: SqliteStorage, maxPackBytes: number, maxObjectBytes: number, maxDecodedBytes = maxPackBytes) { 27+ if (!Number.isSafeInteger(maxPackBytes) || maxPackBytes < 32) throw Error("invalid pack limit"); 28+ this.maxPackBytes = maxPackBytes; 29+ this.resolver = new GitPackResolver(storage, maxObjectBytes, maxDecodedBytes); 30+ this.spool = new GitPackSpool(storage); 32+ attach(cache: object): void { 33+ const key = Symbol.for("abso.git.disk-pack"); 34+ if (key in cache) throw Error("Git disk intake already attached"); 35+ Object.defineProperty(cache, key, { value: this }); 38+ chunks: AsyncIterable<Uint8Array> | Iterable<Uint8Array> | AsyncIterator<Uint8Array>, 39+ createInflater: () => PackInflater, 41+ if (this.state !== "new") throw Error("Git disk intake already used"); 42+ this.state = "loading"; 44+ Symbol.asyncIterator in chunks 45+ ? chunks[Symbol.asyncIterator]() 46+ : Symbol.iterator in chunks 47+ ? chunks[Symbol.iterator]() 51+ [Symbol.asyncIterator]: () => ({ 52+ next: async () => iterator.next(), 53+ return: async () => (iterator.return ? iterator.return() : { done: true as const, value: undefined }), 56+ await this.spool.ingest(source, this.maxPackBytes); 57+ if (this.state !== "loading") throw Error("Git disk intake closed during download"); 58+ decodeGitPack(this.spool, this.resolver, createInflater); 59+ this.spool.dispose(); 60+ this.state = "ready"; 63+ await iterator.return?.(); 65+ /* Preserve the intake failure. */ 71+ bytes(oid: string): StoredGitBytes | undefined { 72+ if (this.state === "closed") throw Error("Git disk intake is closed"); 73+ if (this.state !== "ready") return undefined; 74+ return this.resolver.find(oid); 76+ read(oid: string): { format: "content"; type: string; object: Buffer } | undefined { 77+ const stored = this.bytes(oid); 78+ if (!stored) return undefined; 79+ // Existing Git consumers require one complete object. This does not claim 80+ // bounded blob consumption; it removes whole-pack retention and caching. 81+ const object = Buffer.alloc(stored.byteLength); 82+ for (let at = 0; at < stored.byteLength; at += 65536) { 83+ object.set(stored.read(at, Math.min(65536, stored.byteLength - at)), at); 85+ return { format: "content", type: stored.type, object }; 87+ assertReady(): void { 88+ if (this.state !== "ready") throw Error("Git disk intake was not used by the installed runtime"); 90+ [Symbol.dispose](): void { 94+ if (this.state === "closed") return; 95+ this.state = "closed"; 97+ this.resolver.dispose(); 99+ this.spool.dispose();