From 09a270b8405c5bd4a37557d8f8dc16cbfdd13f73 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 13 May 2024 20:49:28 +0800 Subject: [PATCH] timeout if downloading content takes more than 10 minutes --- src/api.ts | 64 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/api.ts b/src/api.ts index 541104b..6797c90 100644 --- a/src/api.ts +++ b/src/api.ts @@ -52,6 +52,41 @@ const downloadFromUrl = async (url: string): Promise => { } } +const fetchContentForItems = async ( + endpoint: string, + apiKey: string, + items: Item[], +) => { + const content = await getContent( + endpoint, + apiKey, + items.map((a) => a.id), + ) + + await Promise.allSettled( + content.data.map(async (c) => { + if (c.error) { + console.error('Error fetching content', c.error) + return + } + + const item = items.find((i) => i.id === c.libraryItemId) + if (!item) { + console.error('Item not found', c.libraryItemId) + return + } + + // timeout if download takes too long + item.content = await Promise.race([ + downloadFromUrl(c.downloadUrl), + new Promise( + (_, reject) => setTimeout(() => reject('Timeout'), 600_000), // 10 minutes + ), + ]) + }), + ) +} + export const getItems = async ( endpoint: string, apiKey: string, @@ -76,37 +111,16 @@ export const getItems = async ( format, }) - const articles = response.edges.map((e) => e.node) - if (includeContent && articles.length > 0) { + const items = response.edges.map((e) => e.node) + if (includeContent && items.length > 0) { try { - const content = await getContent( - endpoint, - apiKey, - articles.map((a) => a.id), - ) - - await Promise.allSettled( - content.data.map(async (c, index) => { - if (c.error) { - console.error('Error fetching content', c.error) - return - } - - // timeout if download takes too long - articles[index].content = await Promise.race([ - downloadFromUrl(c.downloadUrl), - new Promise( - (_, reject) => setTimeout(() => reject('Timeout'), 10000), // 10 seconds - ), - ]) - }), - ) + await fetchContentForItems(endpoint, apiKey, items) } catch (error) { console.error('Error fetching content', error) } } - return [articles, response.pageInfo.hasNextPage] + return [items, response.pageInfo.hasNextPage] } export const deleteItem = async (