Skip to content

Commit a6623c1

Browse files
committed
feat: new super simplified API
1 parent 5d4d94e commit a6623c1

File tree

3 files changed

+53
-124
lines changed

3 files changed

+53
-124
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class Node extends libp2p {
168168

169169
> Check if libp2p is started
170170
171+
#### `libp2p.ping(peer [, options], callback)`
172+
173+
> Ping a node in the network
174+
171175
#### `libp2p.peerBook`
172176

173177
> PeerBook instance of the node

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@
3434
},
3535
"homepage": "https://github.com/libp2p/js-libp2p",
3636
"devDependencies": {
37-
"aegir": "^11.0.0",
37+
"aegir": "^11.0.1",
3838
"chai": "^3.5.0",
3939
"dirty-chai": "^1.2.2",
4040
"pre-commit": "^1.2.2"
4141
},
4242
"dependencies": {
4343
"libp2p-ping": "~0.3.2",
4444
"libp2p-swarm": "~0.26.19",
45-
"mafmt": "^2.1.6",
46-
"multiaddr": "^2.2.2",
45+
"multiaddr": "^2.2.3",
4746
"peer-book": "~0.3.1",
4847
"peer-id": "~0.8.4",
4948
"peer-info": "~0.8.4"
@@ -55,4 +54,4 @@
5554
"greenkeeperio-bot <support@greenkeeper.io>",
5655
"mayerwin <mayerwin@users.noreply.github.com>"
5756
]
58-
}
57+
}

src/index.js

+46-120
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ const PeerId = require('peer-id')
55
const PeerInfo = require('peer-info')
66
const PeerBook = require('peer-book')
77
const multiaddr = require('multiaddr')
8-
const mafmt = require('mafmt')
9-
const EE = require('events').EventEmitter
8+
const EventEmitter = require('events').EventEmitter
109
const assert = require('assert')
1110
const Ping = require('libp2p-ping')
1211
const setImmediate = require('async/setImmediate')
1312

1413
exports = module.exports
1514

1615
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
17-
const IPFS_CODE = 421
1816

19-
class Node {
17+
class Node extends EventEmitter {
2018
constructor (_modules, _peerInfo, _peerBook, _options) {
19+
super()
2120
assert(_modules, 'requires modules to equip libp2p with features')
2221
assert(_peerInfo, 'requires a PeerInfo instance')
2322

@@ -26,8 +25,6 @@ class Node {
2625
this.peerBook = _peerBook || new PeerBook()
2726
this.isOnline = false
2827

29-
this.discovery = new EE()
30-
3128
this.swarm = new Swarm(this.peerInfo)
3229

3330
// Attach stream multiplexers
@@ -66,9 +63,7 @@ class Node {
6663
let discoveries = this.modules.discovery
6764
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
6865
discoveries.forEach((discovery) => {
69-
discovery.on('peer', (peerInfo) => {
70-
this.discovery.emit('peer', peerInfo)
71-
})
66+
discovery.on('peer', (peerInfo) => this.emit('peer', peerInfo))
7267
})
7368
}
7469

@@ -142,88 +137,26 @@ class Node {
142137
this.swarm.close(callback)
143138
}
144139

145-
//
146-
// Ping
147-
//
148-
149-
// TODO
150-
pingById (id, callback) {
151-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
152-
callback(new Error('not implemented yet'))
153-
}
154-
155-
// TODO
156-
pingByMultiaddr (maddr, callback) {
157-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
158-
callback(new Error('not implemented yet'))
140+
isOn () {
141+
return this.isOnline
159142
}
160143

161-
pingByPeerInfo (peerInfo, callback) {
162-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
144+
ping (peer, callback) {
145+
assert(this.isOn, OFFLINE_ERROR_MESSAGE)
146+
const peerInfo = this._getPeerInfo(peer)
163147
callback(null, new Ping(this.swarm, peerInfo))
164148
}
165149

166-
//
167-
// Dialing methods
168-
//
169-
170-
// TODO
171-
dialById (id, protocol, callback) {
172-
// NOTE: dialById only works if a previous dial was made. This will
173-
// change once we have PeerRouting
174-
175-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
176-
177-
if (typeof protocol === 'function') {
178-
callback = protocol
179-
protocol = undefined
180-
}
181-
182-
callback(new Error('not implemented yet'))
183-
}
184-
185-
dialByMultiaddr (maddr, protocol, callback) {
186-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
187-
188-
if (typeof protocol === 'function') {
189-
callback = protocol
190-
protocol = undefined
191-
}
192-
193-
if (typeof maddr === 'string') {
194-
maddr = multiaddr(maddr)
195-
}
196-
197-
if (!mafmt.IPFS.matches(maddr.toString())) {
198-
return callback(new Error('multiaddr not valid'))
199-
}
200-
201-
const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
202-
if (tuple[0] === IPFS_CODE) {
203-
return true
204-
}
205-
})[0][1]
206-
207-
let peer
208-
try {
209-
peer = this.peerBook.getByB58String(ipfsIdB58String)
210-
} catch (err) {
211-
peer = new PeerInfo(PeerId.createFromB58String(ipfsIdB58String))
212-
}
213-
214-
peer.multiaddr.add(maddr)
215-
this.dialByPeerInfo(peer, protocol, callback)
216-
}
217-
218-
dialByPeerInfo (peer, protocol, callback) {
150+
dial (peer, protocol, callback) {
219151
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
152+
const peerInfo = this._getPeerInfo(peer)
220153

221154
if (typeof protocol === 'function') {
222155
callback = protocol
223156
protocol = undefined
224157
}
225158

226-
this.swarm.dial(peer, protocol, (err, conn) => {
159+
this.swarm.dial(peerInfo, protocol, (err, conn) => {
227160
if (err) {
228161
return callback(err)
229162
}
@@ -232,59 +165,52 @@ class Node {
232165
})
233166
}
234167

235-
//
236-
// Disconnecting (hangUp) methods
237-
//
238-
239-
hangUpById (id, callback) {
240-
// TODO
241-
callback(new Error('not implemented yet'))
242-
}
243-
244-
hangUpByMultiaddr (maddr, callback) {
168+
hangUp (peer, callback) {
245169
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
170+
const peerInfo = this._getPeerInfo(peer)
246171

247-
if (typeof maddr === 'string') {
248-
maddr = multiaddr(maddr)
249-
}
250-
251-
if (!mafmt.IPFS.matches(maddr.toString())) {
252-
return callback(new Error('multiaddr not valid'))
253-
}
254-
255-
const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
256-
if (tuple[0] === IPFS_CODE) {
257-
return true
258-
}
259-
})[0][1]
260-
261-
try {
262-
const pi = this.peerBook.getByB58String(ipfsIdB58String)
263-
this.hangUpByPeerInfo(pi, callback)
264-
} catch (err) {
265-
// already disconnected
266-
callback()
267-
}
268-
}
269-
270-
hangUpByPeerInfo (peer, callback) {
271-
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
272-
273-
this.peerBook.removeByB58String(peer.id.toB58String())
172+
this.peerBook.removeByB58String(peerInfo.id.toB58String())
274173
this.swarm.hangUp(peer, callback)
275174
}
276175

277-
//
278-
// Protocol multiplexing handling
279-
//
280-
281176
handle (protocol, handlerFunc, matchFunc) {
282177
this.swarm.handle(protocol, handlerFunc, matchFunc)
283178
}
284179

285180
unhandle (protocol) {
286181
this.swarm.unhandle(protocol)
287182
}
183+
184+
/*
185+
* Helper method to check the data type of peer and convert it to PeerInfo
186+
*/
187+
_getPeerInfo (peer) {
188+
let p
189+
switch (peer) {
190+
case PeerInfo.isPeerInfo(peer): p = peer; break
191+
case multiaddr.isMultiaddr(peer): {
192+
const peerIdB58Str = multiaddr.getPeerId(peer)
193+
try {
194+
p = this.peerBook.getByB58String(peerIdB58Str)
195+
} catch (err) {
196+
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
197+
}
198+
p.multiaddr.add(peer)
199+
} break
200+
case PeerId.isPeerId(peer): {
201+
const peerIdB58Str = peer.toB58String()
202+
try {
203+
p = this.peerBook.getByB58String(peerIdB58Str)
204+
} catch (err) {
205+
// TODO this is where PeerRouting comes into place
206+
throw new Error('No knowledge about: ' + peerIdB58Str)
207+
}
208+
} break
209+
default: throw new Error('Peer type not recognized')
210+
}
211+
212+
return p
213+
}
288214
}
289215

290216
module.exports = Node

0 commit comments

Comments
 (0)