- Update test expectation: 'defaults to enable on Node 22' - Update comment in fetch.ts to explain IPv4 fallback rationale - Addresses greptile review feedback
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import * as net from "node:net";
|
|
import type { TelegramNetworkConfig } from "../config/types.telegram.js";
|
|
import { resolveFetch } from "../infra/fetch.js";
|
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
|
import { resolveTelegramAutoSelectFamilyDecision } from "./network-config.js";
|
|
|
|
let appliedAutoSelectFamily: boolean | null = null;
|
|
const log = createSubsystemLogger("telegram/network");
|
|
|
|
// Node 22 workaround: enable autoSelectFamily to allow IPv4 fallback on broken IPv6 networks.
|
|
// Many networks have IPv6 configured but not routed, causing "Network is unreachable" errors.
|
|
// See: https://github.com/nodejs/node/issues/54359
|
|
function applyTelegramNetworkWorkarounds(network?: TelegramNetworkConfig): void {
|
|
const decision = resolveTelegramAutoSelectFamilyDecision({ network });
|
|
if (decision.value === null || decision.value === appliedAutoSelectFamily) {
|
|
return;
|
|
}
|
|
appliedAutoSelectFamily = decision.value;
|
|
|
|
if (typeof net.setDefaultAutoSelectFamily === "function") {
|
|
try {
|
|
net.setDefaultAutoSelectFamily(decision.value);
|
|
const label = decision.source ? ` (${decision.source})` : "";
|
|
log.info(`telegram: autoSelectFamily=${decision.value}${label}`);
|
|
} catch {
|
|
// ignore if unsupported by the runtime
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prefer wrapped fetch when available to normalize AbortSignal across runtimes.
|
|
export function resolveTelegramFetch(
|
|
proxyFetch?: typeof fetch,
|
|
options?: { network?: TelegramNetworkConfig },
|
|
): typeof fetch | undefined {
|
|
applyTelegramNetworkWorkarounds(options?.network);
|
|
if (proxyFetch) {
|
|
return resolveFetch(proxyFetch);
|
|
}
|
|
const fetchImpl = resolveFetch();
|
|
if (!fetchImpl) {
|
|
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
|
}
|
|
return fetchImpl;
|
|
}
|
|
|
|
export function resetTelegramFetchStateForTests(): void {
|
|
appliedAutoSelectFamily = null;
|
|
}
|