Skip to content

Commit 8d52867

Browse files
author
Dave Conway-Jones
committed
reimplement $(env var) replace to share common code.
and add test to utils
1 parent 74f2180 commit 8d52867

4 files changed

Lines changed: 53 additions & 48 deletions

File tree

red/runtime/nodes/flows/Flow.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -259,32 +259,6 @@ function Flow(global,flow) {
259259
}
260260
}
261261
}
262-
263-
}
264-
265-
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
266-
267-
function mapEnvVarProperties(obj,prop) {
268-
if (Buffer.isBuffer(obj[prop])) {
269-
return;
270-
} else if (Array.isArray(obj[prop])) {
271-
for (var i=0;i<obj[prop].length;i++) {
272-
mapEnvVarProperties(obj[prop],i);
273-
}
274-
} else if (typeof obj[prop] === 'string') {
275-
var m;
276-
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
277-
if (process.env.hasOwnProperty(m[1])) {
278-
obj[prop] = process.env[m[1]];
279-
}
280-
}
281-
} else {
282-
for (var p in obj[prop]) {
283-
if (obj[prop].hasOwnProperty(p)) {
284-
mapEnvVarProperties(obj[prop],p);
285-
}
286-
}
287-
}
288262
}
289263

290264
function createNode(type,config) {
@@ -295,7 +269,7 @@ function createNode(type,config) {
295269
delete conf.credentials;
296270
for (var p in conf) {
297271
if (conf.hasOwnProperty(p)) {
298-
mapEnvVarProperties(conf,p);
272+
flowUtil.mapEnvVarProperties(conf,p);
299273
}
300274
}
301275
try {

red/runtime/nodes/flows/util.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,35 @@ function diffNodes(oldNode,newNode) {
3737
return false;
3838
}
3939

40+
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
41+
42+
function mapEnvVarProperties(obj,prop) {
43+
if (Buffer.isBuffer(obj[prop])) {
44+
return;
45+
} else if (Array.isArray(obj[prop])) {
46+
for (var i=0;i<obj[prop].length;i++) {
47+
mapEnvVarProperties(obj[prop],i);
48+
}
49+
} else if (typeof obj[prop] === 'string') {
50+
var m;
51+
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
52+
if (process.env.hasOwnProperty(m[1])) {
53+
obj[prop] = process.env[m[1]];
54+
}
55+
}
56+
} else {
57+
for (var p in obj[prop]) {
58+
if (obj[prop].hasOwnProperty(p)) {
59+
mapEnvVarProperties(obj[prop],p);
60+
}
61+
}
62+
}
63+
}
64+
4065
module.exports = {
4166

4267
diffNodes: diffNodes,
68+
mapEnvVarProperties: mapEnvVarProperties,
4369

4470
parseConfig: function(config) {
4571
var flow = {};
@@ -301,7 +327,7 @@ module.exports = {
301327
}
302328
}
303329
}
304-
} while(madeChange===true)
330+
} while (madeChange===true)
305331

306332
// Find any nodes that exist on a subflow template and remove from changed
307333
// list as the parent subflow will now be marked as containing a change
@@ -316,7 +342,7 @@ module.exports = {
316342

317343
// Recursively mark all instances of changed subflows as changed
318344
var changedSubflowStack = Object.keys(changedSubflows);
319-
while(changedSubflowStack.length > 0) {
345+
while (changedSubflowStack.length > 0) {
320346
var subflowId = changedSubflowStack.pop();
321347
for (id in newConfig.allNodes) {
322348
if (newConfig.allNodes.hasOwnProperty(id)) {
@@ -350,7 +376,7 @@ module.exports = {
350376
// Traverse the links of all modified nodes to mark the connected nodes
351377
var modifiedNodes = diff.added.concat(diff.changed).concat(diff.removed).concat(diff.rewired);
352378
var visited = {};
353-
while(modifiedNodes.length > 0) {
379+
while (modifiedNodes.length > 0) {
354380
node = modifiedNodes.pop();
355381
if (!visited[node]) {
356382
visited[node] = true;

red/runtime/nodes/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var fs = require("fs");
2121
var registry = require("./registry");
2222
var credentials = require("./credentials");
2323
var flows = require("./flows");
24+
var flowUtil = require("./flows/util")
2425
var context = require("./context");
2526
var Node = require("./Node");
2627
var log = require("../log");
@@ -70,19 +71,11 @@ function createNode(node,def) {
7071
if (creds) {
7172
//console.log("Attaching credentials to ",node.id);
7273
// allow $(foo) syntax to substitute env variables for credentials also...
73-
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
74-
var loopOver = function (obj) {
75-
for (var o in obj) {
76-
if (typeof obj[o] === "object" && obj[o] !== null) { loopOver(obj[o]); }
77-
else {
78-
var m;
79-
if ( (m = EnvVarPropertyRE.exec(obj[o])) !== null ) {
80-
if (process.env.hasOwnProperty(m[1])) { obj[o] = process.env[m[1]]; }
81-
}
82-
}
74+
for (var p in creds) {
75+
if (creds.hasOwnProperty(p)) {
76+
flowUtil.mapEnvVarProperties(creds,p);
8377
}
8478
}
85-
loopOver(creds);
8679
node.credentials = creds;
8780
} else if (credentials.getDefinition(node.type)) {
8881
node.credentials = {};
@@ -160,7 +153,6 @@ module.exports = {
160153
// disableFlow: flows.disableFlow,
161154
// enableFlow: flows.enableFlow,
162155

163-
164156
// Credentials
165157
addCredentials: credentials.add,
166158
getCredentials: credentials.get,

test/red/runtime/nodes/flows/util_spec.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ describe('flows/util', function() {
3434
getType.restore();
3535
});
3636

37-
37+
describe('#mapEnvVarProperties',function() {
38+
it('handles ENV substitutions in an object', function() {
39+
process.env.foo1 = "bar1";
40+
process.env.foo2 = "bar2";
41+
process.env.foo3 = "bar3";
42+
var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};
43+
for (var p in foo) {
44+
if (foo.hasOwnProperty(p)) {
45+
flowUtil.mapEnvVarProperties(foo,p);
46+
}
47+
}
48+
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
49+
});
50+
});
3851

3952
describe('#diffNodes',function() {
4053
it('handles a null old node', function() {
@@ -189,7 +202,7 @@ describe('flows/util', function() {
189202

190203
});
191204
it('identifies nodes with changed properties, including upstream linked', function() {
192-
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
205+
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
193206
var newConfig = clone(config);
194207
newConfig[1].bar = "c";
195208

@@ -207,7 +220,7 @@ describe('flows/util', function() {
207220
});
208221

209222
it('identifies nodes with changed credentials, including downstream linked', function() {
210-
var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
223+
var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
211224
var newConfig = clone(config);
212225
newConfig[0].credentials = {};
213226

@@ -225,7 +238,7 @@ describe('flows/util', function() {
225238
});
226239

227240
it('identifies nodes with changed wiring', function() {
228-
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
241+
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
229242
var newConfig = clone(config);
230243
newConfig[1].wires[0][0] = "3";
231244

@@ -243,7 +256,7 @@ describe('flows/util', function() {
243256
});
244257

245258
it('identifies nodes with changed wiring - second connection added', function() {
246-
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
259+
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
247260
var newConfig = clone(config);
248261
newConfig[1].wires[0].push("1");
249262

@@ -261,7 +274,7 @@ describe('flows/util', function() {
261274
});
262275

263276
it('identifies nodes with changed wiring - node connected', function() {
264-
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}];
277+
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}];
265278
var newConfig = clone(config);
266279
newConfig[1].wires.push("3");
267280

0 commit comments

Comments
 (0)