Skip to content
Myr

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 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–65why, one step back
1import { NodeHttpServer } from "@effect/platform-node";
2import type { RepoName } from "@repo/artifacts/repo-host";3import { CommitSha } from "@repo/protocol/ids";4import { gitOk } from "@repo/testkit/git";5import { httpBackend } from "@repo/testkit/http-backend";6import { Context, Effect, FileSystem, Layer, Option, Stream } from "effect";7import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";8import type { ChildProcessSpawner } from "effect/process";910import { Custody } from "../src/custody.ts";11import { gateway } from "../src/gateway.ts";12import { Exchange, Upstream } from "../src/upstream.ts";1314export const admitAll = Custody.of({15 admit: () => Effect.succeed([]),16 review: () => Effect.succeed([]),17});1819export const commit = (20 cwd: string,21 message: string,22 files: Readonly<Record<string, string>>23) =>24 Effect.gen(function* () {25 const fs = yield* FileSystem.FileSystem;2627 for (const [path, content] of Object.entries(files)) {28 yield* fs.writeFileString(`${cwd}/${path}`, content).pipe(Effect.orDie);29 }3031 yield* gitOk(cwd, "add", "--all");32 yield* gitOk(cwd, "commit", "--quiet", "--message", message);3334 return CommitSha.make(yield* gitOk(cwd, "rev-parse", "HEAD"));35 });3637const requestOf = (repo: RepoName, exchange: Exchange) =>38 Exchange.$match(exchange, {39 Advertise: ({ headers, service }) =>40 Effect.succeed(41 new Request(42 `http://upstream/${repo}.git/info/refs?service=${service}`,43 { headers }44 )45 ),46 // Read whole: a streamed request body needs Node's `duplex` option, which47 // the Workers types lack.48 Call: ({ body, headers, service }) =>49 Stream.fromReadableStream({50 evaluate: () => body,51 onError: (cause) => cause,52 }).pipe(53 Stream.runCollect,54 Effect.orDie,55 Effect.map(56 (chunks) =>57 new Request(`http://upstream/${repo}.git/${service}`, {58 body: new Blob(chunks),59 headers,60 method: "POST",61 })62 )63 ),64 });65

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 66why, one step back
66/** Like Artifacts, it takes no credentials from the client: an exchange carrying any is refused. */

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 67–132why, one step back
67const upstreamAt = (
68 root: string,69 atPush: Effect.Effect<70 Option.Option<Response>,71 never,72 ChildProcessSpawner.ChildProcessSpawner73 >74) =>75 Effect.map(76 Effect.context<ChildProcessSpawner.ChildProcessSpawner>(),77 (context) => {78 const serve = httpBackend(root);7980 return Upstream.of({81 exchange: (repo, exchange) =>82 Effect.scoped(83 Effect.gen(function* () {84 if (exchange.headers.has("authorization")) {85 return new Response(86 "the client's credentials reached the upstream",87 { status: 400 }88 );89 }9091 const instead =92 Exchange.$is("Call")(exchange) &&93 exchange.service === "git-receive-pack"94 ? yield* atPush95 : Option.none();9697 const request = yield* requestOf(repo, exchange);9899 return Option.isSome(instead)100 ? instead.value101 : yield* serve(request);102 })103 ).pipe(Effect.provide(context)),104 });105 }106 );107108const app = Effect.gen(function* () {109 const request = yield* HttpServerRequest.HttpServerRequest;110 const response = yield* gateway(yield* HttpServerRequest.toWeb(request));111112 return HttpServerResponse.fromWeb(response);113}).pipe(Effect.orDie);114115export interface Repos {116 readonly seed: string;117 readonly upstream: string;118}119120export const world = (121 custody: Custody["Service"],122 atPush: (123 repos: Repos124 ) => Effect.Effect<125 Option.Option<Response>,126 never,127 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem128 > = () => Effect.succeedNone129) =>130 Effect.gen(function* () {131 const fs = yield* FileSystem.FileSystem;132 const root = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 133why, one step back
133 const upstream = `${root}/alice__demo.git`;

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 134–175why, one step back
134 const seed = `${root}/seed`;
135136 yield* gitOk(137 root,138 "init",139 "--quiet",140 "--bare",141 "--initial-branch=main",142 upstream143 );144 yield* gitOk(root, "init", "--quiet", "--initial-branch=main", seed);145 yield* commit(seed, "first", { "README.md": "demo\n" });146 yield* gitOk(seed, "push", "--quiet", upstream, "HEAD:refs/heads/main");147148 const served = yield* Layer.build(149 HttpServer.serve(app).pipe(150 Layer.provideMerge(NodeHttpServer.layerTest),151 Layer.provide(152 Layer.effect(153 Upstream,154 upstreamAt(155 root,156 atPush({ seed, upstream }).pipe(157 Effect.provideService(FileSystem.FileSystem, fs)158 )159 )160 )161 ),162 Layer.provide(Layer.succeed(Custody, custody))163 )164 ).pipe(Effect.orDie);165166 const { address } = Context.get(served, HttpServer.HttpServer);167168 if (!("port" in address)) {169 return yield* Effect.die("the gateway listens on a unix socket");170 }171172 return {173 root,174 seed,175 upstream,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 176why, one step back
176 url: `http://127.0.0.1:${address.port}/alice/demo.git`,

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 177–178why, one step back
177 };
178 });