The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** Streaming smart-HTTP decoding and report-status encoding. No authorization policy. */ |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as HttpServerRequest from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import { concatBytes } from "../Protocol/ObjectCodec.ts"; |
| 7 | import { errPkt, flushPkt, pktText } from "../Protocol/Pkt.ts"; |
| 8 | import { sidebandFrames } from "../Protocol/Sideband.ts"; |
| 9 | import { StoreError } from "../Protocol/Store.ts"; |
| 10 | import { incomingStates, type PushInput } from "../Push.ts"; |
| 11 | import { parseReceivePackRequest, type CommitPushResult } from "../RepoObject.ts"; |
| 12 | import { feedBody, HEAD_BYTES } from "../Store/IncomingBody.ts"; |
| 13 | import { makeStreamingSource } from "../Store/StreamingSource.ts"; |
| 14 | |
| 15 | export { ReceivePack as endpoint } from "../Api/Protocol.ts"; |
| 16 | |
| 17 | export interface Push { |
| 18 | readonly _tag: "Push"; |
| 19 | readonly input: PushInput; |
| 20 | readonly updates: PushInput["updates"]; |
| 21 | readonly capabilities: ReadonlySet<string>; |
| 22 | } |
| 23 | |
| 24 | const headers = { |
| 25 | "cache-control": "no-cache", |
| 26 | "content-type": "application/x-git-receive-pack-result", |
| 27 | }; |
| 28 | const reply = (bytes: Uint8Array) => HttpServerResponse.uint8Array(bytes, { headers }); |
| 29 | export const probeResponse = () => HttpServerResponse.empty({ status: 200, headers }); |
| 30 | |
| 31 | /** Decode only the bounded command header. The body remains streaming until preparePush. */ |
| 32 | export const decode = (request: HttpServerRequest.HttpServerRequest) => |
| 33 | Effect.gen(function* () { |
| 34 | const web = yield* HttpServerRequest.toWeb(request).pipe( |
| 35 | Effect.mapError((error) => StoreError.make({ reason: `incoming body: ${error.message}` })), |
| 36 | ); |
| 37 | const feeder = makeStreamingSource(); |
| 38 | const gzip = /\bgzip\b/i.test(request.headers["content-encoding"] ?? ""); |
| 39 | const body = |
| 40 | gzip && web.body !== null ? web.body.pipeThrough(new DecompressionStream("gzip")) : web.body; |
| 41 | const receiving = yield* feedBody(body, feeder).pipe(Effect.result, Effect.forkScoped); |
| 42 | const state = { |
| 43 | feeder, |
| 44 | receiving, |
| 45 | packStart: 0, |
| 46 | declaredBytes: undefined as number | undefined, |
| 47 | active: true, |
| 48 | claimed: false, |
| 49 | }; |
| 50 | yield* Effect.addFinalizer(() => |
| 51 | Effect.gen(function* () { |
| 52 | state.active = false; |
| 53 | feeder.fail(StoreError.make({ reason: "push scope closed" })); |
| 54 | yield* Fiber.interrupt(receiving); |
| 55 | }), |
| 56 | ); |
| 57 | const head = yield* feeder.source.read(0, HEAD_BYTES); |
| 58 | if (!gzip && head[0] === 0x1f && head[1] === 0x8b) { |
| 59 | return yield* StoreError.make({ |
| 60 | reason: "gzip-encoded push without content-encoding", |
| 61 | }); |
| 62 | } |
| 63 | const parsed = yield* parseReceivePackRequest(head); |
| 64 | if (parsed.probe) return { _tag: "Probe" } as const; |
| 65 | const updates = Object.freeze(parsed.commands.map((command) => Object.freeze({ ...command }))); |
| 66 | const input: PushInput = Object.freeze({ |
| 67 | updates, |
| 68 | atomic: parsed.capabilities.has("atomic"), |
| 69 | }); |
| 70 | state.packStart = parsed.packStart; |
| 71 | const length = Number.parseInt(request.headers["content-length"] ?? "", 10); |
| 72 | state.declaredBytes = Number.isNaN(length) ? undefined : length; |
| 73 | incomingStates.set(input, state); |
| 74 | return { |
| 75 | _tag: "Push", |
| 76 | input, |
| 77 | updates, |
| 78 | capabilities: parsed.capabilities, |
| 79 | } satisfies Push; |
| 80 | }); |
| 81 | |
| 82 | /** Encode the negotiated Git report; policy rejection is a Git result, not JSON. */ |
| 83 | export const response = (push: Push, result: CommitPushResult) => { |
| 84 | if (!push.capabilities.has("report-status") && !push.capabilities.has("report-status-v2")) |
| 85 | return probeResponse(); |
| 86 | const lines = [ |
| 87 | pktText(`unpack ${result.unpack}`), |
| 88 | ...result.results.map((ref) => |
| 89 | pktText(ref.ok ? `ok ${ref.ref}` : `ng ${ref.ref} ${ref.reason ?? "failed"}`), |
| 90 | ), |
| 91 | flushPkt, |
| 92 | ]; |
| 93 | const report = concatBytes(lines); |
| 94 | return reply( |
| 95 | push.capabilities.has("side-band-64k") |
| 96 | ? concatBytes([...sidebandFrames(1, report), flushPkt]) |
| 97 | : report, |
| 98 | ); |
| 99 | }; |
| 100 | |
| 101 | /** Refuse the whole push without ingesting objects or moving any refs. */ |
| 102 | export const reject = (push: Push, reason: string) => |
| 103 | response(push, { |
| 104 | unpack: "ok", |
| 105 | results: push.updates.map((update) => ({ |
| 106 | ref: update.ref, |
| 107 | ok: false, |
| 108 | reason: reason.replace(/[\r\n\0]/g, " "), |
| 109 | })), |
| 110 | }); |
| 111 | |
| 112 | /** Encode malformed protocol input or a failed unpack operation. */ |
| 113 | export const failure = (reason: string) => reply(errPkt(reason.replace(/[\r\n\0]/g, " "))); |