| 1 | import { Hono } from "hono"; |
| 2 | import type { Env } from "../env.js"; |
| 3 | import type { AppVars } from "../auth/principal.js"; |
| 4 | import { HttpError } from "../lib/util.js"; |
| 5 | import { loadRepo } from "./common.js"; |
| 6 | |
| 7 | /** |
| 8 | * Git smart HTTP (protocol v2 for fetch/clone, v0 for push) and the Git LFS |
| 9 | * batch API, at https://diffinery.com/<owner>/<repo>.git |
| 10 | */ |
| 11 | export const git = new Hono<{ Bindings: Env; Variables: AppVars }>(); |
| 12 | |
| 13 | const noCache = { "cache-control": "no-cache, max-age=0, must-revalidate", pragma: "no-cache" }; |
| 14 | |
| 15 | git.get("/:owner/:repo/info/refs", async (c) => { |
| 16 | const service = c.req.query("service"); |
| 17 | if (service !== "git-upload-pack" && service !== "git-receive-pack") throw new HttpError(403, "dumb HTTP protocol is not supported; update git"); |
| 18 | const { stub } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), service === "git-receive-pack" ? "write" : "read"); |
| 19 | if (service === "git-upload-pack" && !/version=2/.test(c.req.header("git-protocol") ?? "")) { |
| 20 | throw new HttpError(400, "Diffinery requires Git protocol v2 (git 2.26 or newer, or run: git config --global protocol.version 2)"); |
| 21 | } |
| 22 | const body = await stub.gitInfoRefs(service); |
| 23 | return new Response(body, { headers: { "content-type": `application/x-${service}-advertisement`, ...noCache } }); |
| 24 | }); |
| 25 | |
| 26 | git.post("/:owner/:repo/git-upload-pack", async (c) => { |
| 27 | const { stub } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), "read"); |
| 28 | const body = await readBody(c.req.raw); |
| 29 | const out = await stub.gitUploadPack(body); |
| 30 | return new Response(out, { headers: { "content-type": "application/x-git-upload-pack-result", ...noCache } }); |
| 31 | }); |
| 32 | |
| 33 | git.post("/:owner/:repo/git-receive-pack", async (c) => { |
| 34 | const { stub, caller, repo } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), "write"); |
| 35 | const body = await readBody(c.req.raw); |
| 36 | const out = await stub.gitReceivePack(body, caller); |
| 37 | c.executionCtx.waitUntil(c.get("dir").touchPushed(repo.id)); |
| 38 | return new Response(out, { headers: { "content-type": "application/x-git-receive-pack-result", ...noCache } }); |
| 39 | }); |
| 40 | |
| 41 | async function readBody(req: Request): Promise<Uint8Array> { |
| 42 | let stream = req.body; |
| 43 | if (!stream) return new Uint8Array(0); |
| 44 | if (req.headers.get("content-encoding") === "gzip") stream = stream.pipeThrough(new DecompressionStream("gzip")); |
| 45 | return new Uint8Array(await new Response(stream).arrayBuffer()); |
| 46 | } |
| 47 | |
| 48 | // ---------------------------------------------------------------- Git LFS |
| 49 | |
| 50 | const lfsKey = (repoId: string, oid: string) => `repos/${repoId}/lfs/${oid.slice(0, 2)}/${oid.slice(2, 4)}/${oid}`; |
| 51 | const LFS_TYPE = "application/vnd.git-lfs+json"; |
| 52 | |
| 53 | git.post("/:owner/:repo/info/lfs/objects/batch", async (c) => { |
| 54 | const req = (await c.req.json()) as { operation: "upload" | "download"; objects: Array<{ oid: string; size: number }> }; |
| 55 | const { repo } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), req.operation === "upload" ? "write" : "read"); |
| 56 | const base = `${c.env.PUBLIC_URL}/${c.req.param("owner")}/${c.req.param("repo")}/info/lfs/objects`; |
| 57 | const auth = c.req.header("authorization"); |
| 58 | const objects = []; |
| 59 | for (const o of req.objects.slice(0, 1000)) { |
| 60 | if (!/^[0-9a-f]{64}$/.test(o.oid)) { |
| 61 | objects.push({ oid: o.oid, size: o.size, error: { code: 422, message: "invalid oid" } }); |
| 62 | continue; |
| 63 | } |
| 64 | const head = await c.env.BUCKET.head(lfsKey(repo.id, o.oid)); |
| 65 | if (req.operation === "download") { |
| 66 | if (!head) objects.push({ oid: o.oid, size: o.size, error: { code: 404, message: "object not found" } }); |
| 67 | else objects.push({ oid: o.oid, size: head.size, authenticated: true, actions: { download: { href: `${base}/${o.oid}`, header: auth ? { Authorization: auth } : {}, expires_in: 3600 } } }); |
| 68 | } else { |
| 69 | objects.push(head ? { oid: o.oid, size: o.size, authenticated: true } : { oid: o.oid, size: o.size, authenticated: true, actions: { upload: { href: `${base}/${o.oid}`, header: auth ? { Authorization: auth } : {}, expires_in: 3600 } } }); |
| 70 | } |
| 71 | } |
| 72 | return c.body(JSON.stringify({ transfer: "basic", objects, hash_algo: "sha256" }), 200, { "content-type": LFS_TYPE }); |
| 73 | }); |
| 74 | |
| 75 | git.get("/:owner/:repo/info/lfs/objects/:oid", async (c) => { |
| 76 | const { repo } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), "read"); |
| 77 | const obj = await c.env.BUCKET.get(lfsKey(repo.id, c.req.param("oid"))); |
| 78 | if (!obj) throw new HttpError(404, "object not found"); |
| 79 | return new Response(obj.body, { headers: { "content-type": "application/octet-stream", "content-length": String(obj.size) } }); |
| 80 | }); |
| 81 | |
| 82 | git.put("/:owner/:repo/info/lfs/objects/:oid", async (c) => { |
| 83 | const { repo } = await loadRepo(c, c.req.param("owner"), c.req.param("repo"), "write"); |
| 84 | const oid = c.req.param("oid"); |
| 85 | if (!/^[0-9a-f]{64}$/.test(oid)) throw new HttpError(422, "invalid oid"); |
| 86 | // Verify the content hash while storing: R2 checks the SHA-256 we pass. |
| 87 | const body = await c.req.arrayBuffer(); |
| 88 | const digest = [...new Uint8Array(await crypto.subtle.digest("SHA-256", body))].map((b) => b.toString(16).padStart(2, "0")).join(""); |
| 89 | if (digest !== oid) throw new HttpError(422, "content does not match oid"); |
| 90 | await c.env.BUCKET.put(lfsKey(repo.id, oid), body); |
| 91 | return c.body(null, 200); |
| 92 | }); |