Merge pull request #3463 from Steve-Mcl/dynamic-link-call

Dynamic link call
This commit is contained in:
Nick O'Leary 2022-03-23 10:14:48 +00:00 committed by GitHub
commit 37f0e36c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 448 additions and 79 deletions

View File

@ -32,8 +32,17 @@
<label for="node-input-timeout"><span data-i18n="exec.label.timeout"></span></label>
<input type="text" id="node-input-timeout" placeholder="30" style="width: 70px; margin-right: 5px;"><span data-i18n="inject.seconds"></span>
</div>
<div style="position:relative; height: 30px; text-align: right;"><div style="display:inline-block"><input type="text" id="node-input-link-target-filter"></div></div>
<div class="form-row node-input-link-row"></div>
<div class="form-row">
<label for="node-input-linkType" data-i18n="link.linkCallType"></label>
<select id="node-input-linkType" style="width: 70%">
<option value="static" data-i18n="link.staticLinkCall"></option>
<option value="dynamic" data-i18n="link.dynamicLinkCall"></option>
</select>
</div>
<div class="link-call-target-tree" style="position:relative; height: 30px; text-align: right;">
<div style="display:inline-block"><input type="text" id="node-input-link-target-filter"></div>
</div>
<div class="form-row node-input-link-row link-call-target-tree"></div>
</script>
<script type="text/javascript">
@ -261,6 +270,7 @@
defaults: {
name: {value:""},
links: { value: [], type:"link in[]"},
linkType: {value:"static"},
timeout: { value: "30", validate:RED.validators.number(true) }
},
inputs: 1,
@ -273,7 +283,9 @@
if (this.name) {
return this.name;
}
if (this.links.length > 0) {
if (this.linkType === "dynamic") {
return this._("link.dynamicLinkLabel");
} else if (this.links.length > 0) {
var targetNode = RED.nodes.node(this.links[0]);
return targetNode && (targetNode.name || this._("link.linkCall"));
}
@ -283,6 +295,22 @@
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
console.log("link call oneditprepare")
const updateVisibility = function() {
const static = $('#node-input-linkType').val() !== "dynamic";
if(static) {
$("div.link-call-target-tree").show();
} else {
$("div.link-call-target-tree").hide();
}
}
$("#node-input-linkType").on("change",function(d){
updateVisibility();
});
if (["static","dynamic"].indexOf(this.linkType) < 0) {
$("#node-input-linkType").val('static');
}
updateVisibility();
onEditPrepare(this,"link in");
},
oneditsave: function() {

View File

@ -14,10 +14,119 @@
* limitations under the License.
**/
/**
* @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
* @property {boolean} isSubFlow - True if the link-in node exists in a subflow instance
*/
module.exports = function(RED) {
"use strict";
const crypto = require("crypto");
const targetCache = (function () {
const registry = { id: {}, name: {} };
function getIndex(/** @type {[LinkTarget]}*/ targets, id) {
for (let index = 0; index < (targets || []).length; index++) {
const element = targets[index];
if (element.id === id) {
return index;
}
}
return -1;
}
/**
* Generate a target object from a node
* @param {LinkInNode} node
* @returns {LinkTarget} a link target object
*/
function generateTarget(node) {
const isSubFlow = node._flow.TYPE === "subflow";
return {
id: node.id,
name: node.name || node.id,
flowId: node._flow.flow.id,
flowName: isSubFlow ? node._flow.subflowDef.name : node._flow.flow.label,
isSubFlow: isSubFlow
}
}
return {
/**
* Get a list of targets registerd to this name
* @param {string} name Name of the target
* @param {boolean} [excludeSubflows] set `true` to exclude
* @returns {[LinkTarget]} Targets registerd to this name.
*/
getTargets(name, excludeSubflows) {
const targets = registry.name[name] || [];
if (excludeSubflows) {
return targets.filter(e => e.isSubFlow != true);
}
return targets;
},
/**
* 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
* @param {string} [flowId]
* @returns {LinkTarget} target
*/
getTarget(name, flowId) {
/** @type {[LinkTarget]}*/
let possibleTargets = this.getTargets(name);
/** @type {LinkTarget}*/
let target;
if (possibleTargets.length && flowId) {
possibleTargets = possibleTargets.filter(e => e.flowId == flowId);
}
if (possibleTargets.length === 1) {
target = possibleTargets[0];
}
return target;
},
/**
* Get a target by node ID
* @param {string} nodeId ID of the node
* @returns {LinkTarget} target
*/
getTargetById(nodeId) {
return registry.id[nodeId];
},
register(/** @type {LinkInNode} */ node) {
const target = generateTarget(node);
const tByName = this.getTarget(target.name, target.flowId);
if (!tByName || tByName.id !== target.id) {
registry.name[target.name] = registry.name[target.name] || [];
registry.name[target.name].push(target)
}
registry.id[target.id] = target;
return target;
},
remove(node) {
const target = generateTarget(node);
const tn = this.getTarget(target.name, target.flowId);
if (tn) {
const targs = this.getTargets(tn.name);
const idx = getIndex(targs, tn.id);
if (idx > -1) {
targs.splice(idx, 1);
}
if (targs.length === 0) {
delete registry.name[tn.name];
}
}
delete registry.id[target.id];
},
clear() {
registry = { id: {}, name: {} };
}
}
})();
function LinkInNode(n) {
RED.nodes.createNode(this,n);
@ -27,12 +136,14 @@ module.exports = function(RED) {
msg._event = n.event;
node.receive(msg);
}
targetCache.register(node);
RED.events.on(event,handler);
this.on("input", function(msg, send, done) {
send(msg);
done();
});
this.on("close",function() {
targetCache.remove(node);
RED.events.removeListener(event,handler);
});
}
@ -74,31 +185,69 @@ module.exports = function(RED) {
function LinkCallNode(n) {
RED.nodes.createNode(this,n);
const node = this;
const target = n.links[0];
const staticTarget = typeof n.links === "string" ? n.links : n.links[0];
const linkType = n.linkType;
const messageEvents = {};
let timeout = parseFloat(n.timeout || "30")*1000;
let timeout = parseFloat(n.timeout || "30") * 1000;
if (isNaN(timeout)) {
timeout = 30000;
}
function getTargetNode(msg) {
const dynamicMode = linkType === "dynamic";
const target = dynamicMode ? msg.target : staticTarget
this.on("input", function(msg, send, done) {
msg._linkSource = msg._linkSource || [];
const messageEvent = {
id: crypto.randomBytes(14).toString('hex'),
node: node.id,
////1st see if the target is a direct node id
let foundNode;
if (targetCache.getTargetById(target)) {
foundNode = RED.nodes.getNode(target)
}
messageEvents[messageEvent.id] = {
msg: RED.util.cloneMessage(msg),
send,
done,
ts: setTimeout(function() {
timeoutMessage(messageEvent.id)
}, timeout )
};
msg._linkSource.push(messageEvent);
var targetNode = RED.nodes.getNode(target);
if (targetNode) {
targetNode.receive(msg);
if (target && !foundNode && dynamicMode) {
//next, look in **this flow only** for the node
let cachedTarget = targetCache.getTarget(target, node._flow.flow.id);
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
cachedTarget = possibleTargets[0];
} else if (possibleTargets.length > 1) {
//more than 1 link-in has this name, raise an error
throw new Error(`Multiple link-in nodes named '${target}' found`);
}
}
if (cachedTarget) {
foundNode = RED.nodes.getNode(cachedTarget.id);
}
}
if (foundNode instanceof LinkInNode) {
return foundNode;
}
throw new Error(`target link-in node '${target || ""}' not found`);
}
this.on("input", function (msg, send, done) {
try {
const targetNode = getTargetNode(msg);
if (targetNode instanceof LinkInNode) {
msg._linkSource = msg._linkSource || [];
const messageEvent = {
id: crypto.randomBytes(14).toString('hex'),
node: node.id,
}
messageEvents[messageEvent.id] = {
msg: RED.util.cloneMessage(msg),
send,
done,
ts: setTimeout(function () {
timeoutMessage(messageEvent.id)
}, timeout)
};
msg._linkSource.push(messageEvent);
targetNode.receive(msg);
}
} catch (error) {
node.error(error, msg);
}
});

View File

@ -38,13 +38,28 @@
</script>
<script type="text/html" data-help-name="link call">
<p>Calls a flow that starts with a <code>link in</code> node and passes on the response.</p>
<h3>Details</h3>
<p>This node can be connected to a <code>link in</code> node that exists on any tab.
The flow connected to that node must end with a <code>link out</code> node configured
in 'return' mode.</p>
<p>When this node receives a message, it is passed to the connected <code>link in</code> node.
It then waits for a response which it then sends on.</o>
<p>If no response is received within the configured timeout, default 30 seconds, the node
will log an error that can be caught using the <code>catch</code> node.</p>
<p>Calls a flow that starts with a <code>link in</code> node and passes on the response.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt class="optional">target<span class="property-type">string</span></dt>
<dd>When the option <b>Link Type</b> is set to "Dynamic target", set <code>msg.target</code> to the name of the
<code>link in</code> node you wish to call.</dd>
</dl>
<h3>Details</h3>
<p>This node can be connected to a <code>link in</code> node that exists on any tab.
The flow connected to that node must end with a <code>link out</code> node configured
in 'return' mode.</p>
<p>When this node receives a message, it is passed to the connected <code>link in</code> node.
It then waits for a response which it then sends on.</p>
<p>If no response is received within the configured timeout, default 30 seconds, the node
will log an error that can be caught using the <code>catch</code> node.</p>
<p>When the option <b>Link Type</b> is set to "Dynamic target" <code>msg.target</code> can be used to call a
<code>link in</code> by name. The target <code>link in</code> node must be named.
<ul>
<li>If there are 2 <code>link in</code> nodes with the same name, an error will be raised</li>
<li>A <code>link call</code> cannot call a <code>link in</code> node inside a subflow</li>
</ul>
</p>
The flow connected to that node must end with a <code>link out</code> node configured
in 'return' mode.</p>
</script>

View File

@ -170,6 +170,10 @@
"outMode": "Mode",
"sendToAll": "Send to all connected link nodes",
"returnToCaller": "Return to calling link node",
"linkCallType": "Link Type",
"staticLinkCall": "Fixed target",
"dynamicLinkCall": "Dynamic target (msg.target)",
"dynamicLinkLabel": "Dynamic",
"error": {
"missingReturn": "Missing return node information"
}

View File

@ -120,68 +120,241 @@ describe('link Node', function() {
});
describe("link-call node", function() {
it('should call link-in node and get response', function(done) {
var flow = [{id:"link-in-1", type:"link in", wires: [[ "func"]]},
{id:"func", type:"helper", wires: [["link-out-1"]]},
{id:"link-out-1", type:"link out", mode: "return"},
{id:"link-call", type:"link call", links:["link-in-1"], wires:[["n4"]]},
{id:"n4", type:"helper"} ];
helper.load(linkNode, flow, function() {
it('should call static link-in node and get response', function (done) {
var flow = [{ id: "link-in-1", type: "link in", wires: [["func"]] },
{ id: "func", type: "helper", wires: [["link-out-1"]] },
{ id: "link-out-1", type: "link out", mode: "return" },
{ id: "link-call", type: "link call", links: ["link-in-1"], wires: [["n4"]] },
{ id: "n4", type: "helper" }];
helper.load(linkNode, flow, function () {
var func = helper.getNode("func");
func.on("input", function(msg, send, done) {
func.on("input", function (msg, send, done) {
msg.payload = "123";
send(msg);
done();
})
var n1 = helper.getNode("link-call");
var n4 = helper.getNode("n4");
n4.on("input", function(msg) {
n4.on("input", function (msg) {
try {
msg.should.have.property('payload', '123');
done();
} catch (err) {
done(err);
}
});
n1.receive({ payload: "hello" });
});
})
it('should call link-in node by name and get response', function (done) {
this.timeout(500);
var payload = Date.now();
var flow = [
{ id: "tab-flow-1", type: "tab", label: "Flow 1" },
{ id: "tab-flow-2", type: "tab", label: "Flow 2" },
{ id: "link-in-1", z: "tab-flow-1", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "link-in-2", z: "tab-flow-2", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "func", z: "tab-flow-1", type: "helper", wires: [["link-out-1"]] },
{ id: "link-out-1", z: "tab-flow-1", type: "link out", mode: "return" },
{ id: "link-call", z: "tab-flow-1", type: "link call", linkType: "dynamic", links: [], wires: [["n4"]] },
{ id: "n4", z: "tab-flow-1", type: "helper" }
];
helper.load(linkNode, flow, function () {
var func = helper.getNode("func");
func.on("input", function (msg, send, done) {
msg.payload += msg.payload;
send(msg);
done();
})
var n1 = helper.getNode("link-call");
var n4 = helper.getNode("n4");
n4.on("input", function (msg) {
try {
msg.should.have.property('payload');
msg.payload.should.eql(payload + payload);
done();
} catch (err) {
done(err);
}
});
n1.receive({ payload: payload, target: "double payload" });
});
})
// //TODO: This test is DISABLED - helper.load() calls callback but none of the nodes are available (issue loading a flow with a subflow)
// it('should call link-in node by name in subflow', function (done) {
// this.timeout(9999500);
// var payload = Date.now();
// var flow = [
// {"id":"sub-flow-template","type":"subflow","name":"Subflow","info":"","category":"","in":[{"wires":[{"id":"link-call-1"}]}],"out":[{"wires":[{"id":"link-call-1","port":0}]}]},
// {"id":"link-call-1","type":"link call","z":"sub-flow-template","name":"","links":[],"linkType":"dynamic","timeout":"5","wires":[[]]},
// {"id":"link-in-1","type":"link in","z":"sub-flow-template","name":"double payload","links":[],"wires":[["func"]]},
// {"id":"func","type":"function","z":"sub-flow-template","name":"payload.a x payload.b","func":"msg.payload += msg.payload\nreturn msg;\n","outputs":1,"wires":[["link-out-1"]]},
// {"id":"link-out-1","type":"link out","z":"sub-flow-template","name":"","mode":"return","links":[],"wires":[]},
// {"id":"sub-flow-1","type":"subflow:sub-flow-template","z":"tab-flow-1","name":"","wires":[["n4"]]},
// { id: "n4", z: "tab-flow-1", type: "helper" }
// ];
// helper.load(linkNode, flow, function () {
// var sf = helper.getNode("sub-flow-1");
// var func = helper.getNode("func");
// var n4 = helper.getNode("n4");
// func.on("input", function (msg, send, done) {
// msg.payload += msg.payload;
// send(msg);
// done();
// })
// n4.on("input", function (msg) {
// try {
// msg.should.have.property('payload');
// msg.payload.should.eql(payload + payload);
// done();
// } catch (err) {
// done(err);
// }
// });
// sf.receive({ payload: payload, target: "double payload" });
// });
// })
it('should timeout waiting for link return', function (done) {
this.timeout(1000);
const flow = [
{ id: "tab-flow-1", type: "tab", label: "Flow 1" },
{ id: "link-in-1", z: "tab-flow-1", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "func", z: "tab-flow-1", type: "helper", wires: [["link-out-1"]] },
{ id: "link-out-1", z: "tab-flow-1", type: "link out", mode: "" }, //not return mode, cause link-call timeout
{ id: "link-call", z: "tab-flow-1", type: "link call", linkType: "static", "timeout": "0.5", links: ["link-in-1"], wires: [["n4"]] },
{ id: "catch-all", z: "tab-flow-1", type: "catch", scope: ["link-call"], uncaught: true, wires: [["n4"]] },
{ id: "n4", z: "tab-flow-1", type: "helper" }
];
helper.load(linkNode, flow, function () {
const funcNode = helper.getNode("func");
const linkCallNode = helper.getNode("link-call");
const helperNode = helper.getNode("n4");
funcNode.on("input", function (msg, send, done) {
msg.payload += msg.payload;
send(msg);
done();
})
helperNode.on("input", function (msg) {
try {
msg.should.have.property("target", "double payload");
msg.should.have.property("error");
msg.error.should.have.property("message", "timeout");
msg.error.should.have.property("source");
done();
} catch (err) {
done(err);
}
});
linkCallNode.receive({ payload: "hello", target: "double payload" });
});
})
it('should raise error due to multiple targets on same tab', function (done) {
this.timeout(55500);
const flow = [
{ id: "tab-flow-1", type: "tab", label: "Flow 1" },
{ id: "link-in-1", z: "tab-flow-1", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "link-in-2", z: "tab-flow-1", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "func", z: "tab-flow-1", type: "helper", wires: [["link-out-1"]] },
{ id: "link-out-1", z: "tab-flow-1", type: "link out", mode: "return" },
{ id: "link-call", z: "tab-flow-1", type: "link call", linkType: "dynamic", links: [], wires: [["n4"]] },
{ id: "catch-all", z: "tab-flow-1", type: "catch", scope: ["link-call"], uncaught: true, wires: [["n4"]] },
{ id: "n4", z: "tab-flow-1", type: "helper" }
];
helper.load(linkNode, flow, function () {
const funcNode = helper.getNode("func");
const linkCall = helper.getNode("link-call");
const helperNode = helper.getNode("n4");
funcNode.on("input", function (msg, send, _done) {
done(new Error("Function should not be called"))
})
helperNode.on("input", function (msg) {
try {
msg.should.have.property("target", "double payload");
msg.should.have.property("error");
msg.error.should.have.property("message");
msg.error.message.should.match(/.*Multiple link-in nodes.*/)
msg.error.should.have.property("source");
done();
} catch (err) {
done(err);
}
});
linkCall.receive({ payload: "hello", target: "double payload" });
});
})
it('should raise error due to multiple targets on different tabs', function (done) {
this.timeout(500);
const flow = [
{ id: "tab-flow-1", type: "tab", label: "Flow 1" },
{ id: "tab-flow-2", type: "tab", label: "Flow 2" },
{ id: "tab-flow-3", type: "tab", label: "Flow 3" },
{ id: "link-in-1", z: "tab-flow-2", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "link-in-2", z: "tab-flow-3", type: "link in", name: "double payload", wires: [["func"]] },
{ id: "func", z: "tab-flow-1", type: "helper", wires: [["link-out-1"]] },
{ id: "link-out-1", z: "tab-flow-1", type: "link out", mode: "return" },
{ id: "link-call", z: "tab-flow-1", type: "link call", linkType: "dynamic", links: [], wires: [["n4"]] },
{ id: "catch-all", z: "tab-flow-1", type: "catch", scope: ["link-call"], uncaught: true, wires: [["n4"]] },
{ id: "n4", z: "tab-flow-1", type: "helper" }
];
helper.load(linkNode, flow, function () {
const funcNode = helper.getNode("func");
const linkCall = helper.getNode("link-call");
const helperNode = helper.getNode("n4");
funcNode.on("input", function (msg, send, _done) {
done(new Error("Function should not be called"))
})
helperNode.on("input", function (msg) {
try {
msg.should.have.property("target", "double payload");
msg.should.have.property("error");
msg.error.should.have.property("message");
msg.error.message.should.match(/.*Multiple link-in nodes.*/)
msg.error.should.have.property("source");
done();
} catch (err) {
done(err);
}
});
linkCall.receive({ payload: "hello", target: "double payload" });
});
})
it('should allow nested link-call flows', function(done) {
this.timeout(500);
var flow = [/** Multiply by 2 link flow **/
{id:"li1", type:"link in", wires: [[ "m2"]]},
{id:"m2", type:"helper", wires: [["lo1"]]},
{id:"lo1", type:"link out", mode: "return"},
/** Multiply by 3 link flow **/
{id:"li2", type:"link in", wires: [[ "m3"]]},
{id:"m3", type:"helper", wires: [["lo2"]]},
{id:"lo2", type:"link out", mode: "return"},
/** Multiply by 6 link flow **/
{id:"li3", type:"link in", wires: [[ "link-call-1"]]},
{id:"link-call-1", type:"link call", links:["li1"], wires:[["link-call-2"]]},
{id:"link-call-2", type:"link call", links:["li2"], wires:[["lo3"]]},
{id:"lo3", type:"link out", mode: "return"},
/** Test Flow Entry **/
{id:"link-call", type:"link call", links:["li3"], wires:[["n4"]]},
{id:"n4", type:"helper"} ];
helper.load(linkNode, flow, function() {
var m2 = helper.getNode("m2");
m2.on("input", function(msg, send, done) { msg.payload *= 2 ; send(msg); done(); })
var m3 = helper.getNode("m3");
m3.on("input", function(msg, send, done) { msg.payload *= 3 ; send(msg); done(); })
var n1 = helper.getNode("link-call");
var n4 = helper.getNode("n4");
n4.on("input", function(msg) {
try {
msg.should.have.property('payload', 24);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:"hello"});
n1.receive({payload:4});
});
})
});
it('should allow nested link-call flows', function(done) {
var flow = [/** Multiply by 2 link flow **/
{id:"li1", type:"link in", wires: [[ "m2"]]},
{id:"m2", type:"helper", wires: [["lo1"]]},
{id:"lo1", type:"link out", mode: "return"},
/** Multiply by 3 link flow **/
{id:"li2", type:"link in", wires: [[ "m3"]]},
{id:"m3", type:"helper", wires: [["lo2"]]},
{id:"lo2", type:"link out", mode: "return"},
/** Multiply by 6 link flow **/
{id:"li3", type:"link in", wires: [[ "link-call-1"]]},
{id:"link-call-1", type:"link call", links:["m2"], wires:[["link-call-2"]]},
{id:"link-call-2", type:"link call", links:["m3"], wires:[["lo3"]]},
{id:"lo3", type:"link out", mode: "return"},
/** Test Flow Entry **/
{id:"link-call", type:"link call", links:["li3"], wires:[["n4"]]},
{id:"n4", type:"helper"} ];
helper.load(linkNode, flow, function() {
var m2 = helper.getNode("m2");
m2.on("input", function(msg, send, done) { msg.payload *= 2 ; send(msg); done(); })
var m3 = helper.getNode("m3");
m3.on("input", function(msg, send, done) { msg.payload *= 3 ; send(msg); done(); })
var n1 = helper.getNode("link-call");
var n4 = helper.getNode("n4");
n4.on("input", function(msg) {
try {
msg.should.have.property('payload', 24);
done();
} catch(err) {
done(err);
}
});
n1.receive({payload:4});
});
})
});