Compare commits

...

49 Commits

Author SHA1 Message Date
4e8ac0b7a8 chore: release version v0.21.0 2018-06-28 10:34:11 +02:00
b593adef56 chore: update contributors 2018-06-28 10:34:11 +02:00
8c803d5901 chore: remove pre-push 2018-06-28 10:11:54 +02:00
9fadd1d7b8 chore: update mplex 2018-06-28 10:11:29 +02:00
6905f1ba41 feat: (BREAKING CHANGE) overhaul libp2p config and constructor
* docs: update chat example and add info to its readme
* docs: update echo example
* docs: update libp2p in browser example
* docs: update pubsub example
* docs: update peer and content routing examples
* docs: update discovery mechanisms example
* docs: update encrypted comms example
* docs: update protocol and stream muxing example
* feat: add config validation
* test: update CI configs, use only node 8
2018-06-28 10:06:25 +02:00
b80e89269c docs: add weekly core dev calls url 2018-06-22 00:05:53 +01:00
deba7ea28e docs: add mgmt 2018-06-22 00:03:52 +01:00
d5972045da feat: set and hook up libp2p-connection-manager (#184) 2018-06-20 11:19:37 +01:00
28ffa0c7dc chore: update libp2p-switch 2018-06-07 19:58:22 +01:00
1790ded144 docs: Minor fixes in Transport example readme (#199)
* docs: Fixes typos, adds instructions

* docs: adds .md bash syntax highlight
2018-06-04 09:56:57 +01:00
abc6257bf7 docs: update badges 2018-05-29 13:45:48 +01:00
dd84190d47 docs: libp2p-swarm was renamed to libp2p-switch (#196)
* docs: libp2p-swarm was renamed to libp2p-switch

Update the README accordingly.

* Fixing another typo
2018-05-28 14:29:43 +01:00
65073792aa fix: lock wrtc to 0.1.1
* chore: update deps

* fix: use only wrtc 0.1.1

* chore: update webrtc-star

* chore: update circle

* chore: no node10
2018-05-12 19:45:05 +01:00
0bf203b087 docs: lead maintainer (#190) 2018-05-06 18:32:26 +02:00
37471135fa chore: release version v0.20.4 2018-04-30 22:41:32 +01:00
2a21c20ea3 chore: update contributors 2018-04-30 22:41:32 +01:00
9a2d4e3d72 chore: release version v0.20.3 2018-04-30 22:41:08 +01:00
7c2a19e3cc chore: update deps 2018-04-30 22:41:08 +01:00
26d5e69c62 chore: update deps 2018-04-30 22:41:08 +01:00
5042e09bb4 docs: typo in examples readme.md 2018-04-30 15:29:12 +01:00
409e7a8e1f chore: release version v0.20.2 2018-04-10 14:04:44 +09:00
52938e9f39 chore: update contributors 2018-04-10 14:04:44 +09:00
32941a807a chore: fix deps 2018-04-10 14:03:29 +09:00
32d34d3b83 chore: release version v0.20.1 2018-04-10 12:15:57 +09:00
074b8af09d chore: update contributors 2018-04-10 12:15:57 +09:00
4117bd7552 chore: add needed dep (websockets) 2018-04-10 12:13:11 +09:00
db3f6dbb06 chore: release version v0.20.0 2018-04-06 17:03:47 +01:00
3808c365b1 chore: update contributors 2018-04-06 17:03:47 +01:00
19528ef15e chore: update deps 2018-04-06 17:02:51 +01:00
bb0c9905ed feat: use class-is for type checks 2018-04-05 19:47:02 +01:00
141920cd14 chore: update deps 2018-04-05 19:47:02 +01:00
2dc94cd907 chore: release version v0.19.2 2018-03-28 15:34:00 -07:00
7fc1cd0f7c chore: update contributors 2018-03-28 15:33:59 -07:00
26f3f9a319 chore: release version v0.19.1 2018-03-28 15:32:20 -07:00
e9ce4ac795 chore: update deps 2018-03-28 15:32:20 -07:00
bca86873cc docs: enable missing syntax highlighting (#178)
Enable missing syntax highlighting for example code in `discovery-mechanisms/README.md`.
2018-03-20 21:43:56 -07:00
bcca813171 chore: release version v0.19.0 2018-03-15 08:16:12 -07:00
cfc5958a4b chore: update contributors 2018-03-15 08:16:12 -07:00
4527d5fff1 stats: tests: bind to localhost only 2018-03-15 08:09:56 -07:00
2c04a71007 stats: exposed and documented 2018-03-15 08:09:56 -07:00
642b8ad751 chore: update deps 2018-03-15 07:57:30 -07:00
ada73221b0 chore: update deps 2018-03-14 06:27:53 -07:00
2e40e9dda1 docs: update echo and chat examples to use libp2p-switch (#174) 2018-03-06 13:25:55 +01:00
e531b1bf3d docs: fix pubsub example, solves #167 2018-02-22 07:06:21 +00:00
fdfc237780 chore: release version v0.18.0 2018-02-19 09:59:47 +00:00
83a09dbc0e chore: update contributors 2018-02-19 09:59:47 +00:00
b28eba067e chore: update deps 2018-02-19 09:58:08 +00:00
b871bb0a1a docs: update examples to use libp2p-mplex module 2018-02-19 09:58:08 +00:00
14d3578eaf test: update tests to use libp2p-mplex module 2018-02-19 09:58:08 +00:00
61 changed files with 1735 additions and 848 deletions

View File

@ -5,10 +5,11 @@ const PeerId = require('peer-id')
const pull = require('pull-stream')
const parallel = require('async/parallel')
const rawPeer = require('./test/fixtures/test-peer.json')
const Node = require('./test/utils/bundle.node.js')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
const rawPeer = require('./test/fixtures/test-peer.json')
const Node = require('./test/utils/bundle-nodejs.js')
let wrtcRendezvous
let wsRendezvous
@ -21,7 +22,9 @@ const before = (done) => {
port: 15555
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
}, (err, server) => {
if (err) { return cb(err) }
if (err) {
return cb(err)
}
wrtcRendezvous = server
cb()
})
@ -33,7 +36,9 @@ const before = (done) => {
strictMultiaddr: false,
cryptoChallenge: true
}, (err, _server) => {
if (err) { return cb(err) }
if (err) {
return cb(err)
}
wsRendezvous = _server
cb()
})
@ -47,7 +52,9 @@ const before = (done) => {
peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')
node = new Node(peer)
node = new Node({
peerInfo: peer
})
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
node.start(cb)
})
@ -56,11 +63,11 @@ const before = (done) => {
}
const after = (done) => {
setTimeout(() => parallel(
[node, wrtcRendezvous, wsRendezvous].map((s) => {
return (cb) => s.stop(cb)
})
, done), 2000)
setTimeout(() =>
parallel(
[node, wrtcRendezvous, wsRendezvous].map((s) => (cb) => s.stop(cb)),
done),
2000)
}
module.exports = {

View File

@ -1,23 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
sudo: false
language: node_js
matrix:
include:
- node_js: 'stable'
env: CXX=g++-4.8
script:
- npm run test
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

View File

@ -1,3 +1,69 @@
<a name="0.21.0"></a>
# [0.21.0](https://github.com/libp2p/js-libp2p/compare/v0.20.4...v0.21.0) (2018-06-28)
### Bug Fixes
* lock wrtc to 0.1.1 ([6507379](https://github.com/libp2p/js-libp2p/commit/6507379))
### Features
* (BREAKING CHANGE) overhaul libp2p config and constructor ([6905f1b](https://github.com/libp2p/js-libp2p/commit/6905f1b))
* set and hook up libp2p-connection-manager ([#184](https://github.com/libp2p/js-libp2p/issues/184)) ([d597204](https://github.com/libp2p/js-libp2p/commit/d597204))
<a name="0.20.4"></a>
## [0.20.4](https://github.com/libp2p/js-libp2p/compare/v0.20.2...v0.20.4) (2018-04-30)
<a name="0.20.3"></a>
## [0.20.3](https://github.com/libp2p/js-libp2p/compare/v0.20.2...v0.20.3) (2018-04-30)
<a name="0.20.2"></a>
## [0.20.2](https://github.com/libp2p/js-libp2p/compare/v0.20.1...v0.20.2) (2018-04-10)
<a name="0.20.1"></a>
## [0.20.1](https://github.com/libp2p/js-libp2p/compare/v0.20.0...v0.20.1) (2018-04-10)
<a name="0.20.0"></a>
# [0.20.0](https://github.com/libp2p/js-libp2p/compare/v0.19.2...v0.20.0) (2018-04-06)
### Features
* use class-is for type checks ([bb0c990](https://github.com/libp2p/js-libp2p/commit/bb0c990))
<a name="0.19.2"></a>
## [0.19.2](https://github.com/libp2p/js-libp2p/compare/v0.19.0...v0.19.2) (2018-03-28)
<a name="0.19.1"></a>
## [0.19.1](https://github.com/libp2p/js-libp2p/compare/v0.19.0...v0.19.1) (2018-03-28)
<a name="0.19.0"></a>
# [0.19.0](https://github.com/libp2p/js-libp2p/compare/v0.18.0...v0.19.0) (2018-03-15)
<a name="0.18.0"></a>
# [0.18.0](https://github.com/libp2p/js-libp2p/compare/v0.17.0...v0.18.0) (2018-02-19)
<a name="0.17.0"></a>
# [0.17.0](https://github.com/libp2p/js-libp2p/compare/v0.16.5...v0.17.0) (2018-02-16)

View File

@ -2,7 +2,7 @@
libp2p as a project, including js-libp2p and all of its modules, follows the [standard IPFS Community contributing guidelines](https://github.com/ipfs/community/blob/master/contribution-guidelines.md).
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/js-project-guidelines.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
We also adhere to the [IPFS JavaScript Community contributing guidelines](https://github.com/ipfs/community/blob/master/js-code-guidelines.md) which provide additional information of how to collaborate and contribute in the JavaScript implementation of libp2p.
We appreciate your time and attention for going over these. Please open an issue on [ipfs/community](https://github.com/ipfs/community) if you have any question.

36
MGMT.md Normal file
View File

@ -0,0 +1,36 @@
# Core Dev Team Work Tracking & Managment
## How work gets organized (a tl;dr;)
The js-ipfs core working group follows the OKR structure established for the IPFS project to set the quarterly targets. Within each quarter, work gets tracked using Github and Waffle.
- Github is used for discussions and track current endeavours.
- Waffle gives us a [Kanban](https://en.wikipedia.org/wiki/Kanban) view over the work at hand.
![](https://ipfs.io/ipfs/QmWNd86qtjyFnygSAHkZDy4fUB1WnRa4WNt8gt1rSiq7of)
In the Waffle board, we have 4 columns:
- **Inbox** - New issues or PRs that haven't been evaluated yet
- **Backlog** - Issues that are blocked or discussion threads that are not currently active
- **Ready** - Issues Ready to be worked on
- **In Progress** - Issues that someone is already tackling. Contributors should focus on a few things rather than many at once.
- **Done** - Issues are automatically moved here when the issue is closed or the PR merged.
We track work for the JavaScript implementation of the IPFS protocol in 3 separate waffle boards:
- [js-ipfs](http://waffle.io/ipfs/js-ipfs)
- [js-libp2p](http://waffle.io/libp2p/js-libp2p)
- [js-ipld](http://waffle.io/ipld/js-ipld)
## Issue labels and how to use filters
We use labels to tag urgency and the difficulty of an issue. The current label system has:
- `difficulty:{easy, moderate, hard}` - This is an instinctive measure give by the project lead or leads. It is a subjective best guess, however the current golden rule is that an issue with difficulty:easy should not require more than a morning (3~4 hours) to do and it should not require having to mess with multiple modules to complete. Issues with difficulty moderate or hard might require some discussion around the problem or even request that another team (i.e go-ipfs) makes some changes. The length of moderate or hard issue might be a day to ad-aeternum.
- `priority (P0, P1, P2, P3, P4)` - P0 is the most important while P4 is the least.
- `help wanted` - Issues perfect for new contributors. They will have the information necessary or the pointers for a new contributor to figure out what is required. These issues are never blocked on some other issue be done first.
## Weekly Core Dev Team Calls
[⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽](https://github.com/ipfs/pm/issues/650)

205
README.md
View File

@ -6,9 +6,9 @@
<p align="center">
<a href="http://ipn.io"><img src="https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square" /></a>
<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square" /></a>
<a href="http://webchat.freenode.net/?channels=%23ipfs"><img src="https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square" /></a>
<a href="https://waffle.io/libp2p/libp2p"><img src="https://img.shields.io/badge/pm-waffle-blue.svg?style=flat-square" /></a>
<a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
<a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
<a href="https://waffle.io/libp2p/libp2p"><img src="https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square" /></a>
</p>
<p align="center">
@ -32,6 +32,16 @@ We've come a long way, but this project is still in Alpha, lots of development i
[![Throughput Graph](https://graphs.waffle.io/libp2p/js-libp2p/throughput.svg)](https://waffle.io/libp2p/js-libp2p/metrics/throughput)
[**`Weekly Core Dev Calls`**](https://github.com/ipfs/pm/issues/650)
## Tech Lead
[David Dias](https://github.com/diasdavid/)
## Lead Maintainer
[David Dias](https://github.com/diasdavid/)
## Table of Contents
- [Background](#background)
@ -93,47 +103,83 @@ libp2p becomes very simple and basically acts as a glue for every module that co
```JavaScript
// Creating a bundle that adds:
// transport: websockets + tcp
// stream-muxing: SPDY
// stream-muxing: spdy & mplex
// crypto-channel: secio
// discovery: multicast-dns
const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const secio = require('libp2p-secio')
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const DHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep')
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
constructor (_peerInfo, _peerBook, _options) {
const defaults = {
peerInfo: _peerInfo // The Identity of your Peer
peerBook: _peerBook, // Where peers get tracked, if undefined libp2p will create one instance
const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: [
spdy
// The libp2p modules for this libp2p bundle
modules: {
transport: [
TCP,
new WS() // It can take instances too!
],
crypto: [
secio
streamMuxer: [
SPDY,
MPLEX
],
connEncryption: [
SECIO
]
peerDiscovery: [
MulticastDNS
],
peerRouting: {}, // Currently both peerRouting and contentRouting are patched through the DHT,
contentRouting: {} // this will change once we factor that into two modules, for now do the following line:
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components
},
// libp2p config options (typically found on a config.json)
config: { // The config object is the part of the config that can go into a file, config.json.
peerDiscovery: {
mdns: { // mdns options
interval: 1000 // ms
enabled: true
},
webrtcStar: { // webrtc-star options
interval: 1000 // ms
enabled: false
}
// .. other discovery module options.
},
peerRouting: {},
contentRouting: {},
relay: { // Circuit Relay options
enabled: false,
hop: {
enabled: false,
active: false
}
}
// Enable/Disable Experimental features
EXPERIMENTAL: { // Experimental features ("behind a flag")
pubsub: false,
dht: false
}
},
discovery: [
new MulticastDNS(peerInfo)
],
// DHT is passed as its own enabling PeerRouting, ContentRouting and DHT itself components
dht: DHT
}
super(modules, peerInfo, peerBook, options)
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
super(defaultsDeep(_options, defaults))
}
}
// Now all the nodes you create, will have TCP, WebSockets, SPDY, SECIO and MulticastDNS support.
// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.
```
### API
@ -277,6 +323,113 @@ class Node extends libp2p {
[multiaddr]: https://github.com/multiformats/js-multiaddr
[Connection]: https://github.com/libp2p/interface-connection
-------
### Switch Stats API
##### `libp2p.stats.emit('update')`
Every time any stat value changes, this object emits an `update` event.
#### Global stats
##### `libp2p.stats.global.snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.global.movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-transport stats
##### `libp2p.stats.transports()`
Returns an array containing the tags (string) for each observed transport.
##### `libp2p.stats.forTransport(transportTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forTransport(transportTag).movingAverages`
Returns an object containing the following keys:
dataSent
dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-protocol stats
##### `libp2p.stats.protocols()`
Returns an array containing the tags (string) for each observed protocol.
##### `libp2p.stats.forProtocol(protocolTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forProtocol(protocolTag).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-peer stats
##### `libp2p.stats.peers()`
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
##### `libp2p.stats.forPeer(peerId:String).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forPeer(peerId:String).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Stats update interval
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout`, defined in miliseconds.
## Development
**Clone and install dependencies:**
@ -345,7 +498,7 @@ List of packages currently in existence for libp2p
| [`libp2p-distributed-record-store`](//github.com/libp2p/js-libp2p-distributed-record-store) | [![npm](https://img.shields.io/npm/v/undefined.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-distributed-record-store/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-distributed-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-distributed-record-store) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-distributed-record-store/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-distributed-record-store?type=dev) |
| [`libp2p-kad-record-store`](//github.com/libp2p/js-libp2p-kad-record-store) | [![npm](https://img.shields.io/npm/v/libp2p-kad-record-store.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-record-store/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-kad-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-record-store) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-kad-record-store/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-record-store?type=dev) |
| **Generics** |
| [`libp2p-swarm`](//github.com/libp2p/js-libp2p-swarm) | [![npm](https://img.shields.io/npm/v/libp2p-swarm.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-swarm/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-swarm) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-swarm/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-swarm?type=dev) |
| [`libp2p-switch`](//github.com/libp2p/js-libp2p-switch) | [![npm](https://img.shields.io/npm/v/libp2p-switch.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-switch/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-switch.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-switch) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-switch/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-switch?type=dev) |
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [![npm](https://img.shields.io/npm/v/libp2p-ping.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-ping/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-ping.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-ping/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping?type=dev) |
| [`multistream-select`](//github.com/libp2p/js-multistream) | [![npm](https://img.shields.io/npm/v/multistream-select.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-multistream/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-multistream.svg?style=flat-square)](https://david-dm.org/libp2p/js-multistream) | [![devDependency Status](https://david-dm.org/libp2p/js-multistream/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-multistream?type=dev) |
| **Data Types** |

2
ci/Jenkinsfile vendored
View File

@ -1,2 +1,2 @@
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
javascript()
javascript(['nodejs_versions': ['8.11.3']])

View File

@ -1,10 +1,8 @@
machine:
node:
version: stable
version: 8.11.3
test:
pre:
- npm run lint
post:
- npm run coverage -- --upload --providers coveralls

View File

@ -22,4 +22,4 @@ Let us know if you find any issue or if you want to contribute and add a new tut
- [Running libp2p in the Browser](./libp2p-in-the-browser)
- Running libp2p in the Electron (future)
- [The standard echo net example with libp2p](./echo)
- [A simple chat app with](./chat)
- [A simple chat app with libp2p](./chat)

View File

@ -1 +1,13 @@
# Chat example with libp2p
This example creates a simple chat app in your terminal.
## Setup
1. Install the modules, `npm install`.
2. Open 2 terminal windows in the `./src` directory.
## Running
1. Run the listener in window 1, `node listener.js`
2. Run the dialer in window 2, `node dialer.js`
3. Type a message in either window and hit _enter_
4. Tell youself secrets to your hearts content!

View File

@ -31,7 +31,9 @@ async.parallel([
if (err) throw err
const peerDialer = new PeerInfo(ids[0])
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
const nodeDialer = new Node(peerDialer)
const nodeDialer = new Node({
peerInfo: peerDialer
})
const peerListener = new PeerInfo(ids[1])
idListener = ids[1]

View File

@ -3,11 +3,12 @@
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const Bootstrap = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const multiplex = require('libp2p-multiplex')
const mplex = require('libp2p-mplex')
const secio = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')
function mapMuxers (list) {
@ -17,7 +18,7 @@ function mapMuxers (list) {
}
switch (pref.trim().toLowerCase()) {
case 'spdy': return spdy
case 'multiplex': return multiplex
case 'mplex': return mplex
default:
throw new Error(pref + ' muxer not available')
}
@ -31,49 +32,45 @@ function getMuxers (muxers) {
} else if (muxers) {
return mapMuxers(muxers)
} else {
return [multiplex, spdy]
return [mplex, spdy]
}
}
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [ secio ]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WS
],
streamMuxer: getMuxers(_options.muxer),
connEncryption: [ secio ],
peerDiscovery: [
MulticastDNS,
Bootstrap
],
dht: KadDHT
},
discovery: []
config: {
peerDiscovery: {
mdns: {
interval: 10000,
enabled: false
},
bootstrap: {
interval: 10000,
enabled: false,
list: _options.bootstrapList
}
},
dht: {
kBucketSize: 20
}
}
}
if (options.dht) {
modules.DHT = KadDHT
}
if (options.mdns) {
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
modules.discovery.push(mdns)
}
if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}
if (options.modules && options.modules.transport) {
options.modules.transport.forEach((t) => modules.transport.push(t))
}
if (options.modules && options.modules.discovery) {
options.modules.discovery.forEach((d) => modules.discovery.push(d))
}
super(modules, peerInfo, peerBook, options)
super(defaultsDeep(_options, defaults))
}
}

View File

@ -14,14 +14,16 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
}
const peerListener = new PeerInfo(idListener)
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
const nodeListener = new Node(peerListener)
const nodeListener = new Node({
peerInfo: peerListener
})
nodeListener.start((err) => {
if (err) {
throw err
}
nodeListener.swarm.on('peer-mux-established', (peerInfo) => {
nodeListener.on('peer:connect', (peerInfo) => {
console.log(peerInfo.id.toB58String())
})

View File

@ -1,12 +1,13 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const Railing = require('libp2p-railing')
const Bootstrap = require('libp2p-railing')
const waterfall = require('async/waterfall')
const defaultsDeep = require('@nodeutils/defaults-deep')
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
const bootstrapers = [
@ -22,16 +23,26 @@ const bootstrapers = [
]
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ Bootstrap ]
},
discovery: [new Railing(bootstrapers)]
config: {
peerDiscovery: {
bootstrap: {
interval: 2000,
enabled: true,
list: bootstrapers
}
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -41,7 +52,9 @@ waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => {

View File

@ -1,25 +1,35 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const MulticastDNS = require('libp2p-mdns')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ MulticastDNS ]
},
discovery: [new MulticastDNS(peerInfo, { interval: 1000 })]
config: {
peerDiscovery: {
mdns: {
interval: 1000,
enabled: true
}
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -30,7 +40,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -8,22 +8,33 @@ These mechanisms save configuration and enable a node to operate without any exp
## 1. Bootstrap list of Peers when booting a node
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, multiplex and SECIO. You can see the complete example at [1.js](./1.js).
For this demo, we will connect to IPFS default bootstrapper nodes and so, we will need to support the same set of features those nodes have, that are: TCP, mplex and SECIO. You can see the complete example at [1.js](./1.js).
First, we create our libp2p bundle.
```JavaScript
const Bootstrap = require('libp2p-railing')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ Bootstrap ]
},
discovery: [new Railing(bootstrapers)]
config: {
peerDiscovery: {
bootstrap: {
interval: 2000,
enabled: true,
list: bootstrapers
}
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```
@ -46,14 +57,16 @@ const bootstrapers = [
Now, once we create and start the node, we can listen for events such as `peer:discovery` and `peer:connect`, these events tell us when we found a peer, independently of the discovery mechanism used and when we actually dialed to that peer.
```
```JavaScript
let node
waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => {
@ -108,17 +121,25 @@ Update your libp2p bundle to include MulticastDNS.
```JavaScript
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ MulticastDNS ]
},
// We set the interval here to 1 second so that is faster to observe. The
// default is 10 seconds.
discovery: [new MulticastDNS(peerInfo, { interval: 1000 })]
config: {
peerDiscovery: {
mdns: {
// Run at 1s so we can observe more quickly, default is 10s
interval: 1000,
enabled: true
}
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```

View File

@ -1 +1,13 @@
# Echo example with libp2p
This example performs a simple echo from the listener to the dialer.
## Setup
1. Install the modules, `npm install`.
2. Open 2 terminal windows in the `./src` directory.
## Running
1. Run the listener in window 1, `node listener.js`
2. Run the dialer in window 2, `node dialer.js`
3. You should see console logs showing the dial, and the received echo of _hey_
4. If you look at the listener window, you will see it receiving the dial

View File

@ -21,7 +21,9 @@ async.parallel([
const dialerId = ids[0]
const dialerPeerInfo = new PeerInfo(dialerId)
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
const dialerNode = new Node(dialerPeerInfo)
const dialerNode = new Node({
peerInfo: dialerPeerInfo
})
// Peer to Dial
const listenerPeerInfo = new PeerInfo(ids[1])

View File

@ -3,11 +3,12 @@
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const Bootstrap = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const multiplex = require('libp2p-multiplex')
const mplex = require('libp2p-mplex')
const secio = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')
function mapMuxers (list) {
@ -17,7 +18,7 @@ function mapMuxers (list) {
}
switch (pref.trim().toLowerCase()) {
case 'spdy': return spdy
case 'multiplex': return multiplex
case 'mplex': return mplex
default:
throw new Error(pref + ' muxer not available')
}
@ -31,49 +32,45 @@ function getMuxers (muxers) {
} else if (muxers) {
return mapMuxers(muxers)
} else {
return [multiplex, spdy]
return [mplex, spdy]
}
}
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [ secio ]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WS
],
streamMuxer: getMuxers(_options.muxer),
connEncryption: [ secio ],
peerDiscovery: [
MulticastDNS,
Bootstrap
],
dht: KadDHT
},
discovery: []
config: {
peerDiscovery: {
mdns: {
interval: 10000,
enabled: false
},
bootstrap: {
interval: 10000,
enabled: false,
list: _options.bootstrapList
}
},
dht: {
kBucketSize: 20
}
}
}
if (options.dht) {
modules.DHT = KadDHT
}
if (options.mdns) {
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
modules.discovery.push(mdns)
}
if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}
if (options.modules && options.modules.transport) {
options.modules.transport.forEach((t) => modules.transport.push(t))
}
if (options.modules && options.modules.discovery) {
options.modules.discovery.forEach((d) => modules.discovery.push(d))
}
super(modules, peerInfo, peerBook, options)
super(defaultsDeep(_options, defaults))
}
}

View File

@ -25,9 +25,11 @@ series([
(cb) => {
const listenerPeerInfo = new PeerInfo(listenerId)
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
listenerNode = new Node(listenerPeerInfo)
listenerNode = new Node({
peerInfo: listenerPeerInfo
})
listenerNode.swarm.on('peer-mux-established', (peerInfo) => {
listenerNode.on('peer:connect', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String())
})

View File

@ -1,6 +1,6 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio')
@ -8,17 +8,19 @@ const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const pull = require('pull-stream')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ SPDY ],
connEncryption: [ SECIO ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -29,7 +31,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -19,15 +19,16 @@ const SECIO = require('libp2p-secio')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY],
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ SPDY ],
// Attach secio as the crypto channel to use
crypto: [SECIO]
connEncryption: [ SECIO ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```

View File

@ -13,17 +13,16 @@
"browserify": "^14.5.0",
"concat-stream": "^1.6.0",
"detect-dom-ready": "^1.0.2",
"node-static": "^0.7.10"
"node-static": "~0.7.10"
},
"dependencies": {
"detect-dom-ready": "^1.0.2",
"libp2p": "^0.13.0",
"libp2p-multiplex": "^0.5.0",
"libp2p-railing": "^0.7.1",
"libp2p-secio": "^0.8.1",
"libp2p-spdy": "^0.11.0",
"libp2p-webrtc-star": "^0.13.2",
"libp2p-websockets": "^0.10.4",
"peer-info": "^0.11.0"
"libp2p-mplex": "~0.8.0",
"libp2p-railing": "~0.9.1",
"libp2p-secio": "~0.10.0",
"libp2p-spdy": "~0.12.1",
"libp2p-webrtc-star": "~0.15.3",
"libp2p-websockets": "~0.12.0",
"peer-info": "~0.14.1"
}
}

View File

@ -2,13 +2,12 @@
const WebRTCStar = require('libp2p-webrtc-star')
const WebSockets = require('libp2p-websockets')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio')
const Railing = require('libp2p-railing')
const libp2p = require('libp2p')
const Bootstrap = require('libp2p-railing')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../../../')
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
const bootstrapers = [
@ -25,30 +24,56 @@ const bootstrapers = [
]
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
constructor (_options) {
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
const wstar = new WebRTCStar()
const modules = {
transport: [
wstar,
new WebSockets()
],
connection: {
muxer: [
Multiplex,
const defaults = {
modules: {
transport: [
wrtcStar,
new WebSockets()
],
streamMuxer: [
Mplex,
SPDY
],
crypto: [SECIO]
connEncryption: [
SECIO
],
peerDiscovery: [
wrtcStar.discovery,
Bootstrap
]
},
discovery: [
wstar.discovery,
new Railing(bootstrapers)
]
config: {
peerDiscovery: {
webRTCStar: {
enabled: true
},
websocketStar: {
enabled: true
},
bootstrap: {
interval: 10000,
enabled: false,
list: bootstrapers
}
},
relay: {
enabled: false,
hop: {
enabled: false,
active: false
}
},
EXPERIMENTAL: {
dht: false,
pubsub: false
}
}
}
super(modules, peerInfo, peerBook, options)
super(defaultsDeep(_options, defaults))
}
}

View File

@ -14,7 +14,9 @@ function createNode (callback) {
peerInfo.multiaddrs.add(ma)
const node = new Node(peerInfo)
const node = new Node({
peerInfo
})
node.idStr = peerIdStr
callback(null, node)

View File

@ -1,27 +1,36 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const KadDHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -32,7 +41,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -1,28 +1,37 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const CID = require('cids')
const KadDHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -33,7 +42,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -14,17 +14,27 @@ First, let's update our bundle to support Peer Routing and Content Routing.
```JavaScript
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
// dht must be enabled
dht: true
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```
@ -44,7 +54,7 @@ parallel([
], (err) => {
if (err) { throw err }
//
//
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
if (err) { throw err }

View File

@ -1,18 +1,22 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const pull = require('pull-stream')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -23,7 +27,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -1,6 +1,6 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const PeerInfo = require('peer-info')
@ -8,16 +8,18 @@ const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
const pull = require('pull-stream')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ SPDY ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -28,7 +30,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -1,6 +1,6 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const PeerInfo = require('peer-info')
@ -8,16 +8,18 @@ const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
const pull = require('pull-stream')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ SPDY ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -28,7 +30,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))
@ -60,12 +64,12 @@ parallel([
})
series([
(cb) => node1.dial(node2.peerInfo, '/node-2', (err, conn) => {
(cb) => node1.dialProtocol(node2.peerInfo, '/node-2', (err, conn) => {
if (err) { throw err }
pull(pull.values(['from 1 to 2']), conn)
cb()
}),
(cb) => node2.dial(node1.peerInfo, '/node-1', (err, conn) => {
(cb) => node2.dialProtocol(node1.peerInfo, '/node-1', (err, conn) => {
if (err) { throw err }
pull(pull.values(['from 2 to 1']), conn)
cb()

View File

@ -8,7 +8,7 @@ The feature of agreeing on a protocol over an established connection is what we
Let's see _protocol multiplexing_ in action! You will need the following modules for this example: `libp2p`, `libp2p-tcp`, `peer-info`, `async` and `pull-stream`. This example reuses the base left by the [Transports](../transports) example. You can see the complete solution at [1.js](./1.js).
After creating the nodes, we need to tell libp2p which protocols to handle.
After creating the nodes, we need to tell libp2p which protocols to handle.
```JavaScript
// ...
@ -55,7 +55,7 @@ node1.dialProtocol(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
This feature is super power for network protocols. It works in the same way as versioning your RPC/REST API, but for anything that goes in the wire. We had to use this feature to upgrade protocols within the IPFS Stack (i.e Bitswap) and we successfully managed to do so without any network splits.
There is still one last feature, you can create your custom match functions.
There is still one last feature, you can create your custom match functions.
```JavaScript
node2.handle('/custom-match-func', (protocol, conn) => {
@ -88,23 +88,24 @@ The example above would require a node to create a whole new connection for ever
Stream multiplexing is a old concept, in fact it happens in many of the layers of the [OSI System](https://en.wikipedia.org/wiki/OSI_model), in libp2p we make this feature to our avail by letting the user pick which module for stream multiplexing to use.
Currently, we have two available [libp2p-spdy](https://github.com/libp2p/js-libp2p-spdy) and [libp2p-multiplex](https://github.com/libp2p/js-libp2p-multiplex) and pluging them in is as easy as adding another transport. Let's revisit our libp2p bundle.
Currently, we have two available [libp2p-spdy](https://github.com/libp2p/js-libp2p-spdy) and [libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex) and pluging them in is as easy as adding another transport. Let's revisit our libp2p bundle.
```JavaScript
const SPDY = require('libp2p-spdy')
//...
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
// Here we are adding the SPDY muxer to our libp2p bundle.
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
// Here we are adding the SPDY muxer to our libp2p bundle.
// Thanks to protocol muxing, a libp2p bundle can support multiple Stream Muxers at the same
// time and pick the right one when dialing to a node
connection: {
muxer: [SPDY]
streamMuxer: [ SPDY ]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```

View File

@ -1,28 +1,39 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Multiplex = require('libp2p-multiplex')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const MulticastDNS = require('libp2p-mdns')
const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Multiplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ MulticastDNS ]
},
discovery: [
new MulticastDNS(peerInfo, { interval: 2000 })
]
config: {
peerDiscovery: {
mdns: {
interval: 2000,
enabled: true
}
},
EXPERIMENTAL: {
pubsub: true
}
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -33,7 +44,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))
@ -54,11 +67,17 @@ parallel([
], (err) => {
if (err) { throw err }
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()),
() => {
setInterval(() => {
node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!'))
// Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000)
}
)

View File

@ -21,12 +21,20 @@ series([
], (err) => {
if (err) { throw err }
node1.pubsub.on('news', (msg) => console.log(msg.from, msg.data.toString()))
node1.pubsub.subscribe('news')
setInterval(() => {
node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!'))
}, 1000)
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()),
() => {
setInterval(() => {
// Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000)
}
)
})
```

View File

@ -1,16 +1,22 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('@nodeutils/defaults-deep')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP
]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -20,7 +26,7 @@ waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
node.start(cb)
}
], (err) => {

View File

@ -1,18 +1,24 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('@nodeutils/defaults-deep')
const parallel = require('async/parallel')
const pull = require('pull-stream')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP
]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -23,7 +29,7 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -1,19 +1,26 @@
'use strict'
const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const WebSockets = require('libp2p-websockets')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('@nodeutils/defaults-deep')
const parallel = require('async/parallel')
const pull = require('pull-stream')
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP(), new WebSockets()]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WebSockets
]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
@ -28,7 +35,7 @@ function createNode (addrs, callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -10,10 +10,10 @@ A more complete definition of what is a transport can be found on the [interface
When using libp2p, you always want to create your own libp2p Bundle, that is, pick your set of modules and create your network stack with the properties you need. In this example, we will create a bundle with TCP. You can find the complete solution on the file [1.js](./1.js).
You will need 4 deps total, so go ahead and install all of them with:
You will need 5 deps total, so go ahead and install all of them with:
```
> npm install libp2p libp2p-tcp peer-info async
```bash
> npm install libp2p libp2p-tcp peer-info async @nodeutils/defaults-deep
```
Then, on your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
@ -27,16 +27,22 @@ const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('@nodeutils/defaults-deep')
// This MyBundle class is your libp2p bundle packed with TCP
class MyBundle extends libp2p {
constructor (peerInfo) {
// modules is a JS object that will describe the components
// we want for our libp2p bundle
const modules = {
transport: [new TCP()]
constructor (_options) {
const defaults = {
// modules is a JS object that will describe the components
// we want for our libp2p bundle
modules: {
transport: [
TCP
]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```
@ -57,7 +63,7 @@ waterfall([
// the multiaddr format, a self describable address
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
// Now we can create a node with that PeerInfo object
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
// Last, we start the node!
node.start(cb)
}
@ -76,7 +82,7 @@ waterfall([
})
```
Running this should result in somehting like:
Running this should result in something like:
```bash
> node 1.js
@ -98,6 +104,12 @@ For this step, we will need one more dependency.
> npm install pull-stream
```
And we also need to import the module on our .js file:
```js
const pull = require('pull-stream')
```
We are going to reuse the MyBundle class from step 1, but this time to make things simpler, we will create two functions, one to create nodes and another to print the addrs to avoid duplicating code.
```JavaScript
@ -108,7 +120,7 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
node.start(cb)
}
], (err) => callback(err, node))
@ -118,11 +130,18 @@ function printAddrs (node, number) {
console.log('node %s is listening on:', number)
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
}
```
Now we are going to use `async/parallel` to create two nodes, print their addresses and dial from one node to the other.
```
Now we are going to use `async/parallel` to create two nodes, print their addresses and dial from one node to the other. We already added `async` as a dependency, but still need to import `async/parallel`:
```js
const parallel = require('async/parallel')
```
Then,
```js
parallel([
(cb) => createNode(cb),
(cb) => createNode(cb)
@ -172,7 +191,7 @@ What we are going to do in this step is to create 3 nodes, one with TCP, another
In this example, we will need to also install `libp2p-websockets`, go ahead and install:
```sh
```bash
> npm install libp2p-websockets
```
@ -183,11 +202,17 @@ const WebSockets = require('libp2p-websockets')
// ...
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP(), new WebSockets()]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WebSockets
]
}
}
super(modules, peerInfo)
super(defaultsDeep(_options, defaults))
}
}
```
@ -206,7 +231,7 @@ function createNode (addrs, callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
addrs.forEach((addr) => peerInfo.multiaddrs.add(addr))
node = new MyBundle(peerInfo)
node = new MyBundle({ peerInfo: peerInfo })
node.start(cb)
}
], (err) => callback(err, node))

View File

@ -1,7 +1,8 @@
{
"name": "libp2p",
"version": "0.17.0",
"version": "0.21.0",
"description": "JavaScript base class for libp2p bundles",
"leadMaintainer": "David Dias <daviddias@ipfs.io>",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
@ -26,75 +27,83 @@
"node": ">=6.0.0",
"npm": ">=3.0.0"
},
"pre-push": [
"lint",
"test"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p",
"browser": {
"joi": "joi-browser"
},
"dependencies": {
"async": "^2.6.0",
"libp2p-floodsub": "^0.14.1",
"libp2p-ping": "~0.6.1",
"libp2p-switch": "~0.36.1",
"mafmt": "^4.0.0",
"multiaddr": "^3.0.2",
"peer-book": "~0.5.4",
"peer-id": "~0.10.6",
"peer-info": "~0.11.6"
"joi": "^13.4.0",
"joi-browser": "^13.4.0",
"libp2p-connection-manager": "~0.0.2",
"libp2p-floodsub": "~0.15.0",
"libp2p-ping": "~0.8.0",
"libp2p-switch": "~0.40.4",
"libp2p-websockets": "~0.12.0",
"mafmt": "^6.0.0",
"multiaddr": "^5.0.0",
"peer-book": "~0.8.0",
"peer-id": "~0.10.7",
"peer-info": "~0.14.1"
},
"devDependencies": {
"aegir": "^13.0.0",
"@nodeutils/defaults-deep": "^1.1.0",
"aegir": "^14.0.0",
"chai": "^4.1.2",
"cids": "~0.5.2",
"cids": "~0.5.3",
"dirty-chai": "^2.0.1",
"electron-webrtc": "~0.3.0",
"libp2p-circuit": "~0.1.4",
"libp2p-kad-dht": "~0.8.0",
"libp2p-mdns": "~0.9.2",
"libp2p-multiplex": "~0.5.1",
"libp2p-railing": "~0.7.1",
"libp2p-secio": "~0.9.2",
"libp2p-spdy": "~0.11.0",
"libp2p-tcp": "~0.11.5",
"libp2p-webrtc-star": "~0.13.3",
"libp2p-websockets": "~0.10.4",
"libp2p-websocket-star": "~0.7.6",
"libp2p-circuit": "~0.2.0",
"libp2p-kad-dht": "~0.10.0",
"libp2p-mdns": "~0.12.0",
"libp2p-mplex": "~0.8.0",
"libp2p-railing": "~0.9.1",
"libp2p-secio": "~0.10.0",
"libp2p-spdy": "~0.12.1",
"libp2p-tcp": "~0.12.0",
"libp2p-webrtc-star": "~0.15.3",
"libp2p-websocket-star": "~0.8.1",
"libp2p-websocket-star-rendezvous": "~0.2.3",
"lodash.times": "^4.3.2",
"pre-commit": "^1.2.2",
"pull-goodbye": "0.0.2",
"pull-serializer": "~0.3.2",
"pull-stream": "^3.6.1",
"safe-buffer": "^5.1.1",
"sinon": "^4.3.0",
"wrtc": "0.0.66"
"pull-stream": "^3.6.8",
"sinon": "^5.0.7",
"wrtc": "0.1.1"
},
"contributors": [
"Chris Bratlien <chrisbratlien@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>",
"Daijiro Wachi <daijiro.wachi@gmail.com>",
"David Dias <daviddias.p@gmail.com>",
"Diogo Silva <fsdiogo@gmail.com>",
"Dmitriy Ryajov <dryajov@gmail.com>",
"Elven <mon.samuel@qq.com>",
"Florian-Merle <florian.david.merle@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Giovanni T. Parra <fiatjaf@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"Irakli Gozalishvili <rfobic@gmail.com>",
"Joel Gustafson <joelg@mit.edu>",
"John Rees <johnrees@users.noreply.github.com>",
"João Santos <joaosantos15@users.noreply.github.com>",
"Kevin Kwok <antimatter15@gmail.com>",
"Lars Gierth <lgierth@users.noreply.github.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Nuno Nogueira <nunofmn@gmail.com>",
"Pedro Teixeira <pedro@protocol.ai>",
"Pedro Teixeira <i@pgte.me>",
"RasmusErik Voel Jensen <github@solsort.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Ryan Bell <ryan@piing.net>",
"Sönke Hahn <soenkehahn@gmail.com>",
"Tiago Alves <alvesjtiago@gmail.com>",
"Volker Mische <volker.mische@gmail.com>",
"Zane Starr <zcstarr@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"

83
src/config.js Normal file
View File

@ -0,0 +1,83 @@
'use strict'
const Joi = require('joi')
const schema = Joi.object({
// TODO: create proper validators for the generics
connectionManager: Joi.object(),
peerInfo: Joi.object().required(),
peerBook: Joi.object(),
modules: Joi.object().keys({
transport: Joi.array().items(
Joi.alternatives().try(
Joi.func(),
Joi.object()
)
).min(1).required(),
streamMuxer: Joi.array().items(
Joi.alternatives().try(
Joi.func(),
Joi.object()
)
).allow(null),
connEncryption: Joi.array().items(
Joi.alternatives().try(
Joi.func(),
Joi.object()
)
).allow(null),
peerDiscovery: Joi.array().items(
Joi.alternatives().try(
Joi.func(),
Joi.object()
)
).allow(null),
dht: Joi.alternatives().try(
Joi.func(),
Joi.object()
).allow(null)
}).required(),
config: Joi.object().keys({
peerDiscovery: Joi.object().allow(null),
relay: Joi.object().keys({
enabled: Joi.boolean().default(false),
hop: Joi.object().keys({
enabled: Joi.boolean().default(false),
active: Joi.boolean().default(false)
})
}).default(),
dht: Joi.object().keys({
kBucketSize: Joi.number().allow(null)
}),
EXPERIMENTAL: Joi.object().keys({
dht: Joi.boolean().default(false),
pubsub: Joi.boolean().default(false)
}).default()
}).default()
})
module.exports.validate = (options) => {
let newSchema = schema
// Throw an intial error early for required props
let config = Joi.attempt(options, newSchema)
// Ensure discoveries are properly configured
if (config.modules.peerDiscovery) {
config.modules.peerDiscovery.forEach((discovery) => {
// If it's a function, validate we have configs for it
if (typeof discovery === 'function') {
Joi.reach(schema, 'config.peerDiscovery').keys({
[discovery.tag]: Joi.object().required()
})
}
})
}
// Ensure dht is correct
if (config.config.EXPERIMENTAL && config.config.EXPERIMENTAL.dht) {
newSchema = newSchema.requiredKeys('modules.dht')
}
// Finish validation and return the updated config
return Joi.attempt(config, newSchema)
}

View File

@ -3,99 +3,103 @@
const EventEmitter = require('events').EventEmitter
const assert = require('assert')
const setImmediate = require('async/setImmediate')
const each = require('async/each')
const series = require('async/series')
const parallel = require('async/parallel')
const PeerBook = require('peer-book')
const Switch = require('libp2p-switch')
const Ping = require('libp2p-ping')
const WebSockets = require('libp2p-websockets')
const ConnectionManager = require('libp2p-connection-manager')
const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing')
const dht = require('./dht')
const pubsub = require('./pubsub')
const getPeerInfo = require('./get-peer-info')
const validateConfig = require('./config').validate
exports = module.exports
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
class Node extends EventEmitter {
constructor (_modules, _peerInfo, _peerBook, _options) {
constructor (_options) {
super()
assert(_modules, 'requires modules to equip libp2p with features')
assert(_peerInfo, 'requires a PeerInfo instance')
// validateConfig will ensure the config is correct,
// and add default values where appropriate
_options = validateConfig(_options)
this.modules = _modules
this.peerInfo = _peerInfo
this.peerBook = _peerBook || new PeerBook()
_options = _options || {}
this.peerInfo = _options.peerInfo
this.peerBook = _options.peerBook || new PeerBook()
this._modules = _options.modules
this._config = _options.config
this._isStarted = false
this._transport = [] // Transport instances/references
this._discovery = [] // Discovery service instances/references
this.switch = new Switch(this.peerInfo, this.peerBook)
this._switch = new Switch(this.peerInfo, this.peerBook, _options.switch)
this.stats = this._switch.stats
this.connectionManager = new ConnectionManager(this, _options.connectionManager)
// Attach stream multiplexers
if (this.modules.connection && this.modules.connection.muxer) {
let muxers = this.modules.connection.muxer
muxers = Array.isArray(muxers) ? muxers : [muxers]
muxers.forEach((muxer) => this.switch.connection.addStreamMuxer(muxer))
if (this._modules.streamMuxer) {
let muxers = this._modules.streamMuxer
muxers.forEach((muxer) => this._switch.connection.addStreamMuxer(muxer))
// If muxer exists, we can use Identify
this.switch.connection.reuse()
// If muxer exists
// we can use Identify
this._switch.connection.reuse()
// we can use Relay for listening/dialing
this._switch.connection.enableCircuitRelay(this._config.relay)
// If muxer exists, we can use Relay for listening/dialing
this.switch.connection.enableCircuitRelay(_options.relay)
// Received incommind dial and muxer upgrade happened,
// Received incomming dial and muxer upgrade happened,
// reuse this muxed connection
this.switch.on('peer-mux-established', (peerInfo) => {
this._switch.on('peer-mux-established', (peerInfo) => {
this.emit('peer:connect', peerInfo)
this.peerBook.put(peerInfo)
})
this.switch.on('peer-mux-closed', (peerInfo) => {
this._switch.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
})
}
// Attach crypto channels
if (this.modules.connection && this.modules.connection.crypto) {
let cryptos = this.modules.connection.crypto
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
if (this._modules.connEncryption) {
let cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this.switch.connection.crypto(crypto.tag, crypto.encrypt)
})
}
// Attach discovery mechanisms
if (this.modules.discovery) {
let discoveries = this.modules.discovery
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
discoveries.forEach((discovery) => {
discovery.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
this._switch.connection.crypto(crypto.tag, crypto.encrypt)
})
}
// dht provided components (peerRouting, contentRouting, dht)
if (_modules.DHT) {
this._dht = new this.modules.DHT(this.switch, {
kBucketSize: 20,
datastore: _options.DHT && _options.DHT.datastore
if (this._config.EXPERIMENTAL.dht) {
const DHT = this._modules.dht
this._dht = new DHT(this._switch, {
kBucketSize: this._config.dht.kBucketSize || 20,
// TODO make datastore an option of libp2p itself so
// that other things can use it as well
datastore: dht.datastore
})
}
// enable/disable pubsub
if (this._config.EXPERIMENTAL && this._config.EXPERIMENTAL.pubsub) {
this.pubsub = pubsub(this)
}
// Attach remaining APIs
this.peerRouting = peerRouting(this)
this.contentRouting = contentRouting(this)
this.dht = dht(this)
this.pubsub = pubsub(this)
this._getPeerInfo = getPeerInfo(this)
// Mount default protocols
Ping.mount(this.switch)
Ping.mount(this._switch)
}
/*
@ -103,14 +107,11 @@ class Node extends EventEmitter {
* - create listeners on the multiaddrs the Peer wants to listen
*/
start (callback) {
if (!this.modules.transport) {
if (!this._modules.transport) {
return callback(new Error('no transports were present'))
}
let ws
let transports = this.modules.transport
transports = Array.isArray(transports) ? transports : [transports]
// so that we can have webrtc-star addrs without adding manually the id
const maOld = []
@ -124,31 +125,58 @@ class Node extends EventEmitter {
this.peerInfo.multiaddrs.replace(maOld, maNew)
const multiaddrs = this.peerInfo.multiaddrs.toArray()
transports.forEach((transport) => {
if (transport.filter(multiaddrs).length > 0) {
this.switch.transport.add(
transport.tag || transport.constructor.name, transport)
} else if (transport.constructor &&
transport.constructor.name === 'WebSockets') {
// TODO find a cleaner way to signal that a transport is always
// used for dialing, even if no listener
ws = transport
this._modules.transport.forEach((Transport) => {
let t
if (typeof Transport === 'function') {
t = new Transport()
} else {
t = Transport
}
if (t.filter(multiaddrs).length > 0) {
this._switch.transport.add(t.tag || t.constructor.name, t)
} else if (WebSockets.isWebSockets(t)) {
// TODO find a cleaner way to signal that a transport is always used
// for dialing, even if no listener
ws = t
}
this._transport.push(t)
})
series([
(cb) => this.switch.start(cb),
(cb) => this._switch.start(cb),
(cb) => {
if (ws) {
// always add dialing on websockets
this.switch.transport.add(ws.tag || ws.constructor.name, ws)
this._switch.transport.add(ws.tag || ws.constructor.name, ws)
}
// all transports need to be setup before discover starts
if (this.modules.discovery) {
return each(this.modules.discovery, (d, cb) => d.start(cb), cb)
if (this._modules.peerDiscovery && this._config.peerDiscovery) {
each(this._modules.peerDiscovery, (D, _cb) => {
// If enabled then start it
if (this._config.peerDiscovery[D.tag].enabled) {
let d
if (typeof D === 'function') {
this._config.peerDiscovery[D.tag].peerInfo = this.peerInfo
d = new D(this._config.peerDiscovery[D.tag])
} else {
d = D
}
d.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
this._discovery.push(d)
d.start(_cb)
} else {
_cb()
}
}, cb)
} else {
cb()
}
cb()
},
(cb) => {
// TODO: chicken-and-egg problem #1:
@ -163,7 +191,7 @@ class Node extends EventEmitter {
(cb) => {
// TODO: chicken-and-egg problem #2:
// have to set started here because FloodSub requires libp2p is already started
if (this._options !== false) {
if (this._floodSub) {
this._floodSub.start(cb)
} else {
cb()
@ -174,13 +202,11 @@ class Node extends EventEmitter {
// detect which multiaddrs we don't have a transport for and remove them
const multiaddrs = this.peerInfo.multiaddrs.toArray()
transports.forEach((transport) => {
multiaddrs.forEach((multiaddr) => {
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
!transports.find((transport) => transport.filter(multiaddr).length > 0)) {
this.peerInfo.multiaddrs.delete(multiaddr)
}
})
multiaddrs.forEach((multiaddr) => {
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
!this._transport.find((transport) => transport.filter(multiaddr).length > 0)) {
this.peerInfo.multiaddrs.delete(multiaddr)
}
})
cb()
},
@ -195,17 +221,24 @@ class Node extends EventEmitter {
* Stop the libp2p node by closing its listeners and open connections
*/
stop (callback) {
if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
setImmediate(() => discovery.stop(() => {}))
})
}
series([
(cb) => {
if (this._floodSub.started) {
this._floodSub.stop(cb)
if (this._modules.peerDiscovery) {
// stop all discoveries before continuing with shutdown
return parallel(
this._discovery.map((d) => {
return (_cb) => d.stop(() => { _cb() })
}),
cb
)
}
cb()
},
(cb) => {
if (this._floodSub) {
return this._floodSub.stop(cb)
}
cb()
},
(cb) => {
if (this._dht) {
@ -213,7 +246,7 @@ class Node extends EventEmitter {
}
cb()
},
(cb) => this.switch.stop(cb),
(cb) => this._switch.stop(cb),
(cb) => {
this.emit('stop')
cb()
@ -234,7 +267,7 @@ class Node extends EventEmitter {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this.switch.dial(peerInfo, (err) => {
this._switch.dial(peerInfo, (err) => {
if (err) { return callback(err) }
this.peerBook.put(peerInfo)
@ -254,7 +287,7 @@ class Node extends EventEmitter {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this.switch.dial(peerInfo, protocol, (err, conn) => {
this._switch.dial(peerInfo, protocol, (err, conn) => {
if (err) { return callback(err) }
this.peerBook.put(peerInfo)
callback(null, conn)
@ -268,25 +301,28 @@ class Node extends EventEmitter {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
this.switch.hangUp(peerInfo, callback)
this._switch.hangUp(peerInfo, callback)
})
}
ping (peer, callback) {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
if (!this.isStarted()) {
return callback(new Error(NOT_STARTED_ERROR_MESSAGE))
}
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
callback(null, new Ping(this.switch, peerInfo))
callback(null, new Ping(this._switch, peerInfo))
})
}
handle (protocol, handlerFunc, matchFunc) {
this.switch.handle(protocol, handlerFunc, matchFunc)
this._switch.handle(protocol, handlerFunc, matchFunc)
}
unhandle (protocol) {
this.switch.unhandle(protocol)
this._switch.unhandle(protocol)
}
}

View File

@ -1,14 +0,0 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const libp2p = require('../src')
describe('libp2p', () => {
it('the skeleton is fine, now go build your own libp2p bundle', () => {
expect(libp2p).to.exist()
})
})

View File

@ -1,4 +1,3 @@
'use strict'
require('./base')
require('./transports.browser')

View File

@ -1,20 +1,21 @@
/* eslint-env mocha */
'use strict'
const waterfall = require('async/waterfall')
const series = require('async/series')
const parallel = require('async/parallel')
const utils = require('./utils/node')
const Circuit = require('libp2p-circuit')
const multiaddr = require('multiaddr')
const tryEcho = require('./utils/try-echo')
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const sinon = require('sinon')
const waterfall = require('async/waterfall')
const series = require('async/series')
const parallel = require('async/parallel')
const Circuit = require('libp2p-circuit')
const multiaddr = require('multiaddr')
describe('circuit relay', function () {
const createNode = require('./utils/create-node')
const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
describe('circuit relay', () => {
let handlerSpies = []
let relayNode1
let relayNode2
@ -23,29 +24,32 @@ describe('circuit relay', function () {
let nodeTCP1
let nodeTCP2
function setupNode (addrs, options, cb) {
function setupNode (addrs, options, callback) {
if (typeof options === 'function') {
cb = options
callback = options
options = {}
}
options = options || {}
return utils.createNode(addrs, options, (err, node) => {
return createNode(addrs, options, (err, node) => {
expect(err).to.not.exist()
node.handle('/echo/1.0.0', utils.echo)
node.handle('/echo/1.0.0', echo)
node.start((err) => {
expect(err).to.not.exist()
handlerSpies.push(sinon.spy(node.switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
cb(node)
handlerSpies.push(sinon.spy(
node._switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'
))
callback(node)
})
})
}
before(function (done) {
this.timeout(20000)
this.timeout(20 * 1000)
waterfall([
// set up passive relay
@ -53,11 +57,13 @@ describe('circuit relay', function () {
'/ip4/0.0.0.0/tcp/0/ws',
'/ip4/0.0.0.0/tcp/0'
], {
relay: {
enabled: true,
hop: {
config: {
relay: {
enabled: true,
active: false // passive relay
hop: {
enabled: true,
active: false // passive relay
}
}
}
}, (node) => {
@ -69,11 +75,13 @@ describe('circuit relay', function () {
'/ip4/0.0.0.0/tcp/0/ws',
'/ip4/0.0.0.0/tcp/0'
], {
relay: {
enabled: true,
hop: {
config: {
relay: {
enabled: true,
active: false // passive relay
hop: {
enabled: true,
active: false // passive relay
}
}
}
}, (node) => {
@ -84,8 +92,10 @@ describe('circuit relay', function () {
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws'
], {
relay: {
enabled: true
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeWS1 = node
@ -95,8 +105,10 @@ describe('circuit relay', function () {
(cb) => setupNode([
'/ip4/0.0.0.0/tcp/0/ws'
], {
relay: {
enabled: true
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeWS2 = node
@ -107,8 +119,10 @@ describe('circuit relay', function () {
'/ip4/0.0.0.0/tcp/0',
`/ipfs/${relayNode1.peerInfo.id.toB58String()}/p2p-circuit`
], {
relay: {
enabled: true
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeTCP1 = node
@ -119,8 +133,10 @@ describe('circuit relay', function () {
'/ip4/0.0.0.0/tcp/0',
`/ip4/0.0.0.0/tcp/0/ipfs/${relayNode2.peerInfo.id.toB58String()}/p2p-circuit`
], {
relay: {
enabled: true
config: {
relay: {
enabled: true
}
}
}, (node) => {
nodeTCP2 = node

133
test/config.spec.js Normal file
View File

@ -0,0 +1,133 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-railing')
const validateConfig = require('../src/config').validate
describe('configuration', () => {
let peerInfo
before((done) => {
waterfall([
(cb) => PeerId.create({ bits: 512 }, cb),
(peerId, cb) => PeerInfo.create(peerId, cb),
(info, cb) => {
peerInfo = info
cb()
}
], () => done())
})
it('should throw an error if peerInfo is missing', () => {
expect(() => {
validateConfig({
modules: {
transport: [ WS ]
}
})
}).to.throw()
})
it('should throw an error if modules is missing', () => {
expect(() => {
validateConfig({
peerInfo
})
}).to.throw()
})
it('should throw an error if there are no transports', () => {
expect(() => {
validateConfig({
peerInfo,
modules: {
transport: [ ]
}
})
}).to.throw()
})
it('should add defaults to missing items', () => {
const options = {
peerInfo,
modules: {
transport: [ WS ],
peerDiscovery: [ Bootstrap ]
},
config: {
peerDiscovery: {
bootstrap: {
interval: 1000,
enabled: true
}
}
}
}
const expected = {
peerInfo,
modules: {
transport: [ WS ],
peerDiscovery: [ Bootstrap ]
},
config: {
peerDiscovery: {
bootstrap: {
interval: 1000,
enabled: true
}
},
EXPERIMENTAL: {
pubsub: false,
dht: false
},
relay: {
enabled: false
}
}
}
expect(validateConfig(options)).to.deep.equal(expected)
})
it('should not allow for dht to be enabled without it being provided', () => {
const options = {
peerInfo,
modules: {
transport: [ WS ]
},
config: {
EXPERIMENTAL: {
dht: true
}
}
}
expect(() => validateConfig(options)).to.throw()
})
it('should require a non instanced peerDiscovery module to have associated options', () => {
const options = {
peerInfo,
modules: {
transport: [ WS ],
peerDiscover: [ Bootstrap ]
},
config: {
EXPERIMENTAL: {
dht: true
}
}
}
expect(() => validateConfig(options)).to.throw()
})
})

View File

@ -9,8 +9,8 @@ const expect = chai.expect
const parallel = require('async/parallel')
const _times = require('lodash.times')
const CID = require('cids')
const utils = require('./utils/node')
const createNode = utils.createNode
const createNode = require('./utils/create-node')
describe('.contentRouting', () => {
let nodeA
@ -23,8 +23,11 @@ describe('.contentRouting', () => {
this.timeout(5 * 1000)
const tasks = _times(5, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', {
mdns: false,
dht: true
config: {
EXPERIMENTAL: {
dht: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))

View File

@ -5,7 +5,8 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const series = require('async/series')
const createNode = require('./utils/node').createNode
const createNode = require('./utils/create-node')
describe('multiaddr trim', () => {
it('non used multiaddrs get trimmed', (done) => {
@ -20,7 +21,6 @@ describe('multiaddr trim', () => {
expect(err).to.not.exist()
node = _node
const multiaddrs = node.peerInfo.multiaddrs.toArray()
// multiaddrs.forEach((ma) => console.log(ma.toString()))
expect(multiaddrs).to.have.length(3)
cb()
}),
@ -29,11 +29,10 @@ describe('multiaddr trim', () => {
expect(err).to.not.exist()
const multiaddrs = node.peerInfo.multiaddrs.toArray()
// console.log('--')
// multiaddrs.forEach((ma) => console.log(ma.toString()))
expect(multiaddrs.length).to.at.least(2)
expect(multiaddrs[0].toString()).to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ws\/ipfs\/\w+$/)
expect(multiaddrs[0].toString())
.to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ws\/ipfs\/\w+$/)
node.stop(done)
})
})

View File

@ -1,6 +1,5 @@
'use strict'
require('./base')
require('./transports.node')
require('./stream-muxing.node')
require('./peer-discovery.node')
@ -8,4 +7,5 @@ require('./pubsub.node')
require('./peer-routing.node')
require('./content-routing.node')
require('./circuit-relay.node')
require('./multiaddr-trim')
require('./multiaddr-trim.node')
require('./stats')

View File

@ -6,9 +6,9 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const signalling = require('libp2p-webrtc-star/src/sig-server')
const parallel = require('async/parallel')
const utils = require('./utils/node')
const createNode = utils.createNode
const echo = utils.echo
const createNode = require('./utils/create-node')
const echo = require('./utils/echo')
describe('peer discovery', () => {
let nodeA
@ -52,13 +52,21 @@ describe('peer discovery', () => {
parallel([
(cb) => nodeA.stop(cb),
(cb) => nodeB.stop(cb),
(cb) => ss.stop(done)
(cb) => ss.stop(cb)
], done)
})
}
describe('MulticastDNS', () => {
setup({ mdns: true })
setup({
config: {
peerDiscovery: {
mdns: {
enabled: true
}
}
}
})
it('find a peer', function (done) {
this.timeout(15 * 1000)
@ -87,8 +95,16 @@ describe('peer discovery', () => {
describe('MulticastDNS + WebRTCStar', () => {
setup({
webRTCStar: true,
mdns: true
config: {
peerDiscovery: {
mdns: {
enabled: true
},
webRTCStar: {
enabled: true
}
}
}
})
it('find a peer', function (done) {

View File

@ -8,8 +8,8 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const _times = require('lodash.times')
const utils = require('./utils/node')
const createNode = utils.createNode
const createNode = require('./utils/create-node')
describe('.peerRouting', () => {
let nodeA
@ -23,8 +23,11 @@ describe('.peerRouting', () => {
const tasks = _times(5, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', {
mdns: false,
dht: true
config: {
EXPERIMENTAL: {
dht: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))

View File

@ -9,13 +9,22 @@ const expect = chai.expect
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const _times = require('lodash.times')
const utils = require('./utils/node')
const createNode = utils.createNode
const createNode = require('./utils/create-node')
function startTwo (callback) {
const tasks = _times(2, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', {
mdns: false
config: {
peerDiscovery: {
mdns: {
enabled: false
}
},
EXPERIMENTAL: {
pubsub: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))
@ -69,18 +78,20 @@ describe('.pubsub', () => {
describe('.pubsub off', () => {
it('fail to use pubsub if disabled', (done) => {
createNode('/ip4/0.0.0.0/tcp/0', {
mdns: false,
pubsub: false
config: {
peerDiscovery: {
mdns: {
enabled: false
}
},
EXPERIMENTAL: {
pubsub: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.pubsub.subscribe('news',
(msg) => {},
(err) => {
expect(err).to.exist()
done()
}
)
expect(node.pubsub).to.not.exist()
done()
})
})
})

32
test/stats.js Normal file
View File

@ -0,0 +1,32 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const createNode = require('./utils/create-node')
describe('libp2p', () => {
it('has stats', (done) => {
createNode('/ip4/127.0.0.1/tcp/0', {
config: {
peerDiscovery: {
mdns: {
enabled: false
}
},
EXPERIMENTAL: {
dht: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => {
expect(err).to.not.exist()
expect(node.stats).to.exist()
node.stop(done)
})
})
})
})

View File

@ -6,10 +6,11 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const series = require('async/series')
const utils = require('./utils/node')
const Mplex = require('libp2p-mplex')
const SPDY = require('libp2p-spdy')
const createNode = require('./utils/create-node')
const tryEcho = require('./utils/try-echo')
const createNode = utils.createNode
const echo = utils.echo
const echo = require('./utils/echo')
function test (nodeA, nodeB, callback) {
nodeA.dialProtocol(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
@ -35,7 +36,9 @@ describe('stream muxing', () => {
function setup (callback) {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy']
modules: {
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
@ -43,7 +46,9 @@ describe('stream muxing', () => {
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy']
modules: {
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeB = node
@ -60,14 +65,16 @@ describe('stream muxing', () => {
], done)
})
it('multiplex only', (done) => {
it('mplex only', (done) => {
let nodeA
let nodeB
function setup (callback) {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['multiplex']
modules: {
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
@ -75,7 +82,9 @@ describe('stream muxing', () => {
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['multiplex']
modules: {
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeB = node
@ -92,7 +101,7 @@ describe('stream muxing', () => {
], done)
})
it('spdy + multiplex', function (done) {
it('spdy + mplex', function (done) {
this.timeout(5000)
let nodeA
@ -101,7 +110,9 @@ describe('stream muxing', () => {
function setup (callback) {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy', 'multiplex']
modules: {
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
@ -109,7 +120,9 @@ describe('stream muxing', () => {
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy', 'multiplex']
modules: {
streamMuxer: [ SPDY, Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeB = node
@ -126,7 +139,7 @@ describe('stream muxing', () => {
], done)
})
it('spdy + multiplex switched order', function (done) {
it('spdy + mplex switched order', function (done) {
this.timeout(5 * 1000)
let nodeA
@ -135,7 +148,9 @@ describe('stream muxing', () => {
function setup (callback) {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy', 'multiplex']
modules: {
streamMuxer: [ SPDY, Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
@ -143,7 +158,9 @@ describe('stream muxing', () => {
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['multiplex', 'spdy']
modules: {
streamMuxer: [ Mplex, SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeB = node
@ -169,7 +186,9 @@ describe('stream muxing', () => {
function setup (callback) {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['spdy']
modules: {
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
@ -177,7 +196,9 @@ describe('stream muxing', () => {
node.start(cb)
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
muxer: ['multiplex']
modules: {
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
nodeB = node
@ -191,12 +212,12 @@ describe('stream muxing', () => {
(cb) => setup(cb),
(cb) => {
// it will just 'warm up a conn'
expect(Object.keys(nodeA.switch.muxers)).to.have.length(1)
expect(Object.keys(nodeB.switch.muxers)).to.have.length(1)
expect(Object.keys(nodeA._switch.muxers)).to.have.length(1)
expect(Object.keys(nodeB._switch.muxers)).to.have.length(1)
nodeA.dial(nodeB.peerInfo, (err) => {
expect(err).to.not.exist()
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
cb()
})
},

View File

@ -7,6 +7,7 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const Mplex = require('libp2p-mplex')
const pull = require('pull-stream')
const parallel = require('async/parallel')
const goodbye = require('pull-goodbye')
@ -14,46 +15,49 @@ const serializer = require('pull-serializer')
const w = require('webrtcsupport')
const tryEcho = require('./utils/try-echo')
const Node = require('./utils/bundle.browser')
const rawPeer = require('./fixtures/test-peer.json')
const Node = require('./utils/bundle-browser')
const jsonPeerId = require('./fixtures/test-peer.json')
describe('transports', () => {
describe('websockets', () => {
let peerB
let peerBMultiaddr = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + jsonPeerId.id
let nodeA
before((done) => {
const ma = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id
PeerId.createFromPrivKey(rawPeer.privKey, (err, id) => {
if (err) {
return done(err)
}
PeerId.createFromPrivKey(jsonPeerId.privKey, (err, id) => {
expect(err).to.not.exist()
peerB = new PeerInfo(id)
peerB.multiaddrs.add(ma)
peerB.multiaddrs.add(peerBMultiaddr)
done()
})
})
after((done) => nodeA.stop(done))
it('create libp2pNode', (done) => {
it('create a libp2p Node', (done) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
nodeA = new Node(peerInfo)
nodeA = new Node({
peerInfo: peerInfo
})
done()
})
})
it('create libp2pNode with multiplex only', (done) => {
it('create a libp2p Node with mplex only', (done) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist()
const b = new Node(peerInfo, null, { muxer: ['multiplex'] })
expect(b.modules.connection.muxer).to.eql([require('libp2p-multiplex')])
const b = new Node({
peerInfo: peerInfo,
modules: {
streamMuxer: [ Mplex ]
}
})
expect(b._modules.streamMuxer).to.eql([require('libp2p-mplex')])
done()
})
})
@ -65,7 +69,7 @@ describe('transports', () => {
// General connectivity tests
it('.dial using Multiaddr', (done) => {
nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => {
nodeA.dial(peerBMultiaddr, (err) => {
expect(err).to.not.exist()
setTimeout(check, 500) // Some time for Identify to finish
@ -79,7 +83,7 @@ describe('transports', () => {
})
it('.dialProtocol using Multiaddr', (done) => {
nodeA.dialProtocol(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
nodeA.dialProtocol(peerBMultiaddr, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist()
const peers = nodeA.peerBook.getAll()
@ -90,7 +94,7 @@ describe('transports', () => {
})
it('.hangUp using Multiaddr', (done) => {
nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => {
nodeA.hangUp(peerBMultiaddr, (err) => {
expect(err).to.not.exist()
setTimeout(check, 500)
@ -98,7 +102,7 @@ describe('transports', () => {
function check () {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
done()
}
})
@ -138,7 +142,7 @@ describe('transports', () => {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
done()
}
})
@ -197,8 +201,8 @@ describe('transports', () => {
it('create two peerInfo with webrtc-star addrs', (done) => {
parallel([
(cb) => PeerId.create({ bits: 1024 }, cb),
(cb) => PeerId.create({ bits: 1024 }, cb)
(cb) => PeerId.create({ bits: 512 }, cb),
(cb) => PeerId.create({ bits: 512 }, cb)
], (err, ids) => {
expect(err).to.not.exist()
@ -215,8 +219,12 @@ describe('transports', () => {
})
it('create two libp2p nodes with those peers', (done) => {
node1 = new Node(peer1, null, { webRTCStar: true })
node2 = new Node(peer2, null, { webRTCStar: true })
node1 = new Node({
peerInfo: peer1
})
node2 = new Node({
peerInfo: peer2
})
done()
})
@ -256,24 +264,26 @@ describe('transports', () => {
function check () {
const peers = node1.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
expect(Object.keys(node1._switch.muxedConns)).to.have.length(0)
done()
}
})
})
it('create a third node and check that discovery works', (done) => {
it('create a third node and check that discovery works', function (done) {
this.timeout(60 * 1000)
let counter = 0
function check () {
if (++counter === 3) {
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node1._switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2._switch.muxedConns).length).to.equal(1)
done()
}
}
PeerId.create((err, id3) => {
PeerId.create({ bits: 512 }, (err, id3) => {
expect(err).to.not.exist()
const peer3 = new PeerInfo(id3)
@ -283,7 +293,9 @@ describe('transports', () => {
node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check))
node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check))
const node3 = new Node(peer3, null, { webRTCStar: true })
const node3 = new Node({
peerInfo: peer3
})
node3.start(check)
})
})
@ -297,8 +309,8 @@ describe('transports', () => {
it('create two peerInfo with websocket-star addrs', (done) => {
parallel([
(cb) => PeerId.create({ bits: 1024 }, cb),
(cb) => PeerId.create({ bits: 1024 }, cb)
(cb) => PeerId.create({ bits: 512 }, cb),
(cb) => PeerId.create({ bits: 512 }, cb)
], (err, ids) => {
expect(err).to.not.exist()
@ -315,8 +327,12 @@ describe('transports', () => {
})
it('create two libp2p nodes with those peers', (done) => {
node1 = new Node(peer1, null, { wsStar: true })
node2 = new Node(peer2, null, { wsStar: true })
node1 = new Node({
peerInfo: peer1
})
node2 = new Node({
peerInfo: peer2
})
done()
})
@ -356,19 +372,21 @@ describe('transports', () => {
function check () {
const peers = node1.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
expect(Object.keys(node1._switch.muxedConns)).to.have.length(0)
done()
}
})
})
it('create a third node and check that discovery works', (done) => {
it('create a third node and check that discovery works', function (done) {
this.timeout(10 * 1000)
let counter = 0
function check () {
if (++counter === 3) {
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node1._switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2._switch.muxedConns).length).to.equal(1)
done()
}
}
@ -383,7 +401,9 @@ describe('transports', () => {
node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check))
node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check))
const node3 = new Node(peer3, null, { wsStar: true })
const node3 = new Node({
peerInfo: peer3
})
node3.start(check)
})
})

View File

@ -6,16 +6,17 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const parallel = require('async/parallel')
const series = require('async/series')
const utils = require('./utils/node.js')
const signalling = require('libp2p-webrtc-star/src/sig-server')
const rendezvous = require('libp2p-websocket-star-rendezvous')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const WSStar = require('libp2p-websocket-star')
const WRTCStar = require('libp2p-webrtc-star')
const wrtc = require('wrtc')
const tryEcho = require('./utils/try-echo')
const createNode = utils.createNode
const echo = utils.echo
const createNode = require('./utils/create-node.js')
const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
describe('transports', () => {
describe('TCP only', () => {
@ -90,14 +91,14 @@ describe('transports', () => {
(cb) => {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
cb()
},
(cb) => {
const peers = nodeB.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
cb()
}
], done)
@ -117,14 +118,14 @@ describe('transports', () => {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
cb()
},
(cb) => {
const peers = nodeB.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
cb()
}
], () => tryEcho(conn, done))
@ -143,14 +144,14 @@ describe('transports', () => {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
cb()
},
(cb) => {
const peers = nodeB.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
cb()
}
], done)
@ -169,13 +170,13 @@ describe('transports', () => {
(cb) => {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
cb()
},
(cb) => {
const peers = nodeB.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
cb()
}
], () => tryEcho(conn, done))
@ -193,13 +194,13 @@ describe('transports', () => {
(cb) => {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
cb()
},
(cb) => {
const peers = nodeB.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
cb()
}
], done)
@ -263,13 +264,13 @@ describe('transports', () => {
(cb) => {
const peers = nodeTCP.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeTCP.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeTCP._switch.muxedConns)).to.have.length(1)
cb()
},
(cb) => {
const peers = nodeTCPnWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(1)
cb()
}
], done)
@ -287,14 +288,14 @@ describe('transports', () => {
(cb) => {
const peers = nodeTCP.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeTCP.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeTCP._switch.muxedConns)).to.have.length(0)
cb()
},
(cb) => {
const peers = nodeTCPnWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(0)
cb()
}
], done)
@ -314,13 +315,13 @@ describe('transports', () => {
(cb) => {
const peers = nodeTCPnWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(2)
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(1)
cb()
},
(cb) => {
const peers = nodeWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeWS.switch.muxedConns)).to.have.length(1)
expect(Object.keys(nodeWS._switch.muxedConns)).to.have.length(1)
cb()
}
], done)
@ -338,14 +339,14 @@ describe('transports', () => {
(cb) => {
const peers = nodeTCPnWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(2)
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(0)
cb()
},
(cb) => {
const peers = nodeWS.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeWS.switch.muxedConns)).to.have.length(0)
expect(Object.keys(nodeWS._switch.muxedConns)).to.have.length(0)
cb()
}
], done)
@ -367,7 +368,7 @@ describe('transports', () => {
let nodeAll
let nodeTCP
let nodeWS
let nodeWStar
let nodeWebRTCStar
let ss
@ -375,23 +376,33 @@ describe('transports', () => {
this.timeout(5 * 1000)
parallel([
(cb) => {
signalling.start({ port: 24642 }, (err, server) => {
expect(err).to.not.exist()
ss = server
cb()
})
},
(cb) => signalling.start({ port: 24642 }, (err, server) => {
expect(err).to.not.exist()
ss = server
cb()
}),
(cb) => {
const wstar = new WRTCStar({wrtc: wrtc})
createNode([
'/ip4/0.0.0.0/tcp/0',
'/ip4/127.0.0.1/tcp/25011/ws',
'/ip4/127.0.0.1/tcp/24642/ws/p2p-webrtc-star'
], {
modules: {
transport: [wstar],
discovery: [wstar.discovery]
transport: [
TCP,
WS,
wstar
],
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
@ -425,11 +436,18 @@ describe('transports', () => {
], {
modules: {
transport: [wstar],
discovery: [wstar.discovery]
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeWStar = node
nodeWebRTCStar = node
node.handle('/echo/1.0.0', echo)
node.start(cb)
})
@ -444,7 +462,7 @@ describe('transports', () => {
(cb) => nodeAll.stop(cb),
(cb) => nodeTCP.stop(cb),
(cb) => nodeWS.stop(cb),
(cb) => nodeWStar.stop(cb),
(cb) => nodeWebRTCStar.stop(cb),
(cb) => ss.stop(cb)
], done)
})
@ -453,7 +471,7 @@ describe('transports', () => {
let i = 1;
[nodeAll, otherNode].forEach((node) => {
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
expect(Object.keys(node.switch.muxedConns)).to.have.length(muxed)
expect(Object.keys(node._switch.muxedConns)).to.have.length(muxed)
})
callback()
}
@ -490,20 +508,20 @@ describe('transports', () => {
})
})
it('nodeAll.dial nodeWStar using PeerInfo', function (done) {
it('nodeAll.dial nodeWebRTCStar using PeerInfo', function (done) {
this.timeout(40 * 1000)
nodeAll.dial(nodeWStar.peerInfo, (err) => {
nodeAll.dial(nodeWebRTCStar.peerInfo, (err) => {
expect(err).to.not.exist()
// Some time for Identify to finish
setTimeout(() => check(nodeWStar, 1, 3, done), 500)
setTimeout(() => check(nodeWebRTCStar, 1, 3, done), 500)
})
})
it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => {
nodeAll.hangUp(nodeWStar.peerInfo, (err) => {
it('nodeAll.hangUp nodeWebRTCStar using PeerInfo', (done) => {
nodeAll.hangUp(nodeWebRTCStar.peerInfo, (err) => {
expect(err).to.not.exist()
setTimeout(() => check(nodeWStar, 0, 3, done), 500)
setTimeout(() => check(nodeWebRTCStar, 0, 3, done), 500)
})
})
})
@ -512,7 +530,7 @@ describe('transports', () => {
let nodeAll
let nodeTCP
let nodeWS
let nodeWStar
let nodeWebSocketStar
let ss
@ -527,14 +545,26 @@ describe('transports', () => {
},
(cb) => {
const wstar = new WSStar()
createNode([
'/ip4/0.0.0.0/tcp/0',
'/ip4/127.0.0.1/tcp/25011/ws',
'/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star'
], {
modules: {
transport: [wstar],
discovery: [wstar.discovery]
transport: [
TCP,
WS,
wstar
],
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
@ -569,11 +599,18 @@ describe('transports', () => {
], {
modules: {
transport: [wstar],
discovery: [wstar.discovery]
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeWStar = node
nodeWebSocketStar = node
wstar.lazySetId(node.peerInfo.id)
node.handle('/echo/1.0.0', echo)
node.start(cb)
@ -587,7 +624,7 @@ describe('transports', () => {
(cb) => nodeAll.stop(cb),
(cb) => nodeTCP.stop(cb),
(cb) => nodeWS.stop(cb),
(cb) => nodeWStar.stop(cb),
(cb) => nodeWebSocketStar.stop(cb),
(cb) => ss.stop(cb)
], done)
})
@ -596,7 +633,7 @@ describe('transports', () => {
let i = 1;
[nodeAll, otherNode].forEach((node) => {
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
expect(Object.keys(node.switch.muxedConns)).to.have.length(muxed)
expect(Object.keys(node._switch.muxedConns)).to.have.length(muxed)
})
done()
}
@ -633,19 +670,19 @@ describe('transports', () => {
})
})
it('nodeAll.dial nodeWStar using PeerInfo', (done) => {
nodeAll.dial(nodeWStar.peerInfo, (err) => {
it('nodeAll.dial nodeWebSocketStar using PeerInfo', (done) => {
nodeAll.dial(nodeWebSocketStar.peerInfo, (err) => {
expect(err).to.not.exist()
// Some time for Identify to finish
setTimeout(() => check(nodeWStar, 1, 3, done), 500)
setTimeout(() => check(nodeWebSocketStar, 1, 3, done), 500)
})
})
it('nodeAll.hangUp nodeWStar using PeerInfo', (done) => {
nodeAll.hangUp(nodeWStar.peerInfo, (err) => {
it('nodeAll.hangUp nodeWebSocketStar using PeerInfo', (done) => {
nodeAll.hangUp(nodeWebSocketStar.peerInfo, (err) => {
expect(err).to.not.exist()
// Some time for Identify to finish
setTimeout(() => check(nodeWStar, 0, 3, done), 500)
setTimeout(() => check(nodeWebSocketStar, 0, 3, done), 500)
})
})
})

View File

@ -0,0 +1,92 @@
'use strict'
const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const WebSocketStar = require('libp2p-websocket-star')
const Bootstrap = require('libp2p-railing')
const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const KadDHT = require('libp2p-kad-dht')
const SECIO = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../..')
function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') { return pref }
switch (pref.trim().toLowerCase()) {
case 'spdy': return SPDY
case 'mplex': return MPLEX
default:
throw new Error(pref + ' muxer not available')
}
})
}
function getMuxers (options) {
if (options) {
return mapMuxers(options)
} else {
return [MPLEX, SPDY]
}
}
class Node extends libp2p {
constructor (_options) {
_options = _options || {}
const starOpts = { id: _options.peerInfo.id }
const wrtcStar = new WebRTCStar(starOpts)
const wsStar = new WebSocketStar(starOpts)
const defaults = {
modules: {
transport: [
wrtcStar,
wsStar,
new WS()
],
streamMuxer: getMuxers(_options.muxer),
connEncryption: [
SECIO
],
peerDiscovery: [
wrtcStar.discovery,
wsStar.discovery,
Bootstrap
],
dht: KadDHT
},
config: {
peerDiscovery: {
webRTCStar: {
enabled: true
},
websocketStar: {
enabled: true
},
bootstrap: {
interval: 10000,
enabled: false,
list: _options.boostrapList
}
},
relay: {
enabled: false,
hop: {
enabled: false,
active: false
}
},
EXPERIMENTAL: {
dht: false,
pubsub: false
}
}
}
super(defaultsDeep(_options, defaults))
}
}
module.exports = Node

View File

@ -0,0 +1,88 @@
'use strict'
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-railing')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../..')
function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') { return pref }
switch (pref.trim().toLowerCase()) {
case 'spdy': return SPDY
case 'mplex': return MPLEX
default:
throw new Error(pref + ' muxer not available')
}
})
}
function getMuxers (muxers) {
const muxerPrefs = process.env.LIBP2P_MUXER
if (muxerPrefs && !muxers) {
return mapMuxers(muxerPrefs.split(','))
} else if (muxers) {
return mapMuxers(muxers)
} else {
return [MPLEX, SPDY]
}
}
class Node extends libp2p {
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WS
],
streamMuxer: getMuxers(_options.muxer),
connEncryption: [
SECIO
],
peerDiscovery: [
MulticastDNS,
Bootstrap
],
dht: KadDHT
},
config: {
peerDiscovery: {
mdns: {
interval: 10000,
enabled: false
},
bootstrap: {
interval: 10000,
enabled: false,
list: _options.bootstrapList
}
},
relay: {
enabled: false,
hop: {
enabled: false,
active: false
}
},
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: false,
pubsub: false
}
}
}
super(defaultsDeep(_options, defaults))
}
}
module.exports = Node

View File

@ -1,74 +0,0 @@
'use strict'
const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const WebSocketStar = require('libp2p-websocket-star')
const spdy = require('libp2p-spdy')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const Railing = require('libp2p-railing')
const libp2p = require('../..')
function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') {
return pref
}
switch (pref.trim().toLowerCase()) {
case 'spdy':
return spdy
case 'multiplex':
return multiplex
default:
throw new Error(pref + ' muxer not available')
}
})
}
function getMuxers (options) {
if (options) {
return mapMuxers(options)
} else {
return [multiplex, spdy]
}
}
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const wrtcStar = new WebRTCStar({ id: peerInfo.id })
const wsStar = new WebSocketStar({ id: peerInfo.id })
const modules = {
transport: [
new WS(),
wrtcStar,
wsStar
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [
secio
]
},
discovery: []
}
if (options.webRTCStar) {
modules.discovery.push(wrtcStar.discovery)
}
if (options.wsStar) {
modules.discovery.push(wsStar.discovery)
}
if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}
super(modules, peerInfo, peerBook, options)
}
}
module.exports = Node

View File

@ -1,80 +0,0 @@
'use strict'
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Railing = require('libp2p-railing')
const spdy = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const libp2p = require('../..')
function mapMuxers (list) {
return list.map((pref) => {
if (typeof pref !== 'string') {
return pref
}
switch (pref.trim().toLowerCase()) {
case 'spdy': return spdy
case 'multiplex': return multiplex
default:
throw new Error(pref + ' muxer not available')
}
})
}
function getMuxers (muxers) {
const muxerPrefs = process.env.LIBP2P_MUXER
if (muxerPrefs && !muxers) {
return mapMuxers(muxerPrefs.split(','))
} else if (muxers) {
return mapMuxers(muxers)
} else {
return [multiplex, spdy]
}
}
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: getMuxers(options.muxer),
crypto: [ secio ]
},
discovery: []
}
if (options.dht) {
modules.DHT = KadDHT
}
if (options.mdns) {
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
modules.discovery.push(mdns)
}
if (options.bootstrap) {
const r = new Railing(options.bootstrap)
modules.discovery.push(r)
}
if (options.modules && options.modules.transport) {
options.modules.transport.forEach((t) => modules.transport.push(t))
}
if (options.modules && options.modules.discovery) {
options.modules.discovery.forEach((d) => modules.discovery.push(d))
}
super(modules, peerInfo, peerBook, options)
}
}
module.exports = Node

View File

@ -3,11 +3,10 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const Node = require('./bundle.node')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const waterfall = require('async/waterfall')
const pull = require('pull-stream')
const Node = require('./bundle-nodejs')
function createNode (multiaddrs, options, callback) {
if (typeof options === 'function') {
@ -26,20 +25,10 @@ function createNode (multiaddrs, options, callback) {
(peerId, cb) => PeerInfo.create(peerId, cb),
(peerInfo, cb) => {
multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma))
cb(null, peerInfo)
},
(peerInfo, cb) => {
const node = new Node(peerInfo, undefined, options)
cb(null, node)
options.peerInfo = peerInfo
cb(null, new Node(options))
}
], callback)
}
function echo (protocol, conn) {
pull(conn, conn)
}
module.exports = {
createNode: createNode,
echo: echo
}
module.exports = createNode

10
test/utils/echo.js Normal file
View File

@ -0,0 +1,10 @@
/* eslint-env mocha */
'use strict'
const pull = require('pull-stream')
function echo (protocol, conn) {
pull(conn, conn)
}
module.exports = echo