Skip to content

Commit e3a4c01

Browse files
committed
docs: update peer and content routing examples
1 parent 6f7baac commit e3a4c01

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

examples/peer-and-content-routing/1.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
'use strict'
22

3-
const libp2p = require('libp2p')
3+
const libp2p = require('../../')
44
const TCP = require('libp2p-tcp')
55
const Mplex = require('libp2p-mplex')
66
const SECIO = require('libp2p-secio')
77
const PeerInfo = require('peer-info')
88
const KadDHT = require('libp2p-kad-dht')
9-
9+
const defaultsDeep = require('@nodeutils/defaults-deep')
1010
const waterfall = require('async/waterfall')
1111
const parallel = require('async/parallel')
1212

1313
class MyBundle extends libp2p {
14-
constructor (peerInfo) {
15-
const modules = {
16-
transport: [new TCP()],
17-
connection: {
18-
muxer: [Mplex],
19-
crypto: [SECIO]
14+
constructor (_options) {
15+
const defaults = {
16+
modules: {
17+
transport: [ TCP ],
18+
streamMuxer: [ Mplex ],
19+
connEncryption: [ SECIO ],
20+
// we add the DHT module that will enable Peer and Content Routing
21+
dht: KadDHT
2022
},
21-
// we add the DHT module that will enable Peer and Content Routing
22-
DHT: KadDHT
23+
config: {
24+
dht: {
25+
kBucketSize: 20
26+
},
27+
EXPERIMENTAL: {
28+
dht: true
29+
}
30+
}
2331
}
24-
super(modules, peerInfo)
32+
33+
super(defaultsDeep(_options, defaults))
2534
}
2635
}
2736

@@ -32,7 +41,9 @@ function createNode (callback) {
3241
(cb) => PeerInfo.create(cb),
3342
(peerInfo, cb) => {
3443
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
35-
node = new MyBundle(peerInfo)
44+
node = new MyBundle({
45+
peerInfo
46+
})
3647
node.start(cb)
3748
}
3849
], (err) => callback(err, node))

examples/peer-and-content-routing/2.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
'use strict'
22

3-
const libp2p = require('libp2p')
3+
const libp2p = require('../../')
44
const TCP = require('libp2p-tcp')
55
const Mplex = require('libp2p-mplex')
66
const SECIO = require('libp2p-secio')
77
const PeerInfo = require('peer-info')
88
const CID = require('cids')
99
const KadDHT = require('libp2p-kad-dht')
10-
10+
const defaultsDeep = require('@nodeutils/defaults-deep')
1111
const waterfall = require('async/waterfall')
1212
const parallel = require('async/parallel')
1313

1414
class MyBundle extends libp2p {
15-
constructor (peerInfo) {
16-
const modules = {
17-
transport: [new TCP()],
18-
connection: {
19-
muxer: [Mplex],
20-
crypto: [SECIO]
15+
constructor (_options) {
16+
const defaults = {
17+
modules: {
18+
transport: [ TCP ],
19+
streamMuxer: [ Mplex ],
20+
connEncryption: [ SECIO ],
21+
// we add the DHT module that will enable Peer and Content Routing
22+
dht: KadDHT
2123
},
22-
// we add the DHT module that will enable Peer and Content Routing
23-
DHT: KadDHT
24+
config: {
25+
dht: {
26+
kBucketSize: 20
27+
},
28+
EXPERIMENTAL: {
29+
dht: true
30+
}
31+
}
2432
}
25-
super(modules, peerInfo)
33+
34+
super(defaultsDeep(_options, defaults))
2635
}
2736
}
2837

@@ -33,7 +42,9 @@ function createNode (callback) {
3342
(cb) => PeerInfo.create(cb),
3443
(peerInfo, cb) => {
3544
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
36-
node = new MyBundle(peerInfo)
45+
node = new MyBundle({
46+
peerInfo
47+
})
3748
node.start(cb)
3849
}
3950
], (err) => callback(err, node))

examples/peer-and-content-routing/README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,27 @@ First, let's update our bundle to support Peer Routing and Content Routing.
1414

1515
```JavaScript
1616
class MyBundle extends libp2p {
17-
constructor (peerInfo) {
18-
const modules = {
19-
transport: [new TCP()],
20-
connection: {
21-
muxer: [Mplex],
22-
crypto: [SECIO]
17+
constructor (_options) {
18+
const defaults = {
19+
modules: {
20+
transport: [ TCP ],
21+
streamMuxer: [ Mplex ],
22+
connEncryption: [ SECIO ],
23+
// we add the DHT module that will enable Peer and Content Routing
24+
dht: KadDHT
2325
},
24-
// we add the DHT module that will enable Peer and Content Routing
25-
DHT: KadDHT
26+
config: {
27+
dht: {
28+
kBucketSize: 20
29+
},
30+
EXPERIMENTAL: {
31+
// dht must be enabled
32+
dht: true
33+
}
34+
}
2635
}
27-
super(modules, peerInfo)
36+
37+
super(defaultsDeep(_options, defaults))
2838
}
2939
}
3040
```
@@ -44,7 +54,7 @@ parallel([
4454
], (err) => {
4555
if (err) { throw err }
4656

47-
//
57+
//
4858
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
4959
if (err) { throw err }
5060

0 commit comments

Comments
 (0)