Skip to content

Commit

Permalink
Prob: Outdated code style and contributors.
Browse files Browse the repository at this point in the history
Solv: Updated code style to standard@16.
      Updated contributors.
  • Loading branch information
tristanls committed Feb 5, 2021
1 parent 3d80061 commit 13e8fd6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Kademlia DHT K-bucket implementation as a binary tree.

## Contributors

[@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)

## Installation

Expand Down
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ index.js - Kademlia DHT K-bucket implementation as a binary tree.
The MIT License (MIT)
Copyright (c) 2013-2018 Tristan Slominski
Copyright (c) 2013-2021 Tristan Slominski
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -382,10 +382,8 @@ class KBucket extends EventEmitter {

/**
* 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).
*
* @return {Array} All of the contacts in the tree, as an array
*/
Expand All @@ -401,11 +399,13 @@ class KBucket extends EventEmitter {

/**
* Similar to `toArray()` but instead of buffering everything up into an
* array before returning it, yield contacts as they are encountered while
* walking the tree
* 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;) {
for (const nodes = [this.root]; nodes.length > 0;) {
const node = nodes.pop()
if (node.contacts === null) {
nodes.push(node.right, node.left)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"Kirill Fomichev <[email protected]>",
"Robert Kowalski <[email protected]>",
"Nazar Mokrynskyi <[email protected]>",
"Jimmy Wärting <[email protected]>"
"Jimmy Wärting <[email protected]>",
"Alex Potsides <[email protected]>"
],
"main": "./index.js",
"repository": {
Expand Down
21 changes: 11 additions & 10 deletions test/toIterable.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'
var test = require('tape')
var KBucket = require('../')
const test = require('tape')
const KBucket = require('../')

function collect (iterable) {
var out = []
const out = []

for (const thing of iterable) {
out.push(thing)
Expand All @@ -13,23 +13,24 @@ function collect (iterable) {
}

test('toIterable should return empty iterable if no contacts', function (t) {
var kBucket = new KBucket()
const kBucket = new KBucket()
t.same(collect(kBucket.toIterable()).length, 0)
t.end()
})

test('toIterable should return all contacts in an iterable arranged from low to high buckets', function (t) {
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 = collect(kBucket.toArray())
const contacts = collect(kBucket.toArray())
// console.log(require('util').inspect(contacts, {depth: null}))
t.same(contacts.length, kBucket.numberOfNodesPerKBucket + 1)
t.same(parseInt(contacts[0].id.toString('hex'), 16), 0x80 * 256 + i - 1)
Expand Down

0 comments on commit 13e8fd6

Please sign in to comment.