mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* perf: Add timeout and optimize URL unfurl performance Fixes issue where urls.unfurl endpoint could take 15+ seconds due to external API timeouts and sequential plugin execution. Changes: - Add timeout support to fetch utility with AbortController (defaults to no timeout, configurable per request) - Add 10 second timeout to Iframely plugin requests - Add early URL pattern validation to GitHub and Linear plugins to avoid unnecessary database queries - Add try-catch error handling to URL parsing in GitHub and Linear plugins This reduces worst-case unfurl time from 15+ seconds to ~10 seconds maximum, and eliminates unnecessary overhead for URLs that don't match plugin patterns. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * lint --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import type { JSONObject } from "@shared/types";
|
|
import { UnfurlResourceType } from "@shared/types";
|
|
import Logger from "@server/logging/Logger";
|
|
import type { UnfurlError, UnfurlSignature } from "@server/types";
|
|
import fetch from "@server/utils/fetch";
|
|
import env from "./env";
|
|
|
|
class Iframely {
|
|
public static defaultUrl = "https://iframe.ly";
|
|
|
|
public static async requestResource(
|
|
url: string,
|
|
type = "iframely"
|
|
): Promise<JSONObject | UnfurlError> {
|
|
const isDefaultHost = env.IFRAMELY_URL === this.defaultUrl;
|
|
|
|
// Cloud Iframely requires /api path, while self-hosted does not.
|
|
const apiUrl = isDefaultHost ? `${env.IFRAMELY_URL}/api` : env.IFRAMELY_URL;
|
|
|
|
try {
|
|
const res = await fetch(
|
|
`${apiUrl}/${type}?url=${encodeURIComponent(url)}&api_key=${
|
|
env.IFRAMELY_API_KEY
|
|
}`,
|
|
{
|
|
timeout: 10000, // 10 second timeout
|
|
}
|
|
);
|
|
return await res.json();
|
|
} catch (err) {
|
|
Logger.error(`Error fetching data from Iframely for url: ${url}`, err);
|
|
return { error: err.message || "Unknown error" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param url Resource url
|
|
* @returns An object containing resource details e.g, resource title, description etc.
|
|
*/
|
|
public static unfurl: UnfurlSignature = async (url: string) => {
|
|
const data = await Iframely.requestResource(url);
|
|
return "error" in data // In addition to our custom UnfurlError, sometimes iframely returns error in the response body.
|
|
? ({ error: data.error } as UnfurlError)
|
|
: { ...data, type: UnfurlResourceType.URL };
|
|
};
|
|
}
|
|
|
|
export default Iframely;
|