Skip to content

Commit

Permalink
feat: (BREAKING CHANGE) overhaul libp2p config and constructor
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
daviddias authored Jun 28, 2018
1 parent b80e892 commit 6905f1b
Show file tree
Hide file tree
Showing 56 changed files with 1,399 additions and 787 deletions.
29 changes: 18 additions & 11 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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)
})
Expand All @@ -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 = {
Expand Down
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

82 changes: 59 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,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 || {}

const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: [
spdy
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

// 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
Expand Down
2 changes: 1 addition & 1 deletion ci/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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']])
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
machine:
node:
version: 8.11.1
version: 8.11.3

test:
post:
Expand Down
12 changes: 12 additions & 0 deletions examples/chat/README.md
Original file line number Diff line number Diff line change
@@ -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!
4 changes: 3 additions & 1 deletion examples/chat/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
69 changes: 33 additions & 36 deletions examples/chat/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 mplex = require('libp2p-mplex')
const secio = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')

function mapMuxers (list) {
Expand Down Expand Up @@ -36,44 +37,40 @@ function getMuxers (muxers) {
}

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: []
}

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))
config: {
peerDiscovery: {
mdns: {
interval: 10000,
enabled: false
},
bootstrap: {
interval: 10000,
enabled: false,
list: _options.bootstrapList
}
},
dht: {
kBucketSize: 20
}
}
}

super(modules, peerInfo, peerBook, options)
super(defaultsDeep(_options, defaults))
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/chat/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.switch.on('peer-mux-established', (peerInfo) => {
nodeListener.on('peer:connect', (peerInfo) => {
console.log(peerInfo.id.toB58String())
})

Expand Down
Loading

0 comments on commit 6905f1b

Please sign in to comment.