Comparing version 3.0.3 to 3.1.0
34
index.js
@@ -38,6 +38,2 @@ /* | ||
function defaultArbiter (incumbent, candidate) { | ||
return incumbent.vectorClock > candidate.vectorClock ? incumbent : candidate | ||
} | ||
function createNode () { | ||
@@ -49,2 +45,6 @@ return { contacts: [], dontSplit: false, left: null, right: null } | ||
* `options`: | ||
* `distance`: _Function_ | ||
`function (firstId, secondId) { return distance }` An optional | ||
`distance` function that gets two `id` Buffers | ||
and return distance (as number) between them. | ||
* `arbiter`: _Function_ _(Default: vectorClock arbiter)_ | ||
@@ -73,4 +73,5 @@ `function (incumbent, candidate) { return contact; }` An optional | ||
this.numberOfNodesToPing = options.numberOfNodesToPing || 3 | ||
this.distance = options.distance || KBucket.distance | ||
// use an arbiter from options or vectorClock arbiter by default | ||
this.arbiter = options.arbiter || defaultArbiter | ||
this.arbiter = options.arbiter || KBucket.arbiter | ||
@@ -82,2 +83,6 @@ this.root = createNode() | ||
KBucket.arbiter = function (incumbent, candidate) { | ||
return incumbent.vectorClock > candidate.vectorClock ? incumbent : candidate | ||
} | ||
KBucket.distance = function (firstId, secondId) { | ||
@@ -140,5 +145,6 @@ var distance = 0 | ||
var self = this | ||
function sort (contacts) { | ||
return contacts.slice().sort(function (a, b) { | ||
return KBucket.distance(a.id, id) - KBucket.distance(b.id, id) | ||
return self.distance(a.id, id) - self.distance(b.id, id) | ||
}) | ||
@@ -159,18 +165,2 @@ } | ||
return contacts | ||
/* | ||
function sort (contacts) { | ||
return contacts.slice().sort(function (a, b) { | ||
return KBucket.distance(a, id) - KBucket.distance(b, id) | ||
}) | ||
} | ||
for (var nodes = [ this.root ]; nodes.length > 0 && contacts.length < n;) { | ||
var node = nodes.pop() | ||
if (node.contacts === null) nodes.push(node.right, node.left) | ||
else contacts = contacts.concat(sort(node.contacts)).slice(0, n) | ||
} | ||
return contacts | ||
*/ | ||
} | ||
@@ -177,0 +167,0 @@ |
{ | ||
"name": "k-bucket", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Kademlia DHT K-bucket implementation as a binary tree", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -105,2 +105,3 @@ # k-bucket | ||
**Public API** | ||
* [KBucket.arbiter(incumbent, candidate)](#kbucketarbiterincumbent-candidate) | ||
* [KBucket.distance(firstId, secondId)](#kbucketdistancefirstid-secondid) | ||
@@ -119,2 +120,10 @@ * [new KBucket(options)](#new-kbucketoptions) | ||
#### KBucket.arbiter(incumbent, candidate) | ||
* `incumbent`: _Object_ Contact currently stored in the k-bucket. | ||
* `candidate`: _Object_ Contact being added to the k-bucket. | ||
* Return: _Object_ Contact to updated the k-bucket with. | ||
Default arbiter function for contacts with the same `id`. Uses `contact.vectorClock` to select which contact to update the k-bucket with. Contact with larger `vectorClock` field will be selected. If `vectorClock` is the same, `candidat` will be selected. | ||
#### KBucket.distance(firstId, secondId) | ||
@@ -126,3 +135,3 @@ | ||
Finds the XOR distance between firstId and secondId. | ||
Default distance function. Finds the XOR distance between firstId and secondId. | ||
@@ -133,3 +142,5 @@ #### new KBucket(options) | ||
* `arbiter`: _Function_ _(Default: vectorClock arbiter)_ | ||
`function (incumbent, candidate) { return contact; }` An optional `arbiter` function that givent two `contact` objects with the same `id` returns the desired object to be used for updating the k-bucket. For more details, see [arbiter function](#arbiter-function). | ||
`function (incumbent, candidate) { return contact; }` An optional `arbiter` function that given two `contact` objects with the same `id` returns the desired object to be used for updating the k-bucket. For more details, see [arbiter function](#arbiter-function). | ||
* `distance`: _Function_ | ||
`function (firstId, secondId) { return distance }` An optional `distance` function that gets two `id` Buffers and return distance (as number) between them. | ||
* `localNodeId`: _Buffer_ An optional Buffer representing the local node id. If not provided, a local node id will be created via `crypto.randomBytes(20)`. | ||
@@ -136,0 +147,0 @@ * `numberOfNodesPerKBucket`: _Integer_ _(Default: 20)_ The number of nodes that a k-bucket can contain before being full or split. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51551
280
887