From 6f77f7a11515b7ddc2eea5f8347680bb6c3fa7c0 Mon Sep 17 00:00:00 2001 From: tom <62763456+amplitudesxd@users.noreply.github.com> Date: Wed, 26 Nov 2025 10:30:07 +0000 Subject: [PATCH] feat(api): add abort timeout for resolveRedirects (#2444) --- apps/api/src/lib/validateUrl.ts | 34 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/apps/api/src/lib/validateUrl.ts b/apps/api/src/lib/validateUrl.ts index 0cad62730..a73c06f0d 100644 --- a/apps/api/src/lib/validateUrl.ts +++ b/apps/api/src/lib/validateUrl.ts @@ -197,34 +197,30 @@ export async function resolveRedirects( url: string, abort?: AbortSignal, ): Promise { - if (!protocolIncluded(url)) { - url = `http://${url}`; - } + const targetUrl = protocolIncluded(url) ? url : `http://${url}`; - try { - const response = await undici.fetch(url, { - method: "HEAD", - redirect: "follow", - dispatcher: getSecureDispatcher(false), - signal: abort, - }); + const methods = ["HEAD", "GET"] as const; - return response.url; - } catch (error) { - if (abort?.aborted) throw error; + for (const method of methods) { + const signal = abort + ? AbortSignal.any([abort, AbortSignal.timeout(2000)]) + : AbortSignal.timeout(2000); try { - const response = await undici.fetch(url, { - method: "GET", + const response = await undici.fetch(targetUrl, { + method, redirect: "follow", dispatcher: getSecureDispatcher(false), - signal: abort, + signal, }); return response.url; - } catch (getError) { - if (abort?.aborted) throw getError; - return url; + } catch (error) { + if (abort?.aborted) { + throw error; + } } } + + return targetUrl; }