@devries-cameron20 / t3code-1 / commits / 7368bd307b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

refactor(source-control): drivers build their own services and own host media (#23994)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 24, 2026 at 04:24 UTC 7368bd307bbfdebeddfacdc0049339664d8c27dc
44 files changed +947 -494
apps/mobile/src/features/pull-requests/PullRequestMedia.tsx
+11 -4
@@ -1,8 +1,12 @@
1 import { createContext, useContext, useMemo, useState } from "react";
2 import { pullRequestMediaSource, pullRequestImageSource } from "./pullRequestMediaSource";
3 import { useFilePreviewGallery } from "../../components/FilePreviewGallery";
4 -import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
5 -import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
4 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
5 +import {
6 + SOURCE_CONTROL_MEDIA_ASSET,
7 + type AssetResource,
8 + type EnvironmentId,
9 +} from "@t3tools/contracts";
10 import { videoMimeType } from "@t3tools/shared/video";
11 import { Pressable, View } from "react-native";
12 import { Image } from "expo-image";
@@ -25,9 +29,12 @@ export const PullRequestMediaContext = createContext<{
29
30 export function usePullRequestMediaUrl(url: string) {
31 const context = useContext(PullRequestMediaContext);
28 - const fetchUrl = githubMediaFetchUrl(url);
32 + const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
33 const resource = useMemo<AssetResource | null>(
30 - () => (context && fetchUrl ? { _tag: "github-media", cwd: context.cwd, url: fetchUrl } : null),
34 + () =>
35 + context && fetchUrl
36 + ? { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: context.cwd, url: fetchUrl }
37 + : null,
38 [context, fetchUrl],
39 );
40 const asset = useAssetUrlState(context?.environmentId ?? null, resource);
apps/mobile/src/features/pull-requests/pullRequestMediaSource.ts
+4 -4
@@ -1,5 +1,5 @@
1 -import type { EnvironmentId } from "@t3tools/contracts";
2 -import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
1 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2 +import { SOURCE_CONTROL_MEDIA_ASSET, type EnvironmentId } from "@t3tools/contracts";
3 import type { FilePreviewItem } from "../../components/FilePreviewModal.types";
4
5 export interface PullRequestMediaLocation {
@@ -9,11 +9,11 @@ export interface PullRequestMediaLocation {
9
10 /** Keep the original resource identity so previewers can resolve and renew its capability. */
11 export function pullRequestMediaSource(url: string, location: PullRequestMediaLocation | null) {
12 - const fetchUrl = githubMediaFetchUrl(url);
12 + const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
13 return location && fetchUrl
14 ? {
15 environmentId: location.environmentId,
16 - resource: { _tag: "github-media" as const, cwd: location.cwd, url: fetchUrl },
16 + resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: location.cwd, url: fetchUrl },
17 }
18 : { uri: url };
19 }
apps/mobile/src/lib/videoPreviewSource.ts
+15 -4
@@ -1,4 +1,9 @@
1 -import type { AssetResource, ChatFileAttachment, EnvironmentId } from "@t3tools/contracts";
1 +import {
2 + SOURCE_CONTROL_MEDIA_ASSET,
3 + type AssetResource,
4 + type ChatFileAttachment,
5 + type EnvironmentId,
6 +} from "@t3tools/contracts";
7 import { videoMimeType } from "@t3tools/shared/video";
8
9 import type { DraftComposerFileAttachment } from "./composerImages";
@@ -17,7 +22,13 @@ export type MediaVideoPreviewSource = {
22 readonly environmentId: EnvironmentId;
23 readonly resource: Extract<
24 AssetResource,
20 - { readonly _tag: "attachment" | "media-file" | "draft-workspace-file" | "github-media" }
25 + {
26 + readonly _tag:
27 + | "attachment"
28 + | "media-file"
29 + | "draft-workspace-file"
30 + | typeof SOURCE_CONTROL_MEDIA_ASSET;
31 + }
32 >;
33 }
34 );
@@ -45,10 +56,10 @@ export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string
56 source.resource.path,
57 source.srcFragment ?? "",
58 ]
48 - : source.resource._tag === "github-media"
59 + : source.resource._tag === SOURCE_CONTROL_MEDIA_ASSET
60 ? [
61 "media-video",
51 - "github-media",
62 + SOURCE_CONTROL_MEDIA_ASSET,
63 source.environmentId,
64 source.resource.cwd,
65 source.resource.url,
apps/server/src/assets/AssetAccess.test.ts
+58 -87
@@ -4,7 +4,9 @@ import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
4 import * as NodeFSP from "node:fs/promises";
5 import {
6 AssetAccessError,
7 + AssetCreateUrlInput,
8 AssetPreviewTypeValidationError,
9 + SOURCE_CONTROL_MEDIA_ASSET,
10 ThreadId,
11 TurnItemId,
12 } from "@t3tools/contracts";
@@ -15,12 +17,11 @@ import * as DateTime from "effect/DateTime";
17 import * as Effect from "effect/Effect";
18 import * as FileSystem from "effect/FileSystem";
19 import * as Layer from "effect/Layer";
18 -import * as Redacted from "effect/Redacted";
20 import * as Path from "effect/Path";
21 import * as PlatformError from "effect/PlatformError";
22 import * as Schema from "effect/Schema";
23 import * as TestClock from "effect/testing/TestClock";
23 -import { HttpClient, HttpClientResponse, HttpServerResponse } from "effect/http";
24 +import { HttpClientRequest, HttpClientResponse, HttpServerResponse } from "effect/http";
25 import { vi } from "vite-plus/test";
26
27 import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -33,8 +34,10 @@ import { assetFileResponse } from "../http.ts";
34 import { ASSET_ROUTE_PREFIX, issueAssetUrl, readMcpAppHtml, resolveAsset } from "./AssetAccess.ts";
35 import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
36 import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
36 -import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
37 -import { githubMediaResponse } from "./GitHubMediaFetch.ts";
37 +import type { SourceControlDriverInstance } from "@t3tools/source-control-core/server/driver";
38 +import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts";
39 +import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
40 +import { sourceControlMediaResponse } from "./sourceControlMediaResponse.ts";
41 import * as HostProcess from "@t3tools/shared/HostProcess";
42
43 vi.mock("node:fs/promises", async (importOriginal) => {
@@ -178,53 +181,6 @@ describe("AssetAccess", () => {
181 );
182 });
183
181 - it.effect("loads private media immediately after login with the GitHub credential", () => {
182 - let lookups = 0;
183 - const authorizations: Array<string | undefined> = [];
184 - return Effect.gen(function* () {
185 - const asset = {
186 - url: "https://raw.githubusercontent.com/owner/repo/main/shot.png",
187 - cwd: "/repo",
188 - expiresAt: Number.MAX_SAFE_INTEGER,
189 - };
190 - expect((yield* githubMediaResponse(asset, {})).status).toBe(404);
191 - expect((yield* githubMediaResponse(asset, {})).status).toBe(200);
192 - expect((yield* githubMediaResponse(asset, {})).status).toBe(200);
193 - expect(lookups).toBe(3);
194 - expect(authorizations).toEqual([undefined, "Bearer signed-in", "Bearer signed-in"]);
195 - }).pipe(
196 - Effect.provide(
197 - Layer.mock(GitHubCredentials.GitHubCredentials)({
198 - get: (host) =>
199 - ++lookups === 1
200 - ? Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }))
201 - : Effect.succeed({
202 - host,
203 - token: Redacted.make("signed-in"),
204 - source: "gh" as const,
205 - fingerprint: "fingerprint",
206 - }),
207 - }),
208 - ),
209 - Effect.provideService(
210 - HttpClient.HttpClient,
211 - HttpClient.make((request) => {
212 - authorizations.push(request.headers.authorization);
213 - return Effect.succeed(
214 - HttpClientResponse.fromWeb(
215 - request,
216 - new Response(null, {
217 - status: request.headers.authorization ? 200 : 404,
218 - headers: { "content-type": "image/png" },
219 - }),
220 - ),
221 - );
222 - }),
223 - ),
224 - Effect.scoped,
225 - );
226 - });
227 -
184 it.effect("serves an image a tool returned inline from the stored item", () =>
185 Effect.gen(function* () {
186 const resource = {
@@ -994,51 +950,66 @@ describe("AssetAccess", () => {
950 }).pipe(Effect.provide(layerTest)),
951 );
952
997 - it.effect("serves GitHub-hosted pull request media through the repository's credential", () =>
998 - Effect.gen(function* () {
999 - const resolve = (relativeUrl: string) => {
1000 - const suffix = relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
1001 - const separator = suffix.indexOf("/");
1002 - return resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
1003 - };
1004 - const issue = (url: string) =>
1005 - issueAssetUrl({ resource: { _tag: "github-media", cwd: "/repo", url } });
1006 -
1007 - const attachment = yield* issue(
1008 - "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
1009 - );
1010 - expect(attachment.relativeUrl.endsWith("/1a1842fb-6383-492f-873c-57aa0033fa6c")).toBe(true);
1011 - expect(yield* resolve(attachment.relativeUrl)).toEqual({
1012 - kind: "github-media",
1013 - url: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
1014 - cwd: "/repo",
1015 - expiresAt: attachment.expiresAt,
953 + it.effect("serves a host's credentialed media through that host's driver", () => {
954 + const fetched: Array<{ url: string; headers: Readonly<Record<string, string>> }> = [];
955 + const url = "https://github.com/owner/repo/blob/main/docs/shot.png";
956 + const media = sourceControlClients.mediaUrl(url)!;
957 + const layerDrivers = Layer.succeed(BuiltInDrivers.SourceControlDrivers, [
958 + {
959 + driver: { kind: media.definition.kind, definition: media.definition },
960 + instance: {
961 + sourceControl: {} as SourceControlDriverInstance["sourceControl"],
962 + discovery: {} as SourceControlDriverInstance["discovery"],
963 + pullRequests: null,
964 + fetchMedia: (request) =>
965 + Effect.sync(() => {
966 + fetched.push(request);
967 + return HttpClientResponse.fromWeb(
968 + HttpClientRequest.get(request.url),
969 + new Response("bytes", { status: 206, headers: { "content-length": "5" } }),
970 + );
971 + }),
972 + },
973 + },
974 + ]);
975 + return Effect.gen(function* () {
976 + // Every client and server release spells the resource this way on the wire.
977 + const { resource } = yield* Schema.decodeUnknownEffect(AssetCreateUrlInput)({
978 + resource: { _tag: "github-media", cwd: "/repo", url },
979 });
1017 -
1018 - const committed = yield* issue("https://github.com/owner/repo/blob/main/docs/shot.png");
1019 - expect(yield* resolve(committed.relativeUrl)).toMatchObject({
1020 - url: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
980 + const issued = yield* issueAssetUrl({ resource });
981 + const suffix = issued.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
982 + const separator = suffix.indexOf("/");
983 + const asset = yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
984 + expect(asset).toEqual({
985 + kind: "source-control-media",
986 + url: media.fetchUrl,
987 + cwd: "/repo",
988 + expiresAt: issued.expiresAt,
989 });
990 + if (asset?.kind !== "source-control-media") return;
991
1023 - const legacy = yield* issue("https://github.com/owner/repo/assets/45952064/1a1842fb");
1024 - expect(yield* resolve(legacy.relativeUrl)).toMatchObject({
1025 - url: "https://github.com/owner/repo/assets/45952064/1a1842fb",
1026 - });
1027 - const lfs = yield* issue("https://media.githubusercontent.com/media/owner/repo/main/a.mp4");
1028 - expect(yield* resolve(lfs.relativeUrl)).toMatchObject({
1029 - url: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
992 + const response = yield* sourceControlMediaResponse(asset, {
993 + range: "bytes=0-4",
994 + cookie: "session",
995 });
1031 - const awkward = yield* issue("https://raw.githubusercontent.com/o/r/main/100%.png");
1032 - expect(awkward.relativeUrl.endsWith("/100%25.png")).toBe(true);
996 + expect(fetched).toEqual([{ url: media.fetchUrl, headers: { range: "bytes=0-4" } }]);
997 + expect(response.status).toBe(206);
998 + // The upstream sent no type, so it comes from the file name the host gave the media.
999 + expect(response.headers["content-type"]).toBe("image/png");
1000 + expect(response.headers["content-length"]).toBe("5");
1001 + }).pipe(Effect.provide(Layer.merge(layerTest, layerDrivers)), Effect.scoped);
1002 + });
1003
1004 + it.effect("refuses media no host keeps behind a credential, without echoing the URL", () =>
1005 + Effect.gen(function* () {
1006 for (const url of [
1007 "https://example.com/shot.png",
1008 "https://example.com/shot.png?token=private-media-token",
1037 - "http://github.com/user-attachments/assets/1a1842fb",
1038 - "https://github.com/owner/repo/pull/1",
1039 - "https://github.com/owner/repo/blob/main/",
1009 ]) {
1041 - const error = yield* issue(url).pipe(Effect.flip);
1010 + const error = yield* issueAssetUrl({
1011 + resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: "/repo", url },
1012 + }).pipe(Effect.flip);
1013 expect(error._tag).toBe("AssetGitHubMediaUrlValidationError");
1014 const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(AssetAccessError))(error);
1015 expect(encoded).not.toContain(url);
apps/server/src/assets/AssetAccess.ts
+15 -14
@@ -1,7 +1,7 @@
1 import type { AssetResource } from "@t3tools/contracts";
2 import {
3 AssetAttachmentNotFoundError,
4 - AssetGitHubMediaUrlValidationError,
4 + AssetSourceControlMediaUrlValidationError,
5 AssetPreviewTypeValidationError,
6 AssetProjectFaviconInspectionError,
7 AssetProjectFaviconNotFoundError,
@@ -13,6 +13,7 @@ import {
13 AssetWorkspacePathValidationError,
14 AssetWorkspaceResolutionError,
15 AssetWorkspaceRootNormalizationError,
16 + SOURCE_CONTROL_MEDIA_ASSET,
17 ThreadId,
18 ToolActivityNativeAppReference,
19 TurnItemId,
@@ -30,7 +31,6 @@ import {
31 readImageDimensions,
32 type ImageDimensions,
33 } from "@t3tools/shared/imageDimensions";
33 -import { githubMediaFetchUrl, githubMediaFileName } from "@t3tools/shared/githubMedia";
34 import { MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp";
35 import {
36 isProjectFaviconPath,
@@ -64,6 +64,7 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
64 import { isWithinRoot } from "../workspace/KnownWorkspaces.ts";
65 import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
66 import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
67 +import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
68 import { extractIcnsPng } from "./icns.ts";
69 import { openMediaFile, readMediaFileHeader, type OpenMediaFile } from "./MediaFile.ts";
70 import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -160,8 +161,8 @@ const AssetClaimsSchema = Schema.Union([
161 }),
162 Schema.Struct({
163 version: Schema.Literal(1),
163 - kind: Schema.Literal("github-media"),
164 - /** Already narrowed to a GitHub media host at mint time; the signature is what keeps it there. */
164 + kind: Schema.Literal(SOURCE_CONTROL_MEDIA_ASSET),
165 + /** A host's `fetchUrl`, narrowed at mint time; the signature is what keeps it there. */
166 url: Schema.String,
167 cwd: Schema.String,
168 expiresAt: Schema.Number,
@@ -189,7 +190,7 @@ export type ResolvedAsset =
190 readonly mimeType: string;
191 }
192 | {
192 - readonly kind: "github-media";
193 + readonly kind: "source-control-media";
194 readonly url: string;
195 readonly cwd: string;
196 readonly expiresAt: number;
@@ -803,19 +804,19 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
804 fileName = "native-app-icon.png";
805 break;
806 }
806 - case "github-media": {
807 - const fetchUrl = githubMediaFetchUrl(input.resource.url);
808 - if (fetchUrl === null) {
809 - return yield* new AssetGitHubMediaUrlValidationError({});
807 + case SOURCE_CONTROL_MEDIA_ASSET: {
808 + const media = sourceControlClients.mediaUrl(input.resource.url);
809 + if (media === null) {
810 + return yield* new AssetSourceControlMediaUrlValidationError({});
811 }
812 claims = {
813 version: 1,
813 - kind: "github-media",
814 - url: fetchUrl,
814 + kind: SOURCE_CONTROL_MEDIA_ASSET,
815 + url: media.fetchUrl,
816 cwd: input.resource.cwd,
817 expiresAt,
818 };
818 - fileName = githubMediaFileName(fetchUrl);
819 + fileName = media.fileName;
820 break;
821 }
822 }
@@ -926,9 +927,9 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
927 return faviconPath === claims.filePath ? yield* projectFaviconAsset(faviconPath) : null;
928 }
929
929 - if (claims.kind === "github-media") {
930 + if (claims.kind === SOURCE_CONTROL_MEDIA_ASSET) {
931 return {
931 - kind: "github-media",
932 + kind: "source-control-media",
933 url: claims.url,
934 cwd: claims.cwd,
935 expiresAt: claims.expiresAt,
apps/server/src/assets/GitHubMediaFetch.ts deleted
-138
@@ -1,138 +0,0 @@
1 -import * as Mime from "effect/http/Mime";
2 -import { githubMediaFileName } from "@t3tools/shared/githubMedia";
3 -import * as Clock from "effect/Clock";
4 -import * as Effect from "effect/Effect";
5 -import * as Option from "effect/Option";
6 -import * as Redacted from "effect/Redacted";
7 -import {
8 - FetchHttpClient,
9 - HttpClient,
10 - HttpClientRequest,
11 - HttpServerResponse,
12 - type HttpClientResponse,
13 -} from "effect/http";
14 -
15 -import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
16 -
17 -/**
18 - * Everything a redirect leads to — the presigned object stores GitHub hands assets off to —
19 - * authorizes with its own signature, and some of them reject a request that also carries a
20 - * bearer token.
21 - */
22 -const CREDENTIALED_HOSTS = new Set([
23 - "github.com",
24 - "www.github.com",
25 - "raw.githubusercontent.com",
26 - "media.githubusercontent.com",
27 -]);
28 -const isCredentialedHost = (url: string) => {
29 - try {
30 - return CREDENTIALED_HOSTS.has(new URL(url).hostname.toLowerCase());
31 - } catch {
32 - return false;
33 - }
34 -};
35 -
36 -const MAX_REDIRECTS = 3;
37 -const MANUAL_REDIRECT: RequestInit = { redirect: "manual" };
38 -const FORWARDED_REQUEST_HEADERS = ["range", "if-range"] as const;
39 -const FORWARDED_RESPONSE_HEADERS = [
40 - "content-type",
41 - "content-length",
42 - "content-range",
43 - "accept-ranges",
44 - "etag",
45 - "last-modified",
46 -] as const;
47 -const MEDIA_CONTENT_TYPE_PATTERN = /^(?:image|video|audio)\/[\w!#$&^.+-]+$/i;
48 -const SVG_CONTENT_TYPE = "image/svg+xml";
49 -// An SVG is a document: same policy the asset route gives a workspace SVG, so one embedded in a
50 -// body cannot run script against this origin.
51 -const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
52 -
53 -const githubToken = GitHubCredentials.GitHubCredentials.pipe(
54 - Effect.flatMap((credentials) => credentials.get("github.com")),
55 - Effect.map((credential) => credential.token),
56 - Effect.orElseSucceed(() => null),
57 -);
58 -
59 -const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedirects")(function* (
60 - url: string,
61 - headers: Record<string, string>,
62 - token: Redacted.Redacted | null,
63 -) {
64 - const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
65 - let target = url;
66 - for (let hop = 0; ; hop += 1) {
67 - const authorization =
68 - token !== null && isCredentialedHost(target) ? `Bearer ${Redacted.value(token)}` : null;
69 - const response: HttpClientResponse.HttpClientResponse = yield* httpClient
70 - .execute(
71 - HttpClientRequest.get(target).pipe(
72 - HttpClientRequest.setHeaders({
73 - ...headers,
74 - // The bytes are streamed straight through, so never let an encoding layer in.
75 - "accept-encoding": "identity",
76 - ...(authorization === null ? {} : { authorization }),
77 - }),
78 - ),
79 - )
80 - .pipe(Effect.provideService(FetchHttpClient.RequestInit, MANUAL_REDIRECT));
81 - const location = response.headers.location;
82 - if (response.status < 300 || response.status >= 400) return response;
83 - if (!location || hop >= MAX_REDIRECTS) return null;
84 - const next = new URL(location, target);
85 - if (next.protocol !== "https:") return null;
86 - target = next.toString();
87 - }
88 -});
89 -
90 -export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaResponse")(function* (
91 - asset: { readonly url: string; readonly cwd: string; readonly expiresAt: number },
92 - requestHeaders: Record<string, string | undefined>,
93 -) {
94 - // Both media hosts are served by github.com's account, which is the host `gh` stores it under.
95 - const token = yield* githubToken;
96 - const forwarded: Record<string, string> = {};
97 - for (const name of FORWARDED_REQUEST_HEADERS) {
98 - const value = requestHeaders[name];
99 - if (value !== undefined) forwarded[name] = value;
100 - }
101 - const response = yield* fetchFollowingRedirects(asset.url, forwarded, token);
102 - const remainingSeconds = Math.floor((asset.expiresAt - (yield* Clock.currentTimeMillis)) / 1000);
103 - const headers: Record<string, string> = {
104 - "cache-control":
105 - remainingSeconds > 0 ? `private, max-age=${remainingSeconds}` : "private, no-store",
106 - "x-content-type-options": "nosniff",
107 - };
108 - if (response === null) return HttpServerResponse.empty({ status: 502, headers });
109 - // It carries none of the upstream entity headers: a `content-length` with no body behind it
110 - // holds the connection open until the browser gives up on it.
111 - if (response.status >= 400) {
112 - return HttpServerResponse.empty({
113 - status: response.status >= 500 ? 502 : response.status,
114 - headers,
115 - });
116 - }
117 - const upstreamType =
118 - response.headers["content-type"]?.split(";", 1)[0]?.trim().toLowerCase() ?? "";
119 - const contentType = MEDIA_CONTENT_TYPE_PATTERN.test(upstreamType)
120 - ? upstreamType
121 - : Option.getOrElse(Mime.getType(githubMediaFileName(asset.url)), () => "").toLowerCase();
122 - if (!MEDIA_CONTENT_TYPE_PATTERN.test(contentType)) {
123 - return HttpServerResponse.empty({ status: 415, headers });
124 - }
125 - for (const name of FORWARDED_RESPONSE_HEADERS) {
126 - const value = response.headers[name];
127 - if (value !== undefined) headers[name] = value;
128 - }
129 - headers["content-type"] = contentType;
130 - if (contentType === SVG_CONTENT_TYPE) {
131 - headers["content-security-policy"] = SVG_CONTENT_SECURITY_POLICY;
132 - }
133 - return HttpServerResponse.stream(response.stream, {
134 - status: response.status,
135 - headers,
136 - contentType,
137 - });
138 -});
apps/server/src/assets/sourceControlMediaResponse.ts new
+80
@@ -0,0 +1,80 @@
1 +import * as Mime from "effect/http/Mime";
2 +import * as Clock from "effect/Clock";
3 +import * as Effect from "effect/Effect";
4 +import * as Option from "effect/Option";
5 +import { HttpServerResponse } from "effect/http";
6 +
7 +import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts";
8 +import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
9 +
10 +const FORWARDED_REQUEST_HEADERS = ["range", "if-range"] as const;
11 +const FORWARDED_RESPONSE_HEADERS = [
12 + "content-type",
13 + "content-length",
14 + "content-range",
15 + "accept-ranges",
16 + "etag",
17 + "last-modified",
18 +] as const;
19 +const MEDIA_CONTENT_TYPE_PATTERN = /^(?:image|video|audio)\/[\w!#$&^.+-]+$/i;
20 +const SVG_CONTENT_TYPE = "image/svg+xml";
21 +// An SVG is a document: same policy the asset route gives a workspace SVG, so one embedded in a
22 +// body cannot run script against this origin.
23 +const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
24 +
25 +export const sourceControlMediaResponse = Effect.fn(
26 + "SourceControlMediaResponse.sourceControlMediaResponse",
27 +)(function* (
28 + asset: { readonly url: string; readonly cwd: string; readonly expiresAt: number },
29 + requestHeaders: Record<string, string | undefined>,
30 +) {
31 + const remainingSeconds = Math.floor((asset.expiresAt - (yield* Clock.currentTimeMillis)) / 1000);
32 + const headers: Record<string, string> = {
33 + "cache-control":
34 + remainingSeconds > 0 ? `private, max-age=${remainingSeconds}` : "private, no-store",
35 + "x-content-type-options": "nosniff",
36 + };
37 + const media = sourceControlClients.mediaUrl(asset.url);
38 + const drivers = yield* BuiltInDrivers.SourceControlDrivers;
39 + const fetchMedia = drivers.find(({ driver }) => driver.kind === media?.definition.kind)?.instance
40 + .fetchMedia;
41 + if (media === null || fetchMedia === undefined) {
42 + return HttpServerResponse.empty({ status: 404, headers });
43 + }
44 + const forwarded: Record<string, string> = {};
45 + for (const name of FORWARDED_REQUEST_HEADERS) {
46 + const value = requestHeaders[name];
47 + if (value !== undefined) forwarded[name] = value;
48 + }
49 + const response = yield* fetchMedia({ url: asset.url, headers: forwarded });
50 + if (response === null) return HttpServerResponse.empty({ status: 502, headers });
51 + // It carries none of the upstream entity headers: a `content-length` with no body behind it
52 + // holds the connection open until the browser gives up on it.
53 + if (response.status >= 400) {
54 + return HttpServerResponse.empty({
55 + status: response.status >= 500 ? 502 : response.status,
56 + headers,
57 + });
58 + }
59 + const upstreamType =
60 + response.headers["content-type"]?.split(";", 1)[0]?.trim().toLowerCase() ?? "";
61 + const contentType = MEDIA_CONTENT_TYPE_PATTERN.test(upstreamType)
62 + ? upstreamType
63 + : Option.getOrElse(Mime.getType(media.fileName), () => "").toLowerCase();
64 + if (!MEDIA_CONTENT_TYPE_PATTERN.test(contentType)) {
65 + return HttpServerResponse.empty({ status: 415, headers });
66 + }
67 + for (const name of FORWARDED_RESPONSE_HEADERS) {
68 + const value = response.headers[name];
69 + if (value !== undefined) headers[name] = value;
70 + }
71 + headers["content-type"] = contentType;
72 + if (contentType === SVG_CONTENT_TYPE) {
73 + headers["content-security-policy"] = SVG_CONTENT_SECURITY_POLICY;
74 + }
75 + return HttpServerResponse.stream(response.stream, {
76 + status: response.status,
77 + headers,
78 + contentType,
79 + });
80 +});
apps/server/src/http.ts
+9 -4
@@ -31,7 +31,7 @@ import { OtlpTracer, OtlpSerialization } from "effect/observability";
31
32 import * as ServerConfig from "./config.ts";
33 import { ASSET_ROUTE_PREFIX, readMcpAppHtml, resolveAsset } from "./assets/AssetAccess.ts";
34 -import { githubMediaResponse } from "./assets/GitHubMediaFetch.ts";
34 +import { sourceControlMediaResponse } from "./assets/sourceControlMediaResponse.ts";
35 import { statMediaFile, streamMediaFile, type OpenMediaFile } from "./assets/MediaFile.ts";
36 import {
37 ATTACHMENT_UPLOAD_ROUTE_PREFIX,
@@ -432,10 +432,15 @@ export const layerAssetRoute = HttpRouter.add(
432 headers: { "cache-control": "private, max-age=3600", "x-content-type-options": "nosniff" },
433 });
434 }
435 - if (asset.kind === "github-media") {
436 - return yield* githubMediaResponse(asset, request.headers).pipe(
435 + if (asset.kind === "source-control-media") {
436 + return yield* sourceControlMediaResponse(asset, request.headers).pipe(
437 Effect.tapError((cause) =>
438 - Effect.logWarning("Failed to fetch GitHub media.", { url: asset.url, cause }),
438 + // The URL can carry a token in its query string, and the error keeps the request it
439 + // failed on; the host and the failure kind are enough to diagnose.
440 + Effect.logWarning("Failed to fetch source control media.", {
441 + host: URL.canParse(asset.url) ? new URL(asset.url).host : undefined,
442 + reason: cause.reason._tag,
443 + }),
444 ),
445 Effect.orElseSucceed(() =>
446 HttpServerResponse.empty({
apps/server/src/pullRequest/PullRequestProviderRegistry.ts
+2 -6
@@ -28,12 +28,8 @@ export function fromProviders(
28 * @public Service construction is part of the canonical Effect module API.
29 */
30 export const make = Effect.gen(function* () {
31 - const drivers = yield* Effect.forEach(BuiltInDrivers.BUILT_IN_SOURCE_CONTROL_DRIVERS, (driver) =>
32 - driver.make.pipe(Effect.map((instance) => instance.pullRequests)),
33 - );
34 - return fromProviders(
35 - drivers.filter((provider): provider is PullRequestProviderApi => provider !== null),
36 - );
31 + const drivers = yield* BuiltInDrivers.SourceControlDrivers;
32 + return fromProviders(drivers.flatMap(({ instance }) => instance.pullRequests ?? []));
33 });
34
35 export const layer = Layer.effect(PullRequestProviderRegistry, make).pipe(
apps/server/src/server.ts
+2 -3
@@ -60,6 +60,7 @@ import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRe
60 import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts";
61 import * as CheckpointStore from "./checkpointing/CheckpointStore.ts";
62 import * as SourceControlBuiltInDrivers from "./sourceControl/builtInDrivers.ts";
63 +import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST } from "./sourceControl/sourceControlClients.ts";
64 import * as TextGeneration from "./textGeneration/TextGeneration.ts";
65 import * as ProviderInstanceRegistryHydration from "./provider/ProviderInstanceRegistryHydration.ts";
66 import * as ProjectSettingsOverrideSweep from "./provider/ProjectSettingsOverrideSweep.ts";
@@ -332,9 +333,7 @@ const layerRepositoryIdentityResolver = Layer.effect(
333 const registry = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
334 return yield* RepositoryIdentityResolver.make({
335 refine: Effect.fn(function* (identity: RepositoryIdentity) {
335 - for (const kind of SourceControlBuiltInDrivers.BUILT_IN_SOURCE_CONTROL_DRIVERS.map(
336 - (driver) => driver.kind,
337 - )) {
336 + for (const { kind } of BUILT_IN_SOURCE_CONTROL_DRIVER_LIST) {
337 const provider = yield* registry.get(kind);
338 if (provider.refineRepositoryIdentity === undefined) continue;
339 const refined = yield* provider.refineRepositoryIdentity({
apps/server/src/sourceControl/SourceControlDiscovery.test.ts
+4
@@ -23,6 +23,7 @@ import * as BitbucketPullRequestApi from "@t3tools/source-control-bitbucket/serv
23 import * as GitHubRest from "@t3tools/source-control-github/server/GitHubRest";
24 import * as GitHubRequestPolicy from "@t3tools/source-control-github/server/GitHubRequestPolicy";
25 import * as GitHubPullRequestApi from "@t3tools/source-control-github/server/GitHubPullRequestApi";
26 +import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
27 import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
28 import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
29 import * as ForgejoCli from "@t3tools/source-control-forgejo/server/ForgejoCli";
@@ -30,6 +31,7 @@ import * as GitCafeApi from "@t3tools/source-control-gitcafe/server/GitCafeApi";
31 import * as GitCafeCredentials from "@t3tools/source-control-gitcafe/server/GitCafeCredentials";
32 import * as ForgejoSourceControlProvider from "@t3tools/source-control-forgejo/server/ForgejoSourceControlProvider";
33 import * as SourceControlDiscovery from "./SourceControlDiscovery.ts";
34 +import * as BuiltInDriversTestkit from "./builtInDrivers.testkit.ts";
35 import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
36 import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
37 import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
@@ -39,6 +41,7 @@ const layerSourceControlProviderRegistryTest = (input: {
41 readonly process: Partial<VcsProcess.VcsProcess["Service"]>;
42 }) =>
43 SourceControlProviderRegistry.layer.pipe(
44 + Layer.provide(BuiltInDriversTestkit.layerOverHostServices),
45 Layer.provide(
46 Layer.mergeAll(
47 ServerConfig.layerTest(process.cwd(), {
@@ -50,6 +53,7 @@ const layerSourceControlProviderRegistryTest = (input: {
53 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({}),
54 ServerSettings.ServerSettingsService.layerTest(),
55 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({}),
56 + Layer.mock(GitHubCredentials.GitHubCredentials)({}),
57 Layer.mock(GitHubRest.GitHubRest)({}),
58 // The GitHub provider's GraphQL reads go through these; no test here makes one.
59 Layer.mock(GitHubRequestPolicy.GitHubRequestPolicy)({}),
apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts
+51 -44
@@ -39,6 +39,7 @@ import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/Git
39 import * as ForgejoCli from "@t3tools/source-control-forgejo/server/ForgejoCli";
40 import * as GitCafeApi from "@t3tools/source-control-gitcafe/server/GitCafeApi";
41 import * as GitCafeCredentials from "@t3tools/source-control-gitcafe/server/GitCafeCredentials";
42 +import * as BuiltInDriversTestkit from "./builtInDrivers.testkit.ts";
43 import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
44 import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
45
@@ -114,53 +115,59 @@ function makeRegistry(input: {
115
116 return SourceControlProviderRegistry.make.pipe(
117 Effect.provide(
117 - Layer.mergeAll(
118 - NodeServices.layer,
119 - layerRegistry,
120 - layerProcess,
121 - ServerSourceControlHost.layer.pipe(
122 - Layer.provide(
123 - Layer.mergeAll(
124 - layerProcess,
125 - layerRegistry,
126 - ServerSettings.ServerSettingsService.layerTest(),
127 - Layer.mock(GitVcsDriver.GitVcsDriver)({}),
118 + BuiltInDriversTestkit.layerOverHostServices.pipe(
119 + Layer.provideMerge(
120 + Layer.mergeAll(
121 + NodeServices.layer,
122 + layerRegistry,
123 + layerProcess,
124 + ServerSourceControlHost.layer.pipe(
125 + Layer.provide(
126 + Layer.mergeAll(
127 + layerProcess,
128 + layerRegistry,
129 + ServerSettings.ServerSettingsService.layerTest(),
130 + Layer.mock(GitVcsDriver.GitVcsDriver)({}),
131 + ),
132 + ),
133 ),
134 + Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
135 + Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({}),
136 + Layer.mock(BitbucketApi.BitbucketApi)({}),
137 + Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({}),
138 + input.settings
139 + ? Layer.mock(ServerSettings.ServerSettingsService)(input.settings)
140 + : ServerSettings.ServerSettingsService.layerTest(),
141 + Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({}),
142 + Layer.mock(GitHubCredentials.GitHubCredentials)({}),
143 + // The GitHub provider's GraphQL reads go through these; no test here makes one.
144 + Layer.mock(GitHubRequestPolicy.GitHubRequestPolicy)({}),
145 + Layer.succeed(
146 + HttpClient.HttpClient,
147 + HttpClient.make(() => Effect.die(new Error("Unexpected GitHub GraphQL read"))),
148 + ),
149 + Layer.mock(GitHubRest.GitHubRest)({
150 + // No GitHub credential unless a test supplies one, so custom hosts stay unclaimed.
151 + credential: (host) =>
152 + Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
153 + ...input.githubApi,
154 + }),
155 + Layer.mock(GitVcsDriver.GitVcsDriver)({}),
156 + Layer.mock(GitLabCli.GitLabCli)(input.gitlab ?? {}),
157 + Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({}),
158 + Layer.mock(ForgejoCli.ForgejoCli)({
159 + listLogins: () => Effect.succeed([]),
160 + ...input.forgejo,
161 + }),
162 + Layer.mock(GitCafeApi.GitCafeApi)({}),
163 + Layer.mock(GitCafeCredentials.GitCafeCredentials)({
164 + cliEnv: () => Effect.succeed({}),
165 + }),
166 + ServerConfig.layerTest(process.cwd(), {
167 + prefix: "t3-source-control-registry-test-",
168 + }).pipe(Layer.provide(NodeServices.layer)),
169 ),
170 ),
131 - Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
132 - Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({}),
133 - Layer.mock(BitbucketApi.BitbucketApi)({}),
134 - Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({}),
135 - input.settings
136 - ? Layer.mock(ServerSettings.ServerSettingsService)(input.settings)
137 - : ServerSettings.ServerSettingsService.layerTest(),
138 - Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({}),
139 - // The GitHub provider's GraphQL reads go through these; no test here makes one.
140 - Layer.mock(GitHubRequestPolicy.GitHubRequestPolicy)({}),
141 - Layer.succeed(
142 - HttpClient.HttpClient,
143 - HttpClient.make(() => Effect.die(new Error("Unexpected GitHub GraphQL read"))),
144 - ),
145 - Layer.mock(GitHubRest.GitHubRest)({
146 - // No GitHub credential unless a test supplies one, so custom hosts stay unclaimed.
147 - credential: (host) => Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })),
148 - ...input.githubApi,
149 - }),
150 - Layer.mock(GitVcsDriver.GitVcsDriver)({}),
151 - Layer.mock(GitLabCli.GitLabCli)(input.gitlab ?? {}),
152 - Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({}),
153 - Layer.mock(ForgejoCli.ForgejoCli)({
154 - listLogins: () => Effect.succeed([]),
155 - ...input.forgejo,
156 - }),
157 - Layer.mock(GitCafeApi.GitCafeApi)({}),
158 - Layer.mock(GitCafeCredentials.GitCafeCredentials)({
159 - cliEnv: () => Effect.succeed({}),
160 - }),
161 - ServerConfig.layerTest(process.cwd(), {
162 - prefix: "t3-source-control-registry-test-",
163 - }).pipe(Layer.provide(NodeServices.layer)),
171 ),
172 ),
173 );
apps/server/src/sourceControl/SourceControlProviderRegistry.ts
+7 -9
@@ -358,16 +358,14 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit
358 );
359
360 export const make = Effect.gen(function* () {
361 - const drivers = yield* Effect.forEach(BuiltInDrivers.BUILT_IN_SOURCE_CONTROL_DRIVERS, (driver) =>
362 - driver.make.pipe(
363 - Effect.map((instance): SourceControlProviderRegistration => ({
364 - kind: driver.kind,
365 - provider: instance.sourceControl,
366 - discovery: instance.discovery,
367 - })),
368 - ),
361 + const drivers = yield* BuiltInDrivers.SourceControlDrivers;
362 + return yield* makeWithProviders(
363 + drivers.map(({ driver, instance }): SourceControlProviderRegistration => ({
364 + kind: driver.kind,
365 + provider: instance.sourceControl,
366 + discovery: instance.discovery,
367 + })),
368 );
370 - return yield* makeWithProviders(drivers);
369 });
370
371 export const layer = Layer.effect(SourceControlProviderRegistry, make);
apps/server/src/sourceControl/SourceControlRepositoryService.test.ts
+45
@@ -101,6 +101,51 @@ function layer(input: {
101 : layerService.pipe(Layer.provideMerge(NodeServices.layer));
102 }
103
104 +const lookupFailingWith = (cause: unknown, detail: string) =>
105 + Effect.gen(function* () {
106 + const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
107 + return yield* service
108 + .lookupRepository({ provider: SourceControlProviderKind.make("bitbucket"), repository: "x" })
109 + .pipe(Effect.flip);
110 + }).pipe(
111 + Effect.provide(
112 + layer({
113 + provider: makeProvider({
114 + getRepositoryCloneUrls: (input) =>
115 + Effect.fail(
116 + new SourceControlProviderError({
117 + provider: SourceControlProviderKind.make("bitbucket"),
118 + operation: "getRepositoryCloneUrls",
119 + cwd: input.cwd,
120 + detail,
121 + cause,
122 + }),
123 + ),
124 + }),
125 + }),
126 + ),
127 + );
128 +
129 +it.effect("tells the user a host's fixed explanation of a failed repository lookup", () =>
130 + Effect.gen(function* () {
131 + const error = yield* lookupFailingWith(
132 + { userFacingDetail: "Repositories are named workspace/repository." },
133 + "Repositories are named workspace/repository.",
134 + );
135 + assert.strictEqual(error.detail, "Repositories are named workspace/repository.");
136 + }),
137 +);
138 +
139 +it.effect("keeps a host's raw response text out of a failed repository lookup", () =>
140 + Effect.gen(function* () {
141 + const error = yield* lookupFailingWith(
142 + new Error("upstream"),
143 + "<html>internal token=abc</html>",
144 + );
145 + assert.strictEqual(error.detail, "The source control operation could not be completed.");
146 + }),
147 +);
148 +
149 it.effect("clones a looked-up repository after creating missing parent directories", () =>
150 Effect.gen(function* () {
151 const fs = yield* FileSystem.FileSystem;
apps/server/src/sourceControl/SourceControlRepositoryService.ts
+4 -7
@@ -30,14 +30,13 @@ import {
30 type GitCloneProgressLine,
31 } from "../project/gitCloneProgress.ts";
32 import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
33 -import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi";
33 +import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
34 import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
35 import * as HostProcess from "@t3tools/shared/HostProcess";
36 import { cloneUrlForProtocol, cloneUrlProtocol } from "@t3tools/shared/cloneUrl";
37 const isSourceControlRepositoryError = Schema.is(SourceControlRepositoryError);
38 const isSourceControlProviderError = Schema.is(SourceControlProviderError);
39 const isGitCommandError = Schema.is(GitCommandError);
40 -const isBitbucketRepositoryLocatorError = Schema.is(BitbucketApi.BitbucketRepositoryLocatorError);
40
41 export class SourceControlRepositoryService extends Context.Service<
42 SourceControlRepositoryService,
@@ -95,11 +94,9 @@ function mapRepositoryError(operation: string, provider: SourceControlProviderKi
94 operation,
95 provider,
96 detail:
98 - isSourceControlProviderError(cause) &&
99 - cause.provider === "bitbucket" &&
100 - isBitbucketRepositoryLocatorError(cause.cause)
101 - ? BitbucketApi.BitbucketRepositoryLocatorError.detail
102 - : "The source control operation could not be completed.",
97 + (isSourceControlProviderError(cause)
98 + ? SourceControlProvider.userFacingSourceControlDetail(cause.cause)
99 + : undefined) ?? "The source control operation could not be completed.",
100 cause,
101 }),
102 );
apps/server/src/sourceControl/builtInDrivers.testkit.ts new
+40
@@ -0,0 +1,40 @@
1 +import * as AzureDevOpsDriver from "@t3tools/source-control-azure-devops/server/driver";
2 +import * as BitbucketDriver from "@t3tools/source-control-bitbucket/server/driver";
3 +import * as ForgejoDriver from "@t3tools/source-control-forgejo/server/driver";
4 +import * as GitCafeDriver from "@t3tools/source-control-gitcafe/server/driver";
5 +import * as GitHubDriver from "@t3tools/source-control-github/server/driver";
6 +import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
7 +import type {
8 + SourceControlDriver,
9 + SourceControlDriverInstance,
10 +} from "@t3tools/source-control-core/server/driver";
11 +import * as Effect from "effect/Effect";
12 +import * as Layer from "effect/Layer";
13 +
14 +import * as BuiltInDrivers from "./builtInDrivers.ts";
15 +
16 +const entry = (
17 + driver: SourceControlDriver,
18 + instance: SourceControlDriverInstance,
19 +): BuiltInDrivers.SourceControlDriverEntry => ({ driver, instance });
20 +
21 +/** Every built-in driver over whatever stands in for its host's own services. */
22 +export const layerOverHostServices = Layer.effect(
23 + BuiltInDrivers.SourceControlDrivers,
24 + Effect.all([
25 + GitHubDriver.makeInstance.pipe(Effect.map((instance) => entry(GitHubDriver.driver, instance))),
26 + GitLabDriver.makeInstance.pipe(Effect.map((instance) => entry(GitLabDriver.driver, instance))),
27 + GitCafeDriver.makeInstance.pipe(
28 + Effect.map((instance) => entry(GitCafeDriver.driver, instance)),
29 + ),
30 + ForgejoDriver.makeInstance.pipe(
31 + Effect.map((instance) => entry(ForgejoDriver.driver, instance)),
32 + ),
33 + BitbucketDriver.makeInstance.pipe(
34 + Effect.map((instance) => entry(BitbucketDriver.driver, instance)),
35 + ),
36 + AzureDevOpsDriver.makeInstance.pipe(
37 + Effect.map((instance) => entry(AzureDevOpsDriver.driver, instance)),
38 + ),
39 + ]),
40 +);
apps/server/src/sourceControl/builtInDrivers.ts
+24 -30
@@ -1,37 +1,31 @@
1 -import * as AzureDevOpsCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsCli";
2 -import * as AzureDevOpsPullRequestCli from "@t3tools/source-control-azure-devops/server/AzureDevOpsPullRequestCli";
3 -import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi";
4 -import * as BitbucketPullRequestApi from "@t3tools/source-control-bitbucket/server/BitbucketPullRequestApi";
5 -import * as ForgejoCli from "@t3tools/source-control-forgejo/server/ForgejoCli";
6 -import * as GitCafeApi from "@t3tools/source-control-gitcafe/server/GitCafeApi";
7 -import * as GitCafeCredentials from "@t3tools/source-control-gitcafe/server/GitCafeCredentials";
8 -import * as GitHubRest from "@t3tools/source-control-github/server/GitHubRest";
9 -import * as GitHubPullRequestApi from "@t3tools/source-control-github/server/GitHubPullRequestApi";
10 -import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
11 -import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
12 -import type { SourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import type {
2 + SourceControlDriver,
3 + SourceControlDriverInstance,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
6 +import * as Context from "effect/Context";
7 +import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9
10 import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
17 -import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST as drivers } from "./sourceControlClients.ts";
11 +import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST } from "./sourceControlClients.ts";
12
19 -export type BuiltInSourceControlDriversEnv =
20 - (typeof drivers)[number] extends SourceControlDriver<infer R> ? R : never;
13 +export interface SourceControlDriverEntry {
14 + readonly driver: Pick<SourceControlDriver, "kind" | "definition">;
15 + readonly instance: SourceControlDriverInstance;
16 +}
17
22 -export const BUILT_IN_SOURCE_CONTROL_DRIVERS: ReadonlyArray<
23 - SourceControlDriver<BuiltInSourceControlDriversEnv>
24 -> = drivers;
18 +/** Every driver's instance, in registry order. Everything that reads a host reads it here. */
19 +export class SourceControlDrivers extends Context.Service<
20 + SourceControlDrivers,
21 + ReadonlyArray<SourceControlDriverEntry>
22 +>()("t3/sourceControl/builtInDrivers/SourceControlDrivers") {}
23
26 -export const layer = Layer.mergeAll(
27 - // `GitHubRest.layerWithDependencies` carries the quota reserve and rate-limit pause every GitHub
28 - // reader shares, so the server builds it once, here.
29 - GitHubPullRequestApi.layer.pipe(Layer.provideMerge(GitHubRest.layerWithDependencies)),
30 - AzureDevOpsPullRequestCli.layer.pipe(Layer.provideMerge(AzureDevOpsCli.layer)),
31 - BitbucketPullRequestApi.layer.pipe(
32 - Layer.provideMerge(BitbucketApi.layer.pipe(Layer.provide(SourceControlRateLimit.layer))),
33 - ),
34 - ForgejoCli.layer,
35 - GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer)),
36 - GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
37 -).pipe(Layer.provideMerge(ServerSourceControlHost.layer));
24 +const make = Effect.forEach(BUILT_IN_SOURCE_CONTROL_DRIVER_LIST, (driver) =>
25 + driver.make.pipe(Effect.map((instance) => ({ driver, instance }))),
26 +);
27 +
28 +export const layer = Layer.effect(SourceControlDrivers, make).pipe(
29 + Layer.provideMerge(ServerSourceControlHost.layer),
30 + Layer.provideMerge(SourceControlRateLimit.layer),
31 +);
apps/server/src/ws.ts
+2 -1
@@ -85,6 +85,7 @@ import {
85 FilesystemBrowseError,
86 AssetWorkspaceContextNotFoundError,
87 AssetWorkspaceContextResolutionError,
88 + SOURCE_CONTROL_MEDIA_ASSET,
89 ChatAttachmentId,
90 PersistChatAttachmentsError,
91 RpcClientId,
@@ -2772,7 +2773,7 @@ export const layerWsRpc = (
2773 input.resource._tag === "attachment" ||
2774 input.resource._tag === "native-app-icon" ||
2775 input.resource._tag === "tool-output-image" ||
2775 - input.resource._tag === "github-media" ||
2776 + input.resource._tag === SOURCE_CONTROL_MEDIA_ASSET ||
2777 // A network path is served only from the thread's project, looked up below.
2778 (input.resource._tag === "media-file" &&
2779 path.isAbsolute(input.resource.path) &&
apps/web/src/components/ChatMarkdown.tsx
+20 -14
@@ -8,7 +8,11 @@ import {
8 } from "@t3tools/shared/markdownLinks";
9 import { isHostPath, resolveWorkspaceFilePath } from "@t3tools/shared/path";
10 import { usePullRequestLinking } from "~/hooks/usePullRequestLinking";
11 -import { AuthFilesystemReadScope, AuthOrchestrationOperateScope } from "@t3tools/contracts";
11 +import {
12 + AuthFilesystemReadScope,
13 + AuthOrchestrationOperateScope,
14 + SOURCE_CONTROL_MEDIA_ASSET,
15 +} from "@t3tools/contracts";
16 import {
17 CHAT_MARKDOWN_REMARK_PLUGINS,
18 CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
@@ -45,7 +49,6 @@ import {
49 type ServerProviderSkill,
50 type ThreadPullRequestKey,
51 } from "@t3tools/contracts";
48 -import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
52 import {
53 isAtomCommandInterrupted,
54 squashAtomCommandFailure,
@@ -234,9 +237,9 @@ interface ChatMarkdownProps {
237 onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
238 extraRemarkPlugins?: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
239 renderContextReference?: ((reference: ChatMarkdownContextReference) => ReactNode) | undefined;
237 - /** Loads GitHub-hosted media through `cwd`'s GitHub credential, which a private repository's
238 - uploads need; without it those images and videos load unauthenticated and 404. */
239 - githubMedia?: boolean | undefined;
240 + /** Loads media a source control host keeps behind a credential through the server, which holds
241 + `cwd`'s; without it a private repository's uploads load unauthenticated and 404. */
242 + sourceControlMedia?: boolean | undefined;
243 headingLevelOffset?: number | undefined;
244 }
245
@@ -448,7 +451,7 @@ function useChatMarkdownState({
451 onImageExpand,
452 renderContextReference,
453 headingLevelOffset = 0,
451 - githubMedia = false,
454 + sourceControlMedia = false,
455 }: ChatMarkdownProps) {
456 const resolvedTheme = useRenderedAppearance();
457 const markdownRef = useRef<HTMLElement>(null);
@@ -885,7 +888,7 @@ function useChatMarkdownState({
888 environmentId,
889 expandMedia,
890 fileLinkChip,
888 - githubMedia,
891 + sourceControlMedia,
892 renderContextReference,
893 headingLevelOffset,
894 imageBaseDir,
@@ -1352,7 +1355,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
1355 expandMedia,
1356 cwd,
1357 environmentId,
1355 - githubMedia,
1358 + sourceControlMedia,
1359 imageBaseDir,
1360 threadRef,
1361 workspaceMutationId,
@@ -1384,19 +1387,22 @@ const CHAT_MARKDOWN_COMPONENTS = {
1387 const imageSource = classifyMarkdownImageSource(classifiedSrc, imageBaseDir ?? cwd);
1388 const kind = mediaKindFromPath(classifiedSrc) ?? "image";
1389 const directUri = imageSource._tag === "Direct" ? imageSource.uri : null;
1387 - const githubMediaUrl =
1388 - directUri === null ? null : githubMediaFetchUrl(resolveProtocolRelativeMediaUrl(directUri));
1390 + const hostMediaUrl =
1391 + directUri === null
1392 + ? null
1393 + : (sourceControlClients.mediaUrl(resolveProtocolRelativeMediaUrl(directUri))?.fetchUrl ??
1394 + null);
1395 if (
1390 - githubMedia &&
1396 + sourceControlMedia &&
1397 cwd !== undefined &&
1398 environmentId !== null &&
1399 directUri !== null &&
1394 - githubMediaUrl !== null
1400 + hostMediaUrl !== null
1401 ) {
1402 return (
1403 <ChatMarkdownAssetImage
1404 environmentId={environmentId}
1399 - resource={{ _tag: "github-media", cwd, url: githubMediaUrl }}
1405 + resource={{ _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd, url: hostMediaUrl }}
1406 alt={altText}
1407 kind={kind}
1408 copyMarkdown={copyMarkdown}
@@ -1407,7 +1413,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
1413 srcFragment={markdownImageSourceFragment(classifiedSrc)}
1414 originalUrl={resolveProtocolRelativeMediaUrl(directUri)}
1415 framed={false}
1410 - fallbackSrc={githubMediaUrl}
1416 + fallbackSrc={hostMediaUrl}
1417 onImageExpand={imageExpand}
1418 />
1419 );
apps/web/src/components/chat/ChatMarkdownMedia.tsx
+2 -2
@@ -1,6 +1,6 @@
1 import { MISSING_FILE_ACCESS_MESSAGE } from "@t3tools/client-runtime/state/assets";
2 import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
3 -import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
3 +import type { AssetResource, EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts";
4 import { TriangleAlertIcon } from "lucide-react";
5 import {
6 createContext,
@@ -312,7 +312,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
312 | "attachment"
313 | "workspace-file"
314 | "media-file"
315 - | "github-media"
315 + | typeof SOURCE_CONTROL_MEDIA_ASSET
316 | "tool-output-image";
317 }
318 >;
apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
+17 -10
@@ -1,7 +1,12 @@
1 import { ExternalLinkIcon, PaperclipIcon } from "lucide-react";
2 import { markdownImageSourceFragment } from "@t3tools/client-runtime/markdown-images";
3 -import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
4 -import type { AssetResource, EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
3 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
4 +import {
5 + SOURCE_CONTROL_MEDIA_ASSET,
6 + type AssetResource,
7 + type EnvironmentId,
8 + type ScopedThreadRef,
9 +} from "@t3tools/contracts";
10 import { createContext, useContext } from "react";
11 import type { Options as ReactMarkdownOptions } from "react-markdown";
12
@@ -18,7 +23,7 @@ export const PullRequestMarkdownContext = createContext<{
23 threadRef: ScopedThreadRef | null;
24 } | null>(null);
25
21 -function PullRequestGitHubVideo({
26 +function PullRequestHostVideo({
27 environmentId,
28 cwd,
29 url,
@@ -29,7 +34,7 @@ function PullRequestGitHubVideo({
34 url: string;
35 fetchUrl: string;
36 }) {
32 - const resource: AssetResource = { _tag: "github-media", cwd, url: fetchUrl };
37 + const resource: AssetResource = { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd, url: fetchUrl };
38 const assetUrl = useAssetUrlState(environmentId, resource);
39 const refreshAssetUrl = useAssetUrlRefresh(environmentId, resource);
40 const src =
@@ -85,19 +90,19 @@ export function PullRequestMarkdown({
90 pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF}
91 environmentId={environmentId}
92 extraRemarkPlugins={extraRemarkPlugins}
88 - githubMedia
93 + sourceControlMedia
94 />
95 );
96 }
92 - const githubMediaUrl = segment.media === "video" ? githubMediaFetchUrl(segment.url) : null;
93 - if (githubMediaUrl !== null) {
97 + const hostMedia = sourceControlClients.mediaUrl(segment.url);
98 + if (segment.media === "video" && hostMedia !== null) {
99 return (
95 - <PullRequestGitHubVideo
100 + <PullRequestHostVideo
101 key={`${segment.id}:${segment.url}`}
102 environmentId={environmentId}
103 cwd={cwd}
104 url={segment.url}
100 - fetchUrl={githubMediaUrl}
105 + fetchUrl={hostMedia.fetchUrl}
106 />
107 );
108 }
@@ -125,7 +130,9 @@ export function PullRequestMarkdown({
130 className="flex items-center gap-2 rounded-lg border border-border/60 bg-muted/30 px-3 py-2 text-sm hover:bg-muted/60"
131 >
132 <PaperclipIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
128 - <span className="min-w-0 flex-1 truncate">Open attachment on GitHub</span>
133 + <span className="min-w-0 flex-1 truncate">
134 + Open attachment on {hostMedia?.definition.label ?? new URL(segment.url).hostname}
135 + </span>
136 <ExternalLinkIcon aria-hidden className="size-3 shrink-0 text-muted-foreground" />
137 </a>
138 );
knip.jsonc
+1 -2
@@ -44,7 +44,6 @@
44 "scripts/cli.ts",
45 "scripts/evaluate-thread-titles.ts",
46 "scripts/fetch-expo-schema.ts",
47 - "scripts/measure-pr-preview.ts",
47 "scripts/probe-claude-fork-local-rollback-replay.ts",
48 "scripts/update-test-shard-weights.ts",
49 "scripts/verify-background-live.ts",
@@ -135,7 +134,7 @@
134 },
135 "packages/source-control-github": {
136 // graphqlgen reads its config; the generated operations export every selection's class.
138 - "entry": ["graphql.config.ts"],
137 + "entry": ["graphql.config.ts", "scripts/measure-pr-preview.ts"],
138 "ignoreIssues": { "**/*.graphql.ts": ["exports", "types", "namespaceMembers"] },
139 },
140 },
packages/client-runtime/src/pullRequestMarkdown.ts
+3 -2
@@ -1,3 +1,5 @@
1 +import { sourceControlClients } from "./sourceControlClients.ts";
2 +
3 /** `id` is positional on purpose: the same attachment can be embedded twice in one body. */
4 export type PullRequestBodySegment =
5 | { readonly id: string; readonly kind: "markdown"; readonly text: string }
@@ -21,7 +23,6 @@ const IMAGE_EXTENSION_PATTERN = /\.(?:png|jpe?g|gif|webp|avif|heic|heif|svg)(?:$
23 const MARKDOWN_LINK_PATTERN = /^\[[^\]]*\]\((https?:\/\/[^\s)]+)\)$/u;
24 const VIDEO_EXTENSION_PATTERN = /\.(?:mp4|webm|mov|m4v|ogv)(?:$|[?#])/iu;
25
24 -const GITHUB_ASSET_PATTERN = /^https:\/\/github\.com\/user-attachments\/assets\/[\w-]+$/iu;
26 const VIDEO_TAG_SRC_PATTERN = /<(?:video|source)\b[^>]*\bsrc\s*=\s*["']([^"']+)["']/iu;
27
28 const STANDALONE_VIDEO_TAG_PATTERN = /^\s*<video\b/iu;
@@ -42,7 +43,7 @@ function attachmentFromLine(
43 const url =
44 BARE_URL_PATTERN.exec(line.trim())?.[1] ?? MARKDOWN_LINK_PATTERN.exec(line.trim())?.[1];
45 if (url === undefined || !isWebUrl(url)) return null;
45 - if (VIDEO_EXTENSION_PATTERN.test(url) || GITHUB_ASSET_PATTERN.test(url)) {
46 + if (VIDEO_EXTENSION_PATTERN.test(url) || sourceControlClients.mediaUrl(url)?.kind === "video") {
47 return { url, media: "video" };
48 }
49 if (IMAGE_EXTENSION_PATTERN.test(url)) return { url, media: "image" };
packages/contracts/src/assets.ts
+13 -6
@@ -11,6 +11,12 @@ import { ToolActivityNativeAppReference } from "./providerRuntime.ts";
11
12 const ASSET_PATH_MAX_LENGTH = 1024;
13
14 +/**
15 + * The wire tag of a source control host's credentialed media. It names GitHub because GitHub had
16 + * it first, and servers and clients of different releases must keep understanding each other.
17 + */
18 +export const SOURCE_CONTROL_MEDIA_ASSET = "github-media" as const;
19 +
20 export const AssetResource = Schema.Union([
21 Schema.TaggedStruct("workspace-file", {
22 threadId: ThreadId,
@@ -47,10 +53,10 @@ export const AssetResource = Schema.Union([
53 Schema.TaggedStruct("native-app-icon", {
54 app: ToolActivityNativeAppReference,
55 }),
50 - // An upload a pull request body points at on GitHub. A private repository serves these only
56 + // Media a change request body points at on its host. A private repository serves these only
57 // to a request that carries a credential, which the client has none of, so the server fetches
52 - // them with the `gh` credential the repository at `cwd` authenticates with.
53 - Schema.TaggedStruct("github-media", {
58 + // them with the credential the host's driver holds for the repository at `cwd`.
59 + Schema.TaggedStruct(SOURCE_CONTROL_MEDIA_ASSET, {
60 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
61 url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)),
62 }),
@@ -292,12 +298,13 @@ export class AssetSigningKeyLoadError extends Schema.TaggedError<AssetSigningKey
298 }
299 }
300
295 -export class AssetGitHubMediaUrlValidationError extends Schema.TaggedError<AssetGitHubMediaUrlValidationError>()(
301 +// The tag predates the rename and stays: clients on either side of an update decode it.
302 +export class AssetSourceControlMediaUrlValidationError extends Schema.TaggedError<AssetSourceControlMediaUrlValidationError>()(
303 "AssetGitHubMediaUrlValidationError",
304 {},
305 ) {
306 override get message(): string {
300 - return "Only media hosted by GitHub can be fetched with a GitHub credential.";
307 + return "Only media a source control host keeps behind a credential can be fetched with one.";
308 }
309 }
310
@@ -314,7 +321,7 @@ export const AssetAccessError = Schema.Union([
321 AssetProjectFaviconResolutionError,
322 AssetProjectFaviconInspectionError,
323 AssetProjectFaviconNotFoundError,
317 - AssetGitHubMediaUrlValidationError,
324 + AssetSourceControlMediaUrlValidationError,
325 AssetSigningKeyLoadError,
326 ]);
327 export type AssetAccessError = typeof AssetAccessError.Type;
packages/shared/package.json
-4
@@ -55,10 +55,6 @@
55 "types": "./src/git.ts",
56 "import": "./src/git.ts"
57 },
58 - "./githubMedia": {
59 - "types": "./src/githubMedia.ts",
60 - "import": "./src/githubMedia.ts"
61 - },
58 "./sourceControl": {
59 "types": "./src/sourceControl.ts",
60 "import": "./src/sourceControl.ts"
packages/source-control-azure-devops/src/server/driver.ts
+17 -6
@@ -1,17 +1,28 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7
8 import { definition } from "../client/definition.ts";
9
10 +import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
11 +import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
12 import * as AzureDevOpsPullRequestProvider from "./AzureDevOpsPullRequestProvider.ts";
13 import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
14
15 +const services = AzureDevOpsPullRequestCli.layer.pipe(Layer.provideMerge(AzureDevOpsCli.layer));
16 +
17 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
18 +export const makeInstance = Effect.all({
19 + sourceControl: AzureDevOpsSourceControlProvider.make,
20 + discovery: Effect.succeed(AzureDevOpsSourceControlProvider.discovery),
21 + pullRequests: AzureDevOpsPullRequestProvider.make,
22 +});
23 +
24 export const driver = defineSourceControlDriver({
25 kind: definition.kind,
26 definition,
12 - make: Effect.all({
13 - sourceControl: AzureDevOpsSourceControlProvider.make,
14 - discovery: Effect.succeed(AzureDevOpsSourceControlProvider.discovery),
15 - pullRequests: AzureDevOpsPullRequestProvider.make,
16 - }),
27 + make: withOwnServices(makeInstance, services),
28 });
packages/source-control-bitbucket/src/server/BitbucketApi.ts
+4
@@ -77,6 +77,10 @@ export class BitbucketRepositoryLocatorError extends Schema.TaggedError<Bitbucke
77 return BitbucketRepositoryLocatorError.detail;
78 }
79
80 + get userFacingDetail(): string {
81 + return BitbucketRepositoryLocatorError.detail;
82 + }
83 +
84 override get message(): string {
85 return `Bitbucket API failed: ${this.detail}`;
86 }
packages/source-control-bitbucket/src/server/BitbucketSourceControlProvider.ts
+9 -2
@@ -1,6 +1,7 @@
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import * as Effect from "effect/Effect";
3 import * as Option from "effect/Option";
4 +import * as Schema from "effect/Schema";
5 import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
6
7 import * as BitbucketApi from "./BitbucketApi.ts";
@@ -8,6 +9,12 @@ import type { NormalizedBitbucketPullRequestRecord } from "./bitbucketPullReques
9 import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
10 import type { SourceControlApiDiscoverySpec } from "@t3tools/source-control-core/server/discovery";
11
12 +const isRepositoryLocatorError = Schema.is(BitbucketApi.BitbucketRepositoryLocatorError);
13 +
14 +/** A repository named some other way than `workspace/repository` is the user's to fix. */
15 +const repositoryDetail = (error: BitbucketApi.BitbucketApiError, fallback: string) =>
16 + isRepositoryLocatorError(error) ? error.detail : fallback;
17 +
18 function toChangeRequest(summary: NormalizedBitbucketPullRequestRecord): ChangeRequest {
19 return {
20 provider: SourceControlProviderKind.make("bitbucket"),
@@ -122,7 +129,7 @@ export const make = Effect.gen(function* () {
129 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
130 input.repository,
131 ),
125 - detail: "Failed to get repository clone URLs.",
132 + detail: repositoryDetail(error, "Failed to get repository clone URLs."),
133 cause: error,
134 }),
135 ),
@@ -138,7 +145,7 @@ export const make = Effect.gen(function* () {
145 repository: SourceControlProvider.transportSafeSourceControlErrorValue(
146 input.repository,
147 ),
141 - detail: "Failed to create repository.",
148 + detail: repositoryDetail(error, "Failed to create repository."),
149 cause: error,
150 }),
151 ),
packages/source-control-bitbucket/src/server/driver.ts
+21 -6
@@ -1,17 +1,32 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7
8 import { definition } from "../client/definition.ts";
9
10 +import * as BitbucketApi from "./BitbucketApi.ts";
11 +import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
12 import * as BitbucketPullRequestProvider from "./BitbucketPullRequestProvider.ts";
13 import * as BitbucketSourceControlProvider from "./BitbucketSourceControlProvider.ts";
14
15 +// Every setting the API reads from the environment is optional, so reading them cannot fail.
16 +const services = BitbucketPullRequestApi.layer.pipe(
17 + Layer.provideMerge(BitbucketApi.layer),
18 + Layer.orDie,
19 +);
20 +
21 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
22 +export const makeInstance = Effect.all({
23 + sourceControl: BitbucketSourceControlProvider.make,
24 + discovery: BitbucketSourceControlProvider.makeDiscovery,
25 + pullRequests: BitbucketPullRequestProvider.make,
26 +});
27 +
28 export const driver = defineSourceControlDriver({
29 kind: definition.kind,
30 definition,
12 - make: Effect.all({
13 - sourceControl: BitbucketSourceControlProvider.make,
14 - discovery: BitbucketSourceControlProvider.makeDiscovery,
15 - pullRequests: BitbucketPullRequestProvider.make,
16 - }),
31 + make: withOwnServices(makeInstance, services),
32 });
packages/source-control-core/src/client/definition.ts
+30
@@ -74,6 +74,16 @@ export interface ChangeRequestCheckoutInput {
74 readonly repositoryUrl?: string | null | undefined;
75 }
76
77 +/** Media a host serves only to a request carrying a credential, as `mediaUrl` reads it. */
78 +export interface SourceControlMediaUrl {
79 + /** The canonical URL to fetch, the same for every spelling of one file. */
80 + readonly fetchUrl: string;
81 + /** A file name for the media, safe to put in a path or a download. */
82 + readonly fileName: string;
83 + /** Set when the URL alone says what the media is, as an extensionless upload can. */
84 + readonly kind?: "image" | "video";
85 +}
86 +
87 export interface SourceControlClientDefinition {
88 readonly kind: SourceControlProviderKind;
89 readonly label: string;
@@ -141,6 +151,11 @@ export interface SourceControlClientDefinition {
151 * the provider rather than from the remote.
152 */
153 readonly sshRemoteHidesWebHost?: boolean;
154 + /**
155 + * Null when `url` is not media this host keeps behind a credential. A client cannot load such
156 + * media itself, so it asks the server, whose driver for this host fetches it.
157 + */
158 + readonly mediaUrl?: (url: URL) => SourceControlMediaUrl | null;
159 readonly settings?: SourceControlHostSettingsSchema;
160 }
161
@@ -342,6 +357,10 @@ export interface ParsedChangeRequest {
357 readonly link: ParsedChangeRequestUrl;
358 }
359
360 +export interface SourceControlMedia extends SourceControlMediaUrl {
361 + readonly definition: SourceControlClientDefinition;
362 +}
363 +
364 export interface SourceControlClientRegistry {
365 readonly definitions: ReadonlyArray<SourceControlClientDefinition>;
366 readonly get: (kind: string | null | undefined) => SourceControlClientDefinition;
@@ -370,6 +389,8 @@ export interface SourceControlClientRegistry {
389 identity: ChangeRequestHostIdentity,
390 kind: Kind,
391 ) => string | Kind;
392 + /** The first host, in registry order, that keeps `url` behind a credential. */
393 + readonly mediaUrl: (url: string) => SourceControlMedia | null;
394 }
395
396 function httpUrl(value: string): URL | null {
@@ -494,6 +515,15 @@ export function makeSourceControlClientRegistry(
515 changeRequestHost: (identity, kind) =>
516 (kind === undefined ? undefined : byKind.get(kind)?.changeRequestHost?.(identity)) ??
517 projectChangeRequestHost(identity, kind),
518 + mediaUrl: (value) => {
519 + const url = httpUrl(value);
520 + if (url === null) return null;
521 + for (const definition of definitions) {
522 + const media = definition.mediaUrl?.(url);
523 + if (media != null) return { ...media, definition };
524 + }
525 + return null;
526 + },
527 };
528 }
529
packages/source-control-core/src/server/SourceControlProvider.ts
+13
@@ -38,6 +38,19 @@ export interface SourceControlRefSelector {
38
39 const MAX_ERROR_TRANSPORT_VALUE_LENGTH = 256;
40
41 +/**
42 + * The fixed product text a provider failure carries for the user, if it has one. Any other
43 + * detail may be upstream response text, so callers show a generic message instead.
44 + */
45 +export function userFacingSourceControlDetail(cause: unknown): string | undefined {
46 + return typeof cause === "object" &&
47 + cause !== null &&
48 + "userFacingDetail" in cause &&
49 + typeof cause.userFacingDetail === "string"
50 + ? cause.userFacingDetail
51 + : undefined;
52 +}
53 +
54 export function transportSafeSourceControlErrorValue(value: string): string {
55 let printable = "";
56 for (const character of value) {
packages/source-control-core/src/server/driver.ts
+57 -6
@@ -1,25 +1,76 @@
1 import type { SourceControlProviderKind } from "@t3tools/contracts";
2 -import type * as Effect from "effect/Effect";
2 +import type * as Crypto from "effect/Crypto";
3 +import * as Effect from "effect/Effect";
4 +import type * as FileSystem from "effect/FileSystem";
5 +import type { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
6 +import * as Layer from "effect/Layer";
7 +import type * as Scope from "effect/Scope";
8
9 import type { SourceControlClientDefinition } from "../client/definition.ts";
10
11 import type { SourceControlProviderDiscoverySpec } from "./discovery.ts";
12 import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
13 +import type * as SourceControlHost from "./SourceControlHost.ts";
14 import type * as SourceControlProvider from "./SourceControlProvider.ts";
15 +import type * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
16 +
17 +export 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>>;
22 +}
23
24 export 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 + >;
40 }
41
17 -export interface SourceControlDriver<R = never> {
42 +/** What the server gives every driver. A driver builds anything else it needs itself. */
43 +export type SourceControlDriverServices =
44 + | SourceControlHost.SourceControlHost
45 + | SourceControlRateLimit.SourceControlRateLimit
46 + | HttpClient.HttpClient
47 + | FileSystem.FileSystem
48 + | Crypto.Crypto;
49 +
50 +export interface SourceControlDriver {
51 readonly kind: SourceControlProviderKind;
52 readonly definition: SourceControlClientDefinition;
20 - readonly make: Effect.Effect<SourceControlDriverInstance, never, R>;
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 + >;
63 }
64
23 -export const defineSourceControlDriver = <R>(
24 - driver: SourceControlDriver<R>,
25 -): SourceControlDriver<R> => driver;
65 +export 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 + */
72 +export 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))));
packages/source-control-forgejo/src/server/driver.ts
+15 -6
@@ -1,17 +1,26 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6
7 import { definition } from "../client/definition.ts";
8
9 +import * as ForgejoCli from "./ForgejoCli.ts";
10 import * as ForgejoPullRequestProvider from "./ForgejoPullRequestProvider.ts";
11 import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts";
12
13 +const services = ForgejoCli.layer;
14 +
15 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
16 +export const makeInstance = Effect.all({
17 + sourceControl: ForgejoSourceControlProvider.make,
18 + discovery: ForgejoSourceControlProvider.makeDiscovery,
19 + pullRequests: ForgejoPullRequestProvider.make,
20 +});
21 +
22 export const driver = defineSourceControlDriver({
23 kind: definition.kind,
24 definition,
12 - make: Effect.all({
13 - sourceControl: ForgejoSourceControlProvider.make,
14 - discovery: ForgejoSourceControlProvider.makeDiscovery,
15 - pullRequests: ForgejoPullRequestProvider.make,
16 - }),
25 + make: withOwnServices(makeInstance, services),
26 });
packages/source-control-gitcafe/src/server/driver.ts
+17 -6
@@ -1,17 +1,28 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7
8 import { definition } from "../client/definition.ts";
9
10 +import * as GitCafeApi from "./GitCafeApi.ts";
11 +import * as GitCafeCredentials from "./GitCafeCredentials.ts";
12 import * as GitCafePullRequestProvider from "./GitCafePullRequestProvider.ts";
13 import * as GitCafeSourceControlProvider from "./GitCafeSourceControlProvider.ts";
14
15 +const services = GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer));
16 +
17 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
18 +export const makeInstance = Effect.all({
19 + sourceControl: GitCafeSourceControlProvider.make,
20 + discovery: GitCafeSourceControlProvider.makeDiscovery,
21 + pullRequests: GitCafePullRequestProvider.make,
22 +});
23 +
24 export const driver = defineSourceControlDriver({
25 kind: definition.kind,
26 definition,
12 - make: Effect.all({
13 - sourceControl: GitCafeSourceControlProvider.make,
14 - discovery: GitCafeSourceControlProvider.makeDiscovery,
15 - pullRequests: GitCafePullRequestProvider.make,
16 - }),
27 + make: withOwnServices(makeInstance, services),
28 });
packages/source-control-github/scripts/measure-pr-preview.ts renamed
+44 -21
@@ -1,30 +1,29 @@
1 -// Run with: node apps/server/scripts/measure-pr-preview.ts owner/repo 123 456
1 +// Run with: node packages/source-control-github/scripts/measure-pr-preview.ts owner/repo 123 456
2 // Numbers form a session with shared repository-permission caches. Browser and
3 // service caches are excluded. Uses real GitHub reads, without a server or database.
4 // Requests are counted at the HTTP client. A GraphQL request's cost is how far it moved GitHub's
5 // `x-ratelimit-used` for the `graphql` resource, which is approximate when anything else spends
6 // the same token's quota while this runs.
7 import * as NodeServices from "@effect/platform-node/NodeServices";
8 +import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
9 +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
10 import * as Effect from "effect/Effect";
11 import * as Console from "effect/Console";
12 import * as Layer from "effect/Layer";
13 import * as Schema from "effect/Schema";
14 +import * as Stream from "effect/Stream";
15 import { FetchHttpClient, HttpClient } from "effect/http";
16 +import { ChildProcess, ChildProcessSpawner } from "effect/process";
17
14 -import * as GitHubPullRequestApi from "@t3tools/source-control-github/server/GitHubPullRequestApi";
15 -import * as GitHubPullRequestProvider from "@t3tools/source-control-github/server/GitHubPullRequestProvider";
16 -import * as GitHubRest from "@t3tools/source-control-github/server/GitHubRest";
17 -import * as ServerConfig from "../src/config.ts";
18 -import * as ServerSettings from "../src/serverSettings.ts";
19 -import * as ServerSourceControlHost from "../src/sourceControl/ServerSourceControlHost.ts";
20 -import * as GitVcsDriver from "../src/vcs/GitVcsDriver.ts";
21 -import * as VcsDriverRegistry from "../src/vcs/VcsDriverRegistry.ts";
22 -import * as VcsProcess from "../src/vcs/VcsProcess.ts";
23 -import * as VcsProjectConfig from "../src/vcs/VcsProjectConfig.ts";
18 +import * as GitHubPullRequestApi from "../src/server/GitHubPullRequestApi.ts";
19 +import * as GitHubPullRequestProvider from "../src/server/GitHubPullRequestProvider.ts";
20 +import * as GitHubRest from "../src/server/GitHubRest.ts";
21
22 const [repository, ...numbers] = process.argv.slice(2);
23 if (!repository || numbers.length === 0 || numbers.some((number) => !/^\d+$/.test(number))) {
27 - throw new Error("Usage: node apps/server/scripts/measure-pr-preview.ts owner/repo number...");
24 + throw new Error(
25 + "Usage: node packages/source-control-github/scripts/measure-pr-preview.ts owner/repo number...",
26 + );
27 }
28
29 type Read = { graphqlRequests: number; restRequests: number; cost: number };
@@ -73,18 +72,42 @@ const measuredHttp = Layer.effect(
72 }),
73 ).pipe(Layer.provide(FetchHttpClient.layer));
74
76 -// The server's host port over default settings: gh's own account choice, no saved token.
77 -const sourceControlHost = ServerSourceControlHost.layer.pipe(
78 - Layer.provide(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProjectConfig.layer))),
79 - Layer.provide(GitVcsDriver.layer),
80 - Layer.provide(ServerSettings.layerTest()),
81 - Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-measure-pr-preview-" })),
82 - Layer.provideMerge(VcsProcess.layer),
83 - Layer.provideMerge(NodeServices.layer),
84 -);
75 +// Default settings, so `gh` picks the account and no saved token applies. Only the credential
76 +// lookup runs a process, and it needs no more than its output.
77 +const sourceControlHost = Layer.unwrap(
78 + Effect.gen(function* () {
79 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
80 + return TestSourceControlHost.layer({
81 + process: {
82 + run: (input) =>
83 + Effect.scoped(
84 + Effect.gen(function* () {
85 + const handle = yield* spawner.spawn(
86 + ChildProcess.make(input.command, [...input.args], {
87 + cwd: input.spawnCwd ?? input.cwd,
88 + env: input.env,
89 + extendEnv: true,
90 + }),
91 + );
92 + const [stdout, stderr, exitCode] = yield* Effect.all(
93 + [
94 + Stream.mkString(Stream.decodeText(handle.stdout)),
95 + Stream.mkString(Stream.decodeText(handle.stderr)),
96 + handle.exitCode,
97 + ],
98 + { concurrency: "unbounded" },
99 + );
100 + return { exitCode, stdout, stderr, stdoutTruncated: false, stderrTruncated: false };
101 + }),
102 + ).pipe(Effect.orDie),
103 + },
104 + });
105 + }),
106 +).pipe(Layer.provideMerge(NodeServices.layer));
107
108 const services = GitHubPullRequestApi.layer.pipe(
109 Layer.provideMerge(GitHubRest.layerWithDependencies),
110 + Layer.provideMerge(SourceControlRateLimit.layer),
111 Layer.provideMerge(measuredHttp),
112 Layer.provideMerge(sourceControlHost),
113 );
packages/source-control-github/src/client/definition.test.ts
+38
@@ -121,3 +121,41 @@ describe("GitHub remotes", () => {
121 expect(recognize("https://notgithub.example.com/owner/repo.git")).toBeNull();
122 });
123 });
124 +
125 +describe("GitHub media URLs", () => {
126 + const media = (url: string) => definition.mediaUrl(new URL(url));
127 +
128 + it("fetches every spelling of an upload or committed file from one canonical URL", () => {
129 + expect(
130 + media("https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c"),
131 + ).toEqual({
132 + fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
133 + fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c",
134 + kind: "video",
135 + });
136 + expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({
137 + fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
138 + fileName: "shot.png",
139 + });
140 + expect(media("https://github.com/owner/repo/assets/45952064/1a1842fb")?.fetchUrl).toBe(
141 + "https://github.com/owner/repo/assets/45952064/1a1842fb",
142 + );
143 + expect(media("https://media.githubusercontent.com/media/owner/repo/main/a.mp4")).toEqual({
144 + fetchUrl: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
145 + fileName: "a.mp4",
146 + });
147 + expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png");
148 + });
149 +
150 + it("claims nothing a request without a credential loads just as well", () => {
151 + for (const url of [
152 + "https://example.com/shot.png",
153 + "http://github.com/user-attachments/assets/1a1842fb",
154 + "https://github.com/owner/repo/pull/1",
155 + "https://github.com/owner/repo/blob/main/",
156 + "https://objects.githubusercontent.com/github-production-user-asset/shot.png",
157 + ]) {
158 + expect(media(url), url).toBeNull();
159 + }
160 + });
161 +});
packages/source-control-github/src/client/definition.ts
+3
@@ -18,6 +18,8 @@ import {
18 remoteWebOrigin,
19 } from "@t3tools/source-control-core/client/definition";
20
21 +import { gitHubMediaUrl } from "./gitHubMedia.ts";
22 +
23 const CHECKOUT_COMMAND = /^gh\s+pr\s+checkout\s+(.+)$/i;
24 const CHANGE_REQUEST_REFERENCE =
25 /^https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)(?:[/?#].*)?$/i;
@@ -134,5 +136,6 @@ export const definition = defineSourceControlClient({
136 isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
137 canReadChangeRequestOnHost: (identity, link) =>
138 isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
139 + mediaUrl: gitHubMediaUrl,
140 settings,
141 });
packages/source-control-github/src/client/gitHubMedia.ts renamed
+34 -29
@@ -1,44 +1,25 @@
1 +import type { SourceControlMediaUrl } from "@t3tools/source-control-core/client/definition";
2 +
3 /**
4 * Only hosts where a credential is what decides the answer belong here. `objects.githubusercontent.com`
5 * and the `private-user-images` links GitHub's own HTML carries are already signed and load on their
6 * own, and a token does nothing for them once that signature expires.
7 */
6 -
8 +const WEB_HOSTS = new Set(["github.com", "www.github.com"]);
9 const RAW_HOST = "raw.githubusercontent.com";
10 const LFS_HOST = "media.githubusercontent.com";
11 +export const GITHUB_CREDENTIALED_MEDIA_HOSTS: ReadonlySet<string> = new Set([
12 + ...WEB_HOSTS,
13 + RAW_HOST,
14 + LFS_HOST,
15 +]);
16 +
17 const ATTACHMENT_PATH_PATTERN = /^\/user-attachments\/assets\/[\w-]+$/u;
18 const LEGACY_ATTACHMENT_PATH_PATTERN = /^\/[^/]+\/[^/]+\/assets\/\d+\/[\w-]+$/u;
19 /** `blob` and `raw` both address file bytes; `raw` is the one the token is honoured on. */
20 const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.*[^/])$/u;
21
14 -function canonicalUrl(host: string, url: URL): string {
15 - return `https://${host}${url.pathname}${url.search}`;
16 -}
17 -
18 -export function githubMediaFetchUrl(source: string): string | null {
19 - let url: URL;
20 - try {
21 - url = new URL(source);
22 - } catch {
23 - return null;
24 - }
25 - if (url.protocol !== "https:") return null;
26 - const host = url.hostname.toLowerCase();
27 - if (host === RAW_HOST || host === LFS_HOST) return canonicalUrl(host, url);
28 - if (host !== "github.com" && host !== "www.github.com") return null;
29 - if (
30 - ATTACHMENT_PATH_PATTERN.test(url.pathname) ||
31 - LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)
32 - ) {
33 - return `https://github.com${url.pathname}`;
34 - }
35 - const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname);
36 - return repositoryFile
37 - ? `https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`
38 - : null;
39 -}
40 -
41 -export function githubMediaFileName(fetchUrl: string): string {
22 +function fileName(fetchUrl: string): string {
23 const segment = new URL(fetchUrl).pathname.split("/").pop() ?? "";
24 let decoded: string;
25 try {
@@ -49,3 +30,27 @@ export function githubMediaFileName(fetchUrl: string): string {
30 const name = decoded.replace(/[\p{Cc}\\/]/gu, "");
31 return name.length > 0 ? name : "github-media";
32 }
33 +
34 +function media(fetchUrl: string, kind?: "video"): SourceControlMediaUrl {
35 + return { fetchUrl, fileName: fileName(fetchUrl), ...(kind ? { kind } : {}) };
36 +}
37 +
38 +export function gitHubMediaUrl(url: URL): SourceControlMediaUrl | null {
39 + if (url.protocol !== "https:") return null;
40 + const host = url.hostname.toLowerCase();
41 + if (host === RAW_HOST || host === LFS_HOST) {
42 + return media(`https://${host}${url.pathname}${url.search}`);
43 + }
44 + if (!WEB_HOSTS.has(host)) return null;
45 + // GitHub links an uploaded video bare, with no extension, and an image as markdown.
46 + if (ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
47 + return media(`https://github.com${url.pathname}`, "video");
48 + }
49 + if (LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
50 + return media(`https://github.com${url.pathname}`);
51 + }
52 + const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname);
53 + return repositoryFile
54 + ? media(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`)
55 + : null;
56 +}
packages/source-control-github/src/server/GitHubRest.ts
+3 -4
@@ -232,12 +232,11 @@ export const layer = Layer.effect(GitHubRest, make).pipe(
232 );
233
234 /**
235 - * REST and the request policy, with the credential, quota reserve and rate-limit pause the policy
236 - * reads through: everything both transports need. An application builds this once so every GitHub
237 - * reader shares one view of each quota per host.
235 + * REST and the request policy, with the credential and quota reserve the policy reads through.
236 + * Built once, so every GitHub reader shares one view of each quota per host. The rate-limit pause
237 + * comes from the server, which every host's change request reads share.
238 */
239 export const layerWithDependencies = layer.pipe(
240 Layer.provideMerge(GitHubCredentials.layer),
241 Layer.provideMerge(GitHubQuota.layer),
242 - Layer.provideMerge(SourceControlRateLimit.layer),
242 );
packages/source-control-github/src/server/driver.ts
+23 -6
@@ -1,17 +1,34 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7
8 import { definition } from "../client/definition.ts";
9
10 +import * as GitHubMediaFetch from "./gitHubMediaFetch.ts";
11 +import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
12 import * as GitHubPullRequestProvider from "./GitHubPullRequestProvider.ts";
13 +import * as GitHubRest from "./GitHubRest.ts";
14 import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
15
16 +// `GitHubRest.layerWithDependencies` carries the quota reserve every GitHub reader shares, so it
17 +// must be built once; the driver is.
18 +const services = GitHubPullRequestApi.layer.pipe(
19 + Layer.provideMerge(GitHubRest.layerWithDependencies),
20 +);
21 +
22 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
23 +export const makeInstance = Effect.all({
24 + sourceControl: GitHubSourceControlProvider.make,
25 + discovery: GitHubSourceControlProvider.makeDiscovery,
26 + pullRequests: GitHubPullRequestProvider.make,
27 + fetchMedia: GitHubMediaFetch.make,
28 +});
29 +
30 export const driver = defineSourceControlDriver({
31 kind: definition.kind,
32 definition,
12 - make: Effect.all({
13 - sourceControl: GitHubSourceControlProvider.make,
14 - discovery: GitHubSourceControlProvider.makeDiscovery,
15 - pullRequests: GitHubPullRequestProvider.make,
16 - }),
33 + make: withOwnServices(makeInstance, services),
34 });
packages/source-control-github/src/server/gitHubMediaFetch.test.ts new
+104
@@ -0,0 +1,104 @@
1 +import { describe, expect, it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 +import * as Redacted from "effect/Redacted";
5 +import { HttpClient, HttpClientResponse } from "effect/http";
6 +
7 +import * as GitHubCredentials from "./GitHubCredentials.ts";
8 +import * as GitHubMediaFetch from "./gitHubMediaFetch.ts";
9 +
10 +describe("GitHubMediaFetch", () => {
11 + it.effect("loads private media immediately after login with the GitHub credential", () => {
12 + let lookups = 0;
13 + const authorizations: Array<string | undefined> = [];
14 + return Effect.gen(function* () {
15 + const fetchMedia = yield* GitHubMediaFetch.make;
16 + const request = {
17 + url: "https://raw.githubusercontent.com/owner/repo/main/shot.png",
18 + headers: {},
19 + };
20 + expect((yield* fetchMedia(request))?.status).toBe(404);
21 + expect((yield* fetchMedia(request))?.status).toBe(200);
22 + expect((yield* fetchMedia(request))?.status).toBe(200);
23 + expect(lookups).toBe(3);
24 + expect(authorizations).toEqual([undefined, "Bearer signed-in", "Bearer signed-in"]);
25 + }).pipe(
26 + Effect.provide(
27 + Layer.mock(GitHubCredentials.GitHubCredentials)({
28 + get: (host) =>
29 + ++lookups === 1
30 + ? Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }))
31 + : Effect.succeed({
32 + host,
33 + token: Redacted.make("signed-in"),
34 + source: "gh" as const,
35 + fingerprint: "fingerprint",
36 + }),
37 + }),
38 + ),
39 + Effect.provideService(
40 + HttpClient.HttpClient,
41 + HttpClient.make((request) => {
42 + authorizations.push(request.headers.authorization);
43 + return Effect.succeed(
44 + HttpClientResponse.fromWeb(
45 + request,
46 + new Response(null, {
47 + status: request.headers.authorization ? 200 : 404,
48 + headers: { "content-type": "image/png" },
49 + }),
50 + ),
51 + );
52 + }),
53 + ),
54 + Effect.scoped,
55 + );
56 + });
57 +
58 + it.effect("keeps the credential off the object store a redirect hands the media to", () => {
59 + const authorizations: Array<[string, string | undefined]> = [];
60 + return Effect.gen(function* () {
61 + const fetchMedia = yield* GitHubMediaFetch.make;
62 + const response = yield* fetchMedia({
63 + url: "https://github.com/user-attachments/assets/1a1842fb",
64 + headers: { range: "bytes=0-1" },
65 + });
66 + expect(response?.status).toBe(200);
67 + expect(authorizations).toEqual([
68 + ["github.com", "Bearer signed-in"],
69 + ["objects.example.com", undefined],
70 + ]);
71 + }).pipe(
72 + Effect.provide(
73 + Layer.mock(GitHubCredentials.GitHubCredentials)({
74 + get: (host) =>
75 + Effect.succeed({
76 + host,
77 + token: Redacted.make("signed-in"),
78 + source: "gh" as const,
79 + fingerprint: "fingerprint",
80 + }),
81 + }),
82 + ),
83 + Effect.provideService(
84 + HttpClient.HttpClient,
85 + HttpClient.make((request) => {
86 + const url = new URL(request.url);
87 + authorizations.push([url.hostname, request.headers.authorization]);
88 + return Effect.succeed(
89 + HttpClientResponse.fromWeb(
90 + request,
91 + url.hostname === "github.com"
92 + ? new Response(null, {
93 + status: 302,
94 + headers: { location: "https://objects.example.com/signed" },
95 + })
96 + : new Response(null, { status: 200 }),
97 + ),
98 + );
99 + }),
100 + ),
101 + Effect.scoped,
102 + );
103 + });
104 +});
packages/source-control-github/src/server/gitHubMediaFetch.ts new
+68
@@ -0,0 +1,68 @@
1 +import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver";
2 +import * as Effect from "effect/Effect";
3 +import * as Redacted from "effect/Redacted";
4 +import {
5 + FetchHttpClient,
6 + HttpClient,
7 + HttpClientRequest,
8 + type HttpClientResponse,
9 +} from "effect/http";
10 +
11 +import { GITHUB_CREDENTIALED_MEDIA_HOSTS } from "../client/gitHubMedia.ts";
12 +
13 +import * as GitHubCredentials from "./GitHubCredentials.ts";
14 +
15 +/**
16 + * Everything a redirect leads to — the presigned object stores GitHub hands assets off to —
17 + * authorizes with its own signature, and some of them reject a request that also carries a
18 + * bearer token.
19 + */
20 +const isCredentialedHost = (url: string) => {
21 + try {
22 + return GITHUB_CREDENTIALED_MEDIA_HOSTS.has(new URL(url).hostname.toLowerCase());
23 + } catch {
24 + return false;
25 + }
26 +};
27 +
28 +const MAX_REDIRECTS = 3;
29 +const MANUAL_REDIRECT: RequestInit = { redirect: "manual" };
30 +
31 +export const make = Effect.gen(function* () {
32 + const credentials = yield* GitHubCredentials.GitHubCredentials;
33 + const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
34 +
35 + // Both media hosts are served by github.com's account, which is the host `gh` stores it under.
36 + // Read on every fetch, so media loads as soon as the user signs in.
37 + const token = Effect.suspend(() => credentials.get("github.com")).pipe(
38 + Effect.map((credential) => credential.token),
39 + Effect.orElseSucceed(() => null),
40 + );
41 +
42 + return Effect.fn("GitHubMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) {
43 + const bearer = yield* token;
44 + let target = request.url;
45 + for (let hop = 0; ; hop += 1) {
46 + const authorization =
47 + bearer !== null && isCredentialedHost(target) ? `Bearer ${Redacted.value(bearer)}` : null;
48 + const response: HttpClientResponse.HttpClientResponse = yield* httpClient
49 + .execute(
50 + HttpClientRequest.get(target).pipe(
51 + HttpClientRequest.setHeaders({
52 + ...request.headers,
53 + // The bytes are streamed straight through, so never let an encoding layer in.
54 + "accept-encoding": "identity",
55 + ...(authorization === null ? {} : { authorization }),
56 + }),
57 + ),
58 + )
59 + .pipe(Effect.provideService(FetchHttpClient.RequestInit, MANUAL_REDIRECT));
60 + const location = response.headers.location;
61 + if (response.status < 300 || response.status >= 400) return response;
62 + if (!location || hop >= MAX_REDIRECTS) return null;
63 + const next = new URL(location, target);
64 + if (next.protocol !== "https:") return null;
65 + target = next.toString();
66 + }
67 + });
68 +});
packages/source-control-github/tsconfig.json
+1 -1
@@ -3,5 +3,5 @@
3 "compilerOptions": {
4 "types": ["node"]
5 },
6 - "include": ["src"]
6 + "include": ["src", "scripts"]
7 }
packages/source-control-gitlab/src/server/driver.ts
+17 -6
@@ -1,17 +1,28 @@
1 -import { defineSourceControlDriver } from "@t3tools/source-control-core/server/driver";
1 +import {
2 + defineSourceControlDriver,
3 + withOwnServices,
4 +} from "@t3tools/source-control-core/server/driver";
5 import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7
8 import { definition } from "../client/definition.ts";
9
10 +import * as GitLabCli from "./GitLabCli.ts";
11 +import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
12 import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
13 import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
14
15 +const services = GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer));
16 +
17 +/** Over this host's own services, which `driver` builds; tests stand in for them. */
18 +export const makeInstance = Effect.all({
19 + sourceControl: GitLabSourceControlProvider.make,
20 + discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
21 + pullRequests: GitLabPullRequestProvider.make,
22 +});
23 +
24 export const driver = defineSourceControlDriver({
25 kind: definition.kind,
26 definition,
12 - make: Effect.all({
13 - sourceControl: GitLabSourceControlProvider.make,
14 - discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
15 - pullRequests: GitLabPullRequestProvider.make,
16 - }),
27 + make: withOwnServices(makeInstance, services),
28 });