Comparing version 3.1.0 to 3.2.0
@@ -95,3 +95,3 @@ /* | ||
KBucket.prototype.add = function (contact) { | ||
if (!Buffer.isBuffer(contact.id)) throw new TypeError('contact.id is not a Buffer') | ||
if (!contact || !Buffer.isBuffer(contact.id)) throw new TypeError('contact.id is not a Buffer') | ||
var bitIndex = 0 | ||
@@ -136,6 +136,8 @@ | ||
// id: Buffer *required* node id | ||
// n: Integer *required* maximum number of closest contacts to return | ||
// n: Integer (Default: Infinity) maximum number of closest contacts to return | ||
// Return: Array of maximum of `n` closest contacts to the node id | ||
KBucket.prototype.closest = function (id, n) { | ||
if (!Buffer.isBuffer(id)) throw new TypeError('id is not a Buffer') | ||
if (n === undefined) n = Infinity | ||
if (typeof n !== 'number' || isNaN(n) || n <= 0) throw new TypeError('n is not positive number') | ||
var contacts = [] | ||
@@ -159,2 +161,3 @@ | ||
} | ||
console.log(nodes.length, contacts.length, n) | ||
} | ||
@@ -242,3 +245,3 @@ | ||
// id: Buffer *required* he ID of the contact to remove. | ||
// id: Buffer *required* The ID of the contact to remove. | ||
KBucket.prototype.remove = function (id) { | ||
@@ -245,0 +248,0 @@ if (!Buffer.isBuffer(id)) throw new TypeError('id is not a Buffer') |
{ | ||
"name": "k-bucket", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Kademlia DHT K-bucket implementation as a binary tree", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -41,3 +41,3 @@ # k-bucket | ||
This *k-bucket* implementation implements a conflict resolution mechanism using an `arbiter` function. The purpose of the `arbiter` is to choose between two `contact` objects with the same `id` but perhaps different properties and determine which one should be stored. As the `arbiter` function returns the actual object to be stored, it does not need to make an either/or choice, but instead could perform some sort of operation and return the result as a new object that would then be stored. See [kBucket._update(contact, index)](#kbucket_updatecontact-index) for detailed semantics of which `contact` (`incumbent` or `candidate`) is selected. | ||
This *k-bucket* implementation implements a conflict resolution mechanism using an `arbiter` function. The purpose of the `arbiter` is to choose between two `contact` objects with the same `id` but perhaps different properties and determine which one should be stored. As the `arbiter` function returns the actual object to be stored, it does not need to make an either/or choice, but instead could perform some sort of operation and return the result as a new object that would then be stored. See [kBucket._update(node, index, contact)](#kbucket_updatenode-index-contact) for detailed semantics of which `contact` (`incumbent` or `candidate`) is selected. | ||
@@ -110,3 +110,3 @@ For example, an `arbiter` function implementing a `vectorClock` mechanism would look something like: | ||
* [kBucket.add(contact)](#kbucketaddcontact) | ||
* [kBucket.closest(id, n)](#kbucketclosestid-n) | ||
* [kBucket.closest(id [, n = Infinity])](#kbucketclosestid--n--infinity) | ||
* [kBucket.count()](#kbucketcount) | ||
@@ -159,6 +159,6 @@ * [kBucket.get(id)](#kbucketgetid) | ||
#### kBucket.closest(id, n) | ||
#### kBucket.closest(id [, n = Infinity]) | ||
* `id`: _Buffer_ Contact node id. | ||
* `n`: _Integer_ The maximum number of closest contacts to return. | ||
* `n`: _Integer_ _(Default: Infinity)_ The maximum number of closest contacts to return. | ||
* Return: _Array_ Maximum of `n` closest contacts to the node id. | ||
@@ -194,3 +194,3 @@ | ||
#### kBucket._determineNode(node, id, [bitIndex]) | ||
#### kBucket._determineNode(node, id [, bitIndex = 0]) | ||
@@ -214,3 +214,3 @@ _**CAUTION: reserved for internal use**_ | ||
#### kBucket._split(node, [bitIndex]) | ||
#### kBucket._split(node [, bitIndex]) | ||
@@ -217,0 +217,0 @@ _**CAUTION: reserved for internal use**_ |
@@ -5,8 +5,13 @@ 'use strict' | ||
test('throws TypeError if contact has not property id', function (t) { | ||
t.throws(function () { | ||
(new KBucket()).add(null) | ||
}, /^TypeError: contact.id is not a Buffer$/) | ||
t.end() | ||
}) | ||
test('throws TypeError if contact.id is not a Buffer', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: 'foo' } | ||
t.throws(function () { | ||
kBucket.add(contact) | ||
}) | ||
(new KBucket()).add({ id: 'foo' }) | ||
}, /^TypeError: contact.id is not a Buffer$/) | ||
t.end() | ||
@@ -13,0 +18,0 @@ }) |
@@ -6,10 +6,15 @@ 'use strict' | ||
test('throws TypeError if contact.id is not a Buffer', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: 'foo' } | ||
t.throws(function () { | ||
kBucket.closest(contact.id, 4) | ||
}) | ||
(new KBucket()).closest('foo', 4) | ||
}, /^TypeError: id is not a Buffer$/) | ||
t.end() | ||
}) | ||
test('throw TypeError if n is not number', function (t) { | ||
t.throws(function () { | ||
(new KBucket()).closest(new Buffer(42), null) | ||
}, /^TypeError: n is not positive number$/) | ||
t.end() | ||
}) | ||
test('closest nodes are returned', function (t) { | ||
@@ -27,2 +32,9 @@ var kBucket = new KBucket() | ||
test('n is Infinity by default', function (t) { | ||
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00, 0x00 ]) }) | ||
for (var i = 0; i < 1e3; ++i) kBucket.add({ id: new Buffer([ ~~(i / 256), i % 256 ]) }) | ||
t.true(kBucket.closest(new Buffer([ 0x80, 0x80 ])).length > 100) | ||
t.end() | ||
}) | ||
test('closest nodes are returned (including exact match)', function (t) { | ||
@@ -29,0 +41,0 @@ var kBucket = new KBucket() |
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
52484
904