@@ -25,7 +25,10 @@ import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
2525import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
2626import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
2727import type { SourceControlDriver } from "@t3tools/source-control-core/server/driver";
28+ import * as Undici from "@effect/platform-node/Undici";
29+ import * as Effect from "effect/Effect";
2830import * as Layer from "effect/Layer";
31+ import { FetchHttpClient } from "effect/http";
2932
3033import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
3134
@@ -47,6 +50,56 @@ export const BUILT_IN_SOURCE_CONTROL_DRIVERS: ReadonlyArray<
4750 SourceControlDriver<BuiltInSourceControlDriversEnv>
4851> = drivers;
4952
53+ /**
54+ * The client the HTTP drivers read through: fetch, so redirects and headers behave as before, over
55+ * one pool of its own. The server's fetch drops a connection after four idle seconds and opens one
56+ * per concurrent request, so nearly every pull request opened after a pause paid a new TLS
57+ * handshake per read. This pool keeps connections for minutes, or for as long as the host does,
58+ * and carries a panel's parallel reads over one HTTP/2 connection where the host offers it.
59+ * Fresh, since the server builds the same `FetchHttpClient.layer` for itself and a memoized one
60+ * would ignore the `Fetch` given here.
61+ */
62+ const httpDriverClient = Layer.fresh(FetchHttpClient.layer).pipe(
63+ Layer.provide(
64+ Layer.effect(FetchHttpClient.Fetch)(
65+ Effect.acquireRelease(
66+ Effect.sync(() => {
67+ const options = {
68+ allowH2: true,
69+ keepAliveTimeout: 300_000,
70+ keepAliveMaxTimeout: 300_000,
71+ };
72+ // Match Node's proxy opt-in, including flags supplied through NODE_OPTIONS.
73+ const proxyFlags = [
74+ ...(process.env.NODE_OPTIONS?.match(/--(?:no-)?use-env-proxy\b/g) ?? []),
75+ ...process.execArgv,
76+ ];
77+ const proxyFlag = proxyFlags.findLast(
78+ (flag) => flag === "--use-env-proxy" || flag === "--no-use-env-proxy",
79+ );
80+ const useProxy =
81+ proxyFlag === undefined
82+ ? process.env.NODE_USE_ENV_PROXY === "1"
83+ : proxyFlag === "--use-env-proxy";
84+ return useProxy ? new Undici.EnvHttpProxyAgent(options) : new Undici.Agent(options);
85+ }),
86+ (dispatcher) => Effect.promise(() => dispatcher.destroy()),
87+ ).pipe(
88+ // Node's own fetch types and this undici's describe the same init and response, but name
89+ // a different `Dispatcher`, so they meet through `unknown`.
90+ Effect.map(
91+ (dispatcher): typeof globalThis.fetch =>
92+ (input, init) =>
93+ Undici.fetch(input as Undici.RequestInfo, {
94+ ...(init as unknown as Undici.RequestInit),
95+ dispatcher,
96+ }) as unknown as Promise<Response>,
97+ ),
98+ ),
99+ ),
100+ ),
101+ );
102+
50103/** The services the built-in drivers' packages own, plus the host port they all run against. */
51104export const layer = Layer.mergeAll(
52105 // `GitHubApi.layerWithDependencies` carries the quota reserve and rate-limit pause every GitHub
@@ -57,4 +110,4 @@ export const layer = Layer.mergeAll(
57110 ForgejoCli.layer,
58111 GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer)),
59112 GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
60- ).pipe(Layer.provideMerge(ServerSourceControlHost.layer));
113+ ).pipe(Layer.provide(httpDriverClient), Layer. provideMerge(ServerSourceControlHost.layer));
0 commit comments