Skip to content

Commit d456cd7

Browse files
fix(server): respect gh Unix sockets for GitHub requests (#27733)
Co-authored-by: maria-rcks <[email protected]>
1 parent e6d4256 commit d456cd7

5 files changed

Lines changed: 421 additions & 14 deletions

File tree

‎packages/source-control-github/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"generate": "graphqlgen"
1919
},
2020
"dependencies": {
21+
"@effect/platform-node": "catalog:",
2122
"@t3tools/contracts": "workspace:*",
2223
"@t3tools/provider-core": "workspace:*",
2324
"@t3tools/shared": "workspace:*",
@@ -26,7 +27,6 @@
2627
},
2728
"devDependencies": {
2829
"@effect/graphql-generator": "catalog:",
29-
"@effect/platform-node": "catalog:",
3030
"@effect/vitest": "catalog:",
3131
"@t3tools/source-control-testing": "workspace:*",
3232
"@types/node": "catalog:",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
2+
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
3+
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as Stream from "effect/Stream";
7+
import { HttpBody, HttpClient, HttpClientRequest } from "effect/http";
8+
9+
import { GH_PROCESS_CONCURRENCY } from "./GitHubCredentials.ts";
10+
11+
export const layer = Layer.effect(
12+
HttpClient.HttpClient,
13+
Effect.gen(function* () {
14+
const fallback = yield* HttpClient.HttpClient;
15+
const host = yield* SourceControlHost.SourceControlHost;
16+
const cwd = yield* HostProcess.WorkingDirectory;
17+
const socketPath = yield* host.process
18+
.run({
19+
operation: "GitHubHttpClient.config",
20+
command: "gh",
21+
args: ["config", "get", "http_unix_socket"],
22+
cwd,
23+
env: { GH_DEBUG: "", GH_PROMPT_DISABLED: "1" },
24+
timeoutMs: 10_000,
25+
concurrency: GH_PROCESS_CONCURRENCY,
26+
})
27+
.pipe(
28+
Effect.map((output) => output.stdout.trim()),
29+
Effect.orElseSucceed(() => ""),
30+
);
31+
if (socketPath === "") return fallback;
32+
33+
const agent = yield* NodeHttpClient.makeAgent({ socketPath, keepAlive: false });
34+
const client = yield* NodeHttpClient.makeNodeHttp.pipe(
35+
Effect.provideService(NodeHttpClient.HttpAgent, agent),
36+
);
37+
return HttpClient.make((initialRequest, initialUrl) =>
38+
Effect.gen(function* () {
39+
let request = initialRequest;
40+
let originalUrl = initialUrl;
41+
for (let redirects = 0; ; redirects++) {
42+
const url = new URL(originalUrl);
43+
// gh sends plain HTTP over the socket while preserving the API authority.
44+
url.protocol = "http:";
45+
const response = yield* client.execute(
46+
request.pipe(
47+
HttpClientRequest.setUrl(url),
48+
HttpClientRequest.setHeader("host", originalUrl.host),
49+
),
50+
);
51+
const location = response.headers.location;
52+
if (
53+
![301, 302, 303, 307, 308].includes(response.status) ||
54+
!location ||
55+
redirects >= 10
56+
) {
57+
return response;
58+
}
59+
const destination = new URL(location, originalUrl);
60+
if (destination.origin !== originalUrl.origin) {
61+
if (request.body._tag !== "Empty") return response;
62+
request = request.pipe(
63+
HttpClientRequest.removeHeader("authorization"),
64+
HttpClientRequest.removeHeader("proxy-authorization"),
65+
HttpClientRequest.removeHeader("cookie"),
66+
);
67+
}
68+
if (
69+
((response.status === 301 || response.status === 302) && request.method === "POST") ||
70+
(response.status === 303 && request.method !== "GET" && request.method !== "HEAD")
71+
) {
72+
request = request.pipe(
73+
HttpClientRequest.setMethod("GET"),
74+
HttpClientRequest.setBody(HttpBody.empty),
75+
);
76+
}
77+
yield* Effect.scoped(Effect.asVoid(Stream.toPull(response.stream)));
78+
request = HttpClientRequest.setUrl(request, destination);
79+
originalUrl = destination;
80+
}
81+
}),
82+
);
83+
}),
84+
);

0 commit comments

Comments
 (0)