Skip to content

Commit d38520a

Browse files
committed
fix(telegram): use EnvHttpProxyAgent for global dispatcher to respect HTTP_PROXY (#26207)
1 parent fb76e31 commit d38520a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/telegram/fetch.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const AgentCtor = vi.hoisted(() =>
1010
this.options = options;
1111
}),
1212
);
13+
const EnvHttpProxyAgentCtor = vi.hoisted(() =>
14+
vi.fn(function MockEnvHttpProxyAgent(this: { options: unknown }, options: unknown) {
15+
this.options = options;
16+
}),
17+
);
1318

1419
vi.mock("node:net", async () => {
1520
const actual = await vi.importActual<typeof import("node:net")>("node:net");
@@ -29,6 +34,7 @@ vi.mock("node:dns", async () => {
2934

3035
vi.mock("undici", () => ({
3136
Agent: AgentCtor,
37+
EnvHttpProxyAgent: EnvHttpProxyAgentCtor,
3238
setGlobalDispatcher,
3339
}));
3440

@@ -40,6 +46,7 @@ afterEach(() => {
4046
setDefaultResultOrder.mockReset();
4147
setGlobalDispatcher.mockReset();
4248
AgentCtor.mockClear();
49+
EnvHttpProxyAgentCtor.mockClear();
4350
vi.unstubAllEnvs();
4451
vi.clearAllMocks();
4552
if (originalFetch) {
@@ -147,12 +154,12 @@ describe("resolveTelegramFetch", () => {
147154
expect(setDefaultResultOrder).toHaveBeenCalledTimes(2);
148155
});
149156

150-
it("replaces global undici dispatcher with autoSelectFamily-enabled agent", async () => {
157+
it("replaces global undici dispatcher with proxy-aware autoSelectFamily-enabled agent", async () => {
151158
globalThis.fetch = vi.fn(async () => ({})) as unknown as typeof fetch;
152159
resolveTelegramFetch(undefined, { network: { autoSelectFamily: true } });
153160

154161
expect(setGlobalDispatcher).toHaveBeenCalledTimes(1);
155-
expect(AgentCtor).toHaveBeenCalledWith({
162+
expect(EnvHttpProxyAgentCtor).toHaveBeenCalledWith({
156163
connect: {
157164
autoSelectFamily: true,
158165
autoSelectFamilyAttemptTimeout: 300,
@@ -174,13 +181,13 @@ describe("resolveTelegramFetch", () => {
174181
resolveTelegramFetch(undefined, { network: { autoSelectFamily: false } });
175182

176183
expect(setGlobalDispatcher).toHaveBeenCalledTimes(2);
177-
expect(AgentCtor).toHaveBeenNthCalledWith(1, {
184+
expect(EnvHttpProxyAgentCtor).toHaveBeenNthCalledWith(1, {
178185
connect: {
179186
autoSelectFamily: true,
180187
autoSelectFamilyAttemptTimeout: 300,
181188
},
182189
});
183-
expect(AgentCtor).toHaveBeenNthCalledWith(2, {
190+
expect(EnvHttpProxyAgentCtor).toHaveBeenNthCalledWith(2, {
184191
connect: {
185192
autoSelectFamily: false,
186193
autoSelectFamilyAttemptTimeout: 300,

src/telegram/fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as dns from "node:dns";
22
import * as net from "node:net";
3-
import { Agent, setGlobalDispatcher } from "undici";
3+
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
44
import type { TelegramNetworkConfig } from "../config/types.telegram.js";
55
import { resolveFetch } from "../infra/fetch.js";
66
import { createSubsystemLogger } from "../logging/subsystem.js";
@@ -46,7 +46,7 @@ function applyTelegramNetworkWorkarounds(network?: TelegramNetworkConfig): void
4646
) {
4747
try {
4848
setGlobalDispatcher(
49-
new Agent({
49+
new EnvHttpProxyAgent({
5050
connect: {
5151
autoSelectFamily: autoSelectDecision.value,
5252
autoSelectFamilyAttemptTimeout: 300,

0 commit comments

Comments
 (0)