diff --git a/packages/starpod/src/lib/rss.ts b/packages/starpod/src/lib/rss.ts index dc8116e..d10ced5 100644 --- a/packages/starpod/src/lib/rss.ts +++ b/packages/starpod/src/lib/rss.ts @@ -42,14 +42,32 @@ function once, T>( }; } +// The deadline covers the whole request, body included, so a server that +// stalls fails the build instead of hanging it. Every page needs the feed, so +// it gets longer than the optional transcripts' 10s. +const FEED_TIMEOUT_MS = 30_000; + const fetchFeedXml = once(async (url: string) => { - const response = await fetch(url); - if (!response.ok) { - throw new Error( - `[starpod] Fetching the feed ${url} failed: ${response.status}` - ); + try { + const response = await fetch(url, { + signal: AbortSignal.timeout(FEED_TIMEOUT_MS) + }); + if (!response.ok) { + throw new Error( + `[starpod] Fetching the feed ${url} failed: ${response.status}` + ); + } + return await response.text(); + } catch (error) { + // An abort is a DOMException, which isn't an Error in every runtime. + if ((error as { name?: unknown } | null)?.name === 'TimeoutError') { + throw new Error( + `[starpod] Fetching the feed ${url} timed out after ${FEED_TIMEOUT_MS / 1000}s`, + { cause: error } + ); + } + throw error; } - return response.text(); }); // rss-to-json only takes a URL and fetches it with axios; an axios adapter diff --git a/tests/unit/rss.test.ts b/tests/unit/rss.test.ts index a39b94e..fc3ab30 100644 --- a/tests/unit/rss.test.ts +++ b/tests/unit/rss.test.ts @@ -80,4 +80,16 @@ describe('the feed', () => { await expect(getAllEpisodes(config)).resolves.toHaveLength(1); expect(fetchMock).toHaveBeenCalledTimes(2); }); + + it('is fetched with a deadline, and says so when it runs out', async () => { + fetchMock.mockImplementation(async (_url: string, init?: RequestInit) => { + expect(init?.signal).toBeInstanceOf(AbortSignal); + throw new DOMException('The operation timed out.', 'TimeoutError'); + }); + const { getAllEpisodes } = await loadRss(); + + await expect(getAllEpisodes(config)).rejects.toThrow( + 'Fetching the feed https://feed.example/podcast.xml timed out after 30s' + ); + }); });