Comparing version 5.0.0 to 5.1.0
'use strict' | ||
var crypto = require('crypto') | ||
var KBucket = require('../') | ||
const crypto = require('crypto') | ||
const KBucket = require('../') | ||
@@ -11,3 +11,3 @@ function getNextId () { | ||
var seed = process.env.SEED || crypto.randomBytes(32).toString('hex') | ||
let seed = process.env.SEED || crypto.randomBytes(32).toString('hex') | ||
console.log('Seed: ' + seed) | ||
@@ -17,7 +17,7 @@ getNextId() | ||
console.time('KBucket.add') | ||
for (var i = 0; i < 1e1; ++i) { | ||
var bucket = new KBucket() | ||
for (var j = 0; j < 1e4; ++j) bucket.add({ id: getNextId() }) | ||
for (let i = 0; i < 1e1; ++i) { | ||
const bucket = new KBucket() | ||
for (let j = 0; j < 1e4; ++j) bucket.add({ id: getNextId() }) | ||
} | ||
console.timeEnd('KBucket.add') | ||
console.log('Memory: ', process.memoryUsage()) |
'use strict' | ||
var crypto = require('crypto') | ||
var KBucket = require('../') | ||
const crypto = require('crypto') | ||
const KBucket = require('../') | ||
@@ -11,10 +11,10 @@ function getNextId () { | ||
var seed = process.env.SEED || crypto.randomBytes(32).toString('hex') | ||
let seed = process.env.SEED || crypto.randomBytes(32).toString('hex') | ||
console.log('Seed: ' + seed) | ||
getNextId() | ||
var bucket = new KBucket() | ||
for (var j = 0; j < 1e4; ++j) bucket.add({ id: getNextId() }) | ||
const bucket = new KBucket() | ||
for (let j = 0; j < 1e4; ++j) bucket.add({ id: getNextId() }) | ||
console.time('KBucket.closest') | ||
for (var i = 0; i < 1e4; i++) { | ||
for (let i = 0; i < 1e4; i++) { | ||
bucket.closest(seed, 10) | ||
@@ -21,0 +21,0 @@ } |
'use strict' | ||
var KBucket = require('../index') | ||
const KBucket = require('../index') | ||
var _0000000100100100 = Buffer.from('0124', 'hex') | ||
var _0100000000100100 = Buffer.from('4024', 'hex') | ||
const _0000000100100100 = Buffer.from('0124', 'hex') | ||
const _0100000000100100 = Buffer.from('4024', 'hex') | ||
var hrtime = process.hrtime() | ||
for (var i = 0; i < 1e7; i++) { | ||
const hrtime = process.hrtime() | ||
for (let i = 0; i < 1e7; i++) { | ||
KBucket.distance(_0000000100100100, _0100000000100100) | ||
} | ||
var diff = process.hrtime(hrtime) | ||
const diff = process.hrtime(hrtime) | ||
console.log(diff[0] * 1e9 + diff[1]) |
32
index.js
@@ -6,3 +6,3 @@ /* | ||
Copyright (c) 2013-2018 Tristan Slominski | ||
Copyright (c) 2013-2021 Tristan Slominski | ||
@@ -213,3 +213,3 @@ Permission is hereby granted, free of charge, to any person | ||
for (let nodes = [ this.root ], bitIndex = 0; nodes.length > 0 && contacts.length < n;) { | ||
for (let nodes = [this.root], bitIndex = 0; nodes.length > 0 && contacts.length < n;) { | ||
const node = nodes.pop() | ||
@@ -240,3 +240,3 @@ if (node.contacts === null) { | ||
let count = 0 | ||
for (const nodes = [ this.root ]; nodes.length > 0;) { | ||
for (const nodes = [this.root]; nodes.length > 0;) { | ||
const node = nodes.pop() | ||
@@ -388,6 +388,4 @@ if (node.contacts === null) nodes.push(node.right, node.left) | ||
* Returns all the contacts contained in the tree as an array. | ||
* If this is a leaf, return a copy of the bucket. `slice` is used so that we | ||
* don't accidentally leak an internal reference out that might be | ||
* accidentally misused. If this is not a leaf, return the union of the low | ||
* and high branches (themselves also as arrays). | ||
* If this is a leaf, return a copy of the bucket. If this is not a leaf, | ||
* return the union of the low and high branches (themselves also as arrays). | ||
* | ||
@@ -398,3 +396,3 @@ * @return {Array} All of the contacts in the tree, as an array | ||
let result = [] | ||
for (const nodes = [ this.root ]; nodes.length > 0;) { | ||
for (const nodes = [this.root]; nodes.length > 0;) { | ||
const node = nodes.pop() | ||
@@ -408,2 +406,20 @@ if (node.contacts === null) nodes.push(node.right, node.left) | ||
/** | ||
* Similar to `toArray()` but instead of buffering everything up into an | ||
* array before returning it, yields contacts as they are encountered while | ||
* walking the tree. | ||
* | ||
* @return {Iterable} All of the contacts in the tree, as an iterable | ||
*/ | ||
* toIterable () { | ||
for (const nodes = [this.root]; nodes.length > 0;) { | ||
const node = nodes.pop() | ||
if (node.contacts === null) { | ||
nodes.push(node.right, node.left) | ||
} else { | ||
yield * node.contacts | ||
} | ||
} | ||
} | ||
/** | ||
* Updates the contact selected by the arbiter. | ||
@@ -410,0 +426,0 @@ * If the selection is our old contact and the candidate is some new contact |
{ | ||
"name": "k-bucket", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Kademlia DHT K-bucket implementation as a binary tree", | ||
@@ -24,3 +24,4 @@ "keywords": [ | ||
"Nazar Mokrynskyi <nazar@mokrynskyi.com>", | ||
"Jimmy Wärting <jimmy@warting.se>" | ||
"Jimmy Wärting <jimmy@warting.se>", | ||
"Alex Potsides <alex@achingbrain.net>" | ||
], | ||
@@ -42,10 +43,9 @@ "main": "./index.js", | ||
"dependencies": { | ||
"randombytes": "^2.0.3" | ||
"randombytes": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"buffer-equals": "^1.0.3", | ||
"nyc": "^11.7.3", | ||
"standard": "^11.0.1", | ||
"tape": "^4.5.1" | ||
"nyc": "^15.1.0", | ||
"standard": "^16.0.3", | ||
"tape": "^5.1.1" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # k-bucket | ||
[@tristanls](https://github.com/tristanls), [@mikedeboer](https://github.com/mikedeboer), [@deoxxa](https://github.com/deoxxa), [@feross](https://github.com/feross), [@nathanph](https://github.com/nathanph), [@allouis](https://github.com/allouis), [@fanatid](https://github.com/fanatid), [@robertkowalski](https://github.com/robertkowalski), [@nazar-pc](https://github.com/nazar-pc), [@jimmywarting](https://github.com/jimmywarting) | ||
[@tristanls](https://github.com/tristanls), [@mikedeboer](https://github.com/mikedeboer), [@deoxxa](https://github.com/deoxxa), [@feross](https://github.com/feross), [@nathanph](https://github.com/nathanph), [@allouis](https://github.com/allouis), [@fanatid](https://github.com/fanatid), [@robertkowalski](https://github.com/robertkowalski), [@nazar-pc](https://github.com/nazar-pc), [@jimmywarting](https://github.com/jimmywarting), [@achingbrain](https://github.com/achingbrain) | ||
@@ -125,2 +125,3 @@ ## Installation | ||
* [kBucket.toArray()](#kbuckettoarray) | ||
* [kBucket.toIterable()](#kbuckettoiterable) | ||
* [Event 'added'](#event-added) | ||
@@ -210,2 +211,8 @@ * [Event 'ping'](#event-ping) | ||
#### kBucket.toIterable() | ||
* Return: _Iterable_ All of the contacts in the tree, as an iterable | ||
Traverses the tree, yielding contacts as they are encountered. | ||
#### kBucket._determineNode(node, id [, bitIndex = 0]) | ||
@@ -212,0 +219,0 @@ |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
@@ -20,6 +20,6 @@ test('throws TypeError if contact has not property id', function (t) { | ||
test('adding a contact places it in root node', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
t.same(kBucket.root.contacts, [ contact ]) | ||
t.same(kBucket.root.contacts, [contact]) | ||
t.end() | ||
@@ -29,4 +29,4 @@ }) | ||
test('adding an existing contact does not increase number of contacts in root node', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
@@ -39,4 +39,4 @@ kBucket.add({ id: Buffer.from('a') }) | ||
test('adding same contact moves it to the end of the root node (most-recently-contacted end)', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
@@ -55,15 +55,16 @@ t.same(kBucket.root.contacts.length, 1) | ||
t.plan(3 /* numberOfNodesToPing */ + 2) | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) }) | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
let j | ||
kBucket.on('ping', function (contacts, replacement) { | ||
t.same(contacts.length, kBucket.numberOfNodesToPing) | ||
// console.dir(kBucket.root.right.contacts[0]) | ||
for (var i = 0; i < kBucket.numberOfNodesToPing; ++i) { | ||
for (let i = 0; i < kBucket.numberOfNodesToPing; ++i) { | ||
// the least recently contacted end of the node should be pinged | ||
t.true(contacts[i] === kBucket.root.right.contacts[i]) | ||
} | ||
t.same(replacement, { id: Buffer.from([ 0x80, j ]) }) | ||
t.same(replacement, { id: Buffer.from([0x80, j]) }) | ||
t.end() | ||
}) | ||
for (var j = 0; j < kBucket.numberOfNodesPerKBucket + 1; ++j) { | ||
kBucket.add({ id: Buffer.from([ 0x80, j ]) }) // make sure all go into "far away" node | ||
for (j = 0; j < kBucket.numberOfNodesPerKBucket + 1; ++j) { | ||
kBucket.add({ id: Buffer.from([0x80, j]) }) // make sure all go into "far away" node | ||
} | ||
@@ -74,4 +75,4 @@ }) | ||
t.plan(1) | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.on('added', function (newContact) { | ||
@@ -87,10 +88,10 @@ t.same(newContact, contact) | ||
t.plan(2) | ||
var kBucket = new KBucket({ | ||
const kBucket = new KBucket({ | ||
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits | ||
}) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from('' + i) }) | ||
} | ||
t.same(kBucket.root.contacts, null) | ||
var contact = { id: Buffer.from('a') } | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.on('added', function (newContact) { | ||
@@ -97,0 +98,0 @@ t.same(newContact, contact) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
@@ -20,10 +20,10 @@ test('throws TypeError if contact.id is not a Uint8Array', function (t) { | ||
test('closest nodes are returned', function (t) { | ||
var kBucket = new KBucket() | ||
for (var i = 0; i < 0x12; ++i) kBucket.add({ id: Buffer.from([ i ]) }) | ||
var contact = { id: Buffer.from([ 0x15 ]) } // 00010101 | ||
var contacts = kBucket.closest(contact.id, 3) | ||
const kBucket = new KBucket() | ||
for (let i = 0; i < 0x12; ++i) kBucket.add({ id: Buffer.from([i]) }) | ||
const contact = { id: Buffer.from([0x15]) } // 00010101 | ||
const contacts = kBucket.closest(contact.id, 3) | ||
t.same(contacts.length, 3) | ||
t.same(contacts[0].id, Buffer.from([ 0x11 ])) // distance: 00000100 | ||
t.same(contacts[1].id, Buffer.from([ 0x10 ])) // distance: 00000101 | ||
t.same(contacts[2].id, Buffer.from([ 0x05 ])) // distance: 00010000 | ||
t.same(contacts[0].id, Buffer.from([0x11])) // distance: 00000100 | ||
t.same(contacts[1].id, Buffer.from([0x10])) // distance: 00000101 | ||
t.same(contacts[2].id, Buffer.from([0x05])) // distance: 00010000 | ||
t.end() | ||
@@ -33,5 +33,5 @@ }) | ||
test('n is Infinity by default', function (t) { | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) }) | ||
for (var i = 0; i < 1e3; ++i) kBucket.add({ id: Buffer.from([ ~~(i / 256), i % 256 ]) }) | ||
t.true(kBucket.closest(Buffer.from([ 0x80, 0x80 ])).length > 100) | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
for (let i = 0; i < 1e3; ++i) kBucket.add({ id: Buffer.from([~~(i / 256), i % 256]) }) | ||
t.true(kBucket.closest(Buffer.from([0x80, 0x80])).length > 100) | ||
t.end() | ||
@@ -41,9 +41,9 @@ }) | ||
test('closest nodes are returned (including exact match)', function (t) { | ||
var kBucket = new KBucket() | ||
for (var i = 0; i < 0x12; ++i) kBucket.add({ id: Buffer.from([ i ]) }) | ||
var contact = { id: Buffer.from([ 0x11 ]) } // 00010001 | ||
var contacts = kBucket.closest(contact.id, 3) | ||
t.same(contacts[0].id, Buffer.from([ 0x11 ])) // distance: 00000000 | ||
t.same(contacts[1].id, Buffer.from([ 0x10 ])) // distance: 00000001 | ||
t.same(contacts[2].id, Buffer.from([ 0x01 ])) // distance: 00010000 | ||
const kBucket = new KBucket() | ||
for (let i = 0; i < 0x12; ++i) kBucket.add({ id: Buffer.from([i]) }) | ||
const contact = { id: Buffer.from([0x11]) } // 00010001 | ||
const contacts = kBucket.closest(contact.id, 3) | ||
t.same(contacts[0].id, Buffer.from([0x11])) // distance: 00000000 | ||
t.same(contacts[1].id, Buffer.from([0x10])) // distance: 00000001 | ||
t.same(contacts[2].id, Buffer.from([0x01])) // distance: 00010000 | ||
t.end() | ||
@@ -53,34 +53,34 @@ }) | ||
test('closest nodes are returned even if there isn\'t enough in one bucket', function (t) { | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) }) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; i++) { | ||
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) | ||
kBucket.add({ id: Buffer.from([ 0x01, i ]) }) | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket; i++) { | ||
kBucket.add({ id: Buffer.from([0x80, i]) }) | ||
kBucket.add({ id: Buffer.from([0x01, i]) }) | ||
} | ||
kBucket.add({ id: Buffer.from([ 0x00, 0x01 ]) }) | ||
var contact = { id: Buffer.from([ 0x00, 0x03 ]) } // 0000000000000011 | ||
var contacts = kBucket.closest(contact.id, 22) | ||
t.same(contacts[0].id, Buffer.from([ 0x00, 0x01 ])) // distance: 0000000000000010 | ||
t.same(contacts[1].id, Buffer.from([ 0x01, 0x03 ])) // distance: 0000000100000000 | ||
t.same(contacts[2].id, Buffer.from([ 0x01, 0x02 ])) // distance: 0000000100000010 | ||
t.same(contacts[3].id, Buffer.from([ 0x01, 0x01 ])) | ||
t.same(contacts[4].id, Buffer.from([ 0x01, 0x00 ])) | ||
t.same(contacts[5].id, Buffer.from([ 0x01, 0x07 ])) | ||
t.same(contacts[6].id, Buffer.from([ 0x01, 0x06 ])) | ||
t.same(contacts[7].id, Buffer.from([ 0x01, 0x05 ])) | ||
t.same(contacts[8].id, Buffer.from([ 0x01, 0x04 ])) | ||
t.same(contacts[9].id, Buffer.from([ 0x01, 0x0b ])) | ||
t.same(contacts[10].id, Buffer.from([ 0x01, 0x0a ])) | ||
t.same(contacts[11].id, Buffer.from([ 0x01, 0x09 ])) | ||
t.same(contacts[12].id, Buffer.from([ 0x01, 0x08 ])) | ||
t.same(contacts[13].id, Buffer.from([ 0x01, 0x0f ])) | ||
t.same(contacts[14].id, Buffer.from([ 0x01, 0x0e ])) | ||
t.same(contacts[15].id, Buffer.from([ 0x01, 0x0d ])) | ||
t.same(contacts[16].id, Buffer.from([ 0x01, 0x0c ])) | ||
t.same(contacts[17].id, Buffer.from([ 0x01, 0x13 ])) | ||
t.same(contacts[18].id, Buffer.from([ 0x01, 0x12 ])) | ||
t.same(contacts[19].id, Buffer.from([ 0x01, 0x11 ])) | ||
t.same(contacts[20].id, Buffer.from([ 0x01, 0x10 ])) | ||
t.same(contacts[21].id, Buffer.from([ 0x80, 0x03 ])) // distance: 1000000000000000 | ||
kBucket.add({ id: Buffer.from([0x00, 0x01]) }) | ||
const contact = { id: Buffer.from([0x00, 0x03]) } // 0000000000000011 | ||
const contacts = kBucket.closest(contact.id, 22) | ||
t.same(contacts[0].id, Buffer.from([0x00, 0x01])) // distance: 0000000000000010 | ||
t.same(contacts[1].id, Buffer.from([0x01, 0x03])) // distance: 0000000100000000 | ||
t.same(contacts[2].id, Buffer.from([0x01, 0x02])) // distance: 0000000100000010 | ||
t.same(contacts[3].id, Buffer.from([0x01, 0x01])) | ||
t.same(contacts[4].id, Buffer.from([0x01, 0x00])) | ||
t.same(contacts[5].id, Buffer.from([0x01, 0x07])) | ||
t.same(contacts[6].id, Buffer.from([0x01, 0x06])) | ||
t.same(contacts[7].id, Buffer.from([0x01, 0x05])) | ||
t.same(contacts[8].id, Buffer.from([0x01, 0x04])) | ||
t.same(contacts[9].id, Buffer.from([0x01, 0x0b])) | ||
t.same(contacts[10].id, Buffer.from([0x01, 0x0a])) | ||
t.same(contacts[11].id, Buffer.from([0x01, 0x09])) | ||
t.same(contacts[12].id, Buffer.from([0x01, 0x08])) | ||
t.same(contacts[13].id, Buffer.from([0x01, 0x0f])) | ||
t.same(contacts[14].id, Buffer.from([0x01, 0x0e])) | ||
t.same(contacts[15].id, Buffer.from([0x01, 0x0d])) | ||
t.same(contacts[16].id, Buffer.from([0x01, 0x0c])) | ||
t.same(contacts[17].id, Buffer.from([0x01, 0x13])) | ||
t.same(contacts[18].id, Buffer.from([0x01, 0x12])) | ||
t.same(contacts[19].id, Buffer.from([0x01, 0x11])) | ||
t.same(contacts[20].id, Buffer.from([0x01, 0x10])) | ||
t.same(contacts[21].id, Buffer.from([0x80, 0x03])) // distance: 1000000000000000 | ||
// console.log(require('util').inspect(kBucket, false, null)) | ||
t.end() | ||
}) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('count returns 0 when no contacts in bucket', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.same(kBucket.count(), 0) | ||
@@ -12,4 +12,4 @@ t.end() | ||
test('count returns 1 when 1 contact in bucket', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
@@ -21,4 +21,4 @@ t.same(kBucket.count(), 1) | ||
test('count returns 1 when same contact added to bucket twice', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
@@ -31,3 +31,3 @@ kBucket.add(contact) | ||
test('count returns number of added unique contacts', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
kBucket.add({ id: Buffer.from('a') }) | ||
@@ -34,0 +34,0 @@ kBucket.add({ id: Buffer.from('a') }) |
'use strict' | ||
var bufferEquals = require('buffer-equals') | ||
var EventEmitter = require('events').EventEmitter | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const EventEmitter = require('events').EventEmitter | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('localNodeId should be a random SHA-1 if not provided', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.true(kBucket.localNodeId instanceof Buffer) | ||
@@ -15,6 +14,6 @@ t.same(kBucket.localNodeId.length, 20) // SHA-1 is 160 bits (20 bytes) | ||
test('localNodeId is a Buffer populated from options if options.localNodeId Buffer is provided', function (t) { | ||
var localNodeId = Buffer.from('some length') | ||
var kBucket = new KBucket({ localNodeId: localNodeId }) | ||
const localNodeId = Buffer.from('some length') | ||
const kBucket = new KBucket({ localNodeId: localNodeId }) | ||
t.true(kBucket.localNodeId instanceof Buffer) | ||
t.true(bufferEquals(kBucket.localNodeId, localNodeId)) | ||
t.true(localNodeId.equals(kBucket.localNodeId)) | ||
t.end() | ||
@@ -31,3 +30,3 @@ }) | ||
test('check root node', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.same(kBucket.root, { contacts: [], dontSplit: false, left: null, right: null }) | ||
@@ -38,5 +37,5 @@ t.end() | ||
test('inherits from EventEmitter', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.true(kBucket instanceof EventEmitter) | ||
t.end() | ||
}) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
var bucket = new KBucket() | ||
const bucket = new KBucket() | ||
test('distance between 00000000 and 00000000 is 00000000', function (t) { | ||
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x00 ])), 0) | ||
t.same(bucket.distance(Buffer.from([0x00]), Buffer.from([0x00])), 0) | ||
t.end() | ||
@@ -13,3 +13,3 @@ }) | ||
test('distance between 00000000 and 00000001 is 00000001', function (t) { | ||
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x01 ])), 1) | ||
t.same(bucket.distance(Buffer.from([0x00]), Buffer.from([0x01])), 1) | ||
t.end() | ||
@@ -19,3 +19,3 @@ }) | ||
test('distance between 00000010 and 00000001 is 00000011', function (t) { | ||
t.same(bucket.distance(Buffer.from([ 0x02 ]), Buffer.from([ 0x01 ])), 3) | ||
t.same(bucket.distance(Buffer.from([0x02]), Buffer.from([0x01])), 3) | ||
t.end() | ||
@@ -25,3 +25,3 @@ }) | ||
test('distance between 00000000 and 0000000000000000 is 0000000011111111', function (t) { | ||
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x00, 0x00 ])), 255) | ||
t.same(bucket.distance(Buffer.from([0x00]), Buffer.from([0x00, 0x00])), 255) | ||
t.end() | ||
@@ -31,4 +31,4 @@ }) | ||
test('distance between 0000000100100100 and 0100000000100100 is 0100000100000000', function (t) { | ||
t.same(bucket.distance(Buffer.from([ 0x01, 0x24 ]), Buffer.from([ 0x40, 0x24 ])), 16640) | ||
t.same(bucket.distance(Buffer.from([0x01, 0x24]), Buffer.from([0x40, 0x24])), 16640) | ||
t.end() | ||
}) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
var LEFT_NODE = 0 | ||
var RIGHT_NODE = 1 | ||
var ROOT_NODE = { left: LEFT_NODE, right: RIGHT_NODE } | ||
const LEFT_NODE = 0 | ||
const RIGHT_NODE = 1 | ||
const ROOT_NODE = { left: LEFT_NODE, right: RIGHT_NODE } | ||
test('id 00000000, bitIndex 0, should be low', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x00 ]), 0), LEFT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x00]), 0), LEFT_NODE) | ||
t.end() | ||
@@ -16,4 +16,4 @@ }) | ||
test('id 01000000, bitIndex 0, should be low', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 0), LEFT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x40]), 0), LEFT_NODE) | ||
t.end() | ||
@@ -23,4 +23,4 @@ }) | ||
test('id 01000000, bitIndex 1, should be high', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 1), RIGHT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x40]), 1), RIGHT_NODE) | ||
t.end() | ||
@@ -30,4 +30,4 @@ }) | ||
test('id 01000000, bitIndex 2, should be low', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 2), LEFT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x40]), 2), LEFT_NODE) | ||
t.end() | ||
@@ -37,4 +37,4 @@ }) | ||
test('id 01000000, bitIndex 9, should be low', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 9), LEFT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x40]), 9), LEFT_NODE) | ||
t.end() | ||
@@ -44,4 +44,4 @@ }) | ||
test('id 01000001, bitIndex 7, should be high', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x41 ]), 7), RIGHT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x41]), 7), RIGHT_NODE) | ||
t.end() | ||
@@ -51,4 +51,4 @@ }) | ||
test('id 0100000100000000, bitIndex 7, should be high', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x41, 0x00 ]), 7), RIGHT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x41, 0x00]), 7), RIGHT_NODE) | ||
t.end() | ||
@@ -58,5 +58,5 @@ }) | ||
test('id 000000000100000100000000, bitIndex 15, should be high', function (t) { | ||
var kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x00, 0x41, 0x00 ]), 15), RIGHT_NODE) | ||
const kBucket = new KBucket() | ||
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([0x00, 0x41, 0x00]), 15), RIGHT_NODE) | ||
t.end() | ||
}) |
'use strict' | ||
var bufferEquals = require('buffer-equals') | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('throws TypeError if id is not a Uint8Array', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.throws(function () { | ||
@@ -15,3 +14,3 @@ kBucket.get('foo') | ||
test('get retrieves null if no contacts', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.same(kBucket.get(Buffer.from('foo')), null) | ||
@@ -22,6 +21,6 @@ t.end() | ||
test('get retrieves a contact that was added', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
t.true(bufferEquals(kBucket.get(Buffer.from('a')).id, Buffer.from('a'))) | ||
t.true(Buffer.from('a').equals(kBucket.get(Buffer.from('a')).id)) | ||
t.end() | ||
@@ -31,8 +30,8 @@ }) | ||
test('get retrieves most recently added contact if same id', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a'), foo: 'foo', bar: ':p', vectorClock: 0 } | ||
var contact2 = { id: Buffer.from('a'), foo: 'bar', vectorClock: 1 } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a'), foo: 'foo', bar: ':p', vectorClock: 0 } | ||
const contact2 = { id: Buffer.from('a'), foo: 'bar', vectorClock: 1 } | ||
kBucket.add(contact) | ||
kBucket.add(contact2) | ||
t.true(bufferEquals(kBucket.get(Buffer.from('a')).id, Buffer.from('a'))) | ||
t.true(Buffer.from('a').equals(kBucket.get(Buffer.from('a')).id)) | ||
t.same(kBucket.get(Buffer.from('a')).foo, 'bar') | ||
@@ -44,10 +43,11 @@ t.same(kBucket.get(Buffer.from('a')).bar, undefined) | ||
test('get retrieves contact from nested leaf node', function (t) { | ||
var kBucket = new KBucket({localNodeId: Buffer.from([ 0x00, 0x00 ])}) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) // make sure all go into "far away" bucket | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
let i | ||
for (i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([0x80, i]) }) // make sure all go into "far away" bucket | ||
} | ||
// cause a split to happen | ||
kBucket.add({ id: Buffer.from([ 0x00, i ]), find: 'me' }) | ||
t.same(kBucket.get(Buffer.from([ 0x00, i ])).find, 'me') | ||
kBucket.add({ id: Buffer.from([0x00, i]), find: 'me' }) | ||
t.same(kBucket.get(Buffer.from([0x00, i])).find, 'me') | ||
t.end() | ||
}) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('indexOf returns a contact with id that contains the same byte sequence as the test contact', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
kBucket.add({ id: Buffer.from('a') }) | ||
@@ -13,3 +13,3 @@ t.same(kBucket._indexOf(kBucket.root, Buffer.from('a')), 0) | ||
test('indexOf returns -1 if contact is not found', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
kBucket.add({ id: Buffer.from('a') }) | ||
@@ -16,0 +16,0 @@ t.same(kBucket._indexOf(kBucket.root, Buffer.from('b')), -1) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('throws TypeError if contact.id is not a Uint8Array', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: 'foo' } | ||
const kBucket = new KBucket() | ||
const contact = { id: 'foo' } | ||
t.throws(function () { | ||
@@ -15,12 +15,13 @@ kBucket.remove(contact.id) | ||
test('removing a contact should remove contact from nested buckets', function (t) { | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) }) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) // make sure all go into "far away" bucket | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
let i | ||
for (i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([0x80, i]) }) // make sure all go into "far away" bucket | ||
} | ||
// cause a split to happen | ||
kBucket.add({ id: Buffer.from([ 0x00, i ]) }) | ||
kBucket.add({ id: Buffer.from([0x00, i]) }) | ||
// console.log(require('util').inspect(kBucket, false, null)) | ||
var contactToDelete = { id: Buffer.from([ 0x80, 0x00 ]) } | ||
const contactToDelete = { id: Buffer.from([0x80, 0x00]) } | ||
t.same(kBucket._indexOf(kBucket.root.right, contactToDelete.id), 0) | ||
kBucket.remove(Buffer.from([ 0x80, 0x00 ])) | ||
kBucket.remove(Buffer.from([0x80, 0x00])) | ||
t.same(kBucket._indexOf(kBucket.root.right, contactToDelete.id), -1) | ||
@@ -32,4 +33,4 @@ t.end() | ||
t.plan(1) | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.on('removed', function (removedContact) { | ||
@@ -45,10 +46,10 @@ t.same(removedContact, contact) | ||
t.plan(2) | ||
var kBucket = new KBucket({ | ||
const kBucket = new KBucket({ | ||
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits | ||
}) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from('' + i) }) | ||
} | ||
t.false(kBucket.bucket) | ||
var contact = { id: Buffer.from('a') } | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.on('removed', function (removedContact) { | ||
@@ -55,0 +56,0 @@ t.same(removedContact, contact) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('adding a contact does not split node', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
kBucket.add({ id: Buffer.from('a') }) | ||
@@ -15,4 +15,4 @@ t.same(kBucket.root.left, null) | ||
test('adding maximum number of contacts (per node) [20] into node does not split node', function (t) { | ||
var kBucket = new KBucket() | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
const kBucket = new KBucket() | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from('' + i) }) | ||
@@ -27,4 +27,4 @@ } | ||
test('adding maximum number of contacts (per node) + 1 [21] into node splits the node', function (t) { | ||
var kBucket = new KBucket() | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
const kBucket = new KBucket() | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from('' + i) }) | ||
@@ -40,9 +40,9 @@ } | ||
t.plan(20 /* numberOfNodesPerKBucket */ + 2) | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00 ]) }) | ||
var foundContact = {} | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from([ i ]) }) | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00]) }) | ||
const foundContact = {} | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from([i]) }) | ||
foundContact[i] = false | ||
} | ||
var traverse = function (node) { | ||
const traverse = function (node) { | ||
if (node.contacts === null) { | ||
@@ -65,5 +65,5 @@ traverse(node.left) | ||
t.plan(5) | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00 ]) }) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from([ i ]) }) | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00]) }) | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from([i]) }) | ||
} | ||
@@ -75,3 +75,3 @@ // above algorithm will split left node 4 times and put 0x00 through 0x0f | ||
// there will be one "left" node and four "right" nodes (t.expect(5)) | ||
var traverse = function (node, dontSplit) { | ||
const traverse = function (node, dontSplit) { | ||
if (node.contacts === null) { | ||
@@ -78,0 +78,0 @@ traverse(node.left, false) |
'use strict' | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('toArray should return empty array if no contacts', function (t) { | ||
var kBucket = new KBucket() | ||
const kBucket = new KBucket() | ||
t.same(kBucket.toArray().length, 0) | ||
@@ -13,12 +13,13 @@ t.end() | ||
t.plan(22) | ||
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) }) | ||
var expectedIds = [] | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) // make sure all go into "far away" bucket | ||
const kBucket = new KBucket({ localNodeId: Buffer.from([0x00, 0x00]) }) | ||
const expectedIds = [] | ||
let i | ||
for (i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) { | ||
kBucket.add({ id: Buffer.from([0x80, i]) }) // make sure all go into "far away" bucket | ||
expectedIds.push(0x80 * 256 + i) | ||
} | ||
// cause a split to happen | ||
kBucket.add({ id: Buffer.from([ 0x00, 0x80, i - 1 ]) }) | ||
kBucket.add({ id: Buffer.from([0x00, 0x80, i - 1]) }) | ||
// console.log(require('util').inspect(kBucket, {depth: null})) | ||
var contacts = kBucket.toArray() | ||
const contacts = kBucket.toArray() | ||
// console.log(require('util').inspect(contacts, {depth: null})) | ||
@@ -25,0 +26,0 @@ t.same(contacts.length, kBucket.numberOfNodesPerKBucket + 1) |
'use strict' | ||
var bufferEquals = require('buffer-equals') | ||
var test = require('tape') | ||
var KBucket = require('../') | ||
const test = require('tape') | ||
const KBucket = require('../') | ||
test('invalid index results in exception', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a') } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a') } | ||
kBucket.add(contact) | ||
@@ -17,4 +16,4 @@ t.throws(function () { | ||
test('deprecated vectorClock results in contact drop', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a'), vectorClock: 3 } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a'), vectorClock: 3 } | ||
kBucket.add(contact) | ||
@@ -27,4 +26,4 @@ kBucket._update(kBucket.root, 0, { id: Buffer.from('a'), vectorClock: 2 }) | ||
test('equal vectorClock results in contact marked as most recent', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a'), vectorClock: 3 } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a'), vectorClock: 3 } | ||
kBucket.add(contact) | ||
@@ -38,8 +37,8 @@ kBucket.add({ id: Buffer.from('b') }) | ||
test('more recent vectorClock results in contact update and contact being marked as most recent', function (t) { | ||
var kBucket = new KBucket() | ||
var contact = { id: Buffer.from('a'), old: 'property', vectorClock: 3 } | ||
const kBucket = new KBucket() | ||
const contact = { id: Buffer.from('a'), old: 'property', vectorClock: 3 } | ||
kBucket.add(contact) | ||
kBucket.add({ id: Buffer.from('b') }) | ||
kBucket._update(kBucket.root, 0, { id: Buffer.from('a'), newer: 'property', vectorClock: 4 }) | ||
t.true(bufferEquals(kBucket.root.contacts[1].id, contact.id)) | ||
t.true(contact.id.equals(kBucket.root.contacts[1].id)) | ||
t.same(kBucket.root.contacts[1].vectorClock, 4) | ||
@@ -53,5 +52,5 @@ t.same(kBucket.root.contacts[1].old, undefined) | ||
t.plan(2) | ||
var kBucket = new KBucket() | ||
var contact1 = { id: Buffer.from('a'), vectorClock: 1 } | ||
var contact2 = { id: Buffer.from('a'), vectorClock: 2 } | ||
const kBucket = new KBucket() | ||
const contact1 = { id: Buffer.from('a'), vectorClock: 1 } | ||
const contact2 = { id: Buffer.from('a'), vectorClock: 2 } | ||
kBucket.on('updated', function (oldContact, newContact) { | ||
@@ -68,11 +67,11 @@ t.same(oldContact, contact1) | ||
t.plan(3) | ||
var kBucket = new KBucket({ | ||
const kBucket = new KBucket({ | ||
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits | ||
}) | ||
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
for (let i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) { | ||
kBucket.add({ id: Buffer.from('' + i) }) | ||
} | ||
t.false(kBucket.bucket) | ||
var contact1 = { id: Buffer.from('a'), vectorClock: 1 } | ||
var contact2 = { id: Buffer.from('a'), vectorClock: 2 } | ||
const contact1 = { id: Buffer.from('a'), vectorClock: 1 } | ||
const contact2 = { id: Buffer.from('a'), vectorClock: 2 } | ||
kBucket.on('updated', function (oldContact, newContact) { | ||
@@ -79,0 +78,0 @@ t.same(oldContact, contact1) |
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
59796
3
21
1078
303
Updatedrandombytes@^2.1.0