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

Commit 5dc1162

Browse files
dignifiedquiredaviddias
authored andcommitted
Async Crypto Endeavour (#22)
Async Crypto Endeavour
1 parent 35c7ae4 commit 5dc1162

File tree

7 files changed

+183
-112
lines changed

7 files changed

+183
-112
lines changed

.aegir.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ build/Release
2727
node_modules
2828

2929
dist
30-
lib

.travis.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
sudo: false
22
language: node_js
3-
node_js:
4-
- 4
5-
- 5
3+
4+
matrix:
5+
include:
6+
- node_js: 4
7+
env: CXX=g++-4.8
8+
- node_js: 6
9+
env:
10+
- SAUCE=true
11+
- CXX=g++-4.8
12+
- node_js: stable
13+
env: CXX=g++-4.8
614

715
# Make sure we have new NPM.
816
before_install:
@@ -22,3 +30,11 @@ before_script:
2230

2331
after_success:
2432
- npm run coverage-publish
33+
34+
addons:
35+
firefox: latest
36+
apt:
37+
sources:
38+
- ubuntu-toolchain-r-test
39+
packages:
40+
- g++-4.8

README.md

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
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/libp2p/js-peer-info.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-info)
6+
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-peer-info/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-peer-info?branch=master)
7+
[![Dependency Status](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/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)
10+
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
11+
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
12+
13+
[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-peer-info.svg)](https://saucelabs.com/u/ipfs-js-peer-info)
1014

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

1965
```js
2066
const PeerInfo = require('peer-info')
@@ -32,31 +78,41 @@ peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001'))
3278
peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209'))
3379
```
3480

35-
# API
81+
## API
3682

3783
```js
3884
const PeerInfo = require('peer-info')
3985
```
4086

41-
## const peer = new PeerInfo()
87+
### `PeerInfo.create([id, ] callback)`
4288

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

46-
## const peer = new PeerInfo(peerId)
92+
Creates a new PeerInfo instance and if no `id` is passed it
93+
generates a new underlying [PeerID](https://github.com/libp2p/js-peer-id)
94+
for it.
95+
96+
### `new PeerInfo(id)`
97+
98+
- `id: PeerID`
4799

48100
Creates a new PeerInfo instance from an existing PeerID.
49101

50-
## peer.multiaddrs
102+
### `multiaddrs`
51103

52104
A list of multiaddresses instances that `peer` can be reached at.
53105

54-
## peer.multiaddr.add(addr)
106+
### `multiaddr.add(addr)`
107+
108+
- `addr: Multiaddr`
55109

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

59-
## peer.multiaddr.addSafe(addr)
113+
### `multiaddr.addSafe(addr)`
114+
115+
- `addr: Multiaddr`
60116

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

70-
## peer.multiaddr.rm(addr)
126+
### `multiaddr.rm(addr)`
127+
128+
- `addr: Multiaddr`
71129

72130
Removes a multiaddress instance `addr` from `peer`.
73131

74-
## peer.multiaddr.replace(existing, fresh)
132+
### `multiaddr.replace(existing, fresh)`
133+
134+
- `existing: Multiaddr`
135+
- `fresh: Multiaddr`
75136

76137
Removes the array of multiaddresses `existing` from `peer`, and adds the array
77138
of multiaddresses `fresh`.
78139

140+
## Contribute
79141

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.
142+
PRs accepted.
97143

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-
```
144+
Small note: If editing the Readme, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
103145

104-
# License
146+
## License
105147

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

package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
"name": "peer-info",
33
"version": "0.7.1",
44
"description": "IPFS Peer abstraction JavaScript implementation",
5-
"main": "lib/index.js",
6-
"jsnext:main": "src/index.js",
5+
"main": "src/index.js",
76
"scripts": {
87
"lint": "aegir-lint",
98
"build": "aegir-build",
109
"test": "aegir-test",
11-
"test:node": "aegir-test node",
12-
"test:browser": "aegir-test browser",
10+
"test:node": "aegir-test --env node",
11+
"test:browser": "aegir-test --env browser",
1312
"release": "aegir-release",
1413
"release-minor": "aegir-release --type minor",
1514
"release-major": "aegir-release --type major",
@@ -18,34 +17,33 @@
1817
},
1918
"repository": {
2019
"type": "git",
21-
"url": "https://github.com/diasdavid/js-peer-info.git"
20+
"url": "https://github.com/libp2p/js-peer-info.git"
2221
},
2322
"keywords": [
2423
"IPFS"
2524
],
2625
"engines": {
27-
"node": "^4.3.0"
26+
"node": ">=4.0.0"
2827
},
2928
"author": "David Dias <[email protected]>",
3029
"license": "MIT",
3130
"bugs": {
32-
"url": "https://github.com/diasdavid/js-peer-info/issues"
31+
"url": "https://github.com/libp2p/js-peer-info/issues"
3332
},
34-
"homepage": "https://github.com/diasdavid/js-peer-info",
33+
"homepage": "https://github.com/libp2p/js-peer-info",
3534
"pre-commit": [
3635
"lint",
3736
"test"
3837
],
3938
"devDependencies": {
40-
"aegir": "^8.0.0",
39+
"aegir": "^9.0.1",
4140
"buffer-loader": "0.0.1",
4241
"chai": "^3.5.0",
4342
"pre-commit": "^1.1.3"
4443
},
4544
"dependencies": {
46-
"babel-runtime": "^6.11.6",
4745
"multiaddr": "^2.0.3",
48-
"peer-id": "^0.7.0"
46+
"peer-id": "^0.8.0"
4947
},
5048
"contributors": [
5149
"David Dias <[email protected]>",
@@ -55,4 +53,4 @@
5553
"dignifiedquire <[email protected]>",
5654
"greenkeeperio-bot <[email protected]>"
5755
]
58-
}
56+
}

src/index.js

Lines changed: 25 additions & 7 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,17 +14,17 @@ 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) {
23-
this.id = Id.create()
24-
} else {
25-
this.id = peerId
23+
throw new Error('Missing peerId. Use Peer.create(cb) to create one')
2624
}
2725

26+
this.id = peerId
27+
2828
this.multiaddrs = []
2929
const observedMultiaddrs = []
3030

@@ -91,3 +91,21 @@ function Peer (peerId) {
9191
// TODO: add features to fetch multiaddr using filters
9292
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
9393
}
94+
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+
}
109+
110+
callback(null, new PeerInfo(id))
111+
}

0 commit comments

Comments
 (0)