Skip to content
File

Blob: src/worker/do/repo/storage.ts

typescript58 lines
1/**
2 * Storage operations for Git objects in Durable Object and R2
3 *
4 * Post-closure, loose object writes are no longer part of the production path.
5 * This module retains only readCommitFromStore for the debug endpoint fallback.
6 */
7 
8import type { RepoStateSchema } from "./repoState";
9 
10import { asTypedStorage, objKey } from "./repoState";
11import { r2LooseKey } from "@/worker/keys";
12import { inflateAndParseHeader, parseCommitText } from "@/worker/git";
13 
14/**
15 * Read and parse a commit object directly from DO storage (fallback to R2 if needed).
16 * Legacy: used only by the debug endpoint fallback when a packed read returns a non-commit.
17 * @param ctx - Durable Object state context
18 * @param env - Worker environment
19 * @param prefix - Repository prefix
20 * @param oid - Commit object ID
21 * @returns Parsed commit information or null if not found
22 */
23export async function readCommitFromStore(
24 ctx: DurableObjectState,
25 env: Env,
26 prefix: string,
27 oid: string
28): Promise<{
29 oid: string;
30 tree: string;
31 parents: string[];
32 author?: { name: string; email: string; when: number; tz: string };
33 committer?: { name: string; email: string; when: number; tz: string };
34 message: string;
35} | null> {
36 // Prefer DO-stored loose object to avoid R2/HTTP hops
37 const store = asTypedStorage<RepoStateSchema>(ctx.storage);
38 let data = await store.get(objKey(oid));
39 
40 if (!data) {
41 // Fallback: try R2-stored loose copy
42 try {
43 const obj = await env.REPO_BUCKET.get(r2LooseKey(prefix, oid));
44 if (obj) data = await obj.arrayBuffer();
45 } catch {}
46 }
47 if (!data) return null;
48 
49 const z = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
50 // Decompress (zlib/deflate) and parse git header
51 const parsed = await inflateAndParseHeader(z);
52 if (!parsed || parsed.type !== "commit") return null;
53 const text = new TextDecoder().decode(parsed.payload);
54 
55 const commit = parseCommitText(text);
56 return { oid, ...commit };
57}