Socket
Socket
Sign inDemoInstall

k-bucket

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

k-bucket - npm Package Compare versions

Comparing version 3.3.1 to 4.0.0

4

benchmarks/distance.js
'use strict'
var KBucket = require('../index')
var _0000000100100100 = new Buffer('0124', 'hex')
var _0100000000100100 = new Buffer('4024', 'hex')
var _0000000100100100 = Buffer.from('0124', 'hex')
var _0100000000100100 = Buffer.from('4024', 'hex')

@@ -7,0 +7,0 @@ var hrtime = process.hrtime()

@@ -31,3 +31,2 @@ /*

var bufferEquals = require('buffer-equals')
var randomBytes = require('randombytes')

@@ -39,2 +38,20 @@ var EventEmitter = require('events').EventEmitter

// array1: Uint8Array
// array2: Uint8Array
// Return: boolean
function arrayEquals (array1, array2) {
if (array1 === array2) {
return true
}
if (array1.length !== array2.length) {
return false
}
for (var i = 0, length = array1.length; i < length; ++i) {
if (array1[i] !== array2[i]) {
return false
}
}
return true
}
function createNode () {

@@ -48,3 +65,3 @@ return { contacts: [], dontSplit: false, left: null, right: null }

`function (firstId, secondId) { return distance }` An optional
`distance` function that gets two `id` Buffers
`distance` function that gets two `id` Uint8Arrays
and return distance (as number) between them.

@@ -56,5 +73,4 @@ * `arbiter`: _Function_ _(Default: vectorClock arbiter)_

more details, see [arbiter function](#arbiter-function).
* `localNodeId`: _Buffer_ An optional Buffer representing the local node id.
If not provided, a local node id will be created via
`crypto.randomBytes(20)`.
* `localNodeId`: _Uint8Array_ An optional Uint8Array representing the local node id.
If not provided, a local node id will be created via `randomBytes(20)`.
* `metadata`: _Object_ _(Default: {})_ Optional satellite data to include

@@ -76,3 +92,3 @@ with the k-bucket. `metadata` property is guaranteed not be altered by,

this.localNodeId = options.localNodeId || randomBytes(20)
if (!Buffer.isBuffer(this.localNodeId)) throw new TypeError('localNodeId is not a Buffer')
if (!(this.localNodeId instanceof Uint8Array)) throw new TypeError('localNodeId is not a Uint8Array')
this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket || 20

@@ -106,3 +122,3 @@ this.numberOfNodesToPing = options.numberOfNodesToPing || 3

KBucket.prototype.add = function (contact) {
if (!contact || !Buffer.isBuffer(contact.id)) throw new TypeError('contact.id is not a Buffer')
if (!contact || !(contact.id instanceof Uint8Array)) throw new TypeError('contact.id is not a Uint8Array')
var bitIndex = 0

@@ -146,7 +162,7 @@

// id: Buffer *required* node id
// id: Uint8Array *required* node id
// 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 (!(id instanceof Uint8Array)) throw new TypeError('id is not a Uint8Array')
if (n === undefined) n = Infinity

@@ -192,10 +208,11 @@ if (typeof n !== 'number' || isNaN(n) || n <= 0) throw new TypeError('n is not positive number')

// node: internal object that has 2 leafs: left and right
// id: a Buffer to compare localNodeId with
// bitIndex: the bitIndex to which bit to check in the id Buffer
// id: a Uint8Array to compare localNodeId with
// bitIndex: the bitIndex to which bit to check in the id Uint8Array
KBucket.prototype._determineNode = function (node, id, bitIndex) {
// **NOTE** remember that id is a Buffer and has granularity of
// **NOTE** remember that id is a Uint8Array and has granularity of
// 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

@@ -228,5 +245,5 @@ // if number of bytes is <= no. of bytes described by bitIndex and there

// which branch of the tree to traverse and repeat.
// id: Buffer *required* The ID of the contact to fetch.
// id: Uint8Array *required* The ID of the contact to fetch.
KBucket.prototype.get = function (id) {
if (!Buffer.isBuffer(id)) throw new TypeError('id is not a Buffer')
if (!(id instanceof Uint8Array)) throw new TypeError('id is not a Uint8Array')
var bitIndex = 0

@@ -244,7 +261,7 @@

// node: internal object that has 2 leafs: left and right
// id: Buffer Contact node id.
// id: Uint8Array Contact node id.
// Returns the index of the contact with the given id if it exists
KBucket.prototype._indexOf = function (node, id) {
for (var i = 0; i < node.contacts.length; ++i) {
if (bufferEquals(node.contacts[i].id, id)) return i
if (arrayEquals(node.contacts[i].id, id)) return i
}

@@ -255,5 +272,5 @@

// id: Buffer *required* The ID of the contact to remove.
// id: Uint8Array *required* The ID of the contact to remove.
KBucket.prototype.remove = function (id) {
if (!Buffer.isBuffer(id)) throw new TypeError('id is not a Buffer')
if (!(id instanceof Uint8Array)) throw new TypeError('id is not a Uint8Array')
var bitIndex = 0

@@ -279,3 +296,3 @@

// node: *required* node for splitting
// bitIndex: *required* the bitIndex to which byte to check in the Buffer
// bitIndex: *required* the bitIndex to which byte to check in the Uint8Array
// for navigating the binary tree

@@ -330,3 +347,3 @@ KBucket.prototype._split = function (node, bitIndex) {

// sanity check
if (!bufferEquals(node.contacts[index].id, contact.id)) throw new Error('wrong index for _update')
if (!arrayEquals(node.contacts[index].id, contact.id)) throw new Error('wrong index for _update')

@@ -333,0 +350,0 @@ var incumbent = node.contacts[index]

{
"name": "k-bucket",
"version": "3.3.1",
"version": "4.0.0",
"description": "Kademlia DHT K-bucket implementation as a binary tree",

@@ -31,2 +31,3 @@ "keywords": [

"scripts": {
"benchmark:add": "node benchmarks/add.js",
"benchmark:distance": "node benchmarks/distance.js",

@@ -39,3 +40,2 @@ "coverage": "nyc tape test/*.js",

"dependencies": {
"buffer-equals": "^1.0.3",
"inherits": "^2.0.1",

@@ -45,6 +45,7 @@ "randombytes": "^2.0.3"

"devDependencies": {
"nyc": "^6.4.4",
"standard": "^7.0.1",
"buffer-equals": "^1.0.3",
"nyc": "^11.4.1",
"standard": "^11.0.0",
"tape": "^4.5.1"
}
}

@@ -26,5 +26,15 @@ # 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
})
```

@@ -49,3 +59,3 @@

var contact = {
id: new Buffer('contactId'),
id: Buffer.from('contactId'),
vectorClock: 0

@@ -67,3 +77,3 @@ };

var contact = {
id: new Buffer('workerService'),
id: Buffer.from('workerService'),
workerNodes: {

@@ -133,4 +143,4 @@ '17asdaf7effa2': { host: '127.0.0.1', port: 1337 },

* `firstId`: _Buffer_ Buffer containing first id.
* `secondId`: _Buffer_ Buffer containing second id.
* `firstId`: _Uint8Array_ Uint8Array containing first id.
* `secondId`: _Uint8Array_ Uint8Array containing second id.
* Return: _Integer_ The XOR distance between `firstId` and `secondId`.

@@ -146,4 +156,4 @@

* `distance`: _Function_
`function (firstId, secondId) { return distance }` An optional `distance` function that gets two `id` Buffers and return distance (as number) between them.
* `localNodeId`: _Buffer_ An optional Buffer representing the local node id. If not provided, a local node id will be created via `crypto.randomBytes(20)`.
`function (firstId, secondId) { return distance }` An optional `distance` function that gets two `id` Uint8Arrays and return distance (as number) between them.
* `localNodeId`: _Uint8Array_ An optional Uint8Array representing the local node id. If not provided, a local node id will be created via `crypto.randomBytes(20)`.
* `metadata`: _Object_ _(Default: {})_ Optional satellite data to include with the k-bucket. `metadata` property is guaranteed not be altered, it is provided as an explicit container for users of k-bucket to store implementation-specific data.

@@ -158,3 +168,3 @@ * `numberOfNodesPerKBucket`: _Integer_ _(Default: 20)_ The number of nodes that a k-bucket can contain before being full or split.

* `contact`: _Object_ The contact object to add.
* `id`: _Buffer_ Contact node id.
* `id`: _Uint8Array_ Contact node id.
* Any satellite data that is part of the `contact` object will not be altered, only `id` is used.

@@ -167,3 +177,3 @@ * Return: _Object_ The k-bucket itself.

* `id`: _Buffer_ Contact node id.
* `id`: _Uint8Array_ Contact node id.
* `n`: _Integer_ _(Default: Infinity)_ The maximum number of closest contacts to return.

@@ -182,3 +192,3 @@ * Return: _Array_ Maximum of `n` closest contacts to the node id.

* `id`: _Buffer_ The ID of the `contact` to fetch.
* `id`: _Uint8Array_ The ID of the `contact` to fetch.
* Return: _Object_ The `contact` if available, otherwise null

@@ -196,3 +206,3 @@

* `id`: _Buffer_ The ID of the `contact` to remove.
* `id`: _Uint8Array_ The ID of the `contact` to remove.
* Return: _Object_ The k-bucket itself.

@@ -213,4 +223,4 @@

* `node`: internal object that has 2 leafs: left and right
* `id`: _Buffer_ Id to compare `localNodeId` with.
* `bitIndex`: _Integer_ _(Default: 0)_ The bit index to which bit to check in the `id` Buffer.
* `id`: _Uint8Array_ Id to compare `localNodeId` with.
* `bitIndex`: _Integer_ _(Default: 0)_ The bit index to which bit to check in the `id` Uint8Array.
* Return: _Object_ left leaf if `id` at `bitIndex` is 0, right leaf otherwise.

@@ -223,3 +233,3 @@

* `node`: internal object that has 2 leafs: left and right
* `id`: _Buffer_ Contact node id.
* `id`: _Uint8Array_ Contact node id.
* Return: _Integer_ Index of `contact` with provided `id` if it exists, -1 otherwise.

@@ -234,3 +244,3 @@

* `node`: _Object_ node for splitting
* `bitIndex`: _Integer_ _(Default: 0)_ The bit index to which bit to check in the `id` Buffer.
* `bitIndex`: _Integer_ _(Default: 0)_ The bit index to which bit to check in the `id` Uint8Array.

@@ -246,3 +256,3 @@ Splits the node, redistributes contacts to the new nodes, and marks the node that was split as an inner node of the binary tree of nodes by setting `self.contacts = null`. Also, marks the "far away" node as `dontSplit`.

* `contact`: _Object_ The contact object to update.
* `id`: _Buffer_ Contact node id
* `id`: _Uint8Array_ Contact node id
* Any satellite data that is part of the `contact` object will not be altered, only `id` is used.

@@ -249,0 +259,0 @@

@@ -8,10 +8,10 @@ 'use strict'

(new KBucket()).add(null)
}, /^TypeError: contact.id is not a Buffer$/)
}, /^TypeError: contact.id is not a Uint8Array$/)
t.end()
})
test('throws TypeError if contact.id is not a Buffer', function (t) {
test('throws TypeError if contact.id is not a Uint8Array', function (t) {
t.throws(function () {
(new KBucket()).add({ id: 'foo' })
}, /^TypeError: contact.id is not a Buffer$/)
}, /^TypeError: contact.id is not a Uint8Array$/)
t.end()

@@ -22,3 +22,3 @@ })

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)

@@ -31,5 +31,5 @@ t.same(kBucket.root.contacts, [ contact ])

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)
kBucket.add({ id: new Buffer('a') })
kBucket.add({ id: Buffer.from('a') })
t.same(kBucket.root.contacts.length, 1)

@@ -41,6 +41,6 @@ t.end()

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)
t.same(kBucket.root.contacts.length, 1)
kBucket.add({ id: new Buffer('b') })
kBucket.add({ id: Buffer.from('b') })
t.same(kBucket.root.contacts.length, 2)

@@ -56,3 +56,3 @@ t.true(kBucket.root.contacts[0] === contact) // least-recently-contacted end

t.plan(3 /* numberOfNodesToPing */ + 2)
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00, 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) })
kBucket.on('ping', function (contacts, replacement) {

@@ -65,7 +65,7 @@ t.same(contacts.length, kBucket.numberOfNodesToPing)

}
t.same(replacement, { id: new Buffer([ 0x80, j ]) })
t.same(replacement, { id: Buffer.from([ 0x80, j ]) })
t.end()
})
for (var j = 0; j < kBucket.numberOfNodesPerKBucket + 1; ++j) {
kBucket.add({ id: new Buffer([ 0x80, j ]) }) // make sure all go into "far away" node
kBucket.add({ id: Buffer.from([ 0x80, j ]) }) // make sure all go into "far away" node
}

@@ -77,3 +77,3 @@ })

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.on('added', function (newContact) {

@@ -90,9 +90,9 @@ t.same(newContact, contact)

var kBucket = new KBucket({
localNodeId: new Buffer('') // need non-random localNodeId for deterministic splits
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits
})
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer('' + i) })
kBucket.add({ id: Buffer.from('' + i) })
}
t.same(kBucket.root.contacts, null)
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.on('added', function (newContact) {

@@ -99,0 +99,0 @@ t.same(newContact, contact)

@@ -5,6 +5,6 @@ 'use strict'

test('throws TypeError if contact.id is not a Buffer', function (t) {
test('throws TypeError if contact.id is not a Uint8Array', function (t) {
t.throws(function () {
(new KBucket()).closest('foo', 4)
}, /^TypeError: id is not a Buffer$/)
}, /^TypeError: id is not a Uint8Array$/)
t.end()

@@ -15,3 +15,3 @@ })

t.throws(function () {
(new KBucket()).closest(new Buffer(42), null)
(new KBucket()).closest(Buffer.from([42]), null)
}, /^TypeError: n is not positive number$/)

@@ -23,9 +23,9 @@ t.end()

var kBucket = new KBucket()
for (var i = 0; i < 0x12; ++i) kBucket.add({ id: new Buffer([ i ]) })
var contact = { id: new Buffer([ 0x15 ]) } // 00010101
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)
t.same(contacts.length, 3)
t.same(contacts[0].id, new Buffer([ 0x11 ])) // distance: 00000100
t.same(contacts[1].id, new Buffer([ 0x10 ])) // distance: 00000101
t.same(contacts[2].id, new Buffer([ 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()

@@ -35,5 +35,5 @@ })

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)
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)
t.end()

@@ -44,8 +44,8 @@ })

var kBucket = new KBucket()
for (var i = 0; i < 0x12; ++i) kBucket.add({ id: new Buffer([ i ]) })
var contact = { id: new Buffer([ 0x11 ]) } // 00010001
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, new Buffer([ 0x11 ])) // distance: 00000000
t.same(contacts[1].id, new Buffer([ 0x10 ])) // distance: 00000001
t.same(contacts[2].id, new Buffer([ 0x01 ])) // distance: 00010000
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()

@@ -55,34 +55,34 @@ })

test('closest nodes are returned even if there isn\'t enough in one bucket', function (t) {
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00, 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) })
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; i++) {
kBucket.add({ id: new Buffer([ 0x80, i ]) })
kBucket.add({ id: new Buffer([ 0x01, i ]) })
kBucket.add({ id: Buffer.from([ 0x80, i ]) })
kBucket.add({ id: Buffer.from([ 0x01, i ]) })
}
kBucket.add({ id: new Buffer([ 0x00, 0x01 ]) })
var contact = { id: new Buffer([ 0x00, 0x03 ]) } // 0000000000000011
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, new Buffer([ 0x00, 0x01 ])) // distance: 0000000000000010
t.same(contacts[1].id, new Buffer([ 0x01, 0x03 ])) // distance: 0000000100000000
t.same(contacts[2].id, new Buffer([ 0x01, 0x02 ])) // distance: 0000000100000010
t.same(contacts[3].id, new Buffer([ 0x01, 0x01 ]))
t.same(contacts[4].id, new Buffer([ 0x01, 0x00 ]))
t.same(contacts[5].id, new Buffer([ 0x01, 0x07 ]))
t.same(contacts[6].id, new Buffer([ 0x01, 0x06 ]))
t.same(contacts[7].id, new Buffer([ 0x01, 0x05 ]))
t.same(contacts[8].id, new Buffer([ 0x01, 0x04 ]))
t.same(contacts[9].id, new Buffer([ 0x01, 0x0b ]))
t.same(contacts[10].id, new Buffer([ 0x01, 0x0a ]))
t.same(contacts[11].id, new Buffer([ 0x01, 0x09 ]))
t.same(contacts[12].id, new Buffer([ 0x01, 0x08 ]))
t.same(contacts[13].id, new Buffer([ 0x01, 0x0f ]))
t.same(contacts[14].id, new Buffer([ 0x01, 0x0e ]))
t.same(contacts[15].id, new Buffer([ 0x01, 0x0d ]))
t.same(contacts[16].id, new Buffer([ 0x01, 0x0c ]))
t.same(contacts[17].id, new Buffer([ 0x01, 0x13 ]))
t.same(contacts[18].id, new Buffer([ 0x01, 0x12 ]))
t.same(contacts[19].id, new Buffer([ 0x01, 0x11 ]))
t.same(contacts[20].id, new Buffer([ 0x01, 0x10 ]))
t.same(contacts[21].id, new Buffer([ 0x80, 0x03 ])) // distance: 1000000000000000
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()
})

@@ -13,3 +13,3 @@ 'use strict'

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)

@@ -22,3 +22,3 @@ t.same(kBucket.count(), 1)

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)

@@ -32,14 +32,14 @@ kBucket.add(contact)

var kBucket = new KBucket()
kBucket.add({ id: new Buffer('a') })
kBucket.add({ id: new Buffer('a') })
kBucket.add({ id: new Buffer('b') })
kBucket.add({ id: new Buffer('b') })
kBucket.add({ id: new Buffer('c') })
kBucket.add({ id: new Buffer('d') })
kBucket.add({ id: new Buffer('c') })
kBucket.add({ id: new Buffer('d') })
kBucket.add({ id: new Buffer('e') })
kBucket.add({ id: new Buffer('f') })
kBucket.add({ id: Buffer.from('a') })
kBucket.add({ id: Buffer.from('a') })
kBucket.add({ id: Buffer.from('b') })
kBucket.add({ id: Buffer.from('b') })
kBucket.add({ id: Buffer.from('c') })
kBucket.add({ id: Buffer.from('d') })
kBucket.add({ id: Buffer.from('c') })
kBucket.add({ id: Buffer.from('d') })
kBucket.add({ id: Buffer.from('e') })
kBucket.add({ id: Buffer.from('f') })
t.same(kBucket.count(), 6)
t.end()
})

@@ -15,3 +15,3 @@ 'use strict'

test('localNodeId is a Buffer populated from options if options.localNodeId Buffer is provided', function (t) {
var localNodeId = new Buffer('some length')
var localNodeId = Buffer.from('some length')
var kBucket = new KBucket({ localNodeId: localNodeId })

@@ -18,0 +18,0 @@ t.true(kBucket.localNodeId instanceof Buffer)

@@ -8,3 +8,3 @@ 'use strict'

test('distance between 00000000 and 00000000 is 00000000', function (t) {
t.same(bucket.distance(new Buffer([ 0x00 ]), new Buffer([ 0x00 ])), 0)
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x00 ])), 0)
t.end()

@@ -14,3 +14,3 @@ })

test('distance between 00000000 and 00000001 is 00000001', function (t) {
t.same(bucket.distance(new Buffer([ 0x00 ]), new Buffer([ 0x01 ])), 1)
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x01 ])), 1)
t.end()

@@ -20,3 +20,3 @@ })

test('distance between 00000010 and 00000001 is 00000011', function (t) {
t.same(bucket.distance(new Buffer([ 0x02 ]), new Buffer([ 0x01 ])), 3)
t.same(bucket.distance(Buffer.from([ 0x02 ]), Buffer.from([ 0x01 ])), 3)
t.end()

@@ -26,3 +26,3 @@ })

test('distance between 00000000 and 0000000000000000 is 0000000011111111', function (t) {
t.same(bucket.distance(new Buffer([ 0x00 ]), new Buffer([ 0x00, 0x00 ])), 255)
t.same(bucket.distance(Buffer.from([ 0x00 ]), Buffer.from([ 0x00, 0x00 ])), 255)
t.end()

@@ -32,4 +32,4 @@ })

test('distance between 0000000100100100 and 0100000000100100 is 0100000100000000', function (t) {
t.same(bucket.distance(new Buffer([ 0x01, 0x24 ]), new Buffer([ 0x40, 0x24 ])), 16640)
t.same(bucket.distance(Buffer.from([ 0x01, 0x24 ]), Buffer.from([ 0x40, 0x24 ])), 16640)
t.end()
})

@@ -11,3 +11,3 @@ 'use strict'

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x00 ]), 0), LEFT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x00 ]), 0), LEFT_NODE)
t.end()

@@ -18,3 +18,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x40 ]), 0), LEFT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 0), LEFT_NODE)
t.end()

@@ -25,3 +25,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x40 ]), 1), RIGHT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 1), RIGHT_NODE)
t.end()

@@ -32,3 +32,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x40 ]), 2), LEFT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 2), LEFT_NODE)
t.end()

@@ -39,3 +39,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x40 ]), 9), LEFT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x40 ]), 9), LEFT_NODE)
t.end()

@@ -46,3 +46,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x41 ]), 7), RIGHT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x41 ]), 7), RIGHT_NODE)
t.end()

@@ -53,3 +53,3 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x41, 0x00 ]), 7), RIGHT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x41, 0x00 ]), 7), RIGHT_NODE)
t.end()

@@ -60,4 +60,4 @@ })

var kBucket = new KBucket()
t.same(kBucket._determineNode(ROOT_NODE, new Buffer([ 0x00, 0x41, 0x00 ]), 15), RIGHT_NODE)
t.same(kBucket._determineNode(ROOT_NODE, Buffer.from([ 0x00, 0x41, 0x00 ]), 15), RIGHT_NODE)
t.end()
})

@@ -6,3 +6,3 @@ 'use strict'

test('throws TypeError if id is not a Buffer', function (t) {
test('throws TypeError if id is not a Uint8Array', function (t) {
var kBucket = new KBucket()

@@ -17,3 +17,3 @@ t.throws(function () {

var kBucket = new KBucket()
t.same(kBucket.get(new Buffer('foo')), null)
t.same(kBucket.get(Buffer.from('foo')), null)
t.end()

@@ -24,5 +24,5 @@ })

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)
t.true(bufferEquals(kBucket.get(new Buffer('a')).id, new Buffer('a')))
t.true(bufferEquals(kBucket.get(Buffer.from('a')).id, Buffer.from('a')))
t.end()

@@ -33,9 +33,9 @@ })

var kBucket = new KBucket()
var contact = { id: new Buffer('a'), foo: 'foo', bar: ':p', vectorClock: 0 }
var contact2 = { id: new Buffer('a'), foo: 'bar', vectorClock: 1 }
var contact = { id: Buffer.from('a'), foo: 'foo', bar: ':p', vectorClock: 0 }
var contact2 = { id: Buffer.from('a'), foo: 'bar', vectorClock: 1 }
kBucket.add(contact)
kBucket.add(contact2)
t.true(bufferEquals(kBucket.get(new Buffer('a')).id, new Buffer('a')))
t.same(kBucket.get(new Buffer('a')).foo, 'bar')
t.same(kBucket.get(new Buffer('a')).bar, undefined)
t.true(bufferEquals(kBucket.get(Buffer.from('a')).id, Buffer.from('a')))
t.same(kBucket.get(Buffer.from('a')).foo, 'bar')
t.same(kBucket.get(Buffer.from('a')).bar, undefined)
t.end()

@@ -45,10 +45,10 @@ })

test('get retrieves contact from nested leaf node', function (t) {
var kBucket = new KBucket({localNodeId: new Buffer([ 0x00, 0x00 ])})
var kBucket = new KBucket({localNodeId: Buffer.from([ 0x00, 0x00 ])})
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) {
kBucket.add({ id: new Buffer([ 0x80, i ]) }) // make sure all go into "far away" bucket
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) // make sure all go into "far away" bucket
}
// cause a split to happen
kBucket.add({ id: new Buffer([ 0x00, i ]), find: 'me' })
t.same(kBucket.get(new Buffer([ 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()
})

@@ -7,4 +7,4 @@ 'use strict'

var kBucket = new KBucket()
kBucket.add({ id: new Buffer('a') })
t.same(kBucket._indexOf(kBucket.root, new Buffer('a')), 0)
kBucket.add({ id: Buffer.from('a') })
t.same(kBucket._indexOf(kBucket.root, Buffer.from('a')), 0)
t.end()

@@ -15,5 +15,5 @@ })

var kBucket = new KBucket()
kBucket.add({ id: new Buffer('a') })
t.same(kBucket._indexOf(kBucket.root, new Buffer('b')), -1)
kBucket.add({ id: Buffer.from('a') })
t.same(kBucket._indexOf(kBucket.root, Buffer.from('b')), -1)
t.end()
})

@@ -5,3 +5,3 @@ 'use strict'

test('throws TypeError if contact.id is not a Buffer', function (t) {
test('throws TypeError if contact.id is not a Uint8Array', function (t) {
var kBucket = new KBucket()

@@ -16,12 +16,12 @@ var contact = { id: 'foo' }

test('removing a contact should remove contact from nested buckets', function (t) {
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00, 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) })
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) {
kBucket.add({ id: new Buffer([ 0x80, i ]) }) // make sure all go into "far away" bucket
kBucket.add({ id: Buffer.from([ 0x80, i ]) }) // make sure all go into "far away" bucket
}
// cause a split to happen
kBucket.add({ id: new Buffer([ 0x00, i ]) })
kBucket.add({ id: Buffer.from([ 0x00, i ]) })
// console.log(require('util').inspect(kBucket, false, null))
var contactToDelete = { id: new Buffer([ 0x80, 0x00 ]) }
var contactToDelete = { id: Buffer.from([ 0x80, 0x00 ]) }
t.same(kBucket._indexOf(kBucket.root.right, contactToDelete.id), 0)
kBucket.remove(new Buffer([ 0x80, 0x00 ]))
kBucket.remove(Buffer.from([ 0x80, 0x00 ]))
t.same(kBucket._indexOf(kBucket.root.right, contactToDelete.id), -1)

@@ -34,3 +34,3 @@ t.end()

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.on('removed', function (removedContact) {

@@ -47,9 +47,9 @@ t.same(removedContact, contact)

var kBucket = new KBucket({
localNodeId: new Buffer('') // need non-random localNodeId for deterministic splits
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits
})
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer('' + i) })
kBucket.add({ id: Buffer.from('' + i) })
}
t.false(kBucket.bucket)
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.on('removed', function (removedContact) {

@@ -56,0 +56,0 @@ t.same(removedContact, contact)

@@ -7,3 +7,3 @@ 'use strict'

var kBucket = new KBucket()
kBucket.add({ id: new Buffer('a') })
kBucket.add({ id: Buffer.from('a') })
t.same(kBucket.root.left, null)

@@ -18,3 +18,3 @@ t.same(kBucket.root.right, null)

for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) {
kBucket.add({ id: new Buffer('' + i) })
kBucket.add({ id: Buffer.from('' + i) })
}

@@ -30,3 +30,3 @@ t.same(kBucket.root.left, null)

for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer('' + i) })
kBucket.add({ id: Buffer.from('' + i) })
}

@@ -41,6 +41,6 @@ t.notSame(kBucket.root.left, null)

t.plan(20 /* numberOfNodesPerKBucket */ + 2)
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00 ]) })
var foundContact = {}
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer([ i ]) })
kBucket.add({ id: Buffer.from([ i ]) })
foundContact[i] = false

@@ -66,5 +66,5 @@ }

t.plan(5)
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00 ]) })
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer([ i ]) })
kBucket.add({ id: Buffer.from([ i ]) })
}

@@ -71,0 +71,0 @@ // above algorithm will split left node 4 times and put 0x00 through 0x0f

@@ -13,10 +13,10 @@ 'use strict'

t.plan(22)
var kBucket = new KBucket({ localNodeId: new Buffer([ 0x00, 0x00 ]) })
var kBucket = new KBucket({ localNodeId: Buffer.from([ 0x00, 0x00 ]) })
var expectedIds = []
for (var i = 0; i < kBucket.numberOfNodesPerKBucket; ++i) {
kBucket.add({ id: new Buffer([ 0x80, i ]) }) // make sure all go into "far away" bucket
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: new Buffer([ 0x00, 0x80, i - 1 ]) })
kBucket.add({ id: Buffer.from([ 0x00, 0x80, i - 1 ]) })
// console.log(require('util').inspect(kBucket, {depth: null}))

@@ -23,0 +23,0 @@ var contacts = kBucket.toArray()

@@ -8,3 +8,3 @@ 'use strict'

var kBucket = new KBucket()
var contact = { id: new Buffer('a') }
var contact = { id: Buffer.from('a') }
kBucket.add(contact)

@@ -19,5 +19,5 @@ t.throws(function () {

var kBucket = new KBucket()
var contact = { id: new Buffer('a'), vectorClock: 3 }
var contact = { id: Buffer.from('a'), vectorClock: 3 }
kBucket.add(contact)
kBucket._update(kBucket.root, 0, { id: new Buffer('a'), vectorClock: 2 })
kBucket._update(kBucket.root, 0, { id: Buffer.from('a'), vectorClock: 2 })
t.same(kBucket.root.contacts[0].vectorClock, 3)

@@ -29,5 +29,5 @@ t.end()

var kBucket = new KBucket()
var contact = { id: new Buffer('a'), vectorClock: 3 }
var contact = { id: Buffer.from('a'), vectorClock: 3 }
kBucket.add(contact)
kBucket.add({ id: new Buffer('b') })
kBucket.add({ id: Buffer.from('b') })
kBucket._update(kBucket.root, 0, contact)

@@ -40,6 +40,6 @@ t.same(kBucket.root.contacts[1], contact)

var kBucket = new KBucket()
var contact = { id: new Buffer('a'), old: 'property', vectorClock: 3 }
var contact = { id: Buffer.from('a'), old: 'property', vectorClock: 3 }
kBucket.add(contact)
kBucket.add({ id: new Buffer('b') })
kBucket._update(kBucket.root, 0, { id: new Buffer('a'), newer: 'property', vectorClock: 4 })
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))

@@ -55,4 +55,4 @@ t.same(kBucket.root.contacts[1].vectorClock, 4)

var kBucket = new KBucket()
var contact1 = { id: new Buffer('a'), vectorClock: 1 }
var contact2 = { id: new Buffer('a'), vectorClock: 2 }
var contact1 = { id: Buffer.from('a'), vectorClock: 1 }
var contact2 = { id: Buffer.from('a'), vectorClock: 2 }
kBucket.on('updated', function (oldContact, newContact) {

@@ -70,10 +70,10 @@ t.same(oldContact, contact1)

var kBucket = new KBucket({
localNodeId: new Buffer('') // need non-random localNodeId for deterministic splits
localNodeId: Buffer.from('') // need non-random localNodeId for deterministic splits
})
for (var i = 0; i < kBucket.numberOfNodesPerKBucket + 1; ++i) {
kBucket.add({ id: new Buffer('' + i) })
kBucket.add({ id: Buffer.from('' + i) })
}
t.false(kBucket.bucket)
var contact1 = { id: new Buffer('a'), vectorClock: 1 }
var contact2 = { id: new Buffer('a'), vectorClock: 2 }
var contact1 = { id: Buffer.from('a'), vectorClock: 1 }
var contact2 = { id: Buffer.from('a'), vectorClock: 2 }
kBucket.on('updated', function (oldContact, newContact) {

@@ -80,0 +80,0 @@ t.same(oldContact, contact1)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc