From 4fcb48395e4f0a8bc9809886deaf9f28f9f76cf3 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 29 Jun 2026 22:37:30 +0800 Subject: [PATCH] fix: prevent mindspace space endpoint optional data timeout --- mindspace.mjs | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/mindspace.mjs b/mindspace.mjs index d156960..b5559e3 100644 --- a/mindspace.mjs +++ b/mindspace.mjs @@ -43,6 +43,20 @@ function asNumber(value) { return Number(value ?? 0); } +async function withOptionalTimeout(promise, timeoutMs, fallback) { + let timeout; + try { + return await Promise.race([ + promise, + new Promise((resolve) => { + timeout = setTimeout(() => resolve(fallback), timeoutMs); + }), + ]); + } finally { + if (timeout) clearTimeout(timeout); + } +} + function categoryResponse(row) { return { id: row.id, @@ -139,7 +153,11 @@ export function createMindSpaceService(pool, options = {}) { const scheduleService = options.scheduleService ?? null; const resolvePublicPageLimit = async () => { try { - const config = await loadMindSpaceConfig(pool); + const config = await withOptionalTimeout( + loadMindSpaceConfig(pool), + 1000, + { publicPageLimit: publicPageLimitFallback }, + ); return Number(config.publicPageLimit ?? publicPageLimitFallback); } catch { return publicPageLimitFallback; @@ -195,19 +213,23 @@ export function createMindSpaceService(pool, options = {}) { let schedule = null; if (scheduleService) { try { - const [todayTodoItems, digestSubscriptions] = await Promise.all([ - typeof scheduleService.listTodayTodoItems === 'function' - ? scheduleService.listTodayTodoItems({ userId }) - : Promise.resolve([]), - typeof scheduleService.listDigestSubscriptions === 'function' - ? scheduleService.listDigestSubscriptions({ - userId, - digestType: 'todo_day', - status: ['active', 'locked'], - limit: 10, - }) - : Promise.resolve([]), - ]); + const [todayTodoItems, digestSubscriptions] = await withOptionalTimeout( + Promise.all([ + typeof scheduleService.listTodayTodoItems === 'function' + ? scheduleService.listTodayTodoItems({ userId }) + : Promise.resolve([]), + typeof scheduleService.listDigestSubscriptions === 'function' + ? scheduleService.listDigestSubscriptions({ + userId, + digestType: 'todo_day', + status: ['active', 'locked'], + limit: 10, + }) + : Promise.resolve([]), + ]), + 1000, + [[], []], + ); schedule = { todayTodoItems, digestSubscriptions }; } catch { schedule = null;