This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from libp2p/feat/advanced-peer-book
feat: new peer-book feat: new shiny API
- Loading branch information
Showing
4 changed files
with
194 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,121 @@ | ||
'use strict' | ||
|
||
const bs58 = require('bs58') | ||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
|
||
module.exports = PeerBook | ||
function getB58Str (peer) { | ||
let b58Str | ||
|
||
function PeerBook () { | ||
if (!(this instanceof PeerBook)) { | ||
return new PeerBook() | ||
if (typeof peer === 'string') { | ||
b58Str = peer | ||
} else if (Buffer.isBuffer(peer)) { | ||
b58Str = bs58.encode(peer).toString() | ||
} else if (PeerId.isPeerId(peer)) { | ||
b58Str = peer.toB58String() | ||
} else if (PeerInfo.isPeerInfo(peer)) { | ||
b58Str = peer.id.toB58String() | ||
} else { | ||
throw new Error('not valid PeerId or PeerInfo, or B58Str') | ||
} | ||
|
||
const peers = {} | ||
return b58Str | ||
} | ||
|
||
this.put = (peerInfo, replace) => { | ||
if (peers[peerInfo.id.toB58String()] && !replace) { | ||
// peerInfo.replace merges by default | ||
peers[peerInfo.id.toB58String()].multiaddr.replace([], peerInfo.multiaddrs) | ||
} | ||
peers[peerInfo.id.toB58String()] = peerInfo | ||
class PeerBook { | ||
constructor () { | ||
this._peers = {} | ||
} | ||
|
||
this.getAll = () => { | ||
return peers | ||
// checks if peer exists | ||
// peer can be PeerId, b58String or PeerInfo | ||
has (peer) { | ||
const b58Str = getB58Str(peer) | ||
return Boolean(this._peers[b58Str]) | ||
} | ||
|
||
/** | ||
* Get the info to the given PeerId. | ||
* Stores a peerInfo, if already exist, merges the new into the old. | ||
* | ||
* @param {PeerId} id | ||
* @param {PeerInfo} peerInfo | ||
* @param {replace} boolean | ||
* @returns {PeerInfo} | ||
*/ | ||
this.get = (id) => { | ||
return this.getByB58String(id.toB58String()) | ||
put (peerInfo, replace) { | ||
const localPeerInfo = this._peers[peerInfo.id.toB58String()] | ||
|
||
// insert if doesn't exist or replace if replace flag is true | ||
if (!localPeerInfo || replace) { | ||
this._peers[peerInfo.id.toB58String()] = peerInfo | ||
return peerInfo | ||
} | ||
|
||
// peerInfo.replace merges by default if none to replace are passed | ||
peerInfo.multiaddrs.forEach((ma) => localPeerInfo.multiaddrs.add(ma)) | ||
|
||
// pass active connection state | ||
const ma = peerInfo.isConnected() | ||
if (ma) { | ||
localPeerInfo.connect(ma) | ||
} | ||
|
||
// pass known protocols | ||
peerInfo.protocols.forEach((p) => localPeerInfo.protocols.add(p)) | ||
|
||
if (!localPeerInfo.id.privKey && peerInfo.id.privKey) { | ||
localPeerInfo.id.privKey = peerInfo.id.privKey | ||
} | ||
|
||
if (!localPeerInfo.id.pubKey && peerInfo.id.pubKey) { | ||
localPeerInfo.id.pubKey = peerInfo.id.pubKey | ||
} | ||
|
||
return localPeerInfo | ||
} | ||
|
||
this.getByB58String = (b58String) => { | ||
const peerInfo = peers[b58String] | ||
/** | ||
* Get the info to the given PeerId, PeerInfo or b58Str id | ||
* | ||
* @param {PeerId} id | ||
* @returns {PeerInfo} | ||
*/ | ||
get (peer) { | ||
const b58Str = getB58Str(peer) | ||
|
||
const peerInfo = this._peers[b58Str] | ||
|
||
if (peerInfo) { | ||
return peerInfo | ||
} | ||
throw new Error('PeerInfo not found') | ||
} | ||
|
||
this.getByMultihash = (multihash) => { | ||
const b58multihash = bs58.encode(multihash).toString() | ||
return this.getByB58String(b58multihash) | ||
} | ||
|
||
this.removeByB58String = (b58String) => { | ||
if (peers[b58String]) { | ||
delete peers[b58String] | ||
} | ||
getAll () { | ||
return this._peers | ||
} | ||
|
||
this.removeByMultihash = (multihash) => { | ||
const b58multihash = bs58.encode(multihash).toString() | ||
this.removeByB58String(b58multihash) | ||
getAllArray () { | ||
return Object.keys(this._peers).map((b58Str) => this._peers[b58Str]) | ||
} | ||
|
||
/** | ||
* Return all multiaddrs for a given PeerId. | ||
* | ||
* @param {PeerId} id | ||
* @param {PeerId, PeerInfo, Multihash, B58Str} id | ||
* @returns {Array<Multiaddr>} | ||
*/ | ||
this.getAddrs = (id) => { | ||
const info = this.get(id) | ||
getMultiaddrs (peer) { | ||
const info = this.get(peer) | ||
return info.multiaddrs | ||
} | ||
|
||
// TODO serialize PeerBook into MerkleDAG Objects | ||
remove (peer) { | ||
const b58Str = getB58Str(peer) | ||
|
||
if (this._peers[b58Str]) { | ||
delete this._peers[b58Str] | ||
} | ||
} | ||
} | ||
|
||
module.exports = PeerBook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.