fix(telegram): restrict native fetch to bun
This commit is contained in:
@@ -135,25 +135,52 @@ describe("createTelegramBot", () => {
|
|||||||
expect(useSpy).toHaveBeenCalledWith("throttler");
|
expect(useSpy).toHaveBeenCalledWith("throttler");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("forces native fetch for BAN compatibility", () => {
|
it("forces native fetch only under Bun", () => {
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
|
const originalBun = (globalThis as { Bun?: unknown }).Bun;
|
||||||
const fetchSpy = vi.fn() as unknown as typeof fetch;
|
const fetchSpy = vi.fn() as unknown as typeof fetch;
|
||||||
globalThis.fetch = fetchSpy;
|
globalThis.fetch = fetchSpy;
|
||||||
try {
|
try {
|
||||||
|
(globalThis as { Bun?: unknown }).Bun = {};
|
||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const fetchImpl = resolveTelegramFetch();
|
const fetchImpl = resolveTelegramFetch();
|
||||||
if (fetchImpl) {
|
expect(fetchImpl).toBe(fetchSpy);
|
||||||
expect(botCtorSpy).toHaveBeenCalledWith(
|
expect(botCtorSpy).toHaveBeenCalledWith(
|
||||||
"tok",
|
"tok",
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
client: expect.objectContaining({ fetch: fetchSpy }),
|
client: expect.objectContaining({ fetch: fetchSpy }),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
expect(botCtorSpy).toHaveBeenCalledWith("tok", undefined);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
globalThis.fetch = originalFetch;
|
globalThis.fetch = originalFetch;
|
||||||
|
if (originalBun === undefined) {
|
||||||
|
delete (globalThis as { Bun?: unknown }).Bun;
|
||||||
|
} else {
|
||||||
|
(globalThis as { Bun?: unknown }).Bun = originalBun;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not force native fetch on Node", () => {
|
||||||
|
const originalFetch = globalThis.fetch;
|
||||||
|
const originalBun = (globalThis as { Bun?: unknown }).Bun;
|
||||||
|
const fetchSpy = vi.fn() as unknown as typeof fetch;
|
||||||
|
globalThis.fetch = fetchSpy;
|
||||||
|
try {
|
||||||
|
if (originalBun !== undefined) {
|
||||||
|
delete (globalThis as { Bun?: unknown }).Bun;
|
||||||
|
}
|
||||||
|
createTelegramBot({ token: "tok" });
|
||||||
|
const fetchImpl = resolveTelegramFetch();
|
||||||
|
expect(fetchImpl).toBeUndefined();
|
||||||
|
expect(botCtorSpy).toHaveBeenCalledWith("tok", undefined);
|
||||||
|
} finally {
|
||||||
|
globalThis.fetch = originalFetch;
|
||||||
|
if (originalBun === undefined) {
|
||||||
|
delete (globalThis as { Bun?: unknown }).Bun;
|
||||||
|
} else {
|
||||||
|
(globalThis as { Bun?: unknown }).Bun = originalBun;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
// Ensure native fetch is used when available (Bun + Node 18+).
|
// Bun-only: force native fetch to avoid grammY's Node shim under Bun.
|
||||||
export function resolveTelegramFetch(
|
export function resolveTelegramFetch(
|
||||||
proxyFetch?: typeof fetch,
|
proxyFetch?: typeof fetch,
|
||||||
): typeof fetch | undefined {
|
): typeof fetch | undefined {
|
||||||
if (proxyFetch) return proxyFetch;
|
if (proxyFetch) return proxyFetch;
|
||||||
const fetchImpl = globalThis.fetch;
|
const fetchImpl = globalThis.fetch;
|
||||||
const isBun = "Bun" in globalThis || Boolean(process?.versions?.bun);
|
const isBun = "Bun" in globalThis || Boolean(process?.versions?.bun);
|
||||||
|
if (!isBun) return undefined;
|
||||||
if (!fetchImpl) {
|
if (!fetchImpl) {
|
||||||
if (isBun) {
|
throw new Error("fetch is not available; set telegram.proxy in config");
|
||||||
throw new Error("fetch is not available; set telegram.proxy in config");
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
return fetchImpl;
|
return fetchImpl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user