Browse Source

Prevent crashes from invalid id's in `Clustering.findNode()` (#3166)

* Prevent crashes from invalid id's in `Clustering.findNode()`

Fix for #3163

- Added safeguards in said method, to prevent exceptions happening when invalid id's are passed in.
- Adjusted documentation for said method
- Added notes to flag unused methods in `Clustering.js`

* Removed incorrect NOTE-comments from methods that are indeed used
gemini
wimrijnders 7 years ago
committed by yotamberk
parent
commit
04ecc8575b
2 changed files with 22 additions and 7 deletions
  1. +8
    -3
      docs/network/index.html
  2. +14
    -4
      lib/network/modules/Clustering.js

+ 8
- 3
docs/network/index.html View File

@ -611,13 +611,18 @@ var locales = {
</tr> </tr>
<tr class="collapsible toggle" onclick="toggleTable('methodTable','findNode', this);"> <tr class="collapsible toggle" onclick="toggleTable('methodTable','findNode', this);">
<td colspan="2"><span parent="findNode" class="right-caret" id="method_findNode"></span> findNode( <td colspan="2"><span parent="findNode" class="right-caret" id="method_findNode"></span> findNode(
<code>String nodeId</code>)
<code>String/Number nodeId</code>)
</tr> </tr>
<tr class="hidden" parent="findNode"> <tr class="hidden" parent="findNode">
<td class="midMethods">Returns: Array</td> <td class="midMethods">Returns: Array</td>
<td>Nodes can be in clusters. Clusters can also be in clusters. This function returns and array of <td>Nodes can be in clusters. Clusters can also be in clusters. This function returns and array of
nodeIds
showing where the node is. <br><br> Example:
nodeIds showing where the node is.
<br><br>
If any nodeId in the chain, especially the first passed in as a parameter, is not present in
the current nodes list, an empty array is returned.
<br><br> Example:
cluster 'A' contains cluster 'B', cluster 'A' contains cluster 'B',
cluster 'B' contains cluster 'C', cluster 'B' contains cluster 'C',
cluster 'C' contains node 'fred'. cluster 'C' contains node 'fred'.

+ 14
- 4
lib/network/modules/Clustering.js View File

@ -689,22 +689,32 @@ class ClusterEngine {
/** /**
* Get the stack clusterId's that a certain node resides in. cluster A -> cluster B -> cluster C -> node * Get the stack clusterId's that a certain node resides in. cluster A -> cluster B -> cluster C -> node
* @param nodeId
*
* If a node can't be found in the chain, return an empty array.
*
* @param {string|number} nodeId
* @returns {Array} * @returns {Array}
*/ */
findNode(nodeId) { findNode(nodeId) {
let stack = []; let stack = [];
let max = 100; let max = 100;
let counter = 0; let counter = 0;
let node;
while (this.clusteredNodes[nodeId] !== undefined && counter < max) { while (this.clusteredNodes[nodeId] !== undefined && counter < max) {
stack.push(this.body.nodes[nodeId].id);
node = this.body.nodes[nodeId]
if (node === undefined) return [];
stack.push(node.id);
nodeId = this.clusteredNodes[nodeId].clusterId; nodeId = this.clusteredNodes[nodeId].clusterId;
counter++; counter++;
} }
stack.push(this.body.nodes[nodeId].id);
stack.reverse();
node = this.body.nodes[nodeId]
if (node === undefined) return [];
stack.push(node.id);
stack.reverse();
return stack; return stack;
} }

Loading…
Cancel
Save