Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): add abort timeout for resolveRedirects (#2444)

tom6f77f7a11515
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<string> {
- 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;
}