diff --git a/.changeset/fix-registry-timeout.md b/.changeset/fix-registry-timeout.md new file mode 100644 index 000000000..91e93e180 --- /dev/null +++ b/.changeset/fix-registry-timeout.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/cli": patch +--- + +Add timeout to registry URL validation to prevent CLI hang when registry is unreachable. diff --git a/src/utils/generate/registry.ts b/src/utils/generate/registry.ts index 16fdda2e5..7f0ccb456 100644 --- a/src/utils/generate/registry.ts +++ b/src/utils/generate/registry.ts @@ -1,3 +1,13 @@ +const REGISTRY_TIMEOUT_MS = 10_000; + +/** Custom error class for registry authentication errors */ +class RegistryAuthError extends Error { + constructor(message: string) { + super(message); + this.name = 'RegistryAuthError'; + } +} + export function registryURLParser(input?: string) { if (!input) { return; } const isURL = /^https?:/; @@ -8,12 +18,27 @@ export function registryURLParser(input?: string) { export async function registryValidation(registryUrl?: string, registryAuth?: string, registryToken?: string) { if (!registryUrl) { return; } + + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), REGISTRY_TIMEOUT_MS); + + let response: Response; try { - const response = await fetch(registryUrl as string); - if (response.status === 401 && !registryAuth && !registryToken) { - throw new Error('You Need to pass either registryAuth in username:password encoded in Base64 or need to pass registryToken'); + response = await fetch(registryUrl as string, { + method: 'HEAD', + signal: controller.signal, + }); + } catch (error: unknown) { + clearTimeout(timer); + if (error instanceof Error && error.name === 'AbortError') { + throw new Error(`Registry URL validation timed out after ${REGISTRY_TIMEOUT_MS / 1000}s: ${registryUrl}`); } - } catch { - throw new Error(`Can't fetch registryURL: ${registryUrl}`); + throw new Error(`Unable to reach registry URL: ${registryUrl}`); + } finally { + clearTimeout(timer); + } + + if (response.status === 401 && !registryAuth && !registryToken) { + throw new RegistryAuthError('You Need to pass either registryAuth in username:password encoded in Base64 or need to pass registryToken'); } } diff --git a/test/unit/utils/registry.test.ts b/test/unit/utils/registry.test.ts new file mode 100644 index 000000000..e3d8b5b24 --- /dev/null +++ b/test/unit/utils/registry.test.ts @@ -0,0 +1,80 @@ +import { expect } from 'chai'; +import { registryURLParser, registryValidation } from '../../../src/utils/generate/registry'; + +describe('registryURLParser()', () => { + it('should return undefined for empty input', () => { + expect(registryURLParser(undefined)).to.be.undefined; + expect(registryURLParser('')).to.be.undefined; + }); + + it('should accept valid http URLs', () => { + expect(() => registryURLParser('https://registry.npmjs.org')).to.not.throw(); + expect(() => registryURLParser('http://localhost:4873')).to.not.throw(); + }); + + it('should reject non-http URLs', () => { + expect(() => registryURLParser('ftp://registry.example.com')).to.throw('Invalid --registry-url'); + expect(() => registryURLParser('not-a-url')).to.throw('Invalid --registry-url'); + }); +}); + +describe('registryValidation()', () => { + it('should return undefined when no URL provided', async () => { + const result = await registryValidation(undefined); + expect(result).to.be.undefined; + }); + + it('should fail fast for unreachable URLs instead of hanging', async () => { + // Stub fetch to simulate a timeout scenario without real network calls + const originalFetch = global.fetch; + global.fetch = () => new Promise((_, reject) => { + // Simulate abort after timeout + const abortError = new Error('The operation was aborted'); + abortError.name = 'AbortError'; + setTimeout(() => reject(abortError), 50); + }); + + try { + await registryValidation('http://example.com'); + expect.fail('Should have thrown'); + } catch (error: unknown) { + expect(error).to.be.instanceOf(Error); + const msg = (error as Error).message; + expect(msg).to.include('timed out'); + } finally { + global.fetch = originalFetch; + } + }); + + it('should throw auth error for 401 without credentials', async () => { + const originalFetch = global.fetch; + global.fetch = () => Promise.resolve(new Response(null, { status: 401 })); + + try { + await registryValidation('http://example.com'); + expect.fail('Should have thrown'); + } catch (error: unknown) { + expect(error).to.be.instanceOf(Error); + const msg = (error as Error).message; + expect(msg).to.include('registryAuth'); + } finally { + global.fetch = originalFetch; + } + }); + + it('should throw unreachable error for network failures', async () => { + const originalFetch = global.fetch; + global.fetch = () => Promise.reject(new Error('Network error')); + + try { + await registryValidation('http://example.com'); + expect.fail('Should have thrown'); + } catch (error: unknown) { + expect(error).to.be.instanceOf(Error); + const msg = (error as Error).message; + expect(msg).to.include('Unable to reach'); + } finally { + global.fetch = originalFetch; + } + }); +});