Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 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 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=AZz2bZ0A4256DVCszMRT&open=AZz2bZ0A4256DVCszMRT&pullRequest=2040
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);
}
}
50 changes: 50 additions & 0 deletions test/unit/utils/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect } from 'chai';
import { registryURLParser, registryValidation } from '../../../src/utils/generate/registry';

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

it('should accept valid http URL', () => {
expect(() => registryURLParser('http://registry.npmjs.org')).to.not.throw();
});

it('should accept valid https URL', () => {
expect(() => registryURLParser('https://registry.npmjs.org')).to.not.throw();
});

it('should throw for non-http URL', () => {
expect(() => registryURLParser('ftp://example.com')).to.throw('Invalid --registry-url flag');
});
});

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

it('should throw a timeout error for unreachable host', 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.include('timed out');
}
}).timeout(10000);

it('should throw an error for invalid registry URL', 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