diff --git a/src/utils/generate/registry.ts b/src/utils/generate/registry.ts index 16fdda2e5..720294a0a 100644 --- a/src/utils/generate/registry.ts +++ b/src/utils/generate/registry.ts @@ -6,14 +6,26 @@ export function registryURLParser(input?: string) { } } +const REGISTRY_TIMEOUT_MS = 5000; + export async function registryValidation(registryUrl?: string, registryAuth?: string, registryToken?: string) { if (!registryUrl) { return; } + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), REGISTRY_TIMEOUT_MS); try { - const response = await fetch(registryUrl as string); + const response = await fetch(registryUrl as string, { + method: 'HEAD', + signal: controller.signal, + }); + clearTimeout(timeout); 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'); } - } catch { + } catch (err: any) { + clearTimeout(timeout); + if (err.name === 'AbortError') { + throw new Error(`Registry URL timed out after ${REGISTRY_TIMEOUT_MS / 1000}s: ${registryUrl}. The host is unreachable or too slow.`); + } throw new Error(`Can't fetch registryURL: ${registryUrl}`); } } diff --git a/test/unit/utils/registry.test.ts b/test/unit/utils/registry.test.ts new file mode 100644 index 000000000..85185ea76 --- /dev/null +++ b/test/unit/utils/registry.test.ts @@ -0,0 +1,52 @@ +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.equal(undefined); + expect(registryURLParser('')).to.equal(undefined); + }); + + it('should throw for invalid URL without protocol', () => { + expect(() => registryURLParser('not-a-url')).to.throw('Invalid --registry-url flag. The param requires a valid http/https url.'); + expect(() => registryURLParser('ftp://example.com')).to.throw('Invalid --registry-url flag. The param requires a valid http/https url.'); + }); + + it('should accept valid http URL', () => { + expect(() => registryURLParser('http://example.com')).to.not.throw(); + }); + + it('should accept valid https URL', () => { + expect(() => registryURLParser('https://example.com')).to.not.throw(); + }); +}); + +describe('registryValidation()', () => { + it('should return undefined when no registryUrl is provided', async () => { + const result = await registryValidation(undefined, undefined, undefined); + expect(result).to.equal(undefined); + }); + + it('should throw when URL is unreachable (timeout)', async () => { + // 10.255.255.1 is a blackhole IP - will never respond + const blackholeUrl = 'http://10.255.255.1:9999'; + try { + await registryValidation(blackholeUrl, undefined, undefined); + expect.fail('Should have thrown'); + } catch (err: any) { + expect(err.message).to.include('timed out'); + expect(err.message).to.include(blackholeUrl); + } + }); + + it('should throw when URL is unreachable (connection refused)', async () => { + // localhost:9 is unlikely to have anything listening + const url = 'http://localhost:9'; + try { + await registryValidation(url, undefined, undefined); + expect.fail('Should have thrown'); + } catch (err: any) { + expect(err.message).to.include('Can\'t fetch registryURL'); + } + }); +});