|
| 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