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