mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 12:11:23 +00:00
fix: normalize abort signals for telegram fetch
This commit is contained in:
@@ -157,13 +157,12 @@ describe("createTelegramBot", () => {
|
||||
(globalThis as { Bun?: unknown }).Bun = {};
|
||||
createTelegramBot({ token: "tok" });
|
||||
const fetchImpl = resolveTelegramFetch();
|
||||
expect(fetchImpl).toBe(fetchSpy);
|
||||
expect(botCtorSpy).toHaveBeenCalledWith(
|
||||
"tok",
|
||||
expect.objectContaining({
|
||||
client: expect.objectContaining({ fetch: fetchSpy }),
|
||||
}),
|
||||
);
|
||||
expect(fetchImpl).toBeTypeOf("function");
|
||||
expect(fetchImpl).not.toBe(fetchSpy);
|
||||
const clientFetch = (botCtorSpy.mock.calls[0]?.[1] as { client?: { fetch?: unknown } })
|
||||
?.client?.fetch;
|
||||
expect(clientFetch).toBeTypeOf("function");
|
||||
expect(clientFetch).not.toBe(fetchSpy);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (originalBun === undefined) {
|
||||
|
||||
@@ -284,13 +284,12 @@ describe("createTelegramBot", () => {
|
||||
(globalThis as { Bun?: unknown }).Bun = {};
|
||||
createTelegramBot({ token: "tok" });
|
||||
const fetchImpl = resolveTelegramFetch();
|
||||
expect(fetchImpl).toBe(fetchSpy);
|
||||
expect(botCtorSpy).toHaveBeenCalledWith(
|
||||
"tok",
|
||||
expect.objectContaining({
|
||||
client: expect.objectContaining({ fetch: fetchSpy }),
|
||||
}),
|
||||
);
|
||||
expect(fetchImpl).toBeTypeOf("function");
|
||||
expect(fetchImpl).not.toBe(fetchSpy);
|
||||
const clientFetch = (botCtorSpy.mock.calls[0]?.[1] as { client?: { fetch?: unknown } })
|
||||
?.client?.fetch;
|
||||
expect(clientFetch).toBeTypeOf("function");
|
||||
expect(clientFetch).not.toBe(fetchSpy);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (originalBun === undefined) {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { wrapFetchWithAbortSignal } from "../infra/fetch.js";
|
||||
|
||||
// Bun-only: force native fetch to avoid grammY's Node shim under Bun.
|
||||
export function resolveTelegramFetch(proxyFetch?: typeof fetch): typeof fetch | undefined {
|
||||
if (proxyFetch) return proxyFetch;
|
||||
if (proxyFetch) return wrapFetchWithAbortSignal(proxyFetch);
|
||||
const fetchImpl = globalThis.fetch;
|
||||
const isBun = "Bun" in globalThis || Boolean(process?.versions?.bun);
|
||||
if (!isBun) return undefined;
|
||||
if (!fetchImpl) {
|
||||
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
||||
}
|
||||
return fetchImpl;
|
||||
return wrapFetchWithAbortSignal(fetchImpl);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// @ts-nocheck
|
||||
import { ProxyAgent } from "undici";
|
||||
import { wrapFetchWithAbortSignal } from "../infra/fetch.js";
|
||||
|
||||
export function makeProxyFetch(proxyUrl: string): typeof fetch {
|
||||
const agent = new ProxyAgent(proxyUrl);
|
||||
return (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
return wrapFetchWithAbortSignal((input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const base = init ? { ...init } : {};
|
||||
return fetch(input, { ...base, dispatcher: agent });
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -164,12 +164,10 @@ describe("sendMessageTelegram", () => {
|
||||
});
|
||||
try {
|
||||
await sendMessageTelegram("123", "hi", { token: "tok" });
|
||||
expect(botCtorSpy).toHaveBeenCalledWith(
|
||||
"tok",
|
||||
expect.objectContaining({
|
||||
client: expect.objectContaining({ fetch: fetchSpy }),
|
||||
}),
|
||||
);
|
||||
const clientFetch = (botCtorSpy.mock.calls[0]?.[1] as { client?: { fetch?: unknown } })
|
||||
?.client?.fetch;
|
||||
expect(clientFetch).toBeTypeOf("function");
|
||||
expect(clientFetch).not.toBe(fetchSpy);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
if (originalBun === undefined) {
|
||||
|
||||
Reference in New Issue
Block a user