receive-pack.ts
This file
Path
packages/git/src/receive-pack.ts
274 lines · 7.7 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275import { RefName } from "@repo/artifacts/repo-host";import { CommitSha } from "@repo/protocol/ids";import type { Refusal } from "@repo/protocol/refusal";import { explain, headline } from "@repo/protocol/render";import { Array, Data, Effect, Option, Schema, Stream } from "effect";
import type { Command } from "./custody.ts";import { FLUSH, concat, packetAt, packets, pkt, utf8 } from "./pkt-line.ts";
export class Malformed extends Data.TaggedError("Malformed")<{ readonly reason: string;}> {}
const ZERO = "0".repeat(40);
const COMMAND = /^(?<from>[0-9a-f]{40}) (?<to>[0-9a-f]{40}) (?<ref>[^\0\n ]+)(?:\0(?<capabilities>[^\n]*))?\n?$/u;
const objectId = (hex: string) => hex === ZERO ? Option.none() : Option.some(CommitSha.make(hex));
const isRefName = Schema.is(RefName);
export interface Opening { readonly commands: readonly Command[]; readonly capabilities: ReadonlySet<string>; readonly read: Uint8Array;}
const parse = (lines: readonly string[]) => Effect.gen(function* () { const commands: Command[] = []; let capabilities = new Set<string>();
// A shallow clone names its shallow commits before its commands. for (const line of lines.filter((text) => !text.startsWith("shallow "))) { const fields = COMMAND.exec(line)?.groups;
if (fields?.from === undefined || fields.to === undefined) { return yield* new Malformed({ reason: `not a push command: ${line}` }); }
if (fields.ref === undefined || !isRefName(fields.ref)) { return yield* new Malformed({ reason: `not a ref this gateway carries: ${fields.ref ?? ""}`, }); }
if (commands.length === 0) { capabilities = new Set( (fields.capabilities ?? "").split(" ").filter((word) => word !== "") ); }
commands.push({ from: objectId(fields.from), ref: fields.ref, to: objectId(fields.to), }); }
return { capabilities, commands }; });
const packetIn = (bytes: Uint8Array, at: number) => Effect.try({ catch: () => new Malformed({ reason: `bad pkt-line at byte ${at}` }), try: () => packetAt(bytes, at), });
const chunkOf = (reader: ReadableStreamDefaultReader<Uint8Array>) => Effect.tryPromise({ catch: () => new Malformed({ reason: "the push could not be read" }), try: () => reader.read(), });
/** The pack that follows stays unread in the body. An empty list is git's probe before a large push. */export const readOpening = (body: ReadableStream<Uint8Array>) => Effect.gen(function* () { const reader = body.getReader(); const lines: string[] = []; let read = new Uint8Array(0); let at = 0; let flushed = false;
while (!flushed) { const next = yield* packetIn(read, at);
if (Option.isSome(next)) { const { packet } = next.value;
at = next.value.next; flushed = packet === "Flush";
if (packet !== "Flush") { lines.push(new TextDecoder().decode(packet)); } } else { const chunk = yield* chunkOf(reader);
if (chunk.done) { return yield* new Malformed({ reason: "the push ended inside its command list", }); }
read = concat([read, chunk.value]); } }
reader.releaseLock();
const { capabilities, commands } = yield* parse(lines);
return { capabilities, commands, read } satisfies Opening; });
const rest = (body: ReadableStream<Uint8Array>) => Stream.fromReadableStream({ evaluate: () => body, onError: () => new Malformed({ reason: "the push could not be read" }), });
/** So the client finishes sending. */export const drain = (body: ReadableStream<Uint8Array>) => Stream.runDrain(rest(body)).pipe(Effect.ignore);
export const resume = (read: Uint8Array, body: ReadableStream<Uint8Array>) => Stream.toReadableStream(Stream.concat(Stream.succeed(read), rest(body)));
// The largest payload a side-band packet carries, by the client's capability.const bandLimit = (capabilities: ReadonlySet<string>) => { if (capabilities.has("side-band-64k")) { return Option.some(65_515); }
return capabilities.has("side-band") ? Option.some(995) : Option.none();};
const band = (channel: 1 | 2, payload: Uint8Array) => pkt(concat([Uint8Array.of(channel), payload]));
const chunks = (bytes: Uint8Array, limit: number) => { const parts: Uint8Array[] = [];
for (let at = 0; at < bytes.length; at += limit) { parts.push(bytes.subarray(at, at + limit)); }
return parts;};
export interface RefStatus { readonly ref: string; readonly ng: Option.Option<string>;}
export interface UpstreamReport { readonly messages: readonly Uint8Array[]; readonly unpack: Option.Option<string>; readonly refs: readonly RefStatus[];}
const text = (bytes: Uint8Array) => new TextDecoder().decode(bytes).replace(/\n$/u, "");
const STATUS = /^(?<verdict>ok|ng) (?<ref>\S+)(?: (?<reason>.*))?$/u;
export const readReport = ( capabilities: ReadonlySet<string>, reply: Uint8Array): UpstreamReport => { const messages: Uint8Array[] = []; const status: Uint8Array[] = [];
if (Option.isSome(bandLimit(capabilities))) { for (const packet of packets(reply)) { if (packet !== "Flush") { // Band 1 carries the report, 2 progress, 3 a fatal error. (packet[0] === 1 ? status : messages).push(packet.subarray(1)); } } } else { status.push(reply); }
const lines = packets(concat(status)).flatMap((packet) => packet === "Flush" ? [] : [text(packet)] );
const unpack = Option.fromNullishOr( lines.find((line) => line.startsWith("unpack "))?.slice("unpack ".length) );
const refs = lines.flatMap((line): RefStatus[] => { const fields = STATUS.exec(line)?.groups;
return fields?.ref === undefined ? [] : [ { ng: fields.verdict === "ng" ? Option.some(fields.reason ?? "") : Option.none(), ref: fields.ref, }, ]; });
return { messages, refs, unpack };};
export const writeReport = ( capabilities: ReadonlySet<string>, messages: readonly Uint8Array[], refusals: readonly Refusal[], refs: readonly RefStatus[]) => { const reports = capabilities.has("report-status") || capabilities.has("report-status-v2");
const status = reports ? concat([ pkt(utf8("unpack ok\n")), ...refs.map(({ ng, ref }) => pkt( utf8( Option.match(ng, { onNone: () => `ok ${ref}\n`, onSome: (reason) => `ng ${ref} ${reason}\n`, }) ) ) ), FLUSH, ]) : new Uint8Array(0);
const explained = Array.isReadonlyArrayNonEmpty(refusals) ? explain(refusals) : [];
return Option.match(bandLimit(capabilities), { onNone: () => status, onSome: (limit) => concat([ ...messages.map((message) => band(2, message)), ...explained.map((line) => band(2, utf8(`${line}\n`))), ...chunks(status, limit).map((part) => band(1, part)), FLUSH, ]), });};
const ADVERTISED = /^(?<oid>[0-9a-f]{40}) (?<ref>[^\0\n ]+)/u;
export const readAdvertisement = (advertisement: Uint8Array) => new Map( packets(advertisement).flatMap((packet) => { const fields = packet === "Flush" ? undefined : ADVERTISED.exec(text(packet))?.groups;
return fields?.oid === undefined || fields.ref === undefined ? [] : [[fields.ref, CommitSha.make(fields.oid)] as const]; }) );
export const refuseAll = ( commands: readonly Command[], refusals: Array.NonEmptyReadonlyArray<Refusal>): RefStatus[] => commands.map(({ ref }) => ({ ng: Option.some(headline(refusals)), ref }));