world.ts
This file
Path
packages/git/test/world.ts
178 lines · 5.1 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179import { NodeHttpServer } from "@effect/platform-node";import type { RepoName } from "@repo/artifacts/repo-host";import { CommitSha } from "@repo/protocol/ids";import { gitOk } from "@repo/testkit/git";import { httpBackend } from "@repo/testkit/http-backend";import { Context, Effect, FileSystem, Layer, Option, Stream } from "effect";import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";import type { ChildProcessSpawner } from "effect/process";
import { Custody } from "../src/custody.ts";import { gateway } from "../src/gateway.ts";import { Exchange, Upstream } from "../src/upstream.ts";
export const admitAll = Custody.of({ admit: () => Effect.succeed([]), review: () => Effect.succeed([]),});
export const commit = ( cwd: string, message: string, files: Readonly<Record<string, string>>) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem;
for (const [path, content] of Object.entries(files)) { yield* fs.writeFileString(`${cwd}/${path}`, content).pipe(Effect.orDie); }
yield* gitOk(cwd, "add", "--all"); yield* gitOk(cwd, "commit", "--quiet", "--message", message);
return CommitSha.make(yield* gitOk(cwd, "rev-parse", "HEAD")); });
const requestOf = (repo: RepoName, exchange: Exchange) => Exchange.$match(exchange, { Advertise: ({ headers, service }) => Effect.succeed( new Request( `http://upstream/${repo}.git/info/refs?service=${service}`, { headers } ) ), // Read whole: a streamed request body needs Node's `duplex` option, which // the Workers types lack. Call: ({ body, headers, service }) => Stream.fromReadableStream({ evaluate: () => body, onError: (cause) => cause, }).pipe( Stream.runCollect, Effect.orDie, Effect.map( (chunks) => new Request(`http://upstream/${repo}.git/${service}`, { body: new Blob(chunks), headers, method: "POST", }) ) ), });
/** Like Artifacts, it takes no credentials from the client: an exchange carrying any is refused. */const upstreamAt = ( root: string, atPush: Effect.Effect< Option.Option<Response>, never, ChildProcessSpawner.ChildProcessSpawner >) => Effect.map( Effect.context<ChildProcessSpawner.ChildProcessSpawner>(), (context) => { const serve = httpBackend(root);
return Upstream.of({ exchange: (repo, exchange) => Effect.scoped( Effect.gen(function* () { if (exchange.headers.has("authorization")) { return new Response( "the client's credentials reached the upstream", { status: 400 } ); }
const instead = Exchange.$is("Call")(exchange) && exchange.service === "git-receive-pack" ? yield* atPush : Option.none();
const request = yield* requestOf(repo, exchange);
return Option.isSome(instead) ? instead.value : yield* serve(request); }) ).pipe(Effect.provide(context)), }); } );
const app = Effect.gen(function* () { const request = yield* HttpServerRequest.HttpServerRequest; const response = yield* gateway(yield* HttpServerRequest.toWeb(request));
return HttpServerResponse.fromWeb(response);}).pipe(Effect.orDie);
export interface Repos { readonly seed: string; readonly upstream: string;}
export const world = ( custody: Custody["Service"], atPush: ( repos: Repos ) => Effect.Effect< Option.Option<Response>, never, ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem > = () => Effect.succeedNone) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const root = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie); const upstream = `${root}/alice__demo.git`; const seed = `${root}/seed`;
yield* gitOk( root, "init", "--quiet", "--bare", "--initial-branch=main", upstream ); yield* gitOk(root, "init", "--quiet", "--initial-branch=main", seed); yield* commit(seed, "first", { "README.md": "demo\n" }); yield* gitOk(seed, "push", "--quiet", upstream, "HEAD:refs/heads/main");
const served = yield* Layer.build( HttpServer.serve(app).pipe( Layer.provideMerge(NodeHttpServer.layerTest), Layer.provide( Layer.effect( Upstream, upstreamAt( root, atPush({ seed, upstream }).pipe( Effect.provideService(FileSystem.FileSystem, fs) ) ) ) ), Layer.provide(Layer.succeed(Custody, custody)) ) ).pipe(Effect.orDie);
const { address } = Context.get(served, HttpServer.HttpServer);
if (!("port" in address)) { return yield* Effect.die("the gateway listens on a unix socket"); }
return { root, seed, upstream, url: `http://127.0.0.1:${address.port}/alice/demo.git`, }; });