Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

driver.ts

76 lines3.1 KB

1import type { SourceControlProviderKind } from "@t3tools/contracts";
2import type * as Crypto from "effect/Crypto";
3import * as Effect from "effect/Effect";
4import type * as FileSystem from "effect/FileSystem";
5import type { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
6import * as Layer from "effect/Layer";
7import type * as Scope from "effect/Scope";
8​
9import type { SourceControlDefinition } from "../shared/definition.ts";
10​
11import type { SourceControlProviderDiscoverySpec } from "./discovery.ts";
12import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
13import type * as SourceControlHost from "./SourceControlHost.ts";
14import type * as SourceControlProvider from "./SourceControlProvider.ts";
15import type * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
16​
17export interface SourceControlMediaRequest {
18 /** A `fetchUrl` this host's definition returned from `mediaUrl`. */
19 readonly url: string;
20 /** Request headers to forward, such as `range`. */
21 readonly headers: Readonly<Record<string, string>>;
23​
24export interface SourceControlDriverInstance {
25 readonly sourceControl: SourceControlProvider.SourceControlProvider["Service"];
26 readonly discovery: SourceControlProviderDiscoverySpec;
27 /** Null for a host whose pull requests this build cannot read. */
28 readonly pullRequests: PullRequestProviderApi | null;
29 /**
30 * Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it
31 * to. Null when the fetch went nowhere a response could come from, such as a redirect loop.
32 */
33 readonly fetchMedia?: (
34 request: SourceControlMediaRequest,
35 ) => Effect.Effect<
36 HttpClientResponse.HttpClientResponse | null,
37 HttpClientError.HttpClientError,
38 Scope.Scope
39 >;
41​
42/** What the server gives every driver. A driver builds anything else it needs itself. */
43export type SourceControlDriverServices =
44 | SourceControlHost.SourceControlHost
45 | SourceControlRateLimit.SourceControlRateLimit
46 | HttpClient.HttpClient
47 | FileSystem.FileSystem
48 | Crypto.Crypto;
49​
50export interface SourceControlDriver {
51 readonly kind: SourceControlProviderKind;
52 readonly definition: SourceControlDefinition;
53 /**
54 * Run once per server, in a scope that lasts as long as the server. State a host shares across
55 * everything that talks to it, such as GitHub's quota reserve and rate-limit pause, lives in
56 * what this builds, so building it twice would split that state.
57 */
58 readonly make: Effect.Effect<
59 SourceControlDriverInstance,
60 never,
61 SourceControlDriverServices | Scope.Scope
62 >;
64​
65export const defineSourceControlDriver = (driver: SourceControlDriver): SourceControlDriver =>
66 driver;
67​
68/**
69 * `make` over a host's own `services`, built into the caller's scope so they live as long as the
70 * instance that holds them.
71 */
72export const withOwnServices = <A, R, ROut, RIn>(
73 make: Effect.Effect<A, never, R>,
74 services: Layer.Layer<ROut, never, RIn>,
75): Effect.Effect<A, never, Exclude<R, ROut> | RIn | Scope.Scope> =>
76 Layer.build(services).pipe(Effect.flatMap((context) => make.pipe(Effect.provide(context))));