Files
RedditBot/src/utils/fetch-with-timeout.ts
T
jjbrunton 633bb0ec04 Restructure chat service, web routes, and add test suite
Split monolithic chat service into modular structure with Matrix client
support, extract web server into per-resource route modules, introduce
bootstrap/main-loop separation, and add comprehensive vitest coverage
across services, repositories, and utilities. Adds project docs and
CLAUDE.md agent instructions.
2026-05-21 01:32:10 +01:00

19 lines
672 B
TypeScript

/**
* `fetch` with a hard client-side timeout. Node's global fetch otherwise
* inherits no timeout — a stalled connection will hang the caller forever,
* which has wedged the main loop more than once (incidents on 2026-05-20).
*
* On timeout, throws an error whose `.name === 'TimeoutError'`. Callers that
* want to differentiate timeout-vs-other failures can branch on that.
*/
export async function fetchWithTimeout(
input: string | URL | Request,
init: RequestInit & { timeoutMs?: number } = {},
): Promise<Response> {
const { timeoutMs = 30_000, ...rest } = init;
return fetch(input, {
...rest,
signal: AbortSignal.timeout(timeoutMs),
});
}