From 2eba7548013a0bfba56369abc1bc58f61f61d3ca Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 12 Oct 2024 16:49:09 +0100 Subject: [PATCH 1/6] Fix trigger node date handling for latest time type input to fix #4914 --- .../@node-red/nodes/core/function/89-trigger.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js index 16a00e99d..0fc6ad6fb 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js @@ -166,7 +166,13 @@ module.exports = function(RED) { if (err) { reject(err); } else { - msg.payload = value; + if (node.op1type === "date") { + let d = new Date(); + if (node.op1 == "1") { msg.payload = Date.now(); } + else if (node.op1 == "iso") { msg.payload = d.toISOString(); } + else { msg.payload = d; } + } + else { msg.payload = value; } resolve(); } }); @@ -213,7 +219,12 @@ module.exports = function(RED) { } else { msg2.payload = node.topics[topic].m2; - if (node.op2type === "date") { msg2.payload = Date.now(); } + if (node.op2type === "date") { + let d = new Date(); + if (node.op2 == "0") { msg2.payload = Date.now(); } + else if (node.op2 == "iso") { msg2.payload = d.toISOString(); } + else { msg2.payload = d; } + } if (node.second === true) { msgInfo.send([null,msg2]); } else { msgInfo.send(msg2); } } From 61fd01b8716e62d5f26fbcc1d56f47ca6d05db5d Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 12 Oct 2024 17:09:18 +0100 Subject: [PATCH 2/6] And add some tests --- test/nodes/core/function/89-trigger_spec.js | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/nodes/core/function/89-trigger_spec.js b/test/nodes/core/function/89-trigger_spec.js index 33401e540..e86ed7a31 100644 --- a/test/nodes/core/function/89-trigger_spec.js +++ b/test/nodes/core/function/89-trigger_spec.js @@ -111,7 +111,15 @@ describe('trigger node', function() { try { if (rval) { msg.should.have.property("payload"); - should.deepEqual(msg.payload, rval); + if (type == "date" && val == "1") { + should.deepEqual(Math.round(msg.payload/10000), Math.round(rval/10000)); + } + else if (type == "date" && val == "iso") { + should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11)); + } + else { + should.deepEqual(msg.payload, rval); + } } else { msg.should.have.property("payload", val); @@ -126,6 +134,7 @@ describe('trigger node', function() { }); it('should output 2st value when triggered ('+type+')', function(done) { + if (type == "date" && val == "1") { val = "0"; } var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1:"foo", op1type:"str", op2:val, op2type:type, duration:"20", wires:[["n2"]] }, {id:"n2", type:"helper"} ]; process.env[val] = rval; @@ -142,7 +151,15 @@ describe('trigger node', function() { else { if (rval) { msg.should.have.property("payload"); - should.deepEqual(msg.payload, rval); + if (type == "date" && val == "0") { + should.deepEqual(Math.round(msg.payload/10000), Math.round(rval/10000)); + } + else if (type == "date" && val == "iso") { + should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11)); + } + else { + should.deepEqual(msg.payload, rval); + } } else { msg.should.have.property("payload", val); @@ -166,6 +183,9 @@ describe('trigger node', function() { var val_buf = "[1,2,3,4,5]"; basicTest("bin", val_buf, Buffer.from(JSON.parse(val_buf))); basicTest("env", "NR-TEST", "env-val"); + basicTest("date", "1", Date.now()); + basicTest("date", "iso", (new Date()).toISOString()); + // basicTest("date", "object", Date.now()); it('should output 1 then 0 when triggered (default)', function(done) { var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", duration:"20", wires:[["n2"]] }, From 892933ff758d240109fb0d711d322c73f672250d Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 12 Oct 2024 17:22:24 +0100 Subject: [PATCH 3/6] Update 89-trigger_spec.js --- test/nodes/core/function/89-trigger_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/nodes/core/function/89-trigger_spec.js b/test/nodes/core/function/89-trigger_spec.js index e86ed7a31..f78db0423 100644 --- a/test/nodes/core/function/89-trigger_spec.js +++ b/test/nodes/core/function/89-trigger_spec.js @@ -112,7 +112,7 @@ describe('trigger node', function() { if (rval) { msg.should.have.property("payload"); if (type == "date" && val == "1") { - should.deepEqual(Math.round(msg.payload/10000), Math.round(rval/10000)); + should.deepEqual(Math.round(msg.payload/1000000), Math.round(Date.now()/1000000)); } else if (type == "date" && val == "iso") { should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11)); @@ -152,7 +152,7 @@ describe('trigger node', function() { if (rval) { msg.should.have.property("payload"); if (type == "date" && val == "0") { - should.deepEqual(Math.round(msg.payload/10000), Math.round(rval/10000)); + should.deepEqual(Math.round(msg.payload/1000000), parseInt(Date.now()/1000000)); } else if (type == "date" && val == "iso") { should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11)); From d0ad62a82bd2b3afea4183aa99fb2509589d7226 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 21 Oct 2024 16:24:21 +0100 Subject: [PATCH 4/6] Revert trigger node fix --- .../@node-red/nodes/core/function/89-trigger.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js index 0fc6ad6fb..16a00e99d 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js @@ -166,13 +166,7 @@ module.exports = function(RED) { if (err) { reject(err); } else { - if (node.op1type === "date") { - let d = new Date(); - if (node.op1 == "1") { msg.payload = Date.now(); } - else if (node.op1 == "iso") { msg.payload = d.toISOString(); } - else { msg.payload = d; } - } - else { msg.payload = value; } + msg.payload = value; resolve(); } }); @@ -219,12 +213,7 @@ module.exports = function(RED) { } else { msg2.payload = node.topics[topic].m2; - if (node.op2type === "date") { - let d = new Date(); - if (node.op2 == "0") { msg2.payload = Date.now(); } - else if (node.op2 == "iso") { msg2.payload = d.toISOString(); } - else { msg2.payload = d; } - } + if (node.op2type === "date") { msg2.payload = Date.now(); } if (node.second === true) { msgInfo.send([null,msg2]); } else { msgInfo.send(msg2); } } From b3aff3a3e6847f03ef851d98e68c80eafe27573d Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 21 Oct 2024 16:26:03 +0100 Subject: [PATCH 5/6] Ensure trigger node properties work with evaluateNodeProperty --- .../@node-red/nodes/core/function/89-trigger.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js index 16a00e99d..f0aaaf393 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js @@ -24,6 +24,14 @@ module.exports = function(RED) { this.op2 = n.op2 || "0"; this.op1type = n.op1type || "str"; this.op2type = n.op2type || "str"; + // If the op1/2type is 'date', then we need to leave op1/2 alone so that + // evaluateNodeProperty works as expected. + if (this.op1type === 'date' && this.op1 === '1') { + this.op1 = '' + } + if (this.op2type === 'date' && this.op2 === '0') { + this.op2 = '' + } this.second = (n.outputs == 2) ? true : false; this.topic = n.topic || "topic"; @@ -213,7 +221,6 @@ module.exports = function(RED) { } else { msg2.payload = node.topics[topic].m2; - if (node.op2type === "date") { msg2.payload = Date.now(); } if (node.second === true) { msgInfo.send([null,msg2]); } else { msgInfo.send(msg2); } } From 2220956007347017a37d273ff8e1389f3bd48158 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 21 Oct 2024 16:35:11 +0100 Subject: [PATCH 6/6] Ensure trigger second output is revaluated for date types --- .../node_modules/@node-red/nodes/core/function/89-trigger.js | 2 +- test/nodes/core/function/89-trigger_spec.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js index f0aaaf393..8b3625f2d 100644 --- a/packages/node_modules/@node-red/nodes/core/function/89-trigger.js +++ b/packages/node_modules/@node-red/nodes/core/function/89-trigger.js @@ -201,7 +201,7 @@ module.exports = function(RED) { if (node.op2type !== "nul") { var promise = Promise.resolve(); msg2 = RED.util.cloneMessage(msg); - if (node.op2type === "flow" || node.op2type === "global") { + if (node.op2type === "flow" || node.op2type === "global" || node.op2type === "date") { promise = new Promise((resolve,reject) => { RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg,(err,value) => { if (err) { diff --git a/test/nodes/core/function/89-trigger_spec.js b/test/nodes/core/function/89-trigger_spec.js index f78db0423..913298901 100644 --- a/test/nodes/core/function/89-trigger_spec.js +++ b/test/nodes/core/function/89-trigger_spec.js @@ -152,7 +152,7 @@ describe('trigger node', function() { if (rval) { msg.should.have.property("payload"); if (type == "date" && val == "0") { - should.deepEqual(Math.round(msg.payload/1000000), parseInt(Date.now()/1000000)); + ;(Math.round(msg.payload/1000000)).should.be.approximately(parseInt(Date.now()/1000000), 1); } else if (type == "date" && val == "iso") { should.deepEqual(msg.payload.substr(0,11), rval.substr(0,11));