mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 18:42:15 +00:00
chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
parent
12e48adafa
commit
cb597b57d7
@ -411,7 +411,7 @@ Once a content router succeeds, the iteration will stop. If the DHT is enabled,
|
|||||||
```js
|
```js
|
||||||
// Iterate over the providers found for the given cid
|
// Iterate over the providers found for the given cid
|
||||||
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
|
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
|
||||||
console.log(provider.id, provider.addrs)
|
console.log(provider.id, provider.multiaddrs)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ const log = debug('libp2p:dialer')
|
|||||||
log.error = debug('libp2p:dialer:error')
|
log.error = debug('libp2p:dialer:error')
|
||||||
|
|
||||||
const { DialRequest } = require('./dial-request')
|
const { DialRequest } = require('./dial-request')
|
||||||
const getPeerId = require('../get-peer-id')
|
const getPeer = require('../get-peer')
|
||||||
|
|
||||||
const { codes } = require('../errors')
|
const { codes } = require('../errors')
|
||||||
const {
|
const {
|
||||||
@ -106,8 +106,13 @@ class Dialer {
|
|||||||
* @returns {DialTarget}
|
* @returns {DialTarget}
|
||||||
*/
|
*/
|
||||||
_createDialTarget (peer) {
|
_createDialTarget (peer) {
|
||||||
const peerId = getPeerId(peer, this.peerStore)
|
const { id, multiaddrs } = getPeer(peer)
|
||||||
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(peerId)
|
|
||||||
|
if (multiaddrs) {
|
||||||
|
this.peerStore.addressBook.add(id, multiaddrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
let addrs = this.peerStore.addressBook.getMultiaddrsForPeer(id)
|
||||||
|
|
||||||
// If received a multiaddr to dial, it should be the first to use
|
// If received a multiaddr to dial, it should be the first to use
|
||||||
// But, if we know other multiaddrs for the peer, we should try them too.
|
// But, if we know other multiaddrs for the peer, we should try them too.
|
||||||
@ -117,7 +122,7 @@ class Dialer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: peerId.toB58String(),
|
id: id.toB58String(),
|
||||||
addrs
|
addrs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,13 @@ const errCode = require('err-code')
|
|||||||
const { codes } = require('./errors')
|
const { codes } = require('./errors')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given `peer` to a `PeerId` instance.
|
* Converts the given `peer` to a `Peer` object.
|
||||||
* If a multiaddr is received, the addressBook is updated.
|
* If a multiaddr is received, the addressBook is updated.
|
||||||
* @param {PeerId|Multiaddr|string} peer
|
* @param {PeerId|Multiaddr|string} peer
|
||||||
* @param {PeerStore} peerStore
|
* @param {PeerStore} peerStore
|
||||||
* @returns {PeerId}
|
* @returns {{ id: PeerId, multiaddrs: Array<Multiaddr> }}
|
||||||
*/
|
*/
|
||||||
function getPeerId (peer, peerStore) {
|
function getPeer (peer) {
|
||||||
if (typeof peer === 'string') {
|
if (typeof peer === 'string') {
|
||||||
peer = multiaddr(peer)
|
peer = multiaddr(peer)
|
||||||
}
|
}
|
||||||
@ -31,11 +31,10 @@ function getPeerId (peer, peerStore) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addr && peerStore) {
|
return {
|
||||||
peerStore.addressBook.add(peer, [addr])
|
id: peer,
|
||||||
|
multiaddrs: addr ? [addr] : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return peer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = getPeerId
|
module.exports = getPeer
|
@ -49,7 +49,7 @@ class IdentifyService {
|
|||||||
* @param {Registrar} options.registrar
|
* @param {Registrar} options.registrar
|
||||||
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
|
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
|
||||||
* @param {PeerId} options.peerId The peer running the identify service
|
* @param {PeerId} options.peerId The peer running the identify service
|
||||||
* @param {{ listen: Array<Multiaddr>}} options.addresses The peer aaddresses
|
* @param {{ listen: Array<Multiaddr>}} options.addresses The peer addresses
|
||||||
*/
|
*/
|
||||||
constructor (options) {
|
constructor (options) {
|
||||||
/**
|
/**
|
||||||
|
16
src/index.js
16
src/index.js
@ -11,7 +11,7 @@ const PeerId = require('peer-id')
|
|||||||
const peerRouting = require('./peer-routing')
|
const peerRouting = require('./peer-routing')
|
||||||
const contentRouting = require('./content-routing')
|
const contentRouting = require('./content-routing')
|
||||||
const pubsub = require('./pubsub')
|
const pubsub = require('./pubsub')
|
||||||
const getPeerId = require('./get-peer-id')
|
const getPeer = require('./get-peer')
|
||||||
const { validate: validateConfig } = require('./config')
|
const { validate: validateConfig } = require('./config')
|
||||||
const { codes } = require('./errors')
|
const { codes } = require('./errors')
|
||||||
|
|
||||||
@ -293,11 +293,13 @@ class Libp2p extends EventEmitter {
|
|||||||
* @returns {Promise<Connection|*>}
|
* @returns {Promise<Connection|*>}
|
||||||
*/
|
*/
|
||||||
async dialProtocol (peer, protocols, options) {
|
async dialProtocol (peer, protocols, options) {
|
||||||
const peerId = getPeerId(peer, this.peerStore)
|
const { id, multiaddrs } = getPeer(peer, this.peerStore)
|
||||||
let connection = this.registrar.getConnection(peerId)
|
let connection = this.registrar.getConnection(id)
|
||||||
|
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
connection = await this.dialer.connectToPeer(peer, options)
|
connection = await this.dialer.connectToPeer(peer, options)
|
||||||
|
} else {
|
||||||
|
this.peerStore.addressBook.add(id, multiaddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a protocol was provided, create a new stream
|
// If a protocol was provided, create a new stream
|
||||||
@ -314,9 +316,9 @@ class Libp2p extends EventEmitter {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async hangUp (peer) {
|
async hangUp (peer) {
|
||||||
const peerId = getPeerId(peer)
|
const { id } = getPeer(peer)
|
||||||
|
|
||||||
const connections = this.registrar.connections.get(peerId.toB58String())
|
const connections = this.registrar.connections.get(id.toB58String())
|
||||||
|
|
||||||
if (!connections) {
|
if (!connections) {
|
||||||
return
|
return
|
||||||
@ -335,9 +337,9 @@ class Libp2p extends EventEmitter {
|
|||||||
* @returns {Promise<number>}
|
* @returns {Promise<number>}
|
||||||
*/
|
*/
|
||||||
ping (peer) {
|
ping (peer) {
|
||||||
const peerId = getPeerId(peer)
|
const { id } = getPeer(peer)
|
||||||
|
|
||||||
return ping(this, peerId)
|
return ping(this, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user