Skip to content
Merged
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
68 changes: 68 additions & 0 deletions apidom/package-lock.json

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

5 changes: 3 additions & 2 deletions apidom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
},
"comments": {
"scripts": {
"prebuild": "We have prebuild script to download and initialize the docker image used to build WASM fragments"
"prebuild": "We build WASM here once and copy into specific packages to avoid multiple WASM build"
}
},
"scripts": {
"postinstall": "npm run link-parent-bin",
"prebuild": "tree-sitter build-wasm ./node_modules/tree-sitter-json && rimraf tree-sitter-json.wasm",
"prebuild": "tree-sitter build-wasm ./node_modules/tree-sitter-json && copyfiles tree-sitter-json.wasm packages/apidom-parser-adapter-asyncapi2-0-json && copyfiles tree-sitter-json.wasm packages/apidom-parser-adapter-openapi3-1-json",
"build": "lerna run build",
"lint": "lerna run lint",
"lint:fix": "lerna run lint",
Expand Down Expand Up @@ -55,6 +55,7 @@
"@typescript-eslint/parser": "=3.0.2",
"babel-loader": "=8.1.0",
"chai": "=4.2.0",
"copyfiles": "=2.3.0",
"core-js": "=3.6.5",
"cross-env": "=7.0.2",
"eslint": "=7.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import IdentifierElement from './Identifier';
import ComponentsElement from './Components';
import InfoElement from './Info';

class OpenApi3_1 extends ObjectElement {
class AsyncApi2_0 extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
super(content, meta, attributes);
this.element = 'asyncApi2-0';
this.classes.push('api');
}

get asyncapi(): AsyncapiElement {
return this.get('openapi');
return this.get('asyncapi');
}

get id(): IdentifierElement {
Expand All @@ -28,4 +28,4 @@ class OpenApi3_1 extends ObjectElement {
}
}

export default OpenApi3_1;
export default AsyncApi2_0;
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"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": "tree-sitter build-wasm ../../node_modules/tree-sitter-json",
"build:wasm": "node ./scripts/file-exists.js tree-sitter-json.wasm && exit 0 || tree-sitter build-wasm ../../node_modules/tree-sitter-json",
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix",
"clean": "rimraf ./es ./lib ./dist",
"clean": "rimraf ./es ./lib ./dist tree-sitter-json.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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const fs = require('fs');
const path = require('path');

const cwd = process.cwd();

if (!fs.existsSync(path.join(cwd, process.argv[2]))) {
process.exit(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as parse, namespace } from './parser/index-browser';
export { mediaTypes, detect } 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,8 @@
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 =>
!!source.match(/(["']?)asyncapi\1\s*:\s*(["']?)2\.\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';

import parse from '.';
// @ts-ignore
import treeSitterJson from '../../tree-sitter-json.wasm';

export { namespace } from './index';

const parserP = (async () => {
await Parser.init();
const JsonLanguage = await Parser.Language.load(treeSitterJson);
const parser = new Parser();
parser.setLanguage(JsonLanguage);
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 JSONLanguage from 'tree-sitter-json';
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(JSONLanguage);

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

export default parseNode;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { createNamespace, ParseResultElement } from 'apidom';
import {
Error,
JsonArray,
JsonDocument,
JsonObject,
JsonProperty,
transformTreeSitterJsonCST,
} 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 documentVisitor = resolvedSpecObj.visitors.document.$visitor();

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

const keyMap = {
// @ts-ignore
[JsonDocument.type]: ['children'],
// @ts-ignore
[JsonObject.type]: ['children'],
// @ts-ignore
[JsonProperty.type]: ['children'],
// @ts-ignore
[JsonArray.type]: ['children'],
// @ts-ignore
[Error.type]: ['children'],
};

visit(ast.rootNode, documentVisitor, {
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,7 @@
import { pathEq, pathSatisfies, startsWith, both, curry } from 'ramda';

// isAsyncApiExtension :: (Options, PropertyNode) -> Boolean
// eslint-disable-next-line import/prefer-default-export
export const isAsyncApiExtension = curry((options, node) =>
both(pathEq(['type'], 'property'), pathSatisfies(startsWith('x-'), ['key', 'value']))(node),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { namespace } from 'apidom';

/* eslint-disable import/prefer-default-export */

// @ts-ignore
export const addSourceMap = (node, element) => {
// @ts-ignore
const sourceMap = new namespace.elements.SourceMap();

sourceMap.position = node.position;
sourceMap.astNode = node;

element.meta.set('sourceMap', sourceMap);

return element;
};
Loading