2016-02-13 00:18:08 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2016-02-13 00:18:08 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2022-02-25 17:13:39 +01:00
|
|
|
/**
|
|
|
|
* @typedef LinkTarget
|
|
|
|
* @type {object}
|
|
|
|
* @property {string} id - ID of the target node.
|
|
|
|
* @property {string} name - Name of target Node
|
|
|
|
* @property {number} flowId - ID of flow where the target node exists
|
|
|
|
* @property {string} flowName - Name of flow where the target node exists
|
2022-02-28 14:57:22 +01:00
|
|
|
* @property {boolean} isSubFlow - True if the link-in node exists in a subflow instance
|
2022-02-25 17:13:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-02-13 00:18:08 +01:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2021-09-29 11:45:00 +02:00
|
|
|
const crypto = require("crypto");
|
2022-02-28 14:57:22 +01:00
|
|
|
const targetCache = (function () {
|
2022-07-11 11:27:30 +02:00
|
|
|
let registry = { id: {}, name: {} }
|
2022-07-11 11:28:31 +02:00
|
|
|
function getIndex (/** @type {[LinkTarget]} */ targets, id) {
|
2022-02-25 17:13:39 +01:00
|
|
|
for (let index = 0; index < (targets || []).length; index++) {
|
2022-07-11 11:28:31 +02:00
|
|
|
const element = targets[index]
|
2022-02-25 17:13:39 +01:00
|
|
|
if (element.id === id) {
|
2022-07-11 11:28:31 +02:00
|
|
|
return index
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-02-25 17:13:39 +01:00
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
return -1
|
2022-02-25 17:13:39 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Generate a target object from a node
|
2022-07-11 11:28:31 +02:00
|
|
|
* @param {LinkInNode} node
|
2022-02-25 17:13:39 +01:00
|
|
|
* @returns {LinkTarget} a link target object
|
|
|
|
*/
|
2022-07-11 11:28:31 +02:00
|
|
|
function generateTarget (node) {
|
|
|
|
const isSubFlow = node._flow.TYPE === 'subflow'
|
2022-02-25 17:13:39 +01:00
|
|
|
return {
|
2022-02-28 14:57:22 +01:00
|
|
|
id: node.id,
|
|
|
|
name: node.name || node.id,
|
2022-02-25 17:13:39 +01:00
|
|
|
flowId: node._flow.flow.id,
|
2022-02-28 14:57:22 +01:00
|
|
|
flowName: isSubFlow ? node._flow.subflowDef.name : node._flow.flow.label,
|
2022-02-25 19:39:48 +01:00
|
|
|
isSubFlow: isSubFlow
|
2022-02-25 17:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Get a list of targets registerd to this name
|
2022-02-28 14:57:22 +01:00
|
|
|
* @param {string} name Name of the target
|
2022-07-11 11:28:31 +02:00
|
|
|
* @param {boolean} [excludeSubflows] set `true` to exclude
|
2022-02-25 17:13:39 +01:00
|
|
|
* @returns {[LinkTarget]} Targets registerd to this name.
|
|
|
|
*/
|
2022-07-11 11:28:31 +02:00
|
|
|
getTargets (name, excludeSubflows) {
|
|
|
|
const targets = registry.name[name] || []
|
2022-02-28 14:57:22 +01:00
|
|
|
if (excludeSubflows) {
|
2022-07-11 11:28:31 +02:00
|
|
|
return targets.filter(e => e.isSubFlow !== true)
|
2022-02-28 14:57:22 +01:00
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
return targets
|
2022-02-24 20:46:21 +01:00
|
|
|
},
|
2022-02-25 17:13:39 +01:00
|
|
|
/**
|
|
|
|
* Get a single target by registered name.
|
|
|
|
* To restrict to a single flow, include the `flowId`
|
|
|
|
* If there is no targets OR more than one target, null is returned
|
|
|
|
* @param {string} name Name of the node
|
2022-07-11 11:28:31 +02:00
|
|
|
* @param {string} [flowId]
|
2022-02-25 17:13:39 +01:00
|
|
|
* @returns {LinkTarget} target
|
|
|
|
*/
|
2022-07-11 11:28:31 +02:00
|
|
|
getTarget (name, flowId) {
|
|
|
|
/** @type {[LinkTarget]} */
|
|
|
|
let possibleTargets = this.getTargets(name)
|
|
|
|
/** @type {LinkTarget} */
|
|
|
|
let target
|
2022-02-28 14:57:22 +01:00
|
|
|
if (possibleTargets.length && flowId) {
|
2022-07-11 11:28:31 +02:00
|
|
|
possibleTargets = possibleTargets.filter(e => e.flowId === flowId)
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-02-28 14:57:22 +01:00
|
|
|
if (possibleTargets.length === 1) {
|
2022-07-11 11:28:31 +02:00
|
|
|
target = possibleTargets[0]
|
2022-02-28 14:57:22 +01:00
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
return target
|
2022-02-24 20:46:21 +01:00
|
|
|
},
|
2022-02-25 17:13:39 +01:00
|
|
|
/**
|
|
|
|
* Get a target by node ID
|
|
|
|
* @param {string} nodeId ID of the node
|
|
|
|
* @returns {LinkTarget} target
|
|
|
|
*/
|
2022-07-11 11:28:31 +02:00
|
|
|
getTargetById (nodeId) {
|
|
|
|
return registry.id[nodeId]
|
2022-02-28 14:57:22 +01:00
|
|
|
},
|
2022-07-11 11:28:31 +02:00
|
|
|
register (/** @type {LinkInNode} */ node) {
|
|
|
|
const target = generateTarget(node)
|
|
|
|
const tByName = this.getTarget(target.name, target.flowId)
|
2022-02-28 14:57:22 +01:00
|
|
|
if (!tByName || tByName.id !== target.id) {
|
2022-07-11 11:28:31 +02:00
|
|
|
registry.name[target.name] = registry.name[target.name] || []
|
2022-02-28 14:57:22 +01:00
|
|
|
registry.name[target.name].push(target)
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
registry.id[target.id] = target
|
|
|
|
return target
|
2022-02-24 20:46:21 +01:00
|
|
|
},
|
2022-07-11 11:28:31 +02:00
|
|
|
remove (node) {
|
|
|
|
const target = generateTarget(node)
|
|
|
|
const targs = this.getTargets(target.name)
|
|
|
|
const idx = getIndex(targs, target.id)
|
2022-06-20 19:25:41 +02:00
|
|
|
if (idx > -1) {
|
2022-07-11 11:28:31 +02:00
|
|
|
targs.splice(idx, 1)
|
2022-06-20 19:25:41 +02:00
|
|
|
}
|
|
|
|
if (targs.length === 0) {
|
2022-07-11 11:28:17 +02:00
|
|
|
delete registry.name[target.name]
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
delete registry.id[target.id]
|
2022-02-24 20:46:21 +01:00
|
|
|
},
|
2022-07-11 11:28:31 +02:00
|
|
|
clear () {
|
|
|
|
registry = { id: {}, name: {} }
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-11 11:28:31 +02:00
|
|
|
})()
|
2021-09-29 11:45:00 +02:00
|
|
|
|
2016-02-13 00:18:08 +01:00
|
|
|
function LinkInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
2016-05-17 10:16:58 +02:00
|
|
|
var event = "node:"+n.id;
|
|
|
|
var handler = function(msg) {
|
|
|
|
msg._event = n.event;
|
|
|
|
node.receive(msg);
|
2016-02-13 00:18:08 +01:00
|
|
|
}
|
2022-02-25 17:13:39 +01:00
|
|
|
targetCache.register(node);
|
2016-05-17 10:16:58 +02:00
|
|
|
RED.events.on(event,handler);
|
2019-11-29 01:45:12 +01:00
|
|
|
this.on("input", function(msg, send, done) {
|
|
|
|
send(msg);
|
|
|
|
done();
|
2016-05-17 10:16:58 +02:00
|
|
|
});
|
|
|
|
this.on("close",function() {
|
2022-02-24 20:46:21 +01:00
|
|
|
targetCache.remove(node);
|
2016-05-17 10:16:58 +02:00
|
|
|
RED.events.removeListener(event,handler);
|
|
|
|
});
|
2016-02-13 00:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RED.nodes.registerType("link in",LinkInNode);
|
|
|
|
|
|
|
|
function LinkOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
var node = this;
|
2021-09-29 11:45:00 +02:00
|
|
|
var mode = n.mode || "link";
|
|
|
|
|
2016-05-17 10:16:58 +02:00
|
|
|
var event = "node:"+n.id;
|
2019-11-29 01:45:12 +01:00
|
|
|
this.on("input", function(msg, send, done) {
|
2016-05-17 10:16:58 +02:00
|
|
|
msg._event = event;
|
|
|
|
RED.events.emit(event,msg)
|
2021-09-29 11:45:00 +02:00
|
|
|
|
|
|
|
if (mode === "return") {
|
|
|
|
if (Array.isArray(msg._linkSource) && msg._linkSource.length > 0) {
|
|
|
|
var messageEvent = msg._linkSource.pop();
|
|
|
|
var returnNode = RED.nodes.getNode(messageEvent.node);
|
|
|
|
if (returnNode && returnNode.returnLinkMessage) {
|
|
|
|
returnNode.returnLinkMessage(messageEvent.id, msg);
|
|
|
|
} else {
|
2021-09-29 14:49:55 +02:00
|
|
|
node.warn(RED._("link.error.missingReturn"))
|
2021-09-29 11:45:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-29 14:49:55 +02:00
|
|
|
node.warn(RED._("link.error.missingReturn"))
|
2021-09-29 11:45:00 +02:00
|
|
|
}
|
|
|
|
done();
|
|
|
|
} else if (mode === "link") {
|
|
|
|
send(msg);
|
|
|
|
done();
|
|
|
|
}
|
2016-05-17 10:16:58 +02:00
|
|
|
});
|
2016-02-13 00:18:08 +01:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("link out",LinkOutNode);
|
2021-09-29 11:45:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
function LinkCallNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
const node = this;
|
2022-02-25 19:39:48 +01:00
|
|
|
const staticTarget = typeof n.links === "string" ? n.links : n.links[0];
|
2022-02-24 20:46:21 +01:00
|
|
|
const linkType = n.linkType;
|
2021-09-29 11:45:00 +02:00
|
|
|
const messageEvents = {};
|
2022-02-24 20:46:21 +01:00
|
|
|
|
|
|
|
let timeout = parseFloat(n.timeout || "30") * 1000;
|
2021-09-29 15:28:12 +02:00
|
|
|
if (isNaN(timeout)) {
|
|
|
|
timeout = 30000;
|
|
|
|
}
|
2022-02-28 14:57:22 +01:00
|
|
|
function getTargetNode(msg) {
|
|
|
|
const dynamicMode = linkType === "dynamic";
|
|
|
|
const target = dynamicMode ? msg.target : staticTarget
|
|
|
|
|
|
|
|
////1st see if the target is a direct node id
|
|
|
|
let foundNode;
|
|
|
|
if (targetCache.getTargetById(target)) {
|
|
|
|
foundNode = RED.nodes.getNode(target)
|
|
|
|
}
|
|
|
|
if (target && !foundNode && dynamicMode) {
|
|
|
|
//next, look in **this flow only** for the node
|
2022-02-25 17:13:39 +01:00
|
|
|
let cachedTarget = targetCache.getTarget(target, node._flow.flow.id);
|
2022-02-28 14:57:22 +01:00
|
|
|
if (!cachedTarget) {
|
|
|
|
//single target node not found in registry!
|
|
|
|
//get all possible targets from regular flows (exclude subflow instances)
|
|
|
|
const possibleTargets = targetCache.getTargets(target, true);
|
|
|
|
if (possibleTargets.length === 1) {
|
|
|
|
//only 1 link-in found with this name - good, lets use it
|
2022-02-25 17:13:39 +01:00
|
|
|
cachedTarget = possibleTargets[0];
|
|
|
|
} else if (possibleTargets.length > 1) {
|
2022-02-28 14:57:22 +01:00
|
|
|
//more than 1 link-in has this name, raise an error
|
2022-02-25 17:13:39 +01:00
|
|
|
throw new Error(`Multiple link-in nodes named '${target}' found`);
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:13:39 +01:00
|
|
|
if (cachedTarget) {
|
|
|
|
foundNode = RED.nodes.getNode(cachedTarget.id);
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 14:57:22 +01:00
|
|
|
if (foundNode instanceof LinkInNode) {
|
2022-02-25 17:13:39 +01:00
|
|
|
return foundNode;
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-02-28 14:57:22 +01:00
|
|
|
throw new Error(`target link-in node '${target || ""}' not found`);
|
2022-02-24 20:46:21 +01:00
|
|
|
}
|
2022-02-28 14:57:22 +01:00
|
|
|
this.on("input", function (msg, send, done) {
|
2022-02-24 20:46:21 +01:00
|
|
|
try {
|
2022-02-28 14:57:22 +01:00
|
|
|
const targetNode = getTargetNode(msg);
|
|
|
|
if (targetNode instanceof LinkInNode) {
|
2022-02-24 20:46:21 +01:00
|
|
|
msg._linkSource = msg._linkSource || [];
|
|
|
|
const messageEvent = {
|
|
|
|
id: crypto.randomBytes(14).toString('hex'),
|
|
|
|
node: node.id,
|
2022-02-28 14:57:22 +01:00
|
|
|
}
|
2022-02-24 20:46:21 +01:00
|
|
|
messageEvents[messageEvent.id] = {
|
|
|
|
msg: RED.util.cloneMessage(msg),
|
|
|
|
send,
|
|
|
|
done,
|
2022-02-28 14:57:22 +01:00
|
|
|
ts: setTimeout(function () {
|
2022-02-24 20:46:21 +01:00
|
|
|
timeoutMessage(messageEvent.id)
|
2022-02-28 14:57:22 +01:00
|
|
|
}, timeout)
|
2022-02-24 20:46:21 +01:00
|
|
|
};
|
|
|
|
msg._linkSource.push(messageEvent);
|
|
|
|
targetNode.receive(msg);
|
2022-02-28 14:57:22 +01:00
|
|
|
}
|
2022-02-24 20:46:21 +01:00
|
|
|
} catch (error) {
|
|
|
|
node.error(error, msg);
|
2021-09-29 11:45:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.returnLinkMessage = function(eventId, msg) {
|
|
|
|
if (Array.isArray(msg._linkSource) && msg._linkSource.length === 0) {
|
|
|
|
delete msg._linkSource;
|
|
|
|
}
|
|
|
|
const messageEvent = messageEvents[eventId];
|
|
|
|
if (messageEvent) {
|
2021-09-29 15:28:12 +02:00
|
|
|
clearTimeout(messageEvent.ts);
|
2021-09-29 11:45:00 +02:00
|
|
|
delete messageEvents[eventId];
|
|
|
|
messageEvent.send(msg);
|
|
|
|
messageEvent.done();
|
|
|
|
} else {
|
2021-09-29 14:49:55 +02:00
|
|
|
node.send(msg);
|
2021-09-29 11:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 15:28:12 +02:00
|
|
|
|
|
|
|
function timeoutMessage(eventId) {
|
|
|
|
const messageEvent = messageEvents[eventId];
|
|
|
|
if (messageEvent) {
|
|
|
|
delete messageEvents[eventId];
|
|
|
|
node.error("timeout",messageEvent.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 11:45:00 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("link call",LinkCallNode);
|
|
|
|
|
|
|
|
|
2016-02-13 00:18:08 +01:00
|
|
|
}
|