From a80750170220ff3e38d4957f8b2668a8f6802606 Mon Sep 17 00:00:00 2001 From: Tristan Slominski Date: Tue, 6 Mar 2018 17:45:47 -0600 Subject: [PATCH] Documentation updates, comment format consistency. --- README.md | 18 ++++++++++++++---- index.js | 15 ++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index dee5d76..b4a4106 100644 --- a/README.md +++ b/README.md @@ -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 }) ``` @@ -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 }; @@ -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 } diff --git a/index.js b/index.js index 0e5cc8b..7a1a3ea 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -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