From 8d5286703f6e873b6b4bb7131a6509310a2ec4e0 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 17 Nov 2016 13:56:17 +0000 Subject: [PATCH] reimplement $(env var) replace to share common code. and add test to utils --- red/runtime/nodes/flows/Flow.js | 28 +------------------- red/runtime/nodes/flows/util.js | 32 ++++++++++++++++++++--- red/runtime/nodes/index.js | 16 +++--------- test/red/runtime/nodes/flows/util_spec.js | 25 +++++++++++++----- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/red/runtime/nodes/flows/Flow.js b/red/runtime/nodes/flows/Flow.js index e4dedd97d..05f48d684 100644 --- a/red/runtime/nodes/flows/Flow.js +++ b/red/runtime/nodes/flows/Flow.js @@ -259,32 +259,6 @@ function Flow(global,flow) { } } } - -} - -var EnvVarPropertyRE = /^\$\((\S+)\)$/; - -function mapEnvVarProperties(obj,prop) { - if (Buffer.isBuffer(obj[prop])) { - return; - } else if (Array.isArray(obj[prop])) { - for (var i=0;i 0) { + while (changedSubflowStack.length > 0) { var subflowId = changedSubflowStack.pop(); for (id in newConfig.allNodes) { if (newConfig.allNodes.hasOwnProperty(id)) { @@ -350,7 +376,7 @@ module.exports = { // Traverse the links of all modified nodes to mark the connected nodes var modifiedNodes = diff.added.concat(diff.changed).concat(diff.removed).concat(diff.rewired); var visited = {}; - while(modifiedNodes.length > 0) { + while (modifiedNodes.length > 0) { node = modifiedNodes.pop(); if (!visited[node]) { visited[node] = true; diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index 8368f8bad..585447a2d 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -21,6 +21,7 @@ var fs = require("fs"); var registry = require("./registry"); var credentials = require("./credentials"); var flows = require("./flows"); +var flowUtil = require("./flows/util") var context = require("./context"); var Node = require("./Node"); var log = require("../log"); @@ -70,19 +71,11 @@ function createNode(node,def) { if (creds) { //console.log("Attaching credentials to ",node.id); // allow $(foo) syntax to substitute env variables for credentials also... - var EnvVarPropertyRE = /^\$\((\S+)\)$/; - var loopOver = function (obj) { - for (var o in obj) { - if (typeof obj[o] === "object" && obj[o] !== null) { loopOver(obj[o]); } - else { - var m; - if ( (m = EnvVarPropertyRE.exec(obj[o])) !== null ) { - if (process.env.hasOwnProperty(m[1])) { obj[o] = process.env[m[1]]; } - } - } + for (var p in creds) { + if (creds.hasOwnProperty(p)) { + flowUtil.mapEnvVarProperties(creds,p); } } - loopOver(creds); node.credentials = creds; } else if (credentials.getDefinition(node.type)) { node.credentials = {}; @@ -160,7 +153,6 @@ module.exports = { // disableFlow: flows.disableFlow, // enableFlow: flows.enableFlow, - // Credentials addCredentials: credentials.add, getCredentials: credentials.get, diff --git a/test/red/runtime/nodes/flows/util_spec.js b/test/red/runtime/nodes/flows/util_spec.js index 54f0acfa8..57799aca1 100644 --- a/test/red/runtime/nodes/flows/util_spec.js +++ b/test/red/runtime/nodes/flows/util_spec.js @@ -34,7 +34,20 @@ describe('flows/util', function() { getType.restore(); }); - + describe('#mapEnvVarProperties',function() { + it('handles ENV substitutions in an object', function() { + process.env.foo1 = "bar1"; + process.env.foo2 = "bar2"; + process.env.foo3 = "bar3"; + var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}}; + for (var p in foo) { + if (foo.hasOwnProperty(p)) { + flowUtil.mapEnvVarProperties(foo,p); + } + } + foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } ); + }); + }); describe('#diffNodes',function() { it('handles a null old node', function() { @@ -189,7 +202,7 @@ describe('flows/util', function() { }); it('identifies nodes with changed properties, including upstream linked', function() { - var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; + var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; var newConfig = clone(config); newConfig[1].bar = "c"; @@ -207,7 +220,7 @@ describe('flows/util', function() { }); it('identifies nodes with changed credentials, including downstream linked', function() { - var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; + var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; var newConfig = clone(config); newConfig[0].credentials = {}; @@ -225,7 +238,7 @@ describe('flows/util', function() { }); it('identifies nodes with changed wiring', function() { - var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; + var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; var newConfig = clone(config); newConfig[1].wires[0][0] = "3"; @@ -243,7 +256,7 @@ describe('flows/util', function() { }); it('identifies nodes with changed wiring - second connection added', function() { - var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; + var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}]; var newConfig = clone(config); newConfig[1].wires[0].push("1"); @@ -261,7 +274,7 @@ describe('flows/util', function() { }); it('identifies nodes with changed wiring - node connected', function() { - var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}]; + var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}]; var newConfig = clone(config); newConfig[1].wires.push("3");