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
27 changes: 27 additions & 0 deletions scripts/fetch-asyncapi-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@
const SPEC_EXAMPLES_ZIP_URL = 'https://github.com/asyncapi/spec/archive/refs/heads/master.zip';
const EXAMPLE_DIRECTORY = path.join(__dirname, '../assets/examples');
const TEMP_ZIP_NAME = 'spec-examples.zip';
const EXAMPLES_JSON_PATH = path.join(EXAMPLE_DIRECTORY, 'examples.json');

const shouldFetchExamples = (force = false) => {
if (force) {
console.log('Force flag detected, fetching examples...');
return true;
}
if (!fs.existsSync(EXAMPLE_DIRECTORY)) {
return true;
}
if (!fs.existsSync(EXAMPLES_JSON_PATH)) {
return true;
}
const content = fs.readFileSync(EXAMPLES_JSON_PATH, { encoding: 'utf-8' });
if (!content || content.trim() === '') {
return true;
}
console.log('Examples already exist, skipping fetch. Use --force to refresh.');
return false;
};

const fetchAsyncAPIExamplesFromExternalURL = () => {
try {
Expand Down Expand Up @@ -119,6 +139,13 @@
};

(async () => {
const args = process.argv.slice(2);

Check warning on line 142 in scripts/fetch-asyncapi-example.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

`args` should be a `Set`, and use `args.has()` to check existence or non-existence.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ0ET65OIpZiw5iiN5kE&open=AZ0ET65OIpZiw5iiN5kE&pullRequest=2060
const force = args.includes('--force') || args.includes('-f');

if (!shouldFetchExamples(force)) {
return;
}

await fetchAsyncAPIExamplesFromExternalURL();
await unzipAsyncAPIExamples();
await buildCLIListFromExamples();
Expand Down
15 changes: 1 addition & 14 deletions src/apps/cli/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Args } from '@oclif/core';
import { BaseGeneratorCommand } from '@cli/internal/base/BaseGeneratorCommand';
import { load, Specification } from '@models/SpecificationFile';

Check warning on line 3 in src/apps/cli/commands/generate/fromTemplate.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import of 'load'.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ0ET62eIpZiw5iiN5kA&open=AZ0ET62eIpZiw5iiN5kA&pullRequest=2060

Check warning on line 3 in src/apps/cli/commands/generate/fromTemplate.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import of 'Specification'.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ0ET62fIpZiw5iiN5kB&open=AZ0ET62fIpZiw5iiN5kB&pullRequest=2060
import { ValidationError } from '@errors/validation-error';
import { GeneratorError } from '@errors/generator-error';
import { intro } from '@clack/prompts';
import { inverse } from 'picocolors';
Expand Down Expand Up @@ -65,19 +64,7 @@
const watchTemplate = flags['watch'];
const genOption = this.buildGenOption(flags, parsedFlags);

let specification: Specification;
try {
specification = await load(asyncapi);
} catch {
return this.error(
new ValidationError({

type: 'invalid-file',
filepath: asyncapi,
}),
{ exit: 1 },
);
}
const specification = asyncapiInput;

const result = await this.generatorService.generate(
specification,
Expand Down
66 changes: 63 additions & 3 deletions src/domains/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { promises as fs } from 'fs';
import * as readline from 'readline';

Check warning on line 2 in src/domains/models/SpecificationFile.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `node:readline` over `readline`.

See more on https://sonarcloud.io/project/issues?id=asyncapi_cli&issues=AZ0ET64tIpZiw5iiN5kD&open=AZ0ET64tIpZiw5iiN5kD&pullRequest=2060
import path from 'path';
import { URL } from 'url';
import yaml from 'js-yaml';
import { loadContext } from './Context';
import { ErrorLoadingSpec } from '@errors/specification-file';
import { ErrorLoadingSpec, MultipleYamlDocumentsError } from '@errors/specification-file';
import { MissingContextFileError } from '@errors/context-error';
import { fileFormat } from '@cli/internal/flags/format.flags';
import { HttpsProxyAgent } from 'https-proxy-agent';
Expand All @@ -18,6 +19,17 @@
const TYPE_CONTEXT_NAME = 'context-name';
const TYPE_FILE_PATH = 'file-path';
const TYPE_URL = 'url-path';
const TYPE_STDIN = 'stdin';

/**
* Checks if a YAML string contains multiple documents (separated by ---).
* @param content - The YAML content to check
* @returns true if the content contains multiple YAML documents
*/
function hasMultipleYamlDocuments(content: string): boolean {
const yamlDocs = content.split(/^---$/m);
return yamlDocs.length > 1;
}

export class Specification {
private readonly spec: string;
Expand Down Expand Up @@ -46,8 +58,20 @@

toJson(): Record<string, any> {
try {
return yaml.load(this.spec, { json: true }) as Record<string, any>;
} catch {
// Check for multiple YAML documents before parsing
if (hasMultipleYamlDocuments(this.spec)) {
throw new MultipleYamlDocumentsError();
}
const parsed = yaml.load(this.spec, { json: true });
// yaml.load can return an array if there are multiple documents with certain YAML configurations
if (Array.isArray(parsed)) {
throw new MultipleYamlDocumentsError();
}
return parsed as Record<string, any>;
} catch (err) {
if (err instanceof MultipleYamlDocumentsError) {
throw err;
}
return JSON.parse(this.spec);
}
}
Expand Down Expand Up @@ -134,6 +158,33 @@
fileURL: targetUrl,
});
}

static async fromStdin(): Promise<Specification> {
return new Promise((resolve, reject) => {
const chunks: string[] = [];
const rl = readline.createInterface({
input: process.stdin,
crlfDelay: Infinity,
});

rl.on('line', (line) => {
chunks.push(line);
});

rl.on('close', () => {
const spec = chunks.join('\n');
if (!spec.trim()) {
reject(new Error('No input received from stdin'));
return;
}
resolve(new Specification(spec));
});

rl.on('error', (err) => {
reject(new ErrorLoadingSpec('stdin', '-'));
});
});
}
}

export default class SpecificationFile {
Expand Down Expand Up @@ -166,6 +217,11 @@
// NOSONAR
try {
if (filePathOrContextName) {
// Handle stdin
if (filePathOrContextName === '-') {
return Specification.fromStdin();
}

if (loadType?.file) {
return Specification.fromFile(filePathOrContextName);
}
Expand Down Expand Up @@ -283,6 +339,10 @@
JSON.parse(content);
return 'json';
}
// Check for multiple YAML documents
if (hasMultipleYamlDocuments(content)) {
return undefined;
}
// below yaml.load is not a definitive way to determine if a file is yaml or not.
// it is able to load .txt text files also.
yaml.load(content);
Expand Down
14 changes: 13 additions & 1 deletion src/errors/specification-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ export class SpecificationURLNotFound extends SpecificationFileError {
}
}

type From = 'file' | 'url' | 'context' | 'invalid file';
type From = 'file' | 'url' | 'context' | 'invalid file' | 'stdin' | 'multiple documents';

export class MultipleYamlDocumentsError extends SpecificationFileError {
constructor() {
super();
this.name = 'MultipleYamlDocumentsError';
this.message = 'AsyncAPI files with multiple YAML documents are not supported. Please provide a single AsyncAPI document.';
}
}

export class ErrorLoadingSpec extends Error {
private readonly errorMessages = {
Expand All @@ -55,6 +63,10 @@ export class ErrorLoadingSpec extends Error {
this.name = 'Invalid AsyncAPI file type';
this.message = 'cli only supports yml ,yaml ,json extension';
}
if (from === 'stdin') {
this.name = 'error loading AsyncAPI document from stdin';
this.message = 'Reading from stdin (-) is not supported.';
}

if (!from) {
this.name = 'error locating AsyncAPI document';
Expand Down
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 TIMEOUT_MS = 5000;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 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=AZ0ET63_IpZiw5iiN5kC&open=AZ0ET63_IpZiw5iiN5kC&pullRequest=2060
method: 'HEAD',
signal: controller.signal,
});
clearTimeout(timeoutId);

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 (err: any) {
clearTimeout(timeoutId);
if (err.name === 'AbortError') {
throw new Error(`Registry URL timed out after ${TIMEOUT_MS}ms: ${registryUrl}`);
}
const cause = err.cause ? `\nCaused by: ${err.cause}` : '';
throw new Error(`Can't fetch registryURL: ${registryUrl}${cause}`);
}
}
34 changes: 34 additions & 0 deletions test/unit/services/validation.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,37 @@ describe('ValidationService', () => {
});
});
});

describe('Multi-Document YAML Detection', () => {
let validationService: ValidationService;

beforeEach(() => {
validationService = new ValidationService();
});

const multiDocumentYAML = `asyncapi: '2.6.0'
info:
title: Test API
version: '1.0.0'
channels: {}

---
asyncapi: '2.6.0'
info:
title: Second Doc
version: '1.0.0'
channels: {}`;

it('should throw error for multiple YAML documents', async () => {
const specFile = new Specification(multiDocumentYAML);

try {
specFile.toJson();
// If we get here, the error was not thrown
expect.fail('Expected MultipleYamlDocumentsError to be thrown');
} catch (err: any) {
expect(err.name).to.equal('MultipleYamlDocumentsError');
expect(err.message).to.include('multiple YAML documents');
}
});
});
Loading