Skip to content

Commit

Permalink
feat(AsyncApi2.0.0-Yaml): add AsyncApi 2.0.0 adapter
Browse files Browse the repository at this point in the history
Refs #1
  • Loading branch information
char0n committed Oct 16, 2020
1 parent 3a6667a commit 3aa325a
Show file tree
Hide file tree
Showing 82 changed files with 1,967 additions and 2 deletions.
15 changes: 15 additions & 0 deletions apidom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apidom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"apidom-ns-openapi3-1": "file:packages/apidom-ns-openapi3-1",
"apidom-parser": "file:packages/apidom-parser",
"apidom-parser-adapter-asyncapi2-0-json": "file:packages/apidom-parser-adapter-asyncapi2-0-json",
"apidom-parser-adapter-asyncapi2-0-yaml": "file:packages/apidom-parser-adapter-asyncapi2-0-yaml",
"apidom-parser-adapter-openapi3-1-json": "file:packages/apidom-parser-adapter-openapi3-1-json",
"apidom-parser-adapter-openapi3-1-yaml": "file:packages/apidom-parser-adapter-openapi3-1-yaml"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export const mediaTypes = [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
];

export const detect = (source: string): boolean =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/dist
/es
/lib
/types
/config
/.nyc_output
/node_modules
/**/*.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist
/es
/lib
/types
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
recursive: true,
spec: 'test/**/*.ts',
require: ['test/mocha-bootstrap.js'],
};
2 changes: 2 additions & 0 deletions apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-prefix="="
save=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

const path = require('path');
const { nonMinimizeTrait, minimizeTrait } = require('./traits.config');

const browser = {
mode: 'production',
entry: ['./src/polyfills.ts', './src/adapter-browser.ts'],
target: 'web',
node: {
fs: 'empty',
},
performance: {
maxEntrypointSize: 712000,
maxAssetSize: 712000,
},
output: {
path: path.resolve('./dist'),
filename: 'apidom-parser-adapter-asyncapi2-0-yaml.browser.js',
libraryTarget: 'umd',
library: 'apidomParserAdapterAsyncApi2_0Yaml',
},
resolve: {
extensions: ['.ts', '.mjs', '.js', '.json'],
},
module: {
rules: [
{
test: /\.wasm$/,
loader: 'file-loader',
type: 'javascript/auto',
},
{
test: /\.(ts|js)?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
babelrc: true,
rootMode: 'upward',
},
},
},
],
},
...nonMinimizeTrait,
};

const browserMin = {
mode: 'production',
entry: ['./src/polyfills.ts', './src/adapter-browser.ts'],
target: 'web',
node: {
fs: 'empty',
},
output: {
path: path.resolve('./dist'),
filename: 'apidom-parser-adapter-asyncapi2-0-yaml.browser.min.js',
libraryTarget: 'umd',
library: 'apidomParserAdapterAsyncApi2_0Yaml',
},
resolve: {
extensions: ['.ts', '.mjs', '.js', '.json'],
},
module: {
rules: [
{
test: /\.wasm$/,
loader: 'file-loader',
type: 'javascript/auto',
},
{
test: /\.(ts|js)?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
babelrc: true,
rootMode: 'upward',
},
},
},
],
},
...minimizeTrait,
};

module.exports = [browser, browserMin];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');

const nonMinimizeTrait = {
optimization: {
minimize: false,
usedExports: false,
concatenateModules: false,
},
};

const minimizeTrait = {
plugins: [
new webpack.LoaderOptionsPlugin({
minimize: true,
}),
],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
warnings: false,
},
output: {
comments: false,
},
},
}),
],
},
};

module.exports = {
nonMinimizeTrait,
minimizeTrait,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "apidom-parser-adapter-asyncapi2-0-yaml",
"version": "0.1.0",
"description": "AsyncApi 2.0.0 YAML adapter for ApiDOM parser",
"main": "lib/adapter-node.js",
"module": "es/adapter-browser.js",
"jsnext:main": "es/adapter-browser.js",
"browser": "es/adapter-browser.js",
"unpkg": "dist/apidom-parser-apdater-asyncapi2-0-yaml.browser.min.js",
"scripts": {
"build": "npm run build:es && npm run build:commonjs && npm run build:umd:browser",
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es --extensions '.ts' --root-mode 'upward'",
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --extensions '.ts' --root-mode 'upward'",
"build:umd:browser": "npm run build:wasm && cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress",
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/tree-sitter-yaml/tree-sitter-yaml.wasm && exit 0 || cd ../../node_modules/tree-sitter-yaml && cross-env BABEL_ENV=commonjs NODE_OPTIONS='-r core-js/stable @babel/register' tree-sitter generate ./grammar.js && tree-sitter build-wasm",
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix",
"clean": "rimraf ./es ./lib ./dist tree-sitter-yaml.wasm",
"test": "cross-env BABEL_ENV=commonjs mocha",
"security-audit": "run-s -sc security-audit:prod security-audit:dev",
"security-audit:prod": "npm-audit --production --only=prod --audit-level=low",
"security-audit:dev": "npm-audit --only=dev --audit-level=moderate"
},
"author": "Vladimir Gorej",
"license": "Apache-2.0",
"dependencies": {
"@apidevtools/json-schema-ref-parser": "=9.0.3",
"apidom": "file:../apidom",
"apidom-ast": "file:../apidom-ast",
"apidom-ns-asyncapi2-0": "file:../apidom-ns-asyncapi2-0",
"ramda": "=0.27.0",
"ramda-adjunct": "=2.27.0",
"stampit": "=4.3.1",
"tree-sitter": "=0.17.0",
"tree-sitter-yaml": "=0.3.0",
"web-tree-sitter": "=0.17.0"
},
"devDependencies": {
"tree-sitter-cli": "=0.17.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as parse, namespace } from './parser/index-browser';
export { detect, mediaTypes } from './adapter';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as parse, namespace } from './parser/index-node';
export { mediaTypes, detect } from './adapter';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const mediaTypes = [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
];

export const detect = (source: string): boolean =>
!!source.match(/(["']?)asyncapi\1\s*:\s*(["']?)3\.\d+\.\d+\2/g);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { tail } from 'ramda';
import { isString } from 'ramda-adjunct';
// @ts-ignore
import treeSitterWasm from 'web-tree-sitter/tree-sitter.wasm';

// patch fetch() to let emscripten load the WASM file
const realFetch = window.fetch;
window.fetch = (...args) => {
// @ts-ignore
if (isString(args[0]) && args[0].endsWith('/tree-sitter.wasm')) {
// @ts-ignore
return realFetch.apply(window, [treeSitterWasm, tail(args)]);
}
return realFetch.apply(window, args);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import './index-browser-patch';

import Parser from 'web-tree-sitter';
import * as apiDOM from 'apidom';
// @ts-ignore
import treeSitterYaml from 'tree-sitter-yaml/tree-sitter-yaml.wasm';

import parse from '.';

export { namespace } from './index';

const parserP = (async () => {
await Parser.init();
const YamlLanguage = await Parser.Language.load(treeSitterYaml);
const parser = new Parser();
parser.setLanguage(YamlLanguage);
return parser;
})();

const parseBrowser = async (
source: string,
options: Record<string, unknown> = {},
): Promise<apiDOM.ParseResultElement> => {
const parser = await parserP;
// @ts-ignore
return parse(source, { ...options, parser });
};

export default parseBrowser;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Parser from 'tree-sitter';
// @ts-ignore
import YAMLLanguage from 'tree-sitter-yaml';
import * as apiDOM from 'apidom';

import parse from '.';

export { namespace } from './index';

const parseNode = async (
source: string,
options: Record<string, unknown> = {},
): Promise<apiDOM.ParseResultElement> => {
const parser = new Parser();
parser.setLanguage(YAMLLanguage);

// @ts-ignore
return parse(source, { ...options, parser });
};

export default parseNode;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { createNamespace, ParseResultElement } from 'apidom';
import {
Error,
YamlStream,
YamlDocument,
YamlMapping,
YamlSequence,
YamlKeyValuePair,
transformTreeSitterYamlCST,
} from 'apidom-ast';
import asyncapi2_0 from 'apidom-ns-asyncapi2-0';

import specification from './specification';
import { visit } from './visitors';

export const namespace = createNamespace(asyncapi2_0);

const parse = async (
source: string,
{ sourceMap = false, specObj = specification, parser = null } = {},
): Promise<ParseResultElement> => {
const resolvedSpecObj = await $RefParser.dereference(specObj);
// @ts-ignore
const parseResultElement = new namespace.elements.ParseResult();
// @ts-ignore
const streamVisitor = resolvedSpecObj.visitors.stream.$visitor();

// @ts-ignore
const cst = parser.parse(source);
const ast = transformTreeSitterYamlCST(cst);

const keyMap = {
// @ts-ignore
[YamlStream.type]: ['children'],
// @ts-ignore
[YamlDocument.type]: ['children'],
// @ts-ignore
[YamlMapping.type]: ['children'],
// @ts-ignore
[YamlSequence.type]: ['children'],
// @ts-ignore
[YamlKeyValuePair.type]: ['children'],
// @ts-ignore
[Error.type]: ['children'],
};

visit(ast.rootNode, streamVisitor, {
keyMap,
// @ts-ignore
state: {
namespace,
specObj: resolvedSpecObj,
sourceMap,
element: parseResultElement,
},
});

return parseResultElement;
};

export default parse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Element } from 'minim';

// eslint-disable-next-line import/prefer-default-export
export const appendMetadata = <T extends Element>(metadata: string[], element: T): T => {
metadata.forEach((md: string) => {
element.classes.push(md);
element.getMetaProperty('symbols', []).push(md);
});

return element;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { pathSatisfies, startsWith, both, curry } from 'ramda';
import { isYamlKeyValuePair } from 'apidom-ast';

// isAsyncApiExtension :: (Options, YamlKeyValuePair) -> Boolean
// eslint-disable-next-line import/prefer-default-export
export const isAsyncApiExtension = curry((options, node) =>
both(isYamlKeyValuePair, pathSatisfies(startsWith('x-'), ['key', 'content']))(node),
);
Loading

0 comments on commit 3aa325a

Please sign in to comment.