expose send, status, log, warn, error, debug, trace, name, id in setup code of function node

This commit is contained in:
cinhcet 2020-07-08 01:30:29 +02:00
parent f72903ccc2
commit 5d7a625883
1 changed files with 20 additions and 1 deletions

View File

@ -25,6 +25,7 @@ module.exports = function(RED) {
} else if (!util.isArray(msgs)) {
msgs = [msgs];
}
if(_msgid === "") _msgid = RED.util.generateId();
var msgCount = 0;
for (var m=0; m<msgs.length; m++) {
if (msgs[m]) {
@ -268,7 +269,23 @@ module.exports = function(RED) {
var iniScript = null;
var iniOpt = null;
if (node.ini && (node.ini !== "")) {
var iniText = "(async function () {\n"+node.ini +"\n})();";
var iniText = `
(async function(__send__) {
var node = {
id:__node__.id,
name:__node__.name,
log:__node__.log,
error:__node__.error,
warn:__node__.warn,
debug:__node__.debug,
trace:__node__.trace,
status:__node__.status,
send: function(msgs, cloneMsg) {
__node__.send(__send__, "", msgs, cloneMsg);
}
};
`+ node.ini +`
})(send);`;
iniOpt = createVMOpt(node, " setup");
iniScript = new vm.Script(iniText, iniOpt);
}
@ -280,7 +297,9 @@ module.exports = function(RED) {
}
var promise = Promise.resolve();
if (iniScript) {
context.send = function(m) { node.send(m); };
promise = iniScript.runInContext(context, iniOpt);
delete context.send;
}
function processMessage(msg, send, done) {