- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/src/git-object-bytes.ts?version=178&format=json
146 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 178, 1 version changed it
You're reading version 178. The newest is 318, and 1 version since then changed this file.
1import { createHash } from "node:crypto";
2import type { SqliteStorage } from "./engine-project.ts";
3import type { GitByteRanges } from "./git-delta-stream.ts";
5export type StoredGitBytes = GitByteRanges & {
6 id: string;
7 oid: string;
8 type: string;
9};
11/** Intake-local object bytes; callers own this store's lifetime. */
12export class GitObjectBytes {
13 readonly scope = crypto.randomUUID();
14 private closed = false;
15 private readonly storage: SqliteStorage;
16 private readonly maxDecodedBytes: number;
17 constructor(storage: SqliteStorage, maxDecodedBytes = Number.MAX_SAFE_INTEGER) {
18 if (!Number.isSafeInteger(maxDecodedBytes) || maxDecodedBytes < 0) throw Error("invalid decoded byte limit");
19 this.storage = storage;
20 this.maxDecodedBytes = maxDecodedBytes;
21 storage.sql.exec(
22 "CREATE TABLE IF NOT EXISTS git_object_bytes (scope TEXT, id TEXT, part INTEGER, data BLOB NOT NULL, PRIMARY KEY(scope,id,part))",
23 );
24 storage.sql.exec(
25 "CREATE TABLE IF NOT EXISTS git_object_meta (scope TEXT, id TEXT, type TEXT, bytes INTEGER, oid TEXT, PRIMARY KEY(scope,id))",
26 );
27 storage.sql.exec("CREATE TABLE IF NOT EXISTS git_object_budget (scope TEXT PRIMARY KEY, decoded INTEGER NOT NULL)");
28 storage.sql.exec("INSERT INTO git_object_budget VALUES (?, 0)", this.scope);
29 }
30 private active(): void {
31 if (this.closed) throw Error("Git object store is closed");
32 }
33 put(type: string, produce: (write: (bytes: Uint8Array) => void) => void, maxBytes: number): StoredGitBytes {
34 this.active();
35 if (!["blob", "tree", "commit", "tag"].includes(type) || !Number.isSafeInteger(maxBytes) || maxBytes < 0)
36 throw Error("invalid Git object limits or type");
37 const id = crypto.randomUUID();
38 this.storage.transactionSync(() => {
39 const budget = this.storage.sql
40 .exec("SELECT decoded FROM git_object_budget WHERE scope = ?", this.scope)
41 .toArray() as { decoded: number }[];
42 if (budget.length !== 1) throw Error("missing Git decoded budget");
43 const remaining = this.maxDecodedBytes - budget[0]!.decoded;
44 let length = 0,
45 part = 0,
46 used = 0;
47 const pending = new Uint8Array(65536);
48 const flush = () => {
49 this.storage.sql.exec(
50 "INSERT INTO git_object_bytes VALUES (?, ?, ?, ?)",
51 this.scope,
52 id,
53 part++,
54 pending.slice(0, used),
55 );
56 used = 0;
57 };
58 produce((bytes) => {
59 if (length + bytes.length > maxBytes) throw Error("Git object exceeds byte limit");
60 if (length + bytes.length > remaining) throw Error("Git pack exceeds total decoded byte limit");
61 length += bytes.length;
62 for (let at = 0; at < bytes.length; ) {
63 const count = Math.min(pending.length - used, bytes.length - at);
64 pending.set(bytes.subarray(at, at + count), used);
65 at += count;
66 used += count;
67 if (used === pending.length) flush();
68 }
69 });
70 if (used) flush();
71 const hash = createHash("sha1").update(`${type} ${length}\0`);
72 for (let index = 0; index < part; index++) hash.update(this.chunk(id, index));
73 this.storage.sql.exec(
74 "INSERT INTO git_object_meta VALUES (?, ?, ?, ?, ?)",
75 this.scope,
76 id,
77 type,
78 length,
79 hash.digest("hex"),
80 );
81 this.storage.sql.exec("UPDATE git_object_budget SET decoded = decoded + ? WHERE scope = ?", length, this.scope);
82 });
83 return this.get(id);
84 }
85 private chunk(id: string, part: number): Uint8Array {
86 this.active();
87 const rows = this.storage.sql
88 .exec("SELECT data FROM git_object_bytes WHERE scope = ? AND id = ? AND part = ?", this.scope, id, part)
89 .toArray() as { data: ArrayBuffer }[];
90 if (rows.length !== 1) throw Error("missing Git object chunk");
91 const bytes = new Uint8Array(rows[0]!.data);
92 if (!bytes.length || bytes.length > 65536) throw Error("invalid Git object chunk");
93 return bytes;
94 }
95 get(id: string): StoredGitBytes {
96 this.active();
97 const rows = this.storage.sql
98 .exec("SELECT type, bytes, oid FROM git_object_meta WHERE scope = ? AND id = ?", this.scope, id)
99 .toArray() as { type: string; bytes: number; oid: string }[];
100 if (rows.length !== 1) throw Error("missing Git object");
101 const meta = rows[0]!;
102 return {
103 id,
104 oid: meta.oid,
105 type: meta.type,
106 byteLength: meta.bytes,
107 read: (start, length) => {
108 this.active();
109 if (
110 !Number.isSafeInteger(start) ||
111 !Number.isSafeInteger(length) ||
112 start < 0 ||
113 length < 0 ||
114 length > 65536 ||
115 start + length > meta.bytes
116 )
117 throw Error("invalid Git object range");
118 const result = new Uint8Array(length);
119 for (let copied = 0; copied < length; ) {
120 const position = start + copied,
121 chunk = this.chunk(id, Math.floor(position / 65536));
122 const offset = position % 65536,
123 count = Math.min(length - copied, chunk.length - offset);
124 if (count <= 0) throw Error("truncated Git object chunk");
125 result.set(chunk.subarray(offset, offset + count), copied);
126 copied += count;
127 }
128 return result;
129 },
130 };
131 }
132 /** Remove obsolete delta instructions without refunding their decoded-work budget. */
133 remove(id: string): void {
134 this.active();
135 this.storage.sql.exec("DELETE FROM git_object_bytes WHERE scope = ? AND id = ?", this.scope, id);
136 this.storage.sql.exec("DELETE FROM git_object_meta WHERE scope = ? AND id = ?", this.scope, id);
137 }
138 dispose(): void {
139 this.closed = true;
140 this.storage.transactionSync(() => {
141 this.storage.sql.exec("DELETE FROM git_object_bytes WHERE scope = ?", this.scope);
142 this.storage.sql.exec("DELETE FROM git_object_meta WHERE scope = ?", this.scope);
143 this.storage.sql.exec("DELETE FROM git_object_budget WHERE scope = ?", this.scope);
144 });
145 }
146}