Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

[WIP] Awesome API Documenation #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ build/Release
node_modules

dist
docs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ build/Release
.node_repl_history

test
docs
1 change: 1 addition & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

85 changes: 76 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ function ensureMultiaddr (addr) {
return multiaddr(addr)
}

// Peer represents a peer on the IPFS network
/**
* Creates a new `PeerInfo` instance from an existing `PeerID`.
* @class {PeerInfo}
* @param {PeerId} peerId
* @returns {PeerInfo}
*/
function PeerInfo (peerId) {
if (!(this instanceof PeerInfo)) {
return new PeerInfo(peerId)
Expand All @@ -24,13 +29,41 @@ function PeerInfo (peerId) {
throw new Error('Missing peerId. Use Peer.create(cb) to create one')
}

/**
* The `PeerId` identifying this `PeerInfo`.
*
* @type {PeerId}
*/
this.id = peerId

/**
* A list of `Multiaddr`s that this `peer` can be reached at.
*
* @type {Array<Multiaddr>}
*/
this.multiaddrs = []
const observedMultiaddrs = []

/**
*
* @returns {Array<Array<mixed>>}
*/
this.distinctMultiaddr = () => {
var result = uniqBy(this.multiaddrs, function (item) {
return [item.toOptions().port, item.toOptions().transport].join()
})
return result
}

this.multiaddr = {}

/**
* Adds a new multiaddress that `peer` can be reached at.
* @param {Multiaddr} addr
* @returns {undefined}
* @alias multiaddr.add
* @memberof PeerInfo#
*/
this.multiaddr.add = (addr) => {
addr = ensureMultiaddr(addr)

Expand All @@ -46,7 +79,21 @@ function PeerInfo (peerId) {
}
}

// to prevent multiaddr explosion
/**
* The `addSafe` call, in comparison to `add`, will only add the multiaddr to
* `multiaddrs` if the same multiaddr tries to be added twice.
*
* This is a simple mechanism to prevent `multiaddrs` from becoming bloated with
* unusable addresses, which happens when we exchange observed multiaddrs with
* peers which will not provide a useful multiaddr to be shared to the rest of the
* network (e.g. a multiaddr referring to a peer inside a LAN being shared to the
* outside world).
*
* @param {Multiaddr} addr
* @returns {undefined}
* @alias multiaddr.addSafe
* @memberof PeerInfo#
*/
this.multiaddr.addSafe = (addr) => {
addr = ensureMultiaddr(addr)

Expand All @@ -63,6 +110,14 @@ function PeerInfo (peerId) {
}
}

/**
* Removes a multiaddress instance `addr` from `peer`.
*
* @param {Multiaddr} addr
* @returns {undefined}
* @alias multiaddr.rm
* @memberof PeerInfo#
*/
this.multiaddr.rm = (addr) => {
addr = ensureMultiaddr(addr)

Expand All @@ -74,6 +129,16 @@ function PeerInfo (peerId) {
})
}

/**
* Removes the array of multiaddresses `existing` from `peer`, and adds the array
* of multiaddresses `fresh`.
*
* @param {Array} existing
* @param {Array} fresh
* @returns {undefined}
* @alias multiaddr.replace
* @memberof PeerInfo#
*/
this.multiaddr.replace = (existing, fresh) => {
if (!Array.isArray(existing)) {
existing = [existing]
Expand All @@ -89,17 +154,19 @@ function PeerInfo (peerId) {
})
}

this.distinctMultiaddr = () => {
var result = uniqBy(this.multiaddrs, function (item) {
return [item.toOptions().port, item.toOptions().transport].join()
})
return result
}

// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}

/**
* Creates a new PeerInfo instance and if no `id` is passed it
* generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
* for it.
*
* @param {PeerId=} id
* @param {function(Error, PeerInfo)} callback
* @returns {undefined}
*/
PeerInfo.create = (id, callback) => {
if (typeof id === 'function') {
callback = id
Expand Down