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 Digest from "effect/crypto/Digest";
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 /** `owner/name` of the change request the media is in, else of the checkout it was shown in. */
29 readonly repository?: string;
30}
31
32/** The server side of a definition's `credentials.appSignIn`. */
33export interface SourceControlSignIn {
34 /** Replaces any sign-in already pending for `host`; the driver finishes it on its own. */
35 readonly start: (
36 host: string,
37 ) => Effect.Effect<SourceControlSignInPrompt, SourceControlSignInError>;
38 /** The outcome of the latest sign-in `start` began for `host`, waiting for it if pending. */
39 readonly wait: (
40 host: string,
41 ) => Effect.Effect<SourceControlSignInResult, SourceControlSignInError>;
42 /** Ends the sign-in pending for `host`; its `wait` fails as cancelled. */
43 readonly cancel: (host: string) => Effect.Effect<void>;
44 /** Whether the sign-in reaches `owner/name`, so clients can suggest installing the app there. */
45 readonly access: (
46 host: string,
47 repository: string,
48 ) => Effect.Effect<SourceControlAppAccess["status"]>;
49}
50
51export interface SourceControlDriverInstance {
52 readonly sourceControl: SourceControlProvider.SourceControlProvider["Service"];
53 readonly discovery: SourceControlProviderDiscoverySpec;
54 /** Null for a host whose pull requests this build cannot read. */
55 readonly pullRequests: PullRequestProviderApi | null;
56 /**
57 * Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it
58 * to. Null when the fetch went nowhere a response could come from, such as a redirect loop.
59 */
60 readonly fetchMedia?: (
61 request: SourceControlMediaRequest,
62 ) => Effect.Effect<
63 HttpClientResponse.HttpClientResponse | null,
64 HttpClientError.HttpClientError,
65 Scope.Scope
66 >;
67 readonly signIn?: SourceControlSignIn;
68 /** Whether the server's account on `host` starred `repository` (`owner/name`). */
69 readonly isRepositoryStarred?: (host: string, repository: string) => Effect.Effect<boolean>;
70 /** Stars `repository` as the server's account on `host`; false when the forge refused. */
71 readonly starRepository?: (host: string, repository: string) => Effect.Effect<boolean>;
72}
73
74/** What the server gives every driver. A driver builds anything else it needs itself. */
75export type SourceControlDriverServices =
76 | SourceControlHost.SourceControlHost
77 | SourceControlRateLimit.SourceControlRateLimit
78 | HttpClient.HttpClient
79 | FileSystem.FileSystem
80 | Digest.Digest;
81
82export interface SourceControlDriver {
83 readonly kind: SourceControlProviderKind;
84 readonly definition: SourceControlDefinition;
85 /**
86 * Run once per server, in a scope that lasts as long as the server. State a host shares across
87 * everything that talks to it, such as GitHub's quota reserve and rate-limit pause, lives in
88 * what this builds, so building it twice would split that state.
89 */
90 readonly make: Effect.Effect<
91 SourceControlDriverInstance,
92 never,
93 SourceControlDriverServices | Scope.Scope
94 >;
95}
96
97export const defineSourceControlDriver = (driver: SourceControlDriver): SourceControlDriver =>
98 driver;
99
100/**
101 * `make` over a host's own `services`, built into the caller's scope so they live as long as the
102 * instance that holds them.
103 */
104export const withOwnServices = <A, R, ROut, RIn>(
105 make: Effect.Effect<A, never, R>,
106 services: Layer.Layer<ROut, never, RIn>,
107): Effect.Effect<A, never, Exclude<R, ROut> | RIn | Scope.Scope> =>
108 Layer.build(services).pipe(Effect.flatMap((context) => make.pipe(Effect.provide(context))));