mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-09 02:46:03 +00:00
feat: dialProtocol and small refactor
This commit is contained in:
parent
cd43863db6
commit
6651401f0b
20
src/content-routing.js
Normal file
20
src/content-routing.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
findProviders: (key, timeout, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.findProviders(key, timeout, callback)
|
||||||
|
},
|
||||||
|
provide: (key, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.provide(key, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
src/dht.js
Normal file
27
src/dht.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
put: (key, value, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.put(key, value, callback)
|
||||||
|
},
|
||||||
|
get: (key, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.get(key, callback)
|
||||||
|
},
|
||||||
|
getMany (key, nVals, callback) {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.getMany(key, nVals, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
src/get-peer-info.js
Normal file
48
src/get-peer-info.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
const PeerInfo = require('peer-info')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
/*
|
||||||
|
* Helper method to check the data type of peer and convert it to PeerInfo
|
||||||
|
*/
|
||||||
|
return function (peer, callback) {
|
||||||
|
let p
|
||||||
|
// PeerInfo
|
||||||
|
if (PeerInfo.isPeerInfo(peer)) {
|
||||||
|
p = peer
|
||||||
|
// Multiaddr instance or Multiaddr String
|
||||||
|
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
||||||
|
if (typeof peer === 'string') {
|
||||||
|
peer = multiaddr(peer)
|
||||||
|
}
|
||||||
|
|
||||||
|
const peerIdB58Str = peer.getPeerId()
|
||||||
|
if (!peerIdB58Str) {
|
||||||
|
throw new Error(`peer multiaddr instance or string must include peerId`)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
p = node.peerBook.get(peerIdB58Str)
|
||||||
|
} catch (err) {
|
||||||
|
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
||||||
|
}
|
||||||
|
p.multiaddrs.add(peer)
|
||||||
|
|
||||||
|
// PeerId
|
||||||
|
} else if (PeerId.isPeerId(peer)) {
|
||||||
|
const peerIdB58Str = peer.toB58String()
|
||||||
|
try {
|
||||||
|
p = node.peerBook.get(peerIdB58Str)
|
||||||
|
} catch (err) {
|
||||||
|
return node.peerRouting.findPeer(peer, callback)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return setImmediate(() => callback(new Error('peer type not recognized')))
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(() => callback(null, p))
|
||||||
|
}
|
||||||
|
}
|
149
src/index.js
149
src/index.js
@ -7,12 +7,14 @@ const setImmediate = require('async/setImmediate')
|
|||||||
const each = require('async/each')
|
const each = require('async/each')
|
||||||
const series = require('async/series')
|
const series = require('async/series')
|
||||||
|
|
||||||
const Ping = require('libp2p-ping')
|
|
||||||
const Switch = require('libp2p-switch')
|
|
||||||
const PeerId = require('peer-id')
|
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
const PeerBook = require('peer-book')
|
const PeerBook = require('peer-book')
|
||||||
const multiaddr = require('multiaddr')
|
const Switch = require('libp2p-switch')
|
||||||
|
const Ping = require('libp2p-ping')
|
||||||
|
|
||||||
|
const peerRouting = require('./peer-routing')
|
||||||
|
const contentRouting = require('./content-routing')
|
||||||
|
const dht = require('./dht')
|
||||||
|
const getPeerInfo = require('./get-peer-info')
|
||||||
|
|
||||||
exports = module.exports
|
exports = module.exports
|
||||||
|
|
||||||
@ -76,9 +78,6 @@ class Node extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount default protocols
|
|
||||||
Ping.mount(this.switch)
|
|
||||||
|
|
||||||
// dht provided components (peerRouting, contentRouting, dht)
|
// dht provided components (peerRouting, contentRouting, dht)
|
||||||
if (_modules.DHT) {
|
if (_modules.DHT) {
|
||||||
this._dht = new this.modules.DHT(this.switch, {
|
this._dht = new this.modules.DHT(this.switch, {
|
||||||
@ -87,56 +86,14 @@ class Node extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.peerRouting = {
|
this.peerRouting = peerRouting(this)
|
||||||
findPeer: (id, callback) => {
|
this.contentRouting = contentRouting(this)
|
||||||
if (!this._dht) {
|
this.dht = dht(this)
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.findPeer(id, callback)
|
this._getPeerInfo = getPeerInfo(this)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.contentRouting = {
|
// Mount default protocols
|
||||||
findProviders: (key, timeout, callback) => {
|
Ping.mount(this.switch)
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.findProviders(key, timeout, callback)
|
|
||||||
},
|
|
||||||
provide: (key, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.provide(key, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.dht = {
|
|
||||||
put: (key, value, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.put(key, value, callback)
|
|
||||||
},
|
|
||||||
get: (key, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.get(key, callback)
|
|
||||||
},
|
|
||||||
getMany (key, nVals, callback) {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.getMany(key, nVals, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -252,18 +209,22 @@ class Node extends EventEmitter {
|
|||||||
return this._isStarted
|
return this._isStarted
|
||||||
}
|
}
|
||||||
|
|
||||||
ping (peer, callback) {
|
dial (peer, callback) {
|
||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
|
||||||
if (err) {
|
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, new Ping(this.switch, peerInfo))
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
this.switch.dial(peerInfo, (err, conn) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
this.peerBook.put(peerInfo)
|
||||||
|
callback(null, conn)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
dial (peer, protocol, callback) {
|
dialProtocol (peer, protocol, callback) {
|
||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
|
||||||
if (typeof protocol === 'function') {
|
if (typeof protocol === 'function') {
|
||||||
@ -272,14 +233,10 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.switch.dial(peerInfo, protocol, (err, conn) => {
|
this.switch.dial(peerInfo, protocol, (err, conn) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
this.peerBook.put(peerInfo)
|
this.peerBook.put(peerInfo)
|
||||||
callback(null, conn)
|
callback(null, conn)
|
||||||
})
|
})
|
||||||
@ -290,14 +247,21 @@ class Node extends EventEmitter {
|
|||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.switch.hangUp(peerInfo, callback)
|
this.switch.hangUp(peerInfo, callback)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ping (peer, callback) {
|
||||||
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
callback(null, new Ping(this.switch, peerInfo))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
handle (protocol, handlerFunc, matchFunc) {
|
handle (protocol, handlerFunc, matchFunc) {
|
||||||
this.switch.handle(protocol, handlerFunc, matchFunc)
|
this.switch.handle(protocol, handlerFunc, matchFunc)
|
||||||
}
|
}
|
||||||
@ -305,47 +269,6 @@ class Node extends EventEmitter {
|
|||||||
unhandle (protocol) {
|
unhandle (protocol) {
|
||||||
this.switch.unhandle(protocol)
|
this.switch.unhandle(protocol)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Helper method to check the data type of peer and convert it to PeerInfo
|
|
||||||
*/
|
|
||||||
_getPeerInfo (peer, callback) {
|
|
||||||
let p
|
|
||||||
// PeerInfo
|
|
||||||
if (PeerInfo.isPeerInfo(peer)) {
|
|
||||||
p = peer
|
|
||||||
// Multiaddr instance or Multiaddr String
|
|
||||||
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
|
||||||
if (typeof peer === 'string') {
|
|
||||||
peer = multiaddr(peer)
|
|
||||||
}
|
|
||||||
|
|
||||||
const peerIdB58Str = peer.getPeerId()
|
|
||||||
if (!peerIdB58Str) {
|
|
||||||
throw new Error(`peer multiaddr instance or string must include peerId`)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
p = this.peerBook.get(peerIdB58Str)
|
|
||||||
} catch (err) {
|
|
||||||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
|
||||||
}
|
|
||||||
p.multiaddrs.add(peer)
|
|
||||||
|
|
||||||
// PeerId
|
|
||||||
} else if (PeerId.isPeerId(peer)) {
|
|
||||||
const peerIdB58Str = peer.toB58String()
|
|
||||||
try {
|
|
||||||
p = this.peerBook.get(peerIdB58Str)
|
|
||||||
} catch (err) {
|
|
||||||
return this.peerRouting.findPeer(peer, callback)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return setImmediate(() => callback(new Error('peer type not recognized')))
|
|
||||||
}
|
|
||||||
|
|
||||||
setImmediate(() => callback(null, p))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Node
|
module.exports = Node
|
||||||
|
13
src/peer-routing.js
Normal file
13
src/peer-routing.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
findPeer: (id, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.findPeer(id, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user