diff --git a/.changeset/add-timeout-to-registry-url-check.md b/.changeset/add-timeout-to-registry-url-check.md new file mode 100644 index 000000000..312565f8d --- /dev/null +++ b/.changeset/add-timeout-to-registry-url-check.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/cli": patch +--- + +fix: add timeout and AbortController to registry URL validation to prevent CLI hang on unreachable endpoints diff --git a/cli/src/utils/generate/registry.ts b/cli/src/utils/generate/registry.ts new file mode 100644 index 000000000..178b60f03 --- /dev/null +++ b/cli/src/utils/generate/registry.ts @@ -0,0 +1,28 @@ +const REGISTRY_TIMEOUT_MS = 5000; // 5 seconds timeout for registry validation + +export async function registryValidation(registryUrl: string): Promise { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), REGISTRY_TIMEOUT_MS); + + try { + await fetch(registryUrl, { + method: 'HEAD', // Use HEAD for a lightweight reachability check + signal: controller.signal, + }); + } catch (error: any) { // Use 'any' for error type to handle both AbortError and other network errors + if (error.name === 'AbortError') { + throw new Error( + `Connection to registry URL '${registryUrl}' timed out after ${REGISTRY_TIMEOUT_MS / 1000} seconds. ` + + `Please ensure the URL is correct and the host is reachable.` + ); + } + // Handle other network-related errors (e.g., DNS resolution failure, connection refused) + throw new Error( + `Failed to connect to registry URL '${registryUrl}'. ` + + `Network error: ${error.message}. ` + + `Please check your network connection and the provided registry URL.` + ); + } finally { + clearTimeout(timeoutId); + } +} \ No newline at end of file