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

Commit 365b130

Browse files
cr - round1
1 parent 2061b34 commit 365b130

File tree

3 files changed

+107
-51
lines changed

3 files changed

+107
-51
lines changed

README.md

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
1-
peer-info JavaScript implementation
2-
===================================
1+
# js-peer-info
32

43
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
54
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6-
[![Build Status](https://travis-ci.org/diasdavid/js-peer-info.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-info)
7-
[![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-peer-info?branch=master)
8-
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-info.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-info)
5+
[![Build Status](https://travis-ci.org/multiformats/js-peer-info.svg?style=flat-square)](https://travis-ci.org/multiformats/js-peer-info)
6+
[![Coverage Status](https://coveralls.io/repos/github/multiformats/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/multiformats/js-peer-info?branch=master)
7+
[![Dependency Status](https://david-dm.org/multiformats/js-peer-info.svg?style=flat-square)](https://david-dm.org/multiformats/js-peer-info)
98
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
9+
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
1010

1111
> A PeerInfo object contains information about a
1212
> [PeerID](https://github.com/libp2p/js-peer-id) and its
1313
> [multiaddrs](https://github.com/libp2p/js-multiaddr). This module is used by
1414
> [IPFS](https://github.com/ipfs/ipfs) and
1515
> [libp2p](https://github.com/libp2p/js-libp2p).
1616
17-
# Example
17+
- [Installation](#installation)
18+
- [npm](#npm)
19+
- [Node.JS, Browserify, Webpack](#nodejs-browserify-webpack)
20+
- [Browser: `<script>` Tag](#browser-script-tag)
21+
- [Usage](#usage)
22+
- [API](#api)
23+
- [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback)
24+
- [`new PeerInfo(id)`](#new-peerinfoid)
25+
- [`multiaddrs`](#multiaddrs)
26+
- [`multiaddr.add(addr)`](#multiaddraddaddr)
27+
- [`multiaddr.addSafe(addr)`](#multiaddraddsafeaddr)
28+
- [`multiaddr.rm(addr)`](#multiaddrrmaddr)
29+
- [`multiaddr.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh)
30+
- [Contribute](#contribute)
31+
- [License](#license)
32+
33+
## Installation
34+
35+
### npm
36+
37+
```sh
38+
> npm i peer-info
39+
```
40+
41+
### Node.JS, Browserify, Webpack
42+
43+
```js
44+
const PeerInfo = require('peer-info')
45+
```
46+
47+
### Browser: `<script>` Tag
48+
49+
Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.
50+
51+
```html
52+
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
53+
<!-- OR -->
54+
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
55+
```
56+
57+
## Usage
1858

1959
```js
2060
const PeerInfo = require('peer-info')
@@ -32,31 +72,41 @@ peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001'))
3272
peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209'))
3373
```
3474

35-
# API
75+
## API
3676

3777
```js
3878
const PeerInfo = require('peer-info')
3979
```
4080

41-
## const peer = new PeerInfo()
81+
### `PeerInfo.create([id, ] callback)`
82+
83+
- `id: PeerID`, optional
84+
- `callback: Function`
85+
86+
Creates a new PeerInfo instance and if no `id` is passed it
87+
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
88+
for it.
4289

43-
Creates a new PeerInfo instance and also generates a new underlying
44-
[PeerID](https://github.com/libp2p/js-peer-id) for it.
90+
### `new PeerInfo(id)`
4591

46-
## const peer = new PeerInfo(peerId)
92+
- `id: PeerID`
4793

4894
Creates a new PeerInfo instance from an existing PeerID.
4995

50-
## peer.multiaddrs
96+
### `multiaddrs`
5197

5298
A list of multiaddresses instances that `peer` can be reached at.
5399

54-
## peer.multiaddr.add(addr)
100+
### `multiaddr.add(addr)`
101+
102+
- `addr: Multiaddr`
55103

56104
Adds a new multiaddress that `peer` can be reached at. `addr` is an instance of
57105
a [multiaddr](https://github.com/libp2p/js-multiaddr).
58106

59-
## peer.multiaddr.addSafe(addr)
107+
### `multiaddr.addSafe(addr)`
108+
109+
- `addr: Multiaddr`
60110

61111
The `addSafe` call, in comparison to `add`, will only add the multiaddr to
62112
`multiaddrs` if the same multiaddr tries to be added twice.
@@ -67,40 +117,26 @@ peers which will not provide a useful multiaddr to be shared to the rest of the
67117
network (e.g. a multiaddr referring to a peer inside a LAN being shared to the
68118
outside world).
69119

70-
## peer.multiaddr.rm(addr)
120+
### `multiaddr.rm(addr)`
121+
122+
- `addr: Multiaddr`
71123

72124
Removes a multiaddress instance `addr` from `peer`.
73125

74-
## peer.multiaddr.replace(existing, fresh)
126+
### `multiaddr.replace(existing, fresh)`
127+
128+
- `existing: Multiaddr`
129+
- `fresh: Multiaddr`
75130

76131
Removes the array of multiaddresses `existing` from `peer`, and adds the array
77132
of multiaddresses `fresh`.
78133

134+
## Contribute
79135

80-
# Installation
81-
82-
## npm
83-
84-
```sh
85-
> npm i peer-info
86-
```
87-
88-
## Node.JS, Browserify, Webpack
89-
90-
```JavaScript
91-
var PeerInfo = require('peer-info')
92-
```
93-
94-
## Browser: `<script>` Tag
95-
96-
Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.
136+
PRs accepted.
97137

98-
```html
99-
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
100-
<!-- OR -->
101-
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
102-
```
138+
Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
103139

104-
# License
140+
## License
105141

106-
MIT
142+
[MIT © David Dias](LICENSE)

src/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const Id = require('peer-id')
44
const multiaddr = require('multiaddr')
55

6-
exports = module.exports = Peer
6+
exports = module.exports = PeerInfo
77

88
function ensureMultiaddr (addr) {
99
if (multiaddr.isMultiaddr(addr)) {
@@ -14,9 +14,9 @@ function ensureMultiaddr (addr) {
1414
}
1515

1616
// Peer represents a peer on the IPFS network
17-
function Peer (peerId) {
18-
if (!(this instanceof Peer)) {
19-
return new Peer(peerId)
17+
function PeerInfo (peerId) {
18+
if (!(this instanceof PeerInfo)) {
19+
return new PeerInfo(peerId)
2020
}
2121

2222
if (!peerId) {
@@ -92,12 +92,20 @@ function Peer (peerId) {
9292
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
9393
}
9494

95-
Peer.create = (cb) => {
96-
Id.create((err, key) => {
97-
if (err) {
98-
return cb(err)
99-
}
95+
PeerInfo.create = (id, callback) => {
96+
if (typeof id === 'function') {
97+
callback = id
98+
id = null
99+
100+
Id.create((err, id) => {
101+
if (err) {
102+
return callback(err)
103+
}
104+
105+
callback(null, new PeerInfo(id))
106+
})
107+
return
108+
}
100109

101-
cb(null, new Peer(key))
102-
})
110+
callback(null, new PeerInfo(id))
103111
}

test/peer-info.spec.js renamed to test/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ describe('peer-info', () => {
4747
})
4848
})
4949

50+
it('PeerInfo.create with existing id', (done) => {
51+
PeerId.create((err, id) => {
52+
expect(err).to.not.exist
53+
PeerInfo.create(id, (err, pi) => {
54+
expect(err).to.not.exist
55+
expect(pi.id).to.exist
56+
expect(pi.id).to.deep.equal(id)
57+
done()
58+
})
59+
})
60+
})
61+
5062
it('add multiaddr', () => {
5163
const mh = Multiaddr('/ip4/127.0.0.1/tcp/5001')
5264
pi.multiaddr.add(mh)

0 commit comments

Comments
 (0)