Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/starpod/src/lib/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,32 @@ function once<Args extends Array<unknown>, 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
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/rss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});
Loading