This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
91 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,78 +1,121 @@ | ||
'use strict' | ||
|
||
const bs58 = require('bs58') | ||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
|
||
function getB58Str (peer) { | ||
let b58Str | ||
|
||
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') | ||
} | ||
|
||
return b58Str | ||
} | ||
|
||
class PeerBook { | ||
constructor () { | ||
this._peers = {} | ||
} | ||
|
||
// checks if peer exists | ||
// peer can be PeerId, b58String or PeerInfo | ||
has (peer) { | ||
const b58Str = getB58Str(peer) | ||
return Boolean(this._peers[b58Str]) | ||
} | ||
|
||
/** | ||
* Stores a peerInfo, if already exist, only adds the multiaddrs | ||
* Stores a peerInfo, if already exist, merges the new into the old. | ||
* | ||
* @param {PeerInfo} peerInfo | ||
* @param {replace} boolean | ||
* @returns {null} | ||
* @returns {PeerInfo} | ||
*/ | ||
put (peerInfo, replace) { | ||
if (this._peers[peerInfo.id.toB58String()] && !replace) { | ||
// peerInfo.replace merges by default if none to replace are passed | ||
this._peers[peerInfo.id.toB58String()] | ||
.multiaddrs.replace([], peerInfo.multiaddrs.toArray()) | ||
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 | ||
} | ||
|
||
this._peers[peerInfo.id.toB58String()] = peerInfo | ||
} | ||
// peerInfo.replace merges by default if none to replace are passed | ||
peerInfo.multiaddrs.forEach((ma) => localPeerInfo.multiaddrs.add(ma)) | ||
|
||
getAll () { | ||
return this._peers | ||
// 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 | ||
} | ||
|
||
/** | ||
* Get the info to the given PeerId. | ||
* Get the info to the given PeerId, PeerInfo or b58Str id | ||
* | ||
* @param {PeerId} id | ||
* @returns {PeerInfo} | ||
*/ | ||
get (id) { | ||
return this.getByB58String(id.toB58String()) | ||
} | ||
get (peer) { | ||
const b58Str = getB58Str(peer) | ||
|
||
getByB58String (b58String) { | ||
const peerInfo = this._peers[b58String] | ||
const peerInfo = this._peers[b58Str] | ||
|
||
if (peerInfo) { | ||
return peerInfo | ||
} | ||
throw new Error('PeerInfo not found') | ||
} | ||
|
||
getByMultihash (multihash) { | ||
const b58multihash = bs58.encode(multihash).toString() | ||
return this.getByB58String(b58multihash) | ||
} | ||
|
||
removeByB58String (b58String) { | ||
if (this._peers[b58String]) { | ||
delete this._peers[b58String] | ||
} | ||
getAll () { | ||
return this._peers | ||
} | ||
|
||
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>} | ||
*/ | ||
getAddrs (id) { | ||
const info = this.get(id) | ||
getMultiaddrs (peer) { | ||
const info = this.get(peer) | ||
return info.multiaddrs | ||
} | ||
|
||
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.