fix: normalize abort signals for fetch
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user