Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 0c8d464

Browse files
authored
fix: work in browsers without extra deps (#474)
Removes the k-bucket dep and imports the code here since the random bytes dep it uses doesn't work in browsers. This PR can be reverted if tristanls/k-bucket#53 is ever merged.
1 parent 3a9afd5 commit 0c8d464

File tree

4 files changed

+543
-53
lines changed

4 files changed

+543
-53
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@
178178
"it-pipe": "^3.0.0",
179179
"it-stream-types": "^2.0.1",
180180
"it-take": "^3.0.1",
181-
"k-bucket": "^5.1.0",
182181
"multiformats": "^11.0.0",
183182
"p-defer": "^4.0.0",
184183
"p-queue": "^7.3.4",

src/routing-table/index.ts

+13-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { logger } from '@libp2p/logger'
22
import { PeerSet } from '@libp2p/peer-collections'
3-
// @ts-expect-error no types
4-
import KBuck from 'k-bucket'
53
import Queue from 'p-queue'
64
import * as utils from '../utils.js'
5+
import { KBucket, type PingEventDetails } from './k-bucket.js'
76
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
87
import type { Metric, Metrics } from '@libp2p/interface-metrics'
98
import type { PeerId } from '@libp2p/interface-peer-id'
@@ -17,42 +16,6 @@ export const KBUCKET_SIZE = 20
1716
export const PING_TIMEOUT = 10000
1817
export const PING_CONCURRENCY = 10
1918

20-
export interface KBucketPeer {
21-
id: Uint8Array
22-
peer: PeerId
23-
}
24-
25-
export interface KBucket {
26-
id: Uint8Array
27-
contacts: KBucketPeer[]
28-
dontSplit: boolean
29-
left: KBucket
30-
right: KBucket
31-
}
32-
33-
interface KBucketTreeEvents {
34-
'ping': (oldContacts: KBucketPeer[], newContact: KBucketPeer) => void
35-
'added': (contact: KBucketPeer) => void
36-
'removed': (contact: KBucketPeer) => void
37-
}
38-
39-
export interface KBucketTree {
40-
root: KBucket
41-
localNodeId: Uint8Array
42-
43-
on: <U extends keyof KBucketTreeEvents>(
44-
event: U, listener: KBucketTreeEvents[U]
45-
) => this
46-
47-
closest: (key: Uint8Array, count: number) => KBucketPeer[]
48-
closestPeer: (key: Uint8Array) => KBucketPeer
49-
remove: (key: Uint8Array) => void
50-
add: (peer: KBucketPeer) => void
51-
get: (key: Uint8Array) => Uint8Array
52-
count: () => number
53-
toIterable: () => Iterable<KBucketPeer>
54-
}
55-
5619
export interface RoutingTableInit {
5720
lan: boolean
5821
protocol: string
@@ -76,7 +39,7 @@ export interface RoutingTableComponents {
7639
*/
7740
export class RoutingTable implements Startable {
7841
public kBucketSize: number
79-
public kb?: KBucketTree
42+
public kb?: KBucket
8043
public pingQueue: Queue
8144

8245
private readonly log: Logger
@@ -135,15 +98,15 @@ export class RoutingTable implements Startable {
13598
}
13699
}
137100

138-
const kBuck: KBucketTree = new KBuck({
101+
const kBuck = new KBucket({
139102
localNodeId: await utils.convertPeerId(this.components.peerId),
140103
numberOfNodesPerKBucket: this.kBucketSize,
141104
numberOfNodesToPing: 1
142105
})
143106
this.kb = kBuck
144107

145108
// test whether to evict peers
146-
kBuck.on('ping', this._onPing)
109+
kBuck.addEventListener('ping', this._onPing)
147110

148111
// tag kad-close peers
149112
this._tagPeers(kBuck)
@@ -160,7 +123,7 @@ export class RoutingTable implements Startable {
160123
* - this will lower the chances that connections to them get closed when
161124
* we reach connection limits
162125
*/
163-
_tagPeers (kBuck: KBucketTree): void {
126+
_tagPeers (kBuck: KBucket): void {
164127
let kClosest = new PeerSet()
165128

166129
const updatePeerTags = utils.debounce(() => {
@@ -197,10 +160,10 @@ export class RoutingTable implements Startable {
197160
kClosest = newClosest
198161
})
199162

200-
kBuck.on('added', () => {
163+
kBuck.addEventListener('added', () => {
201164
updatePeerTags()
202165
})
203-
kBuck.on('removed', () => {
166+
kBuck.addEventListener('removed', () => {
204167
updatePeerTags()
205168
})
206169
}
@@ -215,7 +178,12 @@ export class RoutingTable implements Startable {
215178
* `oldContacts` will not be empty and is the list of contacts that
216179
* have not been contacted for the longest.
217180
*/
218-
_onPing (oldContacts: KBucketPeer[], newContact: KBucketPeer): void {
181+
_onPing (evt: CustomEvent<PingEventDetails>): void {
182+
const {
183+
oldContacts,
184+
newContact
185+
} = evt.detail
186+
219187
// add to a queue so multiple ping requests do not overlap and we don't
220188
// flood the network with ping requests if lots of newContact requests
221189
// are received

0 commit comments

Comments
 (0)