Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-registry-url-timeout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/cli": patch
---

fix: add timeout to registry URL validation to prevent CLI hang (#2027)
18 changes: 16 additions & 2 deletions src/utils/generate/registry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const REGISTRY_TIMEOUT_MS = 5000;

export function registryURLParser(input?: string) {
if (!input) { return; }
const isURL = /^https?:/;
Expand All @@ -8,12 +10,24 @@

export async function registryValidation(registryUrl?: string, registryAuth?: string, registryToken?: string) {
if (!registryUrl) { return; }

const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), REGISTRY_TIMEOUT_MS);

try {
const response = await fetch(registryUrl as string);
const response = await fetch(registryUrl as string, {

Check warning on line 18 in src/utils/generate/registry.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This assertion is unnecessary since it does not change the type of the expression.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZz3TN-k1Bw9PZgzaWOu&open=AZz3TN-k1Bw9PZgzaWOu&pullRequest=2045
method: 'HEAD',
signal: controller.signal,
});
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 (error: unknown) {
if (error instanceof Error && error.name === 'AbortError') {
throw new Error(`Registry URL timed out after ${REGISTRY_TIMEOUT_MS / 1000}s: ${registryUrl}`);
}
throw new Error(`Can't fetch registryURL: ${registryUrl}`);
} finally {
clearTimeout(timeoutId);
}
}
47 changes: 47 additions & 0 deletions test/unit/utils/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect } from 'chai';
import { registryURLParser, registryValidation } from '../../../src/utils/generate/registry';

describe('registryURLParser()', () => {
it('should return undefined for no input', () => {
expect(registryURLParser()).to.be.undefined;
expect(registryURLParser('')).to.be.undefined;
});

it('should accept valid http URLs', () => {
expect(registryURLParser('http://registry.example.com')).to.be.undefined;
expect(registryURLParser('https://registry.example.com')).to.be.undefined;
});

it('should throw for invalid URLs', () => {
expect(() => registryURLParser('ftp://example.com')).to.throw('Invalid --registry-url flag');
expect(() => registryURLParser('not-a-url')).to.throw('Invalid --registry-url flag');
});
});

describe('registryValidation()', () => {
it('should return undefined for no input', async () => {
const result = await registryValidation();
expect(result).to.be.undefined;
});

it('should throw a timeout error for unreachable hosts', async () => {
// 10.255.255.1 is a non-routable IP that will cause a timeout
try {
await registryValidation('http://10.255.255.1');
expect.fail('Should have thrown');
} catch (error: unknown) {
expect(error).to.be.instanceOf(Error);
expect((error as Error).message).to.match(/timed out|Can't fetch/);
}
}).timeout(10000);

it('should throw for invalid/unreachable URLs', async () => {
try {
await registryValidation('http://localhost:1');
expect.fail('Should have thrown');
} catch (error: unknown) {
expect(error).to.be.instanceOf(Error);
expect((error as Error).message).to.include('Can\'t fetch registryURL');
}
}).timeout(10000);
});
Loading