fix: normalize abort signals for fetch

This commit is contained in:
Peter Steinberger
2026-01-21 17:29:39 +00:00
parent 8aca606a6f
commit 4e1806947d
8 changed files with 81 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
import { randomUUID } from "node:crypto";
import { resolveFetch } from "../infra/fetch.js";
export type SignalRpcOptions = {
baseUrl: string;
timeoutMs?: number;
@@ -36,10 +38,14 @@ function normalizeBaseUrl(url: string): string {
}
async function fetchWithTimeout(url: string, init: RequestInit, timeoutMs: number) {
const fetchImpl = resolveFetch();
if (!fetchImpl) {
throw new Error("fetch is not available");
}
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...init, signal: controller.signal });
return await fetchImpl(url, { ...init, signal: controller.signal });
} finally {
clearTimeout(timer);
}
@@ -113,7 +119,11 @@ export async function streamSignalEvents(params: {
const url = new URL(`${baseUrl}/api/v1/events`);
if (params.account) url.searchParams.set("account", params.account);
const res = await fetch(url, {
const fetchImpl = resolveFetch();
if (!fetchImpl) {
throw new Error("fetch is not available");
}
const res = await fetchImpl(url, {
method: "GET",
headers: { Accept: "text/event-stream" },
signal: params.abortSignal,