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

Async Crypto Endeavour #22

Merged
merged 11 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from 9 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
16 changes: 0 additions & 16 deletions .aegir.js

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ build/Release
node_modules

dist
lib
22 changes: 19 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
sudo: false
language: node_js
node_js:
- 4
- 5

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env: CXX=g++-4.8
- node_js: stable
env:
- SAUCE=true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do Sauce on LTS

- CXX=g++-4.8

# Make sure we have new NPM.
before_install:
Expand All @@ -22,3 +30,11 @@ before_script:

after_success:
- npm run coverage-publish

addons:
firefox: latest
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
123 changes: 83 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
peer-info JavaScript implementation
===================================
# js-peer-info

[![](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)
[![Build Status](https://travis-ci.org/diasdavid/js-peer-info.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-peer-info)
[![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)
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-info.svg?style=flat-square)](https://david-dm.org/diasdavid/js-peer-info)
[![Build Status](https://travis-ci.org/libp2p/js-peer-info.svg?style=flat-square)](https://travis-ci.org/libp2p/js-peer-info)
[![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)
[![Dependency Status](https://david-dm.org/libp2p/js-peer-info.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-info)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

[![Sauce Test Status](https://saucelabs.com/browser-matrix/js-peer-info.svg)](https://saucelabs.com/u/js-peer
-info)

> A PeerInfo object contains information about a
> [PeerID](https://github.com/libp2p/js-peer-id) and its
> [multiaddrs](https://github.com/libp2p/js-multiaddr). This module is used by
> [IPFS](https://github.com/ipfs/ipfs) and
> [libp2p](https://github.com/libp2p/js-libp2p).

# Example
## Table of Contents

- [Installation](#installation)
- [npm](#npm)
- [Node.JS, Browserify, Webpack](#nodejs-browserify-webpack)
- [Browser: `<script>` Tag](#browser-script-tag)
- [Usage](#usage)
- [API](#api)
- [`PeerInfo.create([id, ] callback)`](#peerinfocreateid-callback)
- [`new PeerInfo(id)`](#new-peerinfoid)
- [`multiaddrs`](#multiaddrs)
- [`multiaddr.add(addr)`](#multiaddraddaddr)
- [`multiaddr.addSafe(addr)`](#multiaddraddsafeaddr)
- [`multiaddr.rm(addr)`](#multiaddrrmaddr)
- [`multiaddr.replace(existing, fresh)`](#multiaddrreplaceexisting-fresh)
- [Contribute](#contribute)
- [License](#license)

## Installation

### npm

```sh
> npm i peer-info
```

### Node.JS, Browserify, Webpack

```js
const PeerInfo = require('peer-info')
```

### Browser: `<script>` Tag

Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.

```html
<script src="https://unpkg.com/peer-info/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/peer-info/dist/index.js"></script>
```

## Usage

```js
const PeerInfo = require('peer-info')
Expand All @@ -32,31 +79,41 @@ peer.multiaddr.add(multiaddr('/ip4/1.2.3.4/udp/8001'))
peer.multiaddr.add(multiaddr('/sonic/bfsk/697/1209'))
```

# API
## API

```js
const PeerInfo = require('peer-info')
```

## const peer = new PeerInfo()
### `PeerInfo.create([id, ] callback)`

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

## const peer = new PeerInfo(peerId)
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.

### `new PeerInfo(id)`

- `id: PeerID`

Creates a new PeerInfo instance from an existing PeerID.

## peer.multiaddrs
### `multiaddrs`

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

## peer.multiaddr.add(addr)
### `multiaddr.add(addr)`

- `addr: Multiaddr`

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

## peer.multiaddr.addSafe(addr)
### `multiaddr.addSafe(addr)`

- `addr: Multiaddr`

The `addSafe` call, in comparison to `add`, will only add the multiaddr to
`multiaddrs` if the same multiaddr tries to be added twice.
Expand All @@ -67,40 +124,26 @@ 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).

## peer.multiaddr.rm(addr)
### `multiaddr.rm(addr)`

- `addr: Multiaddr`

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

## peer.multiaddr.replace(existing, fresh)
### `multiaddr.replace(existing, fresh)`

- `existing: Multiaddr`
- `fresh: Multiaddr`

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

## Contribute

# Installation

## npm

```sh
> npm i peer-info
```

## Node.JS, Browserify, Webpack

```JavaScript
var PeerInfo = require('peer-info')
```

## Browser: `<script>` Tag

Loading this module through a script tag will make the `PeerInfo` obj available in the global namespace.
PRs accepted.

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

# License
## License

MIT
[MIT © David Dias](LICENSE)
20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
"name": "peer-info",
"version": "0.7.1",
"description": "IPFS Peer abstraction JavaScript implementation",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"test:node": "aegir-test --env node",
"test:browser": "aegir-test --env browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
Expand All @@ -18,32 +17,31 @@
},
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-peer-info.git"
"url": "https://github.com/libp2p/js-peer-info.git"
},
"keywords": [
"IPFS"
],
"engines": {
"node": "^4.3.0"
"node": ">=4.0.0"
},
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-peer-info/issues"
"url": "https://github.com/libp2p/js-peer-info/issues"
},
"homepage": "https://github.com/diasdavid/js-peer-info",
"homepage": "https://github.com/libp2p/js-peer-info",
"pre-commit": [
"lint",
"test"
],
"devDependencies": {
"aegir": "^8.0.0",
"aegir": "^9.0.0",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
},
"dependencies": {
"babel-runtime": "^6.11.6",
"multiaddr": "^2.0.3",
"peer-id": "^0.7.0"
},
Expand All @@ -55,4 +53,4 @@
"dignifiedquire <dignifiedquire@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>"
]
}
}
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Id = require('peer-id')
const multiaddr = require('multiaddr')

exports = module.exports = Peer
exports = module.exports = PeerInfo

function ensureMultiaddr (addr) {
if (multiaddr.isMultiaddr(addr)) {
Expand All @@ -14,17 +14,17 @@ function ensureMultiaddr (addr) {
}

// Peer represents a peer on the IPFS network
function Peer (peerId) {
if (!(this instanceof Peer)) {
return new Peer(peerId)
function PeerInfo (peerId) {
if (!(this instanceof PeerInfo)) {
return new PeerInfo(peerId)
}

if (!peerId) {
this.id = Id.create()
} else {
this.id = peerId
throw new Error('Missing peerId. Use Peer.create(cb) to create one')
}

this.id = peerId

this.multiaddrs = []
const observedMultiaddrs = []

Expand Down Expand Up @@ -91,3 +91,21 @@ function Peer (peerId) {
// TODO: add features to fetch multiaddr using filters
// look at https://github.com/whyrusleeping/js-mafmt/blob/master/src/index.js
}

PeerInfo.create = (id, callback) => {
if (typeof id === 'function') {
callback = id
id = null

Id.create((err, id) => {
if (err) {
return callback(err)
}

callback(null, new PeerInfo(id))
})
return
}

callback(null, new PeerInfo(id))
}
Loading