# all files / red/runtime/nodes/flows/ util.js

95.02% Statements 229/241
89.72% Branches 192/214
76.92% Functions 10/13
94.98% Lines 227/239

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449

    1×
    1×
    1×
    1×

    1×
    126×
    1×

    443×
    447×
    125×
    8×

    117×
    297×
    297×
    16×

    101×

    1×

    1×
    773×

    773×
    133×
    74×

    640×
    448×
    448×
    8×
    7×

    192×
    2×
    2×

    1×

    110×
    110×
    110×
    110×
    110×
    110×

    110×
    459×
    459×
    67×
    67×
    67×
    67×

    110×
    459×
    35×
    35×
    35×
    35×

    110×
    110×
    110×
    459×
    357×

    357×
    5×
    5×

    357×
    357×
    144×
    213×
    67×

    357×
    154×
    13×
    13×
    13×

    154×
    153×

    203×
    58×

    145×
    145×

    357×

    357×

    110×

    110×
    110×
    459×
    357×
    1904×

    14×

    357×

    145×
    1×
    1×
    1×
    1×
    1×

    145×
    1×
    1×

    110×

    33×
    33×
    33×
    33×
    33×

    33×
    1×

    33×

    33×
    33×
    33×
    33×

    33×

    33×

    33×
    14×
    3×

    33×
    14×
    11×
    11×
    11×
    2×
    2×
    1×

    1×

    33×
    134×
    134×
    134×

    120×
    102×
    102×
    42×
    42×
    40×
    40×
    40×
    38×
    38×

    120×
    7×

    7×
    1×
    1×
    1×

    113×
    1×

    112×
    14×
    14×
    4×

    14×
    4×
    4×
    2×
    2×

    112×
    6×

    6×
    2×
    2×
    1×
    1×

    33×
    139×
    139×

    139×
    106×
    106×
    42×
    42×
    41×
    5×

    41×
    41×
    40×
    40×
    5×

    139×
    14×

    14×
    9×
    9×
    1×
    1×

    33×

    33×
    37×
    37×
    160×
    160×
    160×
    606×

    278×
    9×
    4×
    4×

    4×
    1×
    1×
    1×

    33×
    139×
    139×
    139×
    23×

    33×
    33×
    11×
    11×
    73×
    73×
    73×
    12×
    12×
    12×
    1×
    1×

    1×
    1×

    33×

    33×
    33×
    33×
    154×
    154×
    107×
    107×
    78×
    37×

    78×

    33×

     /**
     * Copyright JS Foundation and other contributors, http://js.foundation
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     **/
    var clone = require("clone");
    var redUtil = require("../../util");
    var subflowInstanceRE = /^subflow:(.+)$/;
    var typeRegistry = require("../registry");

    function diffNodes(oldNode,newNode) {
        if (oldNode == null) {
            return true;
        }
        var oldKeys = Object.keys(oldNode).filter(function(p) { return p != "x" && p != "y" && p != "wires" });
        var newKeys = Object.keys(newNode).filter(function(p) { return p != "x" && p != "y" && p != "wires" });
        if (oldKeys.length != newKeys.length) {
            return true;
        }
        for (var i=0;i<newKeys.length;i++) {
            var p = newKeys[i];
            if (!redUtil.compareObjects(oldNode[p],newNode[p])) {
                return true;
            }
        }

        return false;
    }

    var EnvVarPropertyRE = /^\$\((\S+)\)$/;

    function mapEnvVarProperties(obj,prop) {
        Iif (Buffer.isBuffer(obj[prop])) {
            return;
        } else if (Array.isArray(obj[prop])) {
            for (var i=0;i<obj[prop].length;i++) {
                mapEnvVarProperties(obj[prop],i);
            }
        } else if (typeof obj[prop] === 'string') {
            var m;
            if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
                if (process.env.hasOwnProperty(m[1])) {
                    obj[prop] = process.env[m[1]];
                }
            }
        } else {
            for (var p in obj[prop]) {
                Eif (obj[prop].hasOwnProperty(p)) {
                    mapEnvVarProperties(obj[prop],p);
                }
            }
        }
    }

    module.exports = {

        diffNodes: diffNodes,
        mapEnvVarProperties: mapEnvVarProperties,

        parseConfig: function(config) {
            var flow = {};
            flow.allNodes = {};
            flow.subflows = {};
            flow.configs = {};
            flow.flows = {};
            flow.missingTypes = [];

            config.forEach(function(n) {
                flow.allNodes[n.id] = clone(n);
                if (n.type === 'tab') {
                    flow.flows[n.id] = n;
                    flow.flows[n.id].subflows = {};
                    flow.flows[n.id].configs = {};
                    flow.flows[n.id].nodes = {};
                }
            });

            config.forEach(function(n) {
                if (n.type === 'subflow') {
                    flow.subflows[n.id] = n;
                    flow.subflows[n.id].configs = {};
                    flow.subflows[n.id].nodes = {};
                    flow.subflows[n.id].instances = [];
                }
            });
            var linkWires = {};
            var linkOutNodes = [];
            config.forEach(function(n) {
                if (n.type !== 'subflow' && n.type !== 'tab') {
                    var subflowDetails = subflowInstanceRE.exec(n.type);

                    if ( (subflowDetails && !flow.subflows[subflowDetails[1]]) || (!subflowDetails && !typeRegistry.get(n.type)) ) {
                        Eif (flow.missingTypes.indexOf(n.type) === -1) {
                            flow.missingTypes.push(n.type);
                        }
                    }
                    var container = null;
                    if (flow.flows[n.z]) {
                        container = flow.flows[n.z];
                    } else if (flow.subflows[n.z]) {
                        container = flow.subflows[n.z];
                    }
                    if (n.hasOwnProperty('x') && n.hasOwnProperty('y')) {
                        if (subflowDetails) {
                            var subflowType = subflowDetails[1]
                            n.subflow = subflowType;
                            flow.subflows[subflowType].instances.push(n)
                        }
                        if (container) {
                            container.nodes[n.id] = n;
                        }
                    } else {
                        if (container) {
                            container.configs[n.id] = n;
                        } else {
                            flow.configs[n.id] = n;
                            flow.configs[n.id]._users = [];
                        }
                    }
                    Iif (n.type === 'link in' && n.links) {
                        // Ensure wires are present in corresponding link out nodes
                        n.links.forEach(function(id) {
                            linkWires[id] = linkWires[id]||{};
                            linkWires[id][n.id] = true;
                        })
                    } else Iif (n.type === 'link out' && n.links) {
                        linkWires[n.id] = linkWires[n.id]||{};
                        n.links.forEach(function(id) {
                            linkWires[n.id][id] = true;
                        })
                        linkOutNodes.push(n);
                    }
                }
            });
            linkOutNodes.forEach(function(n) {
                var links = linkWires[n.id];
                var targets = Object.keys(links);
                n.wires = [targets];
            });

            var addedTabs = {};
            config.forEach(function(n) {
                if (n.type !== 'subflow' && n.type !== 'tab') {
                    for (var prop in n) {
                        if (n.hasOwnProperty(prop) && prop !== 'id' && prop !== 'wires' && prop !== 'type' && prop !== '_users' && flow.configs.hasOwnProperty(n[prop])) {
                            // This property references a global config node
                            flow.configs[n[prop]]._users.push(n.id)
                        }
                    }
                    if (n.z && !flow.subflows[n.z]) {

                        if (!flow.flows[n.z]) {
                            flow.flows[n.z] = {type:'tab',id:n.z};
                            flow.flows[n.z].subflows = {};
                            flow.flows[n.z].configs = {};
                            flow.flows[n.z].nodes = {};
                            addedTabs[n.z] = flow.flows[n.z];
                        }
                        if (addedTabs[n.z]) {
                            Eif (n.hasOwnProperty('x') && n.hasOwnProperty('y')) {
                                addedTabs[n.z].nodes[n.id] = n;
                            } else {
                                addedTabs[n.z].configs[n.id] = n;
                            }
                        }
                    }
                }
            });
            return flow;
        },

        diffConfigs: function(oldConfig, newConfig) {
            var id;
            var node;
            var nn;
            var wires;
            var j,k;

            if (!oldConfig) {
                oldConfig = {
                    flows:{},
                    allNodes:{}
                }
            }
            var changedSubflows = {};

            var added = {};
            var removed = {};
            var changed = {};
            var wiringChanged = {};

            var linkMap = {};

            var changedTabs = {};

            // Look for tabs that have been removed
            for (id in oldConfig.flows) {
                if (oldConfig.flows.hasOwnProperty(id) && (!newConfig.flows.hasOwnProperty(id))) {
                    removed[id] = oldConfig.allNodes[id];
                }
            }

            // Look for tabs that have been disabled
            for (id in oldConfig.flows) {
                if (oldConfig.flows.hasOwnProperty(id) && newConfig.flows.hasOwnProperty(id)) {
                    var originalState = oldConfig.flows[id].disabled||false;
                    var newState = newConfig.flows[id].disabled||false;
                    if (originalState !== newState) {
                        changedTabs[id] = true;
                        if (originalState) {
                            added[id] = oldConfig.allNodes[id];
                        } else {
                            removed[id] = oldConfig.allNodes[id];
                        }
                    }
                }
            }

            for (id in oldConfig.allNodes) {
                Eif (oldConfig.allNodes.hasOwnProperty(id)) {
                    node = oldConfig.allNodes[id];
                    if (node.type !== 'tab') {
                        // build the map of what this node was previously wired to
                        if (node.wires) {
                            linkMap[node.id] = linkMap[node.id] || [];
                            for (j=0;j<node.wires.length;j++) {
                                wires = node.wires[j];
                                for (k=0;k<wires.length;k++) {
                                    linkMap[node.id].push(wires[k]);
                                    nn = oldConfig.allNodes[wires[k]];
                                    if (nn) {
                                        linkMap[nn.id] = linkMap[nn.id] || [];
                                        linkMap[nn.id].push(node.id);
                                    }
                                }
                            }
                        }
                        // This node has been removed
                        if (removed[node.z] || !newConfig.allNodes.hasOwnProperty(id)) {
                            removed[id] = node;
                            // Mark the container as changed
                            if (!removed[node.z] && newConfig.allNodes[removed[id].z]) {
                                changed[removed[id].z] = newConfig.allNodes[removed[id].z];
                                Eif (changed[removed[id].z].type === "subflow") {
                                    changedSubflows[removed[id].z] = changed[removed[id].z];
                                    //delete removed[id];
                                }
                            }
                        } else {
                            if (added[node.z]) {
                                added[id] = node;
                            } else {
                                // This node has a material configuration change
                                if (diffNodes(node,newConfig.allNodes[id]) || newConfig.allNodes[id].credentials) {
                                    changed[id] = newConfig.allNodes[id];
                                    if (changed[id].type === "subflow") {
                                        changedSubflows[id] = changed[id];
                                    }
                                    // Mark the container as changed
                                    if (newConfig.allNodes[changed[id].z]) {
                                        changed[changed[id].z] = newConfig.allNodes[changed[id].z];
                                        if (changed[changed[id].z].type === "subflow") {
                                            changedSubflows[changed[id].z] = changed[changed[id].z];
                                            delete changed[id];
                                        }
                                    }
                                }
                                // This node's wiring has changed
                                if (!redUtil.compareObjects(node.wires,newConfig.allNodes[id].wires)) {
                                    wiringChanged[id] = newConfig.allNodes[id];
                                    // Mark the container as changed
                                    if (newConfig.allNodes[wiringChanged[id].z]) {
                                        changed[wiringChanged[id].z] = newConfig.allNodes[wiringChanged[id].z];
                                        if (changed[wiringChanged[id].z].type === "subflow") {
                                            changedSubflows[wiringChanged[id].z] = changed[wiringChanged[id].z];
                                            delete wiringChanged[id];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // Look for added nodes
            for (id in newConfig.allNodes) {
                Eif (newConfig.allNodes.hasOwnProperty(id)) {
                    node = newConfig.allNodes[id];
                    // build the map of what this node is now wired to
                    if (node.wires) {
                        linkMap[node.id] = linkMap[node.id] || [];
                        for (j=0;j<node.wires.length;j++) {
                            wires = node.wires[j];
                            for (k=0;k<wires.length;k++) {
                                if (linkMap[node.id].indexOf(wires[k]) === -1) {
                                    linkMap[node.id].push(wires[k]);
                                }
                                nn = newConfig.allNodes[wires[k]];
                                if (nn) {
                                    linkMap[nn.id] = linkMap[nn.id] || [];
                                    if (linkMap[nn.id].indexOf(node.id) === -1) {
                                        linkMap[nn.id].push(node.id);
                                    }
                                }
                            }
                        }
                    }
                    // This node has been added
                    if (!oldConfig.allNodes.hasOwnProperty(id)) {
                        added[id] = node;
                        // Mark the container as changed
                        if (newConfig.allNodes[added[id].z]) {
                            changed[added[id].z] = newConfig.allNodes[added[id].z];
                            if (changed[added[id].z].type === "subflow") {
                                changedSubflows[added[id].z] = changed[added[id].z];
                                delete added[id];
                            }
                        }
                    }
                }
            }

            var madeChange;
            // Loop through the nodes looking for references to changed config nodes
            // Repeat the loop if anything is marked as changed as it may need to be
            // propagated to parent nodes.
            // TODO: looping through all nodes every time is a bit inefficient - could be more targeted
            do {
                madeChange = false;
                for (id in newConfig.allNodes) {
                    Eif (newConfig.allNodes.hasOwnProperty(id)) {
                        node = newConfig.allNodes[id];
                        for (var prop in node) {
                            if (node.hasOwnProperty(prop) && prop != "z" && prop != "id" && prop != "wires") {
                                // This node has a property that references a changed/removed node
                                // Assume it is a config node change and mark this node as
                                // changed.
                                if (changed[node[prop]] || removed[node[prop]]) {
                                    if (!changed[node.id]) {
                                        madeChange = true;
                                        changed[node.id] = node;
                                        // This node exists within subflow template
                                        // Mark the template as having changed
                                        if (newConfig.allNodes[node.z]) {
                                            changed[node.z] = newConfig.allNodes[node.z];
                                            Eif (changed[node.z].type === "subflow") {
                                                changedSubflows[node.z] = changed[node.z];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } while (madeChange===true)

            // Find any nodes that exist on a subflow template and remove from changed
            // list as the parent subflow will now be marked as containing a change
            for (id in newConfig.allNodes) {
                Eif (newConfig.allNodes.hasOwnProperty(id)) {
                    node = newConfig.allNodes[id];
                    if (newConfig.allNodes[node.z] && newConfig.allNodes[node.z].type === "subflow") {
                        delete changed[node.id];
                    }
                }
            }

            // Recursively mark all instances of changed subflows as changed
            var changedSubflowStack = Object.keys(changedSubflows);
            while (changedSubflowStack.length > 0) {
                var subflowId = changedSubflowStack.pop();
                for (id in newConfig.allNodes) {
                    Eif (newConfig.allNodes.hasOwnProperty(id)) {
                        node = newConfig.allNodes[id];
                        if (node.type === 'subflow:'+subflowId) {
                            Eif (!changed[node.id]) {
                                changed[node.id] = node;
                                if (!changed[changed[node.id].z] && newConfig.allNodes[changed[node.id].z]) {
                                    changed[changed[node.id].z] = newConfig.allNodes[changed[node.id].z];
                                    Eif (newConfig.allNodes[changed[node.id].z].type === "subflow") {
                                        // This subflow instance is inside a subflow. Add the
                                        // containing subflow to the stack to mark
                                        changedSubflowStack.push(changed[node.id].z);
                                        delete changed[node.id];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            var diff = {
                added:Object.keys(added),
                changed:Object.keys(changed),
                removed:Object.keys(removed),
                rewired:Object.keys(wiringChanged),
                linked:[]
            }

            // 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) {
                node = modifiedNodes.pop();
                if (!visited[node]) {
                    visited[node] = true;
                    if (linkMap[node]) {
                        if (!changed[node] && !added[node] && !removed[node] && !wiringChanged[node]) {
                            diff.linked.push(node);
                        }
                        modifiedNodes = modifiedNodes.concat(linkMap[node]);
                    }
                }
            }
            // console.log(diff);
            // for (id in newConfig.allNodes) {
            //     console.log(
            //         (added[id]?"+":(changed[id]?"!":" "))+(wiringChanged[id]?"w":" ")+(diff.linked.indexOf(id)!==-1?"~":" "),
            //         id,
            //         newConfig.allNodes[id].type,
            //         newConfig.allNodes[id].name||newConfig.allNodes[id].label||""
            //     );
            // }
            // for (id in removed) {
            //     console.log(
            //         "- "+(diff.linked.indexOf(id)!==-1?"~":" "),
            //         id,
            //         oldConfig.allNodes[id].type,
            //         oldConfig.allNodes[id].name||oldConfig.allNodes[id].label||""
            //     );
            // }

            return diff;
        }
    }