Skip to content
Closed
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-timeout-1774022951.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 hanging on unreachable servers
20 changes: 17 additions & 3 deletions src/utils/generate/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@

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

const controller = new AbortController();
const timeoutMs = 5000;
const timeout = setTimeout(() => controller.abort(), timeoutMs);

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

Check warning on line 17 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=AZ0LtsAst_IUpgcOthMR&open=AZ0LtsAst_IUpgcOthMR&pullRequest=2062
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 {
throw new Error(`Can't fetch registryURL: ${registryUrl}`);
} catch (error: unknown) {
if (error instanceof Error && error.name === 'AbortError') {
throw new Error(`Registry URL timed out after ${timeoutMs / 1000} seconds: ${registryUrl}`);
}
const originalMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Can't fetch registryURL: ${registryUrl}. Error: ${originalMessage}`);
} finally {
clearTimeout(timeout);
}
}
82 changes: 82 additions & 0 deletions test/unit/utils/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { registryURLParser, registryValidation } from '../../../src/utils/generate/registry';

describe('registryURLParser', () => {
it('should return undefined for empty input', () => {
expect(registryURLParser()).toBeUndefined();
expect(registryURLParser('')).toBeUndefined();
});

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

it('should reject non-http URLs', () => {
expect(() => registryURLParser('ftp://example.com')).toThrow('Invalid --registry-url flag');
expect(() => registryURLParser('file:///path/to/file')).toThrow('Invalid --registry-url flag');
expect(() => registryURLParser('not-a-url')).toThrow('Invalid --registry-url flag');
});
});

describe('registryValidation', () => {
it('should return undefined for empty URL', async () => {
await expect(registryValidation()).resolves.toBeUndefined();
await expect(registryValidation('')).resolves.toBeUndefined();
});

it('should timeout after 5 seconds on unreachable URL', async () => {
// Use a TEST-NET IP that is guaranteed to be unreachable (RFC 5737)
// This should trigger the AbortController timeout
const result = registryValidation('http://192.0.2.1:9999/registry');
await expect(result).rejects.toThrow('timed out');
}, 10000); // Allow 10s for the test (5s timeout + buffer)

it('should throw with URL context on connection error', async () => {
// localhost:1 should immediately refuse connection
await expect(registryValidation('http://localhost:1/nonexistent'))
.rejects.toThrow('Can\'t fetch registryURL');
});

it('should throw when 401 without auth credentials', async () => {
// Mock fetch to return 401
const originalFetch = global.fetch;
global.fetch = jest.fn().mockResolvedValue({
status: 401,
});

try {
await expect(registryValidation('http://example.com/registry'))
.rejects.toThrow('registryAuth');
} finally {
global.fetch = originalFetch;
}
});

it('should not throw when 401 with registryAuth provided', async () => {
const originalFetch = global.fetch;
global.fetch = jest.fn().mockResolvedValue({
status: 401,
});

try {
await expect(registryValidation('http://example.com/registry', 'dXNlcjpwYXNz'))
.resolves.toBeUndefined();
} finally {
global.fetch = originalFetch;
}
});

it('should not throw when 401 with registryToken provided', async () => {
const originalFetch = global.fetch;
global.fetch = jest.fn().mockResolvedValue({
status: 401,
});

try {
await expect(registryValidation('http://example.com/registry', undefined, 'token123'))
.resolves.toBeUndefined();
} finally {
global.fetch = originalFetch;
}
});
});
Loading