kademlia-routing-table 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -22,12 +22,11 @@ module.exports = class RoutingTable extends EventEmitter {
22
22
  this.emit('row', row)
23
23
  }
24
24
 
25
- if (row.add(node, this.k)) {
26
- this.size++
27
- return true
28
- }
25
+ const len = row.nodes.length
26
+ if (!row.add(node, this.k)) return false
29
27
 
30
- return false
28
+ this.size += row.nodes.length - len
29
+ return true
31
30
  }
32
31
 
33
32
  remove (id) {
@@ -107,6 +106,7 @@ class Row extends EventEmitter {
107
106
  super()
108
107
 
109
108
  this.data = null // can be used be upstream for whatevs
109
+ this.byteOffset = index >> 3
110
110
  this.index = index
111
111
  this.table = table
112
112
  this.nodes = []
@@ -189,8 +189,9 @@ class Row extends EventEmitter {
189
189
  this.emit('remove', this.nodes.pop())
190
190
  }
191
191
 
192
+ // very likely they diverge after a couple of bytes so a simple impl, like this is prop fastest vs Buffer.compare
192
193
  compare (a, b) {
193
- for (let i = this.index; i < a.length; i++) {
194
+ for (let i = this.byteOffset; i < a.length; i++) {
194
195
  const ai = a[i]
195
196
  const bi = b[i]
196
197
  if (ai === bi) continue
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "kademlia-routing-table",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "XOR based routing table used for P2P networks such as a Kademlia DHT.",
5
5
  "main": "index.js",
6
+ "files": [
7
+ "index.js"
8
+ ],
6
9
  "imports": {
7
10
  "events": {
8
11
  "bare": "bare-events",
@@ -1,23 +0,0 @@
1
- name: Build Status
2
- on:
3
- push:
4
- branches:
5
- - master
6
- pull_request:
7
- branches:
8
- - master
9
- jobs:
10
- build:
11
- strategy:
12
- matrix:
13
- node-version: [lts/*]
14
- os: [ubuntu-latest, macos-latest, windows-latest]
15
- runs-on: ${{ matrix.os }}
16
- steps:
17
- - uses: actions/checkout@v3
18
- - name: Use Node.js ${{ matrix.node-version }}
19
- uses: actions/setup-node@v3
20
- with:
21
- node-version: ${{ matrix.node-version }}
22
- - run: npm install
23
- - run: npm test
package/example.js DELETED
@@ -1,26 +0,0 @@
1
- const RoutingTable = require('./')
2
- const { randomBytes } = require('crypto')
3
-
4
- const table = new RoutingTable(randomBytes(32))
5
-
6
- table.on('row', function (row) {
7
- row.on('remove', function (node) {
8
- console.log('remove node', node, 'from row', row.index)
9
- })
10
-
11
- row.on('add', function (node) {
12
- console.log('add node', node, 'to row', row.index)
13
- })
14
-
15
- row.on('full', function (node) {
16
- console.log('row', row.index, 'is full, cannot add', node)
17
- })
18
- })
19
-
20
- for (let i = 0; i < 40; i++) {
21
- table.add({
22
- id: randomBytes(32)
23
- })
24
- }
25
-
26
- console.log('The 20 closest nodes to', table.id, 'are:', table.closest(20))
package/test.js DELETED
@@ -1,32 +0,0 @@
1
- const test = require('brittle')
2
- const { randomBytes } = require('crypto')
3
- const RoutingTable = require('./')
4
-
5
- test('basic', function (t) {
6
- const table = new RoutingTable(id())
7
- const node = { id: id() }
8
-
9
- t.ok(table.add(node))
10
- t.alike(table.closest(id()), [node])
11
- })
12
-
13
- test('random', function (t) {
14
- const table = new RoutingTable(id())
15
-
16
- for (let i = 0; i < 1000; i++) {
17
- table.add({ id: id() })
18
- }
19
-
20
- t.is(table.size, table.toArray().length)
21
-
22
- for (let i = 0; i < 1000; i++) {
23
- if (!table.random()) {
24
- t.fail('should always be a node in there')
25
- break
26
- }
27
- }
28
- })
29
-
30
- function id () {
31
- return randomBytes(32)
32
- }