Clone the first message passed to node.send in Function

Also introduces an optional second arg to node.send in the Function
node that can disable that cloning
This commit is contained in:
Nick O'Leary
2019-09-12 22:08:52 +01:00
parent 89d0d6ec93
commit f0839571d0
2 changed files with 59 additions and 5 deletions

View File

@@ -19,7 +19,7 @@ module.exports = function(RED) {
var util = require("util");
var vm = require("vm");
function sendResults(node,send,_msgid,msgs) {
function sendResults(node,send,_msgid,msgs,cloneFirstMessage) {
if (msgs == null) {
return;
} else if (!util.isArray(msgs)) {
@@ -35,6 +35,10 @@ module.exports = function(RED) {
var msg = msgs[m][n];
if (msg !== null && msg !== undefined) {
if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
if (msgCount === 0 && cloneFirstMessage !== false) {
msgs[m][n] = RED.util.cloneMessage(msgs[m][n]);
msg = msgs[m][n];
}
msg._msgid = _msgid;
msgCount++;
} else {
@@ -80,7 +84,7 @@ module.exports = function(RED) {
"trace:__node__.trace,"+
"on:__node__.on,"+
"status:__node__.status,"+
"send:function(msgs){ __node__.send(__send__,__msgid__,msgs);},"+
"send:function(msgs,cloneMsg){ __node__.send(__send__,__msgid__,msgs,cloneMsg);},"+
"done:__done__"+
"};\n"+
this.func+"\n"+
@@ -114,8 +118,8 @@ module.exports = function(RED) {
trace: function() {
node.trace.apply(node, arguments);
},
send: function(send, id, msgs) {
sendResults(node, send, id, msgs);
send: function(send, id, msgs, cloneMsg) {
sendResults(node, send, id, msgs, cloneMsg);
},
on: function() {
if (arguments[0] === "input") {
@@ -241,7 +245,7 @@ module.exports = function(RED) {
context.done = done;
this.script.runInContext(context);
sendResults(this,send,msg._msgid,context.results);
sendResults(this,send,msg._msgid,context.results,false);
if (handleNodeDoneCall) {
done();
}