kademlia-routing-table 1.0.5 → 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 +3 -1
- package/package.json +4 -1
- package/.github/workflows/test-node.yml +0 -23
- package/example.js +0 -26
- package/test.js +0 -32
package/index.js
CHANGED
|
@@ -106,6 +106,7 @@ class Row extends EventEmitter {
|
|
|
106
106
|
super()
|
|
107
107
|
|
|
108
108
|
this.data = null // can be used be upstream for whatevs
|
|
109
|
+
this.byteOffset = index >> 3
|
|
109
110
|
this.index = index
|
|
110
111
|
this.table = table
|
|
111
112
|
this.nodes = []
|
|
@@ -188,8 +189,9 @@ class Row extends EventEmitter {
|
|
|
188
189
|
this.emit('remove', this.nodes.pop())
|
|
189
190
|
}
|
|
190
191
|
|
|
192
|
+
// very likely they diverge after a couple of bytes so a simple impl, like this is prop fastest vs Buffer.compare
|
|
191
193
|
compare (a, b) {
|
|
192
|
-
for (let i = this.
|
|
194
|
+
for (let i = this.byteOffset; i < a.length; i++) {
|
|
193
195
|
const ai = a[i]
|
|
194
196
|
const bi = b[i]
|
|
195
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.
|
|
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
|
-
}
|