Skip to content
Myr

repo-host.ts

This file

Path

packages/git/src/upstream/repo-host.ts

118 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–64why, one step back
1import { Fetch } from "@repo/artifacts/fetch";
2import { RepoHost } from "@repo/artifacts/repo-host";3import type { RepoName, TokenScope } from "@repo/artifacts/repo-host";4import {5 Cache,6 Data,7 DateTime,8 Duration,9 Effect,10 Exit,11 Layer,12 Redacted,13} from "effect";1415import { Exchange, Upstream, UpstreamFailure } from "../upstream.ts";16import type { Service } from "../upstream.ts";1718const TOKEN_TTL = Duration.hours(1);1920/** A token is dropped this long before it expires, so none expires mid-exchange. */21const MARGIN = Duration.minutes(1);2223const CAPACITY = 1024;2425class TokenKey extends Data.Class<{26 readonly repo: RepoName;27 readonly scope: TokenScope;28}> {}2930const scopeOf = (service: Service): TokenScope =>31 service === "git-receive-pack" ? "write" : "read";3233/**34 * Basic auth as Artifacts takes it: any user name, and the token's secret35 * without its `?expires=` suffix as the password.36 */37const basic = (secret: Redacted.Redacted) => {38 const [password = ""] = Redacted.value(secret).split("?expires=");3940 return `Basic ${btoa(`x:${password}`)}`;41};4243const requestOf = (44 remote: string,45 exchange: Exchange,46 authorization: string47) => {48 const headers = new Headers(exchange.headers);4950 headers.set("authorization", authorization);5152 return Exchange.$match(exchange, {53 Advertise: ({ service }) =>54 new Request(`${remote}/info/refs?service=${service}`, { headers }),55 Call: ({ body, service }) => {56 // Node sends a streamed body only with `duplex: "half"`; the Workers57 // types lack the key, and workerd ignores it.58 const init = { body, duplex: "half", headers, method: "POST" };5960 return new Request(`${remote}/${service}`, init);61 },62 });63};64

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 65why, one step back
65/** Tokens are cached per (repo, scope) in a cache made when the layer is built: one per isolate. */

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 66–118why, one step back
66export const UpstreamRepoHost: Layer.Layer<Upstream, never, RepoHost | Fetch> =
67 Layer.effect(68 Upstream,69 Effect.gen(function* () {70 const host = yield* RepoHost;71 const client = yield* Fetch;7273 const credentials = yield* Cache.makeWith(74 ({ repo, scope }: TokenKey) =>75 Effect.gen(function* () {76 const remote = yield* host.remote(repo);77 const token = yield* host.mintToken(repo, scope, TOKEN_TTL);78 const now = yield* DateTime.now;7980 return {81 authorization: basic(token.secret),82 lasts: Duration.subtract(83 DateTime.distance(now, token.expiresAt),84 MARGIN85 ),86 remote,87 };88 }),89 {90 capacity: CAPACITY,91 // A failed mint is not kept, nor a token too short-lived to use.92 timeToLive: (exit) =>93 Exit.isSuccess(exit) &&94 Duration.isGreaterThan(exit.value.lasts, Duration.zero)95 ? exit.value.lasts96 : Duration.zero,97 }98 );99100 return Upstream.of({101 exchange: (repo, exchange) =>102 Effect.gen(function* () {103 const { authorization, remote } = yield* Cache.get(104 credentials,105 new TokenKey({ repo, scope: scopeOf(exchange.service) })106 );107108 return yield* client109 .fetch(requestOf(remote, exchange, authorization))110 .pipe(111 Effect.mapError(112 ({ reason }) => new UpstreamFailure({ reason, repo })113 )114 );115 }),116 });117 })118 );