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

Commit 598fd94

Browse files
committed
feat: emit peers every 10 secs
1 parent e556d8f commit 598fd94

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"aegir": "^11.0.2",
3434
"chai": "^4.1.0",
3535
"libp2p-swarm": "~0.31.0",
36-
"libp2p-tcp": "~0.10.1",
36+
"libp2p-tcp": "~0.10.2",
3737
"pre-commit": "^1.2.2"
3838
},
3939
"dependencies": {
@@ -56,4 +56,4 @@
5656
"Nuno Nogueira <[email protected]>",
5757
"Richard Littauer <[email protected]>"
5858
]
59-
}
59+
}

src/index.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const EventEmitter = require('events').EventEmitter
77
const debug = require('debug')
8-
const includes = require('lodash/includes')
98
const setImmediate = require('async/setImmediate')
109

1110
const log = debug('libp2p:railing')
@@ -15,49 +14,36 @@ class Railing extends EventEmitter {
1514
constructor (bootstrapers) {
1615
super()
1716
this.bootstrapers = bootstrapers
17+
this.interval = null
1818
}
1919

2020
start (callback) {
2121
setImmediate(() => callback())
22-
setImmediate(() => {
23-
this.bootstrapers.forEach((candidate) => {
24-
candidate = multiaddr(candidate)
22+
if (this.interval) { return }
2523

26-
let ma
27-
if (includes(candidate.protoNames(), 'ipfs')) {
28-
ma = candidate.decapsulate('ipfs')
29-
}
24+
this.interval = setInterval(() => {
25+
this.bootstrapers.forEach((candidate) => {
26+
const ma = multiaddr(candidate)
3027

31-
// TODO: switch for multiaddr.getPeerId once merged
32-
let peerIdB58Str
33-
try {
34-
peerIdB58Str = candidate.stringTuples().filter((tuple) => {
35-
if (tuple[0] === candidate.protos().filter((proto) => {
36-
return proto.name === 'ipfs'
37-
})[0].code) {
38-
return true
39-
}
40-
})[0][1]
41-
} catch (e) {
42-
throw new Error('Error extracting IPFS id from multiaddr', e)
43-
}
28+
const peerId = PeerId.createFromB58String(ma.getPeerId())
4429

45-
const peerId = PeerId.createFromB58String(peerIdB58Str)
4630
PeerInfo.create(peerId, (err, peerInfo) => {
47-
if (err) {
48-
return log.error('Error creating PeerInfo from bootstrap peer', err)
49-
}
31+
if (err) { return log.error('Invalid bootstrap peer id', err) }
5032

5133
peerInfo.multiaddrs.add(ma)
5234

5335
this.emit('peer', peerInfo)
5436
})
5537
})
56-
})
38+
}, 10000)
5739
}
5840

5941
stop (callback) {
6042
setImmediate(callback)
43+
if (this.interval) {
44+
clearInterval(this.interval)
45+
this.interval = null
46+
}
6147
}
6248
}
6349

0 commit comments

Comments
 (0)