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
16 changes: 14 additions & 2 deletions src/utils/generate/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
}
}

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, {

Check warning on line 16 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=AZ0yba49LQrmbX5-HhaS&open=AZ0yba49LQrmbX5-HhaS&pullRequest=2087
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}`);
}
}
52 changes: 52 additions & 0 deletions test/unit/utils/registry.test.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
});
Loading