Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W-14486825] upgrade amf client js #842

Merged
merged 1 commit into from
Nov 17, 2023
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
90 changes: 54 additions & 36 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
// eslint-disable-next-line no-undef
module.exports = {
'env': {
'browser': true,
'es2021': true,
'mocha': true,
env: {
browser: true,
es2021: true,
mocha: true,
},
'extends': 'eslint:recommended',
'parserOptions': {
'ecmaVersion': 12,
'sourceType': 'module',
extends: 'eslint:recommended',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
'rules': {
rules: {
'accessor-pairs': 'error',
'array-bracket-newline': 'error',
'array-bracket-spacing': ['error', 'never'],
'array-callback-return': 'error',
'array-element-newline': [
'error', {
'ArrayExpression': 'consistent',
'ArrayPattern': { 'multiline': true },
'error',
{
ArrayExpression: 'consistent',
ArrayPattern: { multiline: true },
},
],
'arrow-body-style': 'error',
'arrow-parens': 'error',
'arrow-spacing': [
'error', {
'after': true,
'before': true,
'error',
{
after: true,
before: true,
},
],
'block-scoped-var': 'error',
'block-spacing': 'error',
'brace-style': ['error', '1tbs'],
'camelcase': 'error',
camelcase: 'error',
'capitalized-comments': 'off',
'class-methods-use-this': 'error',
'comma-dangle': 'off',
'comma-spacing': [
'error', {
'after': true,
'before': false,
'error',
{
after: true,
before: false,
},
],
'comma-style': ['error', 'last'],
'complexity': 'error',
complexity: 'error',
'computed-property-spacing': ['error', 'never'],
'consistent-return': [
'error', {
'treatUndefinedAsUnspecified': false,
'error',
{
treatUndefinedAsUnspecified: false,
},
],
'consistent-this': 'error',
'curly': 'error',
curly: 'error',
'default-case': 'error',
'default-case-last': 'error',
'default-param-last': 'error',
'dot-location': 'error',
'dot-notation': 'error',
'eol-last': 'error',
'eqeqeq': 'error',
eqeqeq: 'error',
'func-call-spacing': 'error',
'func-name-matching': 'error',
'func-names': 'error',
Expand All @@ -71,14 +75,15 @@ module.exports = {
'id-length': 'off',
'id-match': 'error',
'implicit-arrow-linebreak': ['error', 'beside'],
'indent': 'off',
indent: 'off',
'init-declarations': 'off',
'jsx-quotes': 'error',
'key-spacing': 'error',
'keyword-spacing': [
'error', {
'after': true,
'before': true,
'error',
{
after: true,
before: true,
},
],
'line-comment-position': 'error',
Expand Down Expand Up @@ -204,13 +209,13 @@ module.exports = {
'prefer-spread': 'error',
'prefer-template': 'error',
'quote-props': 'off',
'quotes': ['error', 'single'],
'radix': 'error',
quotes: ['error', 'single'],
radix: 'error',
'require-atomic-updates': 'error',
'require-await': 'error',
'require-unicode-regexp': 'off',
'rest-spread-spacing': 'error',
'semi': 'error',
semi: 'error',
'semi-spacing': 'error',
'semi-style': ['error', 'last'],
'sort-keys': 'off',
Expand All @@ -221,11 +226,12 @@ module.exports = {
'space-infix-ops': 'off',
'space-unary-ops': 'error',
'spaced-comment': ['error', 'always'],
'strict': 'error',
strict: 'error',
'switch-colon-spacing': [
'error', {
'after': true,
'before': false,
'error',
{
after: true,
before: false,
},
],
'symbol-description': 'error',
Expand All @@ -236,6 +242,18 @@ module.exports = {
'wrap-iife': 'error',
'wrap-regex': 'off',
'yield-star-spacing': 'error',
'yoda': ['error', 'never'],
yoda: ['error', 'never'],
},
overrides: [
{
files: ['demo/demo-server/api/amf-service/*.js'],
env: {
node: true,
browser: false,
},
globals: {
process: true,
},
},
],
};
98 changes: 56 additions & 42 deletions demo/demo-server/api/amf-service/amf-parser.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
// eslint-disable-next-line no-undef
const amf = require('amf-client-js');
// import amf from 'amf-client-js';
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
let initied = false;

async function validateDoc(type, doc) {
let validateProfile;
switch (type) {
case 'RAML 1.0': validateProfile = amf.ProfileNames.RAML; break;
case 'RAML 0.8': validateProfile = amf.ProfileNames.RAML08; break;
case 'OAS 1.0':
/** @typedef {import('amf-client-js').AMFConfiguration} AMFConfiguration */

/**
* @typedef ApiSearchResult
* @property {string} type
*/

/**
* @typedef ParserApiConfiguration
* @property {string} source
* @property {ApiSearchResult} from
*/

const {
RAMLConfiguration,
OASConfiguration,
AsyncAPIConfiguration,
RenderOptions,
PipelineId,
} = amf;

/**
* @param {string} vendor
* @returns {AMFConfiguration}
*/
const getConfiguration = (vendor) => {
switch (vendor) {
case 'RAML 0.8': return RAMLConfiguration.RAML08();
case 'RAML 1.0': return RAMLConfiguration.RAML10();
case 'OAS 2.0':
case 'OAS 2':
return OASConfiguration.OAS20();
case 'OAS 3.0':
validateProfile = amf.ProfileNames.OAS;
break;
case 'OAS 3':
return OASConfiguration.OAS30();
case 'ASYNC 2.0': return AsyncAPIConfiguration.Async20();
default: throw new Error(`Unknown vendor: ${vendor}`);
}
const result = await amf.AMF.validate(doc, validateProfile);
process.send({ validation: result.toString() });
}
};

/**
* AMF parser to be called in a child process.
*
* AMF can in extreme cases takes forever to parse API data if, for example,
* RAML type us defined as a number of union types. It may sometimes cause
* the process to crash. To protect the renderer proces this is run as forked
* RAML type is defined as a number of union types. It may sometimes cause
* the process to crash. To protect the renderer process this is run as forked
* process.
*
* @param {Object} data
* @param {ParserApiConfiguration} data
*/
async function processData(data) {
const processData = async (data) => {
const sourceFile = data.source;
const type = data.from.type;
const contentType = data.from.contentType;
const validate = data.validate;
if (!initied) {
await amf.Core.init();
}
/* eslint-disable-next-line require-atomic-updates */
initied = true;
const file = `file://${sourceFile}`;
const parser = amf.Core.parser(type, contentType);
let doc = await parser.parseFileAsync(file);
if (validate) {
await validateDoc(type, doc);
}
const { type } = data.from;

const resolver = amf.Core.resolver(type);
doc = resolver.resolve(doc, 'editing');
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
const opts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
return await generator.generateString(doc, opts);
}
const ro = new RenderOptions().
withSourceMaps().
withCompactUris();
const apiConfiguration = getConfiguration(type).withRenderOptions(ro);
const client = apiConfiguration.baseUnitClient();
const file = `file://${sourceFile}`;
const result = await client.parse(file);
const transformed = client.transform(result.baseUnit, PipelineId.Editing);
return client.render(transformed.baseUnit, 'application/ld+json');
};

process.on('message', async (data) => {
const typed = /** @type ParserApiConfiguration */ (data);
try {
const api = await processData(data);
const api = await processData(typed);
process.send({
api
});
} catch (cause) {
let m = `AMF parser: Unable to parse API ${data.source}.\n`;
let m = `AMF parser: Unable to parse API ${typed.source}.\n`;
m += cause.s$1 || cause.message;
process.send({ error: m });
}
Expand Down
Loading
Loading