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
18 changes: 17 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"es6": true,
Expand All @@ -9,5 +10,20 @@
"ecmaVersion": 8
},
"extends": ["eslint:recommended", "prettier"],
"ignorePatterns": ["dist", "node_modules", "bin"]
"ignorePatterns": ["dist", "node_modules", "bin"],
"plugins": ["@typescript-eslint", "unused-imports"],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["error"],
"unused-imports/no-unused-imports": "error",
"guard-for-in": "error",
"unused-imports/no-unused-vars": [
"warn",
{
"args": "none",
"varsIgnorePattern": "Test"
}
]
}
}
134 changes: 67 additions & 67 deletions node/async-api-converter/src/genChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,73 @@
const { dataToAsyncSchema, extractChannel } = require("./utils");
const { Channel, Operation, Message, MqttOperationBinding, Server } = require("./definitions");

/**
* Add a new server if it doesn't already exist in the servers object
* @param {[key:string]:Server} servers The AsyncAPI servers map
* @param {string|undefined} newServerUri
* @param {string} protocol
*/
function addServer(servers, newServerUri, newServerProtocol, newOptionalPara) {
if (
newServerUri &&
Object.keys(servers).every(
(asyncServer) =>
servers[asyncServer].url !== newServerUri || servers[asyncServer].protocol !== newServerProtocol
)
) {
let i = 0;
while (Object.keys(servers).some((asyncServer) => asyncServer === i.toString())) {
i++;
}
servers[i.toString()] = new Server(newServerUri, newServerProtocol, newOptionalPara);
}
}

/**
* Check if form is relevant
* @param {object} form One form
*/
function scanPropForm(form, channels, propertyName, payload, servers, interactionInfo, security) {
const hasBase = servers.base !== undefined ? true : false;
const isRelative = form.href && form.href.search("://") === -1 ? true : false;

tryProtocols.forEach((tryProtocol) => {

Check failure

Code scanning / ESLint

Disallow the use of variables before they are defined Error

'tryProtocols' was used before it was defined.
if (form.href && (form.href.startsWith(tryProtocol.id + "://") || (hasBase && isRelative))) {
const opArray = typeof form.op === "string" ? [form.op] : form.op;
if (
opArray.some((op) => interactionInfo.ops.some((intOp) => op === intOp)) ||
tryProtocol.checkForm(form)
) {
const { channel, server } = extractChannel(form.href);
let newOptionalPara = {
security: security,
};
// add server
addServer(servers, server, tryProtocol.id, newOptionalPara);

// add channel
if (!channels[channel]) {
Object.assign(
channels,
new Channel(channel, {
subscribe: new Operation({
tdOp: interactionInfo.tdOp,
opName: propertyName,
ixType: interactionInfo.interaction,
message: new Message({
contentType: form.contentType,
payload,
}),
bindings: tryProtocol.addBinding(form),
}),
})
);
}
}
}
});
}

/**
* Maps TD interactions and their forms to AsyncAPI channels
* by iterating over all TD properties & events forms
Expand Down Expand Up @@ -93,71 +160,4 @@
},
];

/**
* Check if form is relevant
* @param {object} form One form
*/
function scanPropForm(form, channels, propertyName, payload, servers, interactionInfo, security) {
const hasBase = servers.base !== undefined ? true : false;
const isRelative = form.href && form.href.search("://") === -1 ? true : false;

tryProtocols.forEach((tryProtocol) => {
if (form.href && (form.href.startsWith(tryProtocol.id + "://") || (hasBase && isRelative))) {
const opArray = typeof form.op === "string" ? [form.op] : form.op;
if (
opArray.some((op) => interactionInfo.ops.some((intOp) => op === intOp)) ||
tryProtocol.checkForm(form)
) {
const { channel, server } = extractChannel(form.href);
let newOptionalPara = {
security: security,
};
// add server
addServer(servers, server, tryProtocol.id, newOptionalPara);

// add channel
if (!channels[channel]) {
Object.assign(
channels,
new Channel(channel, {
subscribe: new Operation({
tdOp: interactionInfo.tdOp,
opName: propertyName,
ixType: interactionInfo.interaction,
message: new Message({
contentType: form.contentType,
payload,
}),
bindings: tryProtocol.addBinding(form),
}),
})
);
}
}
}
});
}

/**
* Add a new server if it doesn't already exist in the servers object
* @param {[key:string]:Server} servers The AsyncAPI servers map
* @param {string|undefined} newServerUri
* @param {string} protocol
*/
function addServer(servers, newServerUri, newServerProtocol, newOptionalPara) {
if (
newServerUri &&
Object.keys(servers).every(
(asyncServer) =>
servers[asyncServer].url !== newServerUri || servers[asyncServer].protocol !== newServerProtocol
)
) {
let i = 0;
while (Object.keys(servers).some((asyncServer) => asyncServer === i.toString())) {
i++;
}
servers[i.toString()] = new Server(newServerUri, newServerProtocol, newOptionalPara);
}
}

module.exports = genChannels;
Loading
Loading