git.ts
This file
Path
apps/platform/src/git.ts
315 lines · 10.3 KiB · TypeScript
Last changed by
Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
History of this fileTake it
The walk stopped after 200 commits: lines older than 878da37 point at it.
Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 1–6why, one step back1/**2 * git's smart HTTP on the platform: clones and pushes of every project's3 * repos, reached at `/<repo>(.git)/(info/refs|git-upload-pack|git-receive-pack)`.4 * `surfaces.ts` hands every such path here, outside the 401 gate, before the5 * RPC and MCP routes.6 *Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 7why, one step back7 * Who may do what (ADR 0004): anyone clones a project's canonicalMerge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 8–10why, one step back8 * repo; a fork is read by a signed-in person; a push needs one, and custody9 * (`adapters/custody.ts`) decides what it may move. A credential the10 * platform does not know is a 401 everywhere. A name the platform neverDrop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
lines 11–12why, one step back11 * makes is a 404, unless `LayerGate` says it is a layer's repo,12 * which only the layer's readers read and its writers push to. TheMerge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 13–15why, one step back13 * client's credential never travels past this file: the gateway forwards14 * only the headers git's protocol needs, and the upstream adds the15 * platform's own.Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 16–20why, one step back16 */17import { Brand } from "@repo/brand";18import { Custody } from "@repo/git/custody";19import { gateway, gitRoute, plain } from "@repo/git/gateway";20import { concat, packetAt, pkt, utf8 } from "@repo/git/pkt-line";Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 21–22why, one step back21import { readUpload } from "@repo/git/upload-pack";22import type { UploadRequest } from "@repo/git/upload-pack";Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 23why, one step back23import { Upstream } from "@repo/git/upstream";Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
line 24why, one step back24import type { Projects } from "@repo/project/projects";Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 25why, one step back25import type { LayerAccess } from "@repo/project/visibility-port";Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 26–29why, one step back26import { RepoName } from "@repo/protocol/ids";27import { Anonymous, Principal } from "@repo/protocol/principal";28import { Effect, Option, Ref } from "effect";29Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 30–35why, one step back30import {31 custodyFor,32 layerCustodyFor,33 layerRead,34 refuseAll,35} from "./adapters/custody.ts";Merge build/417-wire-ratchets-and-escapes-into-the-platform: lintToZero and ratchet asks land end to end, baselines move down at landing and up only by a decision, and reasoned escapes wait on Todayoscabrielbefore adoption80d346c
line 36why, one step back36import { auditOf, credentialOf } from "./credential.ts";Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 37why, one step back37import { LayerGate } from "./layer-gate.ts";Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
line 38why, one step back38import type { LayerReach } from "./layer-gate.ts";Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 39–125why, one step back39import { repoKind, RepoKind } from "./repo-names.ts";40import { Sessions } from "./sessions.ts";4142/** What serving git needs: who is calling, their projects, and the git server behind. */43export type GitServices = Sessions | Projects | Upstream;4445/** A push reads `info/refs` for receive-pack, then posts to it; everything else reads. */46const writes = (endpoint: string, url: URL) =>47 endpoint === "git-receive-pack" ||48 (endpoint === "info/refs" &&49 url.searchParams.get("service") === "git-receive-pack");5051const RESULT = "application/x-git-receive-pack-result";5253/**54 * git asks its credential helper for a password only after a 401 that names55 * Basic; the helper answers with the person's platform token.56 */57const signIn = (reason: string) =>58 plain(401, `${reason}; sign in with ${Brand.cli} login`, {59 "www-authenticate": `Basic realm="${Brand.name}"`,60 });6162const notFound = (name: string) => plain(404, `no repo ${name}`);6364/** Who the request's credential names; `None` when it names no one the platform knows. */65const principalOf = (headers: Headers) =>66 Option.match(credentialOf(headers), {67 onNone: () => Effect.succeedSome<Principal>(Anonymous.make({})),68 onSome: (token) =>69 Sessions.use(({ verify }) => verify(token)).pipe(70 Effect.map(Option.map((person): Principal => person))71 ),72 });7374/** Whether `principal` may read the repo `kind` names: a canonical repo, anyone; a fork, a signed-in person. */75const mayRead = (principal: Principal, kind: RepoKind) =>76 RepoKind.$is("Canonical")(kind) || Principal.guards.Person(principal);7778/**79 * The reply to a push with `notices` first, as `remote:` lines: only when80 * the reply speaks side-band, whose first packet names its band.81 */82const withNotices = (response: Response, notices: readonly string[]) =>83 Effect.gen(function* () {84 if (85 notices.length === 0 ||86 response.status !== 200 ||87 response.headers.get("content-type") !== RESULT88 ) {89 return response;90 }9192 const read = yield* Effect.option(93 Effect.tryPromise(() => response.arrayBuffer())94 );9596 // The gateway's own words when a push's report does not arrive whole.97 if (Option.isNone(read)) {98 return plain(502, "the push's report was cut off");99 }100101 const reply = new Uint8Array(read.value);102103 const first = Option.flatten(Option.liftThrowable(packetAt)(reply, 0));104105 const banded = Option.exists(106 first,107 ({ packet }) =>108 packet !== "Flush" && packet[0] !== undefined && packet[0] <= 3109 );110111 const body = banded112 ? concat([113 ...notices.map((line) =>114 pkt(concat([Uint8Array.of(2), utf8(`${line}\n`)]))115 ),116 reply,117 ])118 : reply;119120 return new Response(body, {121 headers: response.headers,122 status: response.status,123 });124 });125Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 126–132why, one step back126/** How far each access reaches: a writer also reads. */127const REACH = {128 none: 0,129 read: 1,130 write: 2,131} as const satisfies Record<LayerAccess, number>;132Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 133–150why, one step back133/** A request body as git wrote it, inflated if git gzipped it; none if it can't be. */134const inflated = (bytes: Uint8Array, encoding: string | null) => {135 if (encoding === null) {136 return Effect.succeedSome(bytes);137 }138139 return encoding === "gzip"140 ? Effect.tryPromise(() =>141 new Response(142 new Response(bytes).body?.pipeThrough(new DecompressionStream("gzip"))143 ).arrayBuffer()144 ).pipe(145 Effect.map((read) => new Uint8Array(read)),146 Effect.option147 )148 : Effect.succeedNone;149};150Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 151why, one step back151/**Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 152–156why, one step back152 * An upload-pack request, read whole for the read log and sent on153 * unchanged: its body is a fetch's wants and haves, never a pack. A body154 * in an encoding git never sends still counts as a read, with no commit155 * named, so the log errs toward a read. None when the body itself can't be156 * read.Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 157why, one step back157 */Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 158–188why, one step back158const uploadOf = (request: Request) =>159 Effect.gen(function* () {160 const read = yield* Effect.option(161 Effect.tryPromise(() => request.arrayBuffer())162 );163164 if (Option.isNone(read)) {165 return Option.none();166 }167168 const bytes = new Uint8Array(read.value);169170 const upload = Option.match(171 yield* inflated(bytes, request.headers.get("content-encoding")),172 {173 onNone: (): UploadRequest => ({ reads: true, want: Option.none() }),174 onSome: readUpload,175 }176 );177178 return Option.some({179 request: new Request(request.url, {180 body: bytes,181 headers: request.headers,182 method: request.method,183 }),184 upload,185 });186 });187188/**Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 189why, one step back189 * A git request to a project's layer repo (ADR 0007): only theMerge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 190–194why, one step back190 * layer's readers clone it and only its writers push to it, all through the191 * private namespace's upstream. Someone not signed in is asked to sign in192 * (401, so the credential helper answers); a person the layer does not193 * admit gets 403 with one line git prints.194 *Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 195why, one step back195 * Every read is logged with who and which commit: eachMerge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 196–199why, one step back196 * upload-pack request answered 200, but a v2 `ls-refs`, which lists refs197 * and reads nothing, so a clone is one read. A fetch that negotiates over198 * several requests logs each.199 */Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 200–203why, one step back200const serveLayer = (201 request: Request,202 route: { readonly endpoint: string; readonly name: string },203 principal: Principal,Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
line 204why, one step back204 { access, target }: LayerReachPlug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 205–225why, one step back205) =>206 Effect.gen(function* () {207 const gate = yield* LayerGate;208 const pushing = writes(route.endpoint, new URL(request.url));209210 if (!Principal.guards.Person(principal)) {211 return signIn("a layer is read by its readers");212 }213214 if (REACH[access] < REACH[pushing ? "write" : "read"]) {215 return plain(216 403,217 `${principal.handle} may not ${pushing ? "push to" : "read"} ${route.name}`218 );219 }220221 const custody =222 route.endpoint === "git-receive-pack"223 ? yield* layerCustodyFor(principal, target)224 : refuseAll;225Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 226–230why, one step back226 const served = (sent: Request) =>227 gateway(sent).pipe(228 Effect.provideService(Custody, custody),229 Effect.provideService(Upstream, gate.upstream)230 );Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 231why, one step back231Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 232–234why, one step back232 // Only a fetch's request reads content; the rest pass straight through.233 if (route.endpoint !== "git-upload-pack" || request.method !== "POST") {234 return yield* served(request);Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 235–236why, one step back235 }236Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
lines 237–249why, one step back237 const fetched = yield* uploadOf(request);238239 if (Option.isNone(fetched)) {240 return plain(400, "the request could not be read");241 }242243 const { upload } = fetched.value;244 const response = yield* served(fetched.value.request);245246 if (upload.reads && response.status === 200) {247 yield* layerRead(principal, target, upload.want);248 }249Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 250–252why, one step back250 return response;251 });252Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 253–261why, one step back253/** A git request to `name`, as `principal` may make it. */254const serve = (255 request: Request,256 route: { readonly endpoint: string; readonly name: string },257 principal: Principal258) =>259 Effect.gen(function* () {260 const kind = repoKind(RepoName.make(route.name));261Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 262why, one step back262 // A name the platform makes for no repo of its own may be a layer's.Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
line 263why, one step back263 if (RepoKind.$is("Unknown")(kind)) {Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 264why, one step back264 const layer = yield* LayerGate.use((gate) =>Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
line 265why, one step back265 gate.classify(RepoName.make(route.name), principal)Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
lines 266–269why, one step back266 );267268 return yield* Option.match(layer, {269 onNone: () => Effect.succeed(notFound(route.name)),Merge build/606-gateway-layer-tiers: a maintainer clones the maintainers layer, a contributor gets 403, every read is logged, and a push can't carry layer pathsoscabrielbefore adoption66aaefc
line 270why, one step back270 onSome: (reach) => serveLayer(request, route, principal, reach),Plug typed visibility in through neutral ports, so removing the cartridge is a deletionoscabrielbefore adoption9031e57
line 271why, one step back271 });Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 272–288why, one step back272 }273274 const pushing = writes(route.endpoint, new URL(request.url));275276 if (pushing && !Principal.guards.Person(principal)) {277 return signIn("a push needs a signed-in person");278 }279280 if (!mayRead(principal, kind)) {281 return signIn("a fork is read by signed-in people");282 }283284 const notices = yield* Ref.make<readonly string[]>([]);285286 // Only a push's own request is judged; reads never reach custody.287 const custody =288 route.endpoint === "git-receive-pack"Merge build/417-wire-ratchets-and-escapes-into-the-platform: lintToZero and ratchet asks land end to end, baselines move down at landing and up only by a decision, and reasoned escapes wait on Todayoscabrielbefore adoption80d346c
lines 289–293why, one step back289 ? yield* custodyFor(290 principal,291 kind,292 (line) => Ref.update(notices, (said) => [...said, line]),293 auditOf(request.headers)Merge build/420-release-downloads: the CLI installs from the platform itself, with no GitHub in the release pathoscabrielbefore adoption878da37
lines 294–315why, one step back294 )295 : refuseAll;296297 const response = yield* gateway(request).pipe(298 Effect.provideService(Custody, custody)299 );300301 return yield* withNotices(response, yield* Ref.get(notices));302 });303304/** The response to a git request, if `request` is one the gateway serves. */305export const serveGit = (306 request: Request307): Option.Option<Effect.Effect<Response, never, GitServices>> =>308 Option.map(gitRoute(new URL(request.url).pathname), ({ endpoint, name }) =>309 Effect.flatMap(principalOf(request.headers), (principal) =>310 Option.match(principal, {311 onNone: () => Effect.succeed(signIn("that token is not one we know")),312 onSome: (known) => serve(request, { endpoint, name }, known),313 })314 )315 );