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

[WIP] Awesome API Documentation #14

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
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Peer Book JavaScript Implementation
===================================
# Peer Book JavaScript Implementation

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
Expand Down Expand Up @@ -38,16 +37,8 @@ Loading this module through a script tag will make the `PeerBook` obj available
<script src="https://unpkg.com/peer-book/dist/index.js"></script>
```

# Usage
## Usage

### `put`

### `get`

### `getByB58String`

### `getByMultihash`

# License
## License

MIT
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"docs": "aegir-docs",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
"repository": {
"type": "git",
"url": "git+https://github.com/diasdavid/js-peer-book.git"
"url": "git+https://github.com/libp2p/js-peer-book.git"
},
"pre-commit": [
"lint",
Expand All @@ -33,18 +34,18 @@
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-peer-book/issues"
"url": "https://github.com/libp2p/js-peer-book/issues"
},
"homepage": "https://github.com/diasdavid/js-peer-book#readme",
"homepage": "https://github.com/libp2p/js-peer-book#readme",
"dependencies": {
"bs58": "^3.0.0"
"bs58": "^3.1.0"
},
"devDependencies": {
"aegir": "^9.1.1",
"async": "^2.1.2",
"aegir": "^9.2.0",
"async": "^2.1.4",
"chai": "^3.5.0",
"multiaddr": "^2.0.3",
"peer-info": "^0.8.0",
"multiaddr": "^2.1.1",
"peer-info": "^0.8.1",
"pre-commit": "^1.1.3"
},
"contributors": [
Expand Down
41 changes: 41 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ const bs58 = require('bs58')

module.exports = PeerBook

/**
* Peer storage for libp2p and IPFS.
*
* @class PeerBook
*/
function PeerBook () {
if (!(this instanceof PeerBook)) {
return new PeerBook()
}

const peers = {}

/**
* Add a new peer.
*
* @param {PeerInfo} peerInfo
* @param {boolean} [replace=false] - Should this replace an existing entry.
* @returns {undefined}
*/
this.put = (peerInfo, replace) => {
if (peers[peerInfo.id.toB58String()] && !replace) {
// peerInfo.replace merges by default
Expand All @@ -19,10 +31,21 @@ function PeerBook () {
peers[peerInfo.id.toB58String()] = peerInfo
}

/**
* List all currently available peers.
*
* @returns {Object} A map of `base58` encoded peer ids to the full `PeerInfo`.
*/
this.getAll = () => {
return peers
}

/**
* Get a peer by `base58` encoded id.
*
* @param {string} b58String - A `base58` encoded peer id.
* @returns {PeerInfo}
*/
this.getByB58String = (b58String) => {
const peerInfo = peers[b58String]
if (peerInfo) {
Expand All @@ -31,17 +54,35 @@ function PeerBook () {
throw new Error('PeerInfo not found')
}

/**
* Get a peer by multihash.
*
* @param {Buffer} multihash - A peer id as multihash.
* @returns {PeerInfo}
*/
this.getByMultihash = (multihash) => {
const b58multihash = bs58.encode(multihash).toString()
return this.getByB58String(b58multihash)
}

/**
* Remove a peer by `base58` encoded id.
*
* @param {string} b58String - A `base58` encoded peer id.
* @returns {undefined}
*/
this.removeByB58String = (b58String) => {
if (peers[b58String]) {
delete peers[b58String]
}
}

/**
* Remove a peer by multihash.
*
* @param {Buffer} multihash - A peer id as multihash.
* @returns {undefined}
*/
this.removeByMultihash = (multihash) => {
const b58multihash = bs58.encode(multihash).toString()
this.removeByB58String(b58multihash)
Expand Down