Skip to content

abso-core

Public
Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/src/git-pack-spool.ts?format=json
121 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 180, 2 versions changed it
1import { createHash } from "node:crypto";
2import type { SqliteStorage } from "./engine-project.ts";
3
4/** Temporary verified pack bytes. No whole-pack read API is exposed. */
5export class GitPackSpool {
6 static readonly chunkBytes = 65536;
7 /** Cold Durable Object construction only, before any intake starts. */
8 static recover(storage: SqliteStorage): void {
9 GitPackSpool.initialize(storage);
10 storage.sql.exec("DELETE FROM git_pack_spool");
11 }
12 private static initialize(storage: SqliteStorage): void {
13 storage.sql.exec(
14 "CREATE TABLE IF NOT EXISTS git_pack_spool (id TEXT NOT NULL, part INTEGER NOT NULL, data BLOB NOT NULL, PRIMARY KEY (id, part))",
15 );
16 }
17 readonly id = crypto.randomUUID();
18 private size = 0;
19 private sealed = false;
20 private closed = false;
21 private readonly storage: SqliteStorage;
22 constructor(storage: SqliteStorage) {
23 this.storage = storage;
24 GitPackSpool.initialize(storage);
25 }
26 get byteLength(): number {
27 this.assertReadable();
28 return this.size;
29 }
30 private assertReadable(): void {
31 if (!this.sealed || this.closed) throw Error("pack spool is not readable");
32 }
33 async ingest(chunks: AsyncIterable<Uint8Array> | Iterable<Uint8Array>, maxBytes: number): Promise<string> {
34 if (this.size || this.sealed || this.closed) throw Error("pack spool already used");
35 if (!Number.isSafeInteger(maxBytes) || maxBytes < 32) throw Error("invalid pack byte limit");
36 const hash = createHash("sha1");
37 let tail = new Uint8Array(0);
38 let pending = new Uint8Array(GitPackSpool.chunkBytes),
39 used = 0,
40 part = 0;
41 const save = () => {
42 if (this.closed) throw Error("pack spool is closed");
43 this.storage.sql.exec("INSERT INTO git_pack_spool VALUES (?, ?, ?)", this.id, part++, pending.slice(0, used));
44 used = 0;
45 };
46 try {
47 for await (const chunk of chunks) {
48 if (this.closed) throw Error("pack spool is closed");
49 if (!(chunk instanceof Uint8Array)) throw Error("invalid pack chunk");
50 for (let at = 0; at < chunk.length; ) {
51 const count = Math.min(GitPackSpool.chunkBytes - used, chunk.length - at);
52 if (this.size + count > maxBytes) throw new GitIntakeLimitError("pack", "pack exceeds byte limit");
53 const piece = chunk.subarray(at, at + count);
54 const joined = new Uint8Array(tail.length + piece.length);
55 joined.set(tail);
56 joined.set(piece, tail.length);
57 const end = Math.max(0, joined.length - 20);
58 hash.update(joined.subarray(0, end));
59 tail = joined.slice(end);
60 pending.set(piece, used);
61 used += count;
62 at += count;
63 this.size += count;
64 if (used === pending.length) save();
65 }
66 }
67 if (used) save();
68 if (this.size < 32 || tail.length !== 20) throw Error("truncated pack");
69 const digest = hash.digest("hex");
70 if (digest !== Buffer.from(tail).toString("hex")) throw Error("pack checksum mismatch");
71 this.sealed = true;
72 const header = this.read(0, 12);
73 if (new TextDecoder().decode(header.subarray(0, 4)) !== "PACK" || new DataView(header.buffer).getUint32(4) !== 2)
74 throw Error("unsupported pack header");
75 return digest;
76 } catch (error) {
77 this.dispose();
78 throw error;
79 }
80 }
81 read(start: number, length: number): Uint8Array {
82 this.assertReadable();
83 if (
84 !Number.isSafeInteger(start) ||
85 !Number.isSafeInteger(length) ||
86 start < 0 ||
87 length < 0 ||
88 length > GitPackSpool.chunkBytes ||
89 start + length > this.size
90 )
91 throw Error("invalid pack byte range");
92 const result = new Uint8Array(length);
93 let copied = 0;
94 while (copied < length) {
95 const position = start + copied,
96 part = Math.floor(position / GitPackSpool.chunkBytes);
97 const rows = this.storage.sql
98 .exec("SELECT data FROM git_pack_spool WHERE id = ? AND part = ?", this.id, part)
99 .toArray() as { data: ArrayBuffer }[];
100 if (rows.length !== 1) throw Error("missing pack chunk");
101 const data = new Uint8Array(rows[0]!.data),
102 offset = position % GitPackSpool.chunkBytes;
103 const count = Math.min(length - copied, data.length - offset);
104 if (count <= 0 || data.length > GitPackSpool.chunkBytes) throw Error("invalid pack chunk");
105 result.set(data.subarray(offset, offset + count), copied);
106 copied += count;
107 }
108 return result;
109 }
110 *chunks(): IterableIterator<Uint8Array> {
111 this.assertReadable();
112 for (let start = 0; start < this.size; start += GitPackSpool.chunkBytes)
113 yield this.read(start, Math.min(GitPackSpool.chunkBytes, this.size - start));
114 }
115 dispose(): void {
116 this.closed = true;
117 this.storage.sql.exec("DELETE FROM git_pack_spool WHERE id = ?", this.id);
118 }
121import { GitIntakeLimitError } from "./git-intake-limit.ts";