| author | Divy Srivastava <me@littledivy.com> | 2026-08-20 06:17:29 +0530 |
|---|---|---|
| committer | Divy Srivastava <me@littledivy.com> | 2026-08-20 06:17:29 +0530 |
| commit | c75815bb552c4f79c62081e25a2ee45d1bff6cb0 (patch) | |
| tree | f0554984931b4bf89bec1a84ad119da12cd4e1b0 | |
| parent | a5311cc4b9d97954e9beef06bc487eab376accf4 (diff) | |
| download | dgit-c75815bb55.tar.gz zip | |
v0.0.6v0.0.6
Co-authored-by: Sree Narayanan <sreeaadhi07@gmail.com>
Diffstat
| package-lock.json | +2 | -2 |
| package.json | +1 | -1 |
| src/git/packstore.ts | +14 | -4 |
| src/git/protocol.ts | +46 | -9 |
| src/mod.ts | +2 | -2 |
| src/repo.ts | +6 | -1 |
6 files changed, 71 insertions(+), 19 deletions(-)
diff --git a/package-lock.json b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dgit", - "version": "0.1.0", + "version": "0.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dgit", - "version": "0.1.0", + "version": "0.0.6", "dependencies": { "pako": "^2.1.0" }, diff --git a/package.json b/package.json @@ -1,6 +1,6 @@ { "name": "durable-git", - "version": "0.0.5", + "version": "0.0.6", "description": "Durable git: a git server as a Cloudflare Durable Object. Smart HTTP, a cgit-style web interface, pluggable auth, push events, and a typed content API.", "type": "module", "main": "src/mod.ts", diff --git a/src/git/packstore.ts b/src/git/packstore.ts @@ -6,6 +6,10 @@ import { MultipartCapture, beginRawMultipart } from "./multipart"; export const PACK_CHUNK = 1024 * 1024; +/** a pushed pack goes to R2 only when the binding is present AND the pack is at + * least this large; smaller packs stay in SQLite (local ~ms writes/reads). R2's + * purpose is escaping the 10 GB per-DO SQLite cap, which only large repos hit. */ +export const R2_PACK_MIN_BYTES = 16 * 1024 * 1024; /** real Workers isolates have a hard 128MB total; self-hosted celld nodes run multi-GB heaps */ const TIGHT_MEMORY = typeof caches !== "undefined"; const MAX_BUFFERED_ENTRY = (TIGHT_MEMORY ? 8 : 32) * 1024 * 1024; @@ -477,6 +481,10 @@ reader: ReadableStreamDefaultReader<Uint8Array> | null, opts: { maxBytes: number; + /** push body size (content-length ~= pack size); packs this size are + * stored in R2 when a bucket is bound, else in SQLite. Absent/unknown + * (chunked pushes) picks SQLite — the small-push-fast bias. */ + sizeHint?: number; cache: ObjCache; onProgress?: (msg: string) => void; /** called periodically so the runtime can flush its write buffer — @@ -526,13 +534,15 @@ console.log(`[ingest pack ${packId} +${Math.round((Date.now() - started) / 1000)}s] ${msg.trim()}`); }; - // storage backend: with an R2 binding the pack bytes stream to - // raw/<repo>/<packId> via multipart and only the index stays in SQLite; else - // they go to pack_data as before. Prime the backend cache now — before + // storage backend: a large pack (sizeHint >= R2_PACK_MIN_BYTES) with an R2 + // binding streams its bytes to raw/<repo>/<packId> via multipart and only the + // index stays in SQLite; small packs (and any push without the binding or with + // unknown length) go to pack_data as before. Prime the backend cache now — before // pack_meta exists — so phase B/C readRaw hits the right source. A refused // multipart begin (celld makes createMultipartUpload throw) degrades to SQLite. let capture: MultipartCapture | null = null; - if (this.r2) { + const wantR2 = this.r2 !== null && opts.sizeHint !== undefined && opts.sizeHint >= R2_PACK_MIN_BYTES; + if (this.r2 && wantR2) { const mp = await beginRawMultipart(this.r2.bucket, this.rawKey(packId)); if (mp) capture = new MultipartCapture(mp); } diff --git a/src/git/protocol.ts b/src/git/protocol.ts @@ -35,8 +35,11 @@ beginMultipart(key: string, objects: number): Promise<MultipartPackUpload | null>; } -/** GET /info/refs?service=... — smart ref advertisement (protocol v0). */ -export function advertisement(store: GitStore, service: Service): Uint8Array { +/** GET /info/refs?service=... — smart ref advertisement. */ +export function advertisement(store: GitStore, service: Service, v2 = false): Uint8Array { + if (v2 && service === "git-upload-pack") { + return concat([pkt("version 2\n"), pkt(`${AGENT}\n`), pkt("ls-refs\n"), pkt("fetch=shallow\n"), FLUSH]); + } const caps = service === "git-upload-pack" ? [ @@ -124,7 +127,7 @@ /** * Serve a full clone by streaming the R2-cached raw pack straight from the - * Worker: the NAK preamble a full-clone negotiation always produces, the + * Worker: the protocol's pack preamble, the * optional band-2 progress line, then the pack re-chunked into band-1 side-band * frames as R2 hands it over, then flush. Framing is identical to the DO path * and the pack bytes are the stored bytes, so the client receives a @@ -134,7 +137,7 @@ export function streamingCloneResponse( body: ReadableStream<Uint8Array>, objects: number, - noProgress: boolean + noProgress: boolean, v2 = false ): Response { const headers = { "content-type": "application/x-git-upload-pack-result", @@ -149,7 +152,7 @@ try { if (!started) { started = true; - ctrl.enqueue(pkt("NAK\n")); + ctrl.enqueue(pkt(v2 ? "packfile\n" : "NAK\n")); if (!noProgress) { for (const f of sidebandFrames(2, te.encode(`Enumerating objects: ${objects}, done.\n`))) ctrl.enqueue(f); } @@ -174,6 +177,7 @@ } interface UploadRequest { + command: "fetch" | "ls-refs" | null; wants: string[]; haves: string[]; done: boolean; @@ -185,6 +189,7 @@ export function parseUploadRequest(body: Uint8Array): UploadRequest { const parser = new PktParser(body); const req: UploadRequest = { + command: null, wants: [], haves: [], done: false, @@ -195,7 +200,11 @@ for (let p = parser.read(); p !== null; p = parser.read()) { if (p.kind !== "line") continue; const line = p.text; - if (line.startsWith("want ")) { + if (line === "command=fetch" || line === "command=ls-refs") { + req.command = line === "command=fetch" ? "fetch" : "ls-refs"; + } else if (line === "no-progress" || line === "symrefs" || line === "peel") { + req.caps.add(line); + } else if (line.startsWith("want ")) { req.wants.push(line.slice(5, 45)); // capabilities ride on the first want line for (const cap of line.slice(45).trim().split(" ")) if (cap) req.caps.add(cap); @@ -212,6 +221,21 @@ return req; } +async function lsRefs(store: GitStore, req: UploadRequest): Promise<Uint8Array> { + const lines: Uint8Array[] = []; + const head = store.resolveHead(); + if (head) lines.push(pkt(`${head} HEAD${req.caps.has("symrefs") ? ` symref-target:${store.head()}` : ""}\n`)); + for (const ref of store.refs()) { + let peeled = ""; + if (req.caps.has("peel") && ref.name.startsWith("refs/tags/") && store.typeAndSize(ref.target)?.type === "tag") { + const oid = await peelToCommitOid(store, ref.target); + if (oid) peeled = ` peeled:${oid}`; + } + lines.push(pkt(`${ref.target} ${ref.name}${peeled}\n`)); + } + return concat([...lines, FLUSH]); +} + /** Follow tag objects down to the underlying commit oid (or null). */ async function peelToCommitOid(store: GitStore, oid: string): Promise<string | null> { for (let i = 0; i < 10; i++) { @@ -510,6 +534,11 @@ "cache-control": "no-cache", }; const req = parseUploadRequest(body); + if (req.command === "ls-refs") { + release(); + return new Response(await lsRefs(store, req) as unknown as BodyInit, { headers }); + } + const v2 = req.command === "fetch"; if (!req.wants.length || req.wants.some((w) => !isOid(w))) { release(); return new Response(pkt("ERR no valid wants\n") as unknown as BodyInit, { headers }); @@ -528,6 +557,7 @@ // shallow section (only when the client asked to deepen) let commitLimit: Set<string> | null = null; if (req.deepen > 0) { + if (v2) preamble.push(pkt("shallow-info\n")); const clientShallowSet = new Set(req.clientShallows); if (req.deepen >= INFINITE_DEPTH) { // --unshallow: full history, everything the client thought was shallow opens up @@ -544,8 +574,9 @@ if (commits.has(s) && !boundary.has(s)) preamble.push(pkt(`unshallow ${s}\n`)); } } - preamble.push(FLUSH); + preamble.push(v2 ? te.encode("0001") : FLUSH); } + if (v2 && !req.done) preamble.length = 0; // Negotiation. A round with neither haves nor done (shallow discovery) gets // no ack line at all — a stray NAK would desync the client's stateless @@ -562,7 +593,13 @@ const common = req.haves.filter((h) => isOid(h) && store.has(h)); const lastCommon = common.length ? common[common.length - 1] : null; let sendPack = req.done; - if (!req.caps.has("multi_ack_detailed")) { + if (v2) { + if (req.done) preamble.push(pkt("packfile\n")); + else { + const acknowledgments = common.length ? common.map((oid) => pkt(`ACK ${oid}\n`)) : [pkt("NAK\n")]; + preamble.push(pkt("acknowledgments\n"), ...acknowledgments, FLUSH); + } + } else if (!req.caps.has("multi_ack_detailed")) { if (req.done || req.haves.length) { preamble.push(pkt(common.length ? `ACK ${common[0]}\n` : "NAK\n")); } @@ -590,7 +627,7 @@ return new Response(concat(preamble) as unknown as BodyInit, { headers }); } - const sideband = req.caps.has("side-band-64k"); + const sideband = v2 || req.caps.has("side-band-64k"); const noProgress = req.caps.has("no-progress"); // Full-clone fast path: no haves, not shallow/deepen, and the wants are diff --git a/src/mod.ts b/src/mod.ts @@ -320,7 +320,7 @@ req.done && req.deepen === 0 && req.clientShallows.length === 0 && - req.caps.has("side-band-64k"); + (req.command === "fetch" || req.caps.has("side-band-64k")); if (!fullClone) return null; let key: string | null; try { @@ -338,7 +338,7 @@ if (!obj) return null; const objects = parseInt(obj.customMetadata?.objects ?? "", 10); if (!Number.isFinite(objects)) return null; - return streamingCloneResponse(obj.body, objects, req.caps.has("no-progress")); + return streamingCloneResponse(obj.body, objects, req.caps.has("no-progress"), req.command === "fetch"); } function withCors(res: Response, origin: string | undefined): Response { diff --git a/src/repo.ts b/src/repo.ts @@ -193,7 +193,8 @@ if (service !== "git-upload-pack" && service !== "git-receive-pack") { return new Response("smart HTTP only\n", { status: 400 }); } - return new Response(advertisement(this.store, service as Service) as unknown as BodyInit, { + const v2 = req.headers.get("git-protocol")?.split(":").some((value) => value.trim() === "version=2"); + return new Response(advertisement(this.store, service as Service, v2) as unknown as BodyInit, { headers: { "content-type": `application/x-${service}-advertisement`, "cache-control": "no-cache", @@ -766,8 +767,12 @@ // a dedicated cache makes pack-adjacent delta bases nearly free; on // real Workers the whole isolate has a hard 128MB, so stay small there const budget = typeof caches !== "undefined" ? 16 * 1024 * 1024 : 512 * 1024 * 1024; + // content-length ~= pack size; it selects the R2-vs-SQLite backend before + // the first byte lands. Absent/unknown (chunked) leaves it undefined → SQLite. + const declared = parseInt(req.headers.get("content-length") ?? "", 10); await this.store.packs.ingest(firstPackBytes, reader, { maxBytes, + sizeHint: Number.isFinite(declared) ? declared : undefined, cache: new ObjCache(budget), onProgress: progress, flush: () => this.ctx.storage.sync(), |