Skip to content
Myr

http-backend.ts

This file

Path

packages/testkit/src/http-backend.ts

125 lines · 3.4 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–59why, one step back
1import { Effect, Stream } from "effect";
2import type { Scope } from "effect";3import type { ChildProcessSpawner } from "effect/process";45import { spawn } from "./git.ts";67const concat = (chunks: readonly Uint8Array[]) => {8 const all = new Uint8Array(chunks.reduce((n, chunk) => n + chunk.length, 0));9 let at = 0;1011 for (const chunk of chunks) {12 all.set(chunk, at);13 at += chunk.length;14 }1516 return all;17};1819const blankLine = (bytes: Uint8Array) => {20 for (let at = 0; at + 3 < bytes.length; at += 1) {21 if (22 bytes[at] === 13 &&23 bytes[at + 1] === 10 &&24 bytes[at + 2] === 13 &&25 bytes[at + 3] === 1026 ) {27 return at;28 }29 }3031 return -1;32};3334const fromCgi = (output: Uint8Array) => {35 const cut = blankLine(output);36 const head = new TextDecoder().decode(output.slice(0, Math.max(cut, 0)));37 const headers = new Headers();38 let status = 200;3940 for (const line of head.split("\r\n")) {41 const [name = "", ...rest] = line.split(": ");42 const value = rest.join(": ");4344 if (name.toLowerCase() === "status") {45 status = Number(value.split(" ")[0]);46 } else if (name !== "") {47 headers.append(name, value);48 }49 }5051 return new Response(output.slice(cut + 4), { headers, status });52};5354const SMART_HTTP =55 /\/(?<repo>[^/]+)\.git\/(?<path>info\/refs|git-upload-pack|git-receive-pack)$/u;5657const notFound = () =>58 new Response("not a smart-HTTP git URL", { status: 404 });59

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 60why, one step back
60/** The Workers types leave a request body's chunk type open. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 61–84why, one step back
61const bytes = () =>
62 new TransformStream<unknown, Uint8Array>({63 transform: (chunk, controller) => {64 if (chunk instanceof Uint8Array) {65 controller.enqueue(chunk);66 } else {67 controller.error(68 new TypeError("a request body held something other than bytes")69 );70 }71 },72 });7374const stdinOf = (request: Request) => {75 const { body } = request;7677 return body === null78 ? ("ignore" as const)79 : Stream.fromReadableStream({80 evaluate: () => body.pipeThrough(bytes()),81 onError: (cause) => cause,82 }).pipe(Stream.orDie);83};84

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 85why, one step back
85/** It reads no credentials: a test that wants a server to refuse them wraps this handler. */

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 86–125why, one step back
86export const httpBackend =
87 (root: string) =>88 (89 request: Request90 ): Effect.Effect<91 Response,92 never,93 ChildProcessSpawner.ChildProcessSpawner | Scope.Scope94 > =>95 Effect.gen(function* () {96 const url = new URL(request.url);97 const route = SMART_HTTP.exec(url.pathname)?.groups;9899 if (route === undefined) {100 return notFound();101 }102103 const header = (name: string) => request.headers.get(name) ?? "";104105 const child = yield* spawn("git", ["http-backend"], {106 env: {107 CONTENT_TYPE: header("content-type"),108 GIT_HTTP_EXPORT_ALL: "1",109 GIT_PROJECT_ROOT: root,110 GIT_PROTOCOL: header("git-protocol"),111 HTTP_CONTENT_ENCODING: header("content-encoding"),112 PATH_INFO: `/${route.repo}.git/${route.path}`,113 QUERY_STRING: url.search.replace(/^\?/u, ""),114 REMOTE_ADDR: "127.0.0.1",115 // http-backend serves receive-pack only to an authenticated user.116 REMOTE_USER: "tester",117 REQUEST_METHOD: request.method,118 },119 stdin: stdinOf(request),120 });121122 const output = yield* Stream.runCollect(child.stdout).pipe(Effect.orDie);123124 return fromCgi(concat(output));125 });