Skip to content

Commit

Permalink
feat: (BREAKING CHANGE) overhaul libp2p config and constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias authored and jacobheun committed Jun 20, 2018
1 parent d597204 commit c4d9e32
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 469 deletions.
29 changes: 18 additions & 11 deletions .aegir.js
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
84 changes: 61 additions & 23 deletions README.md
Expand Up @@ -101,47 +101,85 @@ 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('lodash.defaultsdeep')

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://lodash.com/docs/4.17.5#defaultsDeep
defaultsDeep(_options, defaults)

super(options)
}
}

// 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
17 changes: 12 additions & 5 deletions examples/transports/1.js
Expand Up @@ -4,13 +4,20 @@ const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('lodash.defaultsdeep')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()]
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP
]
}
}
super(modules, peerInfo)

defaultsDeep(_options, defaults)
super(_options)
}
}

Expand All @@ -20,7 +27,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) => {
Expand Down
17 changes: 12 additions & 5 deletions examples/transports/2.js
Expand Up @@ -4,15 +4,22 @@ const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('lodash.defaultsdeep')
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)

defaultsDeep(_options, defaults)
super(_options)
}
}

Expand All @@ -23,7 +30,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))
Expand Down
18 changes: 13 additions & 5 deletions examples/transports/3.js
Expand Up @@ -5,15 +5,23 @@ const TCP = require('libp2p-tcp')
const WebSockets = require('libp2p-websockets')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('lodash.defaultsdeep')
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)

defaultsDeep(_options, defaults)
super(_options)
}
}

Expand All @@ -28,7 +36,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))
Expand Down
40 changes: 27 additions & 13 deletions examples/transports/README.md
Expand Up @@ -27,16 +27,23 @@ const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const defaultsDeep = require('lodash.defaultsdeep')

// 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)

defaultsDeep(_options, defaults)
super(_options)
}
}
```
Expand All @@ -57,7 +64,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)
}
Expand Down Expand Up @@ -114,7 +121,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))
Expand Down Expand Up @@ -196,11 +203,18 @@ 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)

defaultsDeep(_options, defaults)
super(_options)
}
}
```
Expand All @@ -219,7 +233,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))
Expand Down

0 comments on commit c4d9e32

Please sign in to comment.