Skip to content

Commit 6905f1b

Browse files
authored
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
1 parent b80e892 commit 6905f1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1399
-787
lines changed

.aegir.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ const PeerId = require('peer-id')
55
const pull = require('pull-stream')
66
const parallel = require('async/parallel')
77

8-
const rawPeer = require('./test/fixtures/test-peer.json')
9-
const Node = require('./test/utils/bundle.node.js')
10-
const sigServer = require('libp2p-webrtc-star/src/sig-server')
118
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
9+
const sigServer = require('libp2p-webrtc-star/src/sig-server')
10+
11+
const rawPeer = require('./test/fixtures/test-peer.json')
12+
const Node = require('./test/utils/bundle-nodejs.js')
1213

1314
let wrtcRendezvous
1415
let wsRendezvous
@@ -21,7 +22,9 @@ const before = (done) => {
2122
port: 15555
2223
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
2324
}, (err, server) => {
24-
if (err) { return cb(err) }
25+
if (err) {
26+
return cb(err)
27+
}
2528
wrtcRendezvous = server
2629
cb()
2730
})
@@ -33,7 +36,9 @@ const before = (done) => {
3336
strictMultiaddr: false,
3437
cryptoChallenge: true
3538
}, (err, _server) => {
36-
if (err) { return cb(err) }
39+
if (err) {
40+
return cb(err)
41+
}
3742
wsRendezvous = _server
3843
cb()
3944
})
@@ -47,7 +52,9 @@ const before = (done) => {
4752

4853
peer.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')
4954

50-
node = new Node(peer)
55+
node = new Node({
56+
peerInfo: peer
57+
})
5158
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
5259
node.start(cb)
5360
})
@@ -56,11 +63,11 @@ const before = (done) => {
5663
}
5764

5865
const after = (done) => {
59-
setTimeout(() => parallel(
60-
[node, wrtcRendezvous, wsRendezvous].map((s) => {
61-
return (cb) => s.stop(cb)
62-
})
63-
, done), 2000)
66+
setTimeout(() =>
67+
parallel(
68+
[node, wrtcRendezvous, wsRendezvous].map((s) => (cb) => s.stop(cb)),
69+
done),
70+
2000)
6471
}
6572

6673
module.exports = {

.travis.yml

-23
This file was deleted.

README.md

+59-23
Original file line numberDiff line numberDiff line change
@@ -103,47 +103,83 @@ libp2p becomes very simple and basically acts as a glue for every module that co
103103
```JavaScript
104104
// Creating a bundle that adds:
105105
// transport: websockets + tcp
106-
// stream-muxing: SPDY
106+
// stream-muxing: spdy & mplex
107107
// crypto-channel: secio
108108
// discovery: multicast-dns
109109

110110
const libp2p = require('libp2p')
111111
const TCP = require('libp2p-tcp')
112112
const WS = require('libp2p-websockets')
113-
const spdy = require('libp2p-spdy')
114-
const secio = require('libp2p-secio')
113+
const SPDY = require('libp2p-spdy')
114+
const MPLEX = require('libp2p-mplex')
115+
const SECIO = require('libp2p-secio')
115116
const MulticastDNS = require('libp2p-mdns')
116117
const DHT = require('libp2p-kad-dht')
118+
const defaultsDeep = require('@nodeutils/defaults-deep')
117119

118120
class Node extends libp2p {
119-
constructor (peerInfo, peerBook, options) {
120-
options = options || {}
121-
122-
const modules = {
123-
transport: [
124-
new TCP(),
125-
new WS()
126-
],
127-
connection: {
128-
muxer: [
129-
spdy
121+
constructor (_peerInfo, _peerBook, _options) {
122+
const defaults = {
123+
peerInfo: _peerInfo // The Identity of your Peer
124+
peerBook: _peerBook, // Where peers get tracked, if undefined libp2p will create one instance
125+
126+
// The libp2p modules for this libp2p bundle
127+
modules: {
128+
transport: [
129+
TCP,
130+
new WS() // It can take instances too!
130131
],
131-
crypto: [
132-
secio
132+
streamMuxer: [
133+
SPDY,
134+
MPLEX
135+
],
136+
connEncryption: [
137+
SECIO
133138
]
139+
peerDiscovery: [
140+
MulticastDNS
141+
],
142+
peerRouting: {}, // Currently both peerRouting and contentRouting are patched through the DHT,
143+
contentRouting: {} // this will change once we factor that into two modules, for now do the following line:
144+
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components
145+
},
146+
147+
// libp2p config options (typically found on a config.json)
148+
config: { // The config object is the part of the config that can go into a file, config.json.
149+
peerDiscovery: {
150+
mdns: { // mdns options
151+
interval: 1000 // ms
152+
enabled: true
153+
},
154+
webrtcStar: { // webrtc-star options
155+
interval: 1000 // ms
156+
enabled: false
157+
}
158+
// .. other discovery module options.
159+
},
160+
peerRouting: {},
161+
contentRouting: {},
162+
relay: { // Circuit Relay options
163+
enabled: false,
164+
hop: {
165+
enabled: false,
166+
active: false
167+
}
168+
}
169+
// Enable/Disable Experimental features
170+
EXPERIMENTAL: { // Experimental features ("behind a flag")
171+
pubsub: false,
172+
dht: false
173+
}
134174
},
135-
discovery: [
136-
new MulticastDNS(peerInfo)
137-
],
138-
// DHT is passed as its own enabling PeerRouting, ContentRouting and DHT itself components
139-
dht: DHT
140175
}
141176

142-
super(modules, peerInfo, peerBook, options)
177+
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
178+
super(defaultsDeep(_options, defaults))
143179
}
144180
}
145181

146-
// Now all the nodes you create, will have TCP, WebSockets, SPDY, SECIO and MulticastDNS support.
182+
// Now all the nodes you create, will have TCP, WebSockets, SPDY, MPLEX, SECIO and MulticastDNS support.
147183
```
148184

149185
### API

ci/Jenkinsfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// 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.
2-
javascript()
2+
javascript(['nodejs_versions': ['8.11.3']])

circle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
machine:
22
node:
3-
version: 8.11.1
3+
version: 8.11.3
44

55
test:
66
post:

examples/chat/README.md

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

examples/chat/src/dialer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ async.parallel([
3131
if (err) throw err
3232
const peerDialer = new PeerInfo(ids[0])
3333
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
34-
const nodeDialer = new Node(peerDialer)
34+
const nodeDialer = new Node({
35+
peerInfo: peerDialer
36+
})
3537

3638
const peerListener = new PeerInfo(ids[1])
3739
idListener = ids[1]

examples/chat/src/libp2p-bundle.js

+33-36
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
const TCP = require('libp2p-tcp')
44
const MulticastDNS = require('libp2p-mdns')
55
const WS = require('libp2p-websockets')
6-
const Railing = require('libp2p-railing')
6+
const Bootstrap = require('libp2p-railing')
77
const spdy = require('libp2p-spdy')
88
const KadDHT = require('libp2p-kad-dht')
99
const mplex = require('libp2p-mplex')
1010
const secio = require('libp2p-secio')
11+
const defaultsDeep = require('@nodeutils/defaults-deep')
1112
const libp2p = require('../../..')
1213

1314
function mapMuxers (list) {
@@ -36,44 +37,40 @@ function getMuxers (muxers) {
3637
}
3738

3839
class Node extends libp2p {
39-
constructor (peerInfo, peerBook, options) {
40-
options = options || {}
41-
42-
const modules = {
43-
transport: [
44-
new TCP(),
45-
new WS()
46-
],
47-
connection: {
48-
muxer: getMuxers(options.muxer),
49-
crypto: [ secio ]
40+
constructor (_options) {
41+
const defaults = {
42+
modules: {
43+
transport: [
44+
TCP,
45+
WS
46+
],
47+
streamMuxer: getMuxers(_options.muxer),
48+
connEncryption: [ secio ],
49+
peerDiscovery: [
50+
MulticastDNS,
51+
Bootstrap
52+
],
53+
dht: KadDHT
5054
},
51-
discovery: []
52-
}
53-
54-
if (options.dht) {
55-
modules.DHT = KadDHT
56-
}
57-
58-
if (options.mdns) {
59-
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
60-
modules.discovery.push(mdns)
61-
}
62-
63-
if (options.bootstrap) {
64-
const r = new Railing(options.bootstrap)
65-
modules.discovery.push(r)
66-
}
67-
68-
if (options.modules && options.modules.transport) {
69-
options.modules.transport.forEach((t) => modules.transport.push(t))
70-
}
71-
72-
if (options.modules && options.modules.discovery) {
73-
options.modules.discovery.forEach((d) => modules.discovery.push(d))
55+
config: {
56+
peerDiscovery: {
57+
mdns: {
58+
interval: 10000,
59+
enabled: false
60+
},
61+
bootstrap: {
62+
interval: 10000,
63+
enabled: false,
64+
list: _options.bootstrapList
65+
}
66+
},
67+
dht: {
68+
kBucketSize: 20
69+
}
70+
}
7471
}
7572

76-
super(modules, peerInfo, peerBook, options)
73+
super(defaultsDeep(_options, defaults))
7774
}
7875
}
7976

examples/chat/src/listener.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
1414
}
1515
const peerListener = new PeerInfo(idListener)
1616
peerListener.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
17-
const nodeListener = new Node(peerListener)
17+
const nodeListener = new Node({
18+
peerInfo: peerListener
19+
})
1820

1921
nodeListener.start((err) => {
2022
if (err) {
2123
throw err
2224
}
2325

24-
nodeListener.switch.on('peer-mux-established', (peerInfo) => {
26+
nodeListener.on('peer:connect', (peerInfo) => {
2527
console.log(peerInfo.id.toB58String())
2628
})
2729

0 commit comments

Comments
 (0)