Skip to content

Commit

Permalink
Documentation updates, comment format consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanls committed Mar 6, 2018
1 parent c4970ee commit a807501
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ Kademlia DHT K-bucket implementation as a binary tree.
```javascript
var KBucket = require('k-bucket');

var kBucket = new KBucket({
localNodeId: new Buffer("my node id") // default: random data
var kBucket1 = new KBucket({
localNodeId: Buffer.from("my node id") // default: random data
})
// or without using Buffer (for example, in the browser)
var id = "my node id";
var nodeId = new Uint8Array(id.length);
for (var i = 0, len = nodeId.length; i < len; ++i)
{
nodeId[i] = id.charCodeAt(i);
}
var kBucket2 = new KBucket({
localNodeId: nodeId // default: random data
})
```

Expand All @@ -45,7 +55,7 @@ For example, an `arbiter` function implementing a `vectorClock` mechanism would
```javascript
// contact example
var contact = {
id: new Buffer('contactId'),
id: Buffer.from('contactId'),
vectorClock: 0
};

Expand All @@ -62,7 +72,7 @@ Alternatively, consider an arbiter that implements a Grow-Only-Set CRDT mechanis
```javascript
// contact example
var contact = {
id: new Buffer('workerService'),
id: Buffer.from('workerService'),
workerNodes: {
'17asdaf7effa2': { host: '127.0.0.1', port: 1337 },
'17djsyqeryasu': { host: '127.0.0.1', port: 1338 }
Expand Down
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ var inherits = require('inherits')

module.exports = KBucket

/**
* @param {!Uint8Array} array1
* @param {!Uint8Array} array2
*
* @return {boolean}
*/
// array1: Uint8Array
// array2: Uint8Array
// Return: boolean
function arrayEquals (array1, array2) {
if (array1 === array2) {
return true
Expand Down Expand Up @@ -71,8 +68,7 @@ function createNode () {
returns the desired object to be used for updating the k-bucket. For
more details, see [arbiter function](#arbiter-function).
* `localNodeId`: _Uint8Array_ An optional Uint8Array representing the local node id.
If not provided, a local node id will be created via
`crypto.randomBytes(20)`.
If not provided, a local node id will be created via `randomBytes(20)`.
* `metadata`: _Object_ _(Default: {})_ Optional satellite data to include
with the k-bucket. `metadata` property is guaranteed not be altered by,
it is provided as an explicit container for users of k-bucket to store
Expand Down Expand Up @@ -209,7 +205,8 @@ KBucket.prototype._determineNode = function (node, id, bitIndex) {
// bytes (8 bits), whereas the bitIndex is the _bit_ index (not byte)

// id's that are too short are put in low bucket (1 byte = 8 bits)
// parseInt(bitIndex / 8) finds how many bytes the bitIndex describes
// ~~(bitIndex / 8) finds how many bytes the bitIndex describes, "~~" is
// equivalent to "parseInt"
// bitIndex % 8 checks if we have extra bits beyond byte multiples
// if number of bytes is <= no. of bytes described by bitIndex and there
// are extra bits to consider, this means id has less bits than what
Expand Down

0 comments on commit a807501

Please sign in to comment.