From 6a41cbebc9c84ab7268483394b7fd10fb231f8b4 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Tue, 15 Feb 2022 14:23:01 +0900 Subject: [PATCH 1/2] evaluate env var after path initialization --- .../node_modules/@node-red/runtime/lib/flows/util.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/flows/util.js b/packages/node_modules/@node-red/runtime/lib/flows/util.js index be436eb22..0f3435b77 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/util.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/util.js @@ -77,15 +77,16 @@ function createNode(flow,config) { if (typeof nodeTypeConstructor === "function") { var conf = clone(config); delete conf.credentials; - for (var p in conf) { - if (conf.hasOwnProperty(p)) { - mapEnvVarProperties(conf,p,flow,conf); - } - } try { Object.defineProperty(conf,'_module', {value: typeRegistry.getNodeInfo(type), enumerable: false, writable: true }) Object.defineProperty(conf,'_flow', {value: flow, enumerable: false, writable: true }) Object.defineProperty(conf,'_path', {value: `${flow.path}/${config._alias||config.id}`, enumerable: false, writable: true }) + + for (var p in conf) { + if (conf.hasOwnProperty(p)) { + mapEnvVarProperties(conf,p,flow,conf); + } + } newNode = new nodeTypeConstructor(conf); } catch (err) { Log.log({ From 0f1aea3e0d127392447cc92a8032bc44573d6449 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Tue, 15 Feb 2022 14:41:03 +0900 Subject: [PATCH 2/2] add test case --- test/nodes/core/common/20-inject_spec.js | 136 +++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/test/nodes/core/common/20-inject_spec.js b/test/nodes/core/common/20-inject_spec.js index 8b0143ce9..cb14690d0 100644 --- a/test/nodes/core/common/20-inject_spec.js +++ b/test/nodes/core/common/20-inject_spec.js @@ -242,6 +242,142 @@ describe('inject node', function() { }); + it('inject name of node as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_NODE_NAME}", payloadType: "str", wires: [["n2"]], z: "flow"}, + {id: "n2", type: "helper"}]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "NAME"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('inject id of node as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_NODE_ID}", payloadType: "str", wires: [["n2"]], z: "flow"}, + {id: "n2", type: "helper"}]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "n1"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('inject path of node as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_NODE_PATH}", payloadType: "str", wires: [["n2"]], z: "flow"}, + {id: "n2", type: "helper"}]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "flow/n1"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + + it('inject name of flow as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_FLOW_NAME}", payloadType: "str", wires: [["n2"]], z: "flow"}, + {id: "n2", type: "helper"}, + {id: "flow", type: "tab", label: "FLOW" }, + ]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "FLOW"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('inject id of flow as environment variable ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_FLOW_ID}", payloadType: "str", wires: [["n2"]], z: "flow"}, + {id: "n2", type: "helper"}, + {id: "flow", type: "tab", name: "FLOW" }, + ]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "flow"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('inject name of group as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_GROUP_NAME}", payloadType: "str", wires: [["n2"]], z: "flow", g: "g0"}, + {id: "n2", type: "helper"}, + {id: "g0", type: "group", name: "GROUP" }, + ]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "GROUP"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('inject id of group as environment variable by substitution ', function (done) { + var flow = [{id: "n1", type: "inject", name: "NAME", topnic: "t1", payload: "${NR_GROUP_ID}", payloadType: "str", wires: [["n2"]], z: "flow", g: "g0"}, + {id: "n2", type: "helper"}, + {id: "g0", type: "group", name: "GROUP" }, + ]; + helper.load(injectNode, flow, function () { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function (msg) { + try { + msg.should.have.property("payload", "g0"); + done(); + } catch (err) { + done(err); + } + }); + n1.receive({}); + }); + }); + + it('sets the value of flow context property', function (done) { var flow = [{id: "n1", type: "inject", topic: "t1", payload: "flowValue", payloadType: "flow", wires: [["n2"]], z: "flow"}, {id: "n2", type: "helper"}];