mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 16:10:24 +00:00
fix: normalize abort signals for fetch
This commit is contained in:
37
src/telegram/fetch.test.ts
Normal file
37
src/telegram/fetch.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { resolveTelegramFetch } from "./fetch.js";
|
||||
|
||||
describe("resolveTelegramFetch", () => {
|
||||
it("wraps proxy fetch to normalize foreign abort signals", async () => {
|
||||
let seenSignal: AbortSignal | undefined;
|
||||
const proxyFetch = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||
seenSignal = init?.signal as AbortSignal | undefined;
|
||||
return {} as Response;
|
||||
});
|
||||
|
||||
const fetcher = resolveTelegramFetch(proxyFetch);
|
||||
expect(fetcher).toBeTypeOf("function");
|
||||
|
||||
let abortHandler: (() => void) | null = null;
|
||||
const fakeSignal = {
|
||||
aborted: false,
|
||||
addEventListener: (event: string, handler: () => void) => {
|
||||
if (event === "abort") abortHandler = handler;
|
||||
},
|
||||
removeEventListener: (event: string, handler: () => void) => {
|
||||
if (event === "abort" && abortHandler === handler) abortHandler = null;
|
||||
},
|
||||
} as AbortSignal;
|
||||
|
||||
const promise = fetcher!("https://example.com", { signal: fakeSignal });
|
||||
expect(proxyFetch).toHaveBeenCalledOnce();
|
||||
expect(seenSignal).toBeInstanceOf(AbortSignal);
|
||||
expect(seenSignal).not.toBe(fakeSignal);
|
||||
|
||||
abortHandler?.();
|
||||
expect(seenSignal?.aborted).toBe(true);
|
||||
|
||||
await promise;
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,13 @@
|
||||
import { wrapFetchWithAbortSignal } from "../infra/fetch.js";
|
||||
import { resolveFetch } 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 wrapFetchWithAbortSignal(proxyFetch);
|
||||
const fetchImpl = globalThis.fetch;
|
||||
if (proxyFetch) return resolveFetch(proxyFetch);
|
||||
const isBun = "Bun" in globalThis || Boolean(process?.versions?.bun);
|
||||
if (!isBun) return undefined;
|
||||
const fetchImpl = resolveFetch();
|
||||
if (!fetchImpl) {
|
||||
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
||||
}
|
||||
return wrapFetchWithAbortSignal(fetchImpl);
|
||||
return fetchImpl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user