-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(AsyncApi2.0.0-Yaml): add AsyncApi 2.0.0 adapter
Refs #1
- Loading branch information
Showing
82 changed files
with
1,967 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
apidom/packages/apidom-parser-adapter-asyncapi2-0-json/src/adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/.eslintignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/dist | ||
/es | ||
/lib | ||
/types | ||
/config | ||
/.nyc_output | ||
/node_modules | ||
/**/*.js |
4 changes: 4 additions & 0 deletions
4
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/dist | ||
/es | ||
/lib | ||
/types |
7 changes: 7 additions & 0 deletions
7
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/.mocharc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
2
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/.npmrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
save-prefix="=" | ||
save=false |
88 changes: 88 additions & 0 deletions
88
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/config/webpack/browser.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
39 changes: 39 additions & 0 deletions
39
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/config/webpack/traits.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
41 changes: 41 additions & 0 deletions
41
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/adapter-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
2 changes: 2 additions & 0 deletions
2
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/adapter-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
7 changes: 7 additions & 0 deletions
7
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
15 changes: 15 additions & 0 deletions
15
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/index-browser-patch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
29 changes: 29 additions & 0 deletions
29
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/index-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
21 changes: 21 additions & 0 deletions
21
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/index-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
62 changes: 62 additions & 0 deletions
62
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
11 changes: 11 additions & 0 deletions
11
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/metadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
8 changes: 8 additions & 0 deletions
8
apidom/packages/apidom-parser-adapter-asyncapi2-0-yaml/src/parser/predicates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); |
Oops, something went wrong.