Open History to see the last commit
1import type {
2 SourceControlAppAccess,
3 SourceControlProviderKind,
4 SourceControlSignInError,
5 SourceControlSignInPrompt,
6 SourceControlSignInResult,
7} from "@t3tools/contracts";
8import type * as Crypto from "effect/Crypto";
9import * as Effect from "effect/Effect";
10import type * as FileSystem from "effect/FileSystem";
11import type { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
12import * as Layer from "effect/Layer";
13import type * as Scope from "effect/Scope";
14
15import type { SourceControlDefinition } from "../shared/definition.ts";
16
17import type { SourceControlProviderDiscoverySpec } from "./discovery.ts";
18import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
19import type * as SourceControlHost from "./SourceControlHost.ts";
20import type * as SourceControlProvider from "./SourceControlProvider.ts";
21import type * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
22
23export interface SourceControlMediaRequest {
24 /** A `fetchUrl` this host's definition returned from `mediaUrl`. */
25 readonly url: string;
26 /** Request headers to forward, such as `range`. */
27 readonly headers: Readonly<Record<string, string>>;
28}
29
30/** The server side of a definition's `credentials.appSignIn`. */
31export interface SourceControlSignIn {
32 /** Replaces any sign-in already pending for `host`; the driver finishes it on its own. */
33 readonly start: (
34 host: string,
35 ) => Effect.Effect<SourceControlSignInPrompt, SourceControlSignInError>;
36 /** The outcome of the latest sign-in `start` began for `host`, waiting for it if pending. */
37 readonly wait: (
38 host: string,
39 ) => Effect.Effect<SourceControlSignInResult, SourceControlSignInError>;
40 /** Ends the sign-in pending for `host`; its `wait` fails as cancelled. */
41 readonly cancel: (host: string) => Effect.Effect<void>;
42 /** Whether the sign-in reaches `owner/name`, so clients can suggest installing the app there. */
43 readonly access: (
44 host: string,
45 repository: string,
46 ) => Effect.Effect<SourceControlAppAccess["status"]>;
47}
48
49export interface SourceControlDriverInstance {
50 readonly sourceControl: SourceControlProvider.SourceControlProvider["Service"];
51 readonly discovery: SourceControlProviderDiscoverySpec;
52 /** Null for a host whose pull requests this build cannot read. */
53 readonly pullRequests: PullRequestProviderApi | null;
54 /**
55 * Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it
56 * to. Null when the fetch went nowhere a response could come from, such as a redirect loop.
57 */
58 readonly fetchMedia?: (
59 request: SourceControlMediaRequest,
60 ) => Effect.Effect<
61 HttpClientResponse.HttpClientResponse | null,
62 HttpClientError.HttpClientError,
63 Scope.Scope
64 >;
65 readonly signIn?: SourceControlSignIn;
66}
67
68/** What the server gives every driver. A driver builds anything else it needs itself. */
69export type SourceControlDriverServices =
70 | SourceControlHost.SourceControlHost
71 | SourceControlRateLimit.SourceControlRateLimit
72 | HttpClient.HttpClient
73 | FileSystem.FileSystem
74 | Crypto.Crypto;
75
76export interface SourceControlDriver {
77 readonly kind: SourceControlProviderKind;
78 readonly definition: SourceControlDefinition;
79 /**
80 * Run once per server, in a scope that lasts as long as the server. State a host shares across
81 * everything that talks to it, such as GitHub's quota reserve and rate-limit pause, lives in
82 * what this builds, so building it twice would split that state.
83 */
84 readonly make: Effect.Effect<
85 SourceControlDriverInstance,
86 never,
87 SourceControlDriverServices | Scope.Scope
88 >;
89}
90
91export const defineSourceControlDriver = (driver: SourceControlDriver): SourceControlDriver =>
92 driver;
93
94/**
95 * `make` over a host's own `services`, built into the caller's scope so they live as long as the
96 * instance that holds them.
97 */
98export const withOwnServices = <A, R, ROut, RIn>(
99 make: Effect.Effect<A, never, R>,
100 services: Layer.Layer<ROut, never, RIn>,
101): Effect.Effect<A, never, Exclude<R, ROut> | RIn | Scope.Scope> =>
102 Layer.build(services).pipe(Effect.flatMap((context) => make.pipe(Effect.provide(context))));