-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathfetchWithTimeout.ts
More file actions
209 lines (183 loc) · 6.12 KB
/
Copy pathfetchWithTimeout.ts
File metadata and controls
209 lines (183 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* HTTP timeout utilities for outbound requests to Horizon and external integrations.
*
* Provides configurable connect and read timeouts with structured error handling.
*/
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface FetchTimeoutConfig {
/** Connection timeout in milliseconds (time to establish connection) */
connectTimeoutMs: number;
/** Read timeout in milliseconds (time to receive response after connection) */
readTimeoutMs: number;
}
export interface FetchWithTimeoutOptions extends RequestInit {
/** Custom timeout configuration (overrides defaults) */
timeouts?: Partial<FetchTimeoutConfig>;
}
/**
* Structured error for HTTP timeout failures.
* Distinguishes between connection and read timeouts.
*/
export class HttpTimeoutError extends Error {
public readonly type: 'connect' | 'read';
public readonly url: string;
public readonly timeoutMs: number;
constructor(type: 'connect' | 'read', url: string, timeoutMs: number) {
super(`HTTP ${type} timeout after ${timeoutMs}ms: ${url}`);
this.name = 'HttpTimeoutError';
this.type = type;
this.url = url;
this.timeoutMs = timeoutMs;
}
}
/**
* Structured error for HTTP request failures.
* Wraps underlying fetch errors with context.
*/
export class HttpRequestError extends Error {
public readonly url: string;
public readonly cause?: Error;
constructor(message: string, url: string, cause?: Error) {
super(message);
this.name = 'HttpRequestError';
this.url = url;
this.cause = cause;
}
}
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
/**
* Default timeout values (configurable via environment variables).
*/
export function getDefaultTimeouts(): FetchTimeoutConfig {
return {
connectTimeoutMs: parseInt(process.env['HTTP_CONNECT_TIMEOUT_MS'] ?? '5000', 10),
readTimeoutMs: parseInt(process.env['HTTP_READ_TIMEOUT_MS'] ?? '10000', 10),
};
}
function mergeAbortSignals(signal: AbortSignal | undefined, timeoutSignal: AbortSignal): AbortSignal {
if (!signal) {
return timeoutSignal;
}
if (signal.aborted) {
return signal;
}
if (timeoutSignal.aborted) {
return timeoutSignal;
}
const controller = new AbortController();
const abort = (): void => controller.abort();
signal.addEventListener('abort', abort, { once: true });
timeoutSignal.addEventListener('abort', abort, { once: true });
return controller.signal;
}
// ---------------------------------------------------------------------------
// Core implementation
// ---------------------------------------------------------------------------
/**
* Fetch with configurable connect and read timeouts.
*
* @param url - URL to fetch
* @param options - Fetch options with optional timeout overrides
* @returns Response object
* @throws {HttpTimeoutError} If connection or read timeout is exceeded
* @throws {HttpRequestError} If request fails for other reasons
*
* @example
* ```typescript
* // Use default timeouts from environment
* const response = await fetchWithTimeout('https://horizon-testnet.stellar.org/ledgers');
*
* // Override timeouts for specific request
* const response = await fetchWithTimeout('https://api.example.com/data', {
* timeouts: { connectTimeoutMs: 3000, readTimeoutMs: 15000 }
* });
* ```
*/
export async function fetchWithTimeout(
url: string,
options: FetchWithTimeoutOptions = {}
): Promise<Response> {
const defaults = getDefaultTimeouts();
const timeouts: FetchTimeoutConfig = {
connectTimeoutMs: options.timeouts?.connectTimeoutMs ?? defaults.connectTimeoutMs,
readTimeoutMs: options.timeouts?.readTimeoutMs ?? defaults.readTimeoutMs,
};
// Total timeout is connect + read
const totalTimeoutMs = timeouts.connectTimeoutMs + timeouts.readTimeoutMs;
// Create abort controller for timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), totalTimeoutMs);
try {
const signal = mergeAbortSignals(options.signal ?? undefined, controller.signal);
const response = await fetch(url, {
...options,
signal,
});
clearTimeout(timeoutId);
return response;
} catch (error) {
clearTimeout(timeoutId);
// Handle abort/timeout
if (error instanceof Error && error.name === 'AbortError') {
// Determine if it was connect or read timeout
// Note: In practice, distinguishing these requires more sophisticated tracking
// For now, we use a heuristic based on total timeout
throw new HttpTimeoutError('read', url, totalTimeoutMs);
}
// Handle other fetch errors
if (error instanceof Error) {
throw new HttpRequestError(
`HTTP request failed: ${error.message}`,
url,
error
);
}
// Unknown error type
throw new HttpRequestError('HTTP request failed with unknown error', url);
}
}
/**
* Fetch JSON with timeout and automatic parsing.
*
* @param url - URL to fetch
* @param options - Fetch options with optional timeout overrides
* @returns Parsed JSON response
* @throws {HttpTimeoutError} If connection or read timeout is exceeded
* @throws {HttpRequestError} If request fails or JSON parsing fails
*
* @example
* ```typescript
* interface HorizonResponse {
* _embedded: { records: Array<{ id: string }> };
* }
*
* const data = await fetchJsonWithTimeout<HorizonResponse>(
* 'https://horizon-testnet.stellar.org/ledgers?limit=10'
* );
* ```
*/
export async function fetchJsonWithTimeout<T = unknown>(
url: string,
options: FetchWithTimeoutOptions = {}
): Promise<T> {
const response = await fetchWithTimeout(url, options);
if (!response.ok) {
throw new HttpRequestError(
`HTTP ${response.status} ${response.statusText}`,
url
);
}
try {
return await response.json() as T;
} catch (error) {
throw new HttpRequestError(
'Failed to parse JSON response',
url,
error instanceof Error ? error : undefined
);
}
}