Skip to content

Commit

Permalink
refactor: use different defaultsDeep function and clean up some code
Browse files Browse the repository at this point in the history
docs: update transport example readme
  • Loading branch information
jacobheun committed Jun 20, 2018
1 parent c4d9e32 commit af3aeb2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 55 deletions.
8 changes: 3 additions & 5 deletions README.md
Expand Up @@ -113,7 +113,7 @@ 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')
const defaultsDeep = require('@nodeutils/defaults-deep')

class Node extends libp2p {
constructor (_peerInfo, _peerBook, _options) {
Expand Down Expand Up @@ -172,10 +172,8 @@ class Node extends libp2p {
},
}

// overload any defaults of your bundle using https://lodash.com/docs/4.17.5#defaultsDeep
defaultsDeep(_options, defaults)

super(options)
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
super(defaultsDeep(_options, defaults))
}
}

Expand Down
5 changes: 2 additions & 3 deletions examples/transports/1.js
Expand Up @@ -4,7 +4,7 @@ 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 defaultsDeep = require('@nodeutils/defaults-deep')

class MyBundle extends libp2p {
constructor (_options) {
Expand All @@ -16,8 +16,7 @@ class MyBundle extends libp2p {
}
}

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

Expand Down
5 changes: 2 additions & 3 deletions examples/transports/2.js
Expand Up @@ -4,7 +4,7 @@ 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 defaultsDeep = require('@nodeutils/defaults-deep')
const parallel = require('async/parallel')
const pull = require('pull-stream')

Expand All @@ -18,8 +18,7 @@ class MyBundle extends libp2p {
}
}

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

Expand Down
5 changes: 2 additions & 3 deletions examples/transports/3.js
Expand Up @@ -5,7 +5,7 @@ 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 defaultsDeep = require('@nodeutils/defaults-deep')
const parallel = require('async/parallel')
const pull = require('pull-stream')

Expand All @@ -20,8 +20,7 @@ class MyBundle extends libp2p {
}
}

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

Expand Down
14 changes: 6 additions & 8 deletions examples/transports/README.md
Expand Up @@ -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:

```bash
> npm install libp2p libp2p-tcp peer-info async
> 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`.
Expand All @@ -27,7 +27,7 @@ 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 defaultsDeep = require('@nodeutils/defaults-deep')

// This MyBundle class is your libp2p bundle packed with TCP
class MyBundle extends libp2p {
Expand All @@ -42,8 +42,7 @@ class MyBundle extends libp2p {
}
}

defaultsDeep(_options, defaults)
super(_options)
super(defaultsDeep(_options, defaults))
}
}
```
Expand Down Expand Up @@ -140,7 +139,7 @@ Now we are going to use `async/parallel` to create two nodes, print their addres
const parallel = require('async/parallel')
```

Then,
Then,

```js
parallel([
Expand Down Expand Up @@ -213,8 +212,7 @@ class MyBundle extends libp2p {
}
}

defaultsDeep(_options, defaults)
super(_options)
super(defaultsDeep(_options, defaults))
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,6 +50,7 @@
"peer-info": "~0.14.1"
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"aegir": "^14.0.0",
"chai": "^4.1.2",
"cids": "~0.5.3",
Expand All @@ -66,7 +67,6 @@
"libp2p-webrtc-star": "~0.15.3",
"libp2p-websocket-star": "~0.8.1",
"libp2p-websocket-star-rendezvous": "~0.2.3",
"lodash.defaultsdeep": "^4.6.0",
"lodash.times": "^4.3.2",
"pull-goodbye": "0.0.2",
"pull-serializer": "~0.3.2",
Expand Down
18 changes: 8 additions & 10 deletions src/index.js
Expand Up @@ -159,7 +159,7 @@ class Node extends EventEmitter {

// all transports need to be setup before discover starts
if (this._modules.peerDiscovery && this._config.peerDiscovery) {
each(this._modules.peerDiscovery, (D, cb) => {
each(this._modules.peerDiscovery, (D, _cb) => {
// If enabled then start it
if (this._config.peerDiscovery[D.tag].enabled) {
let d
Expand All @@ -173,9 +173,9 @@ class Node extends EventEmitter {

d.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
this._discovery.push(d)
d.start(cb)
d.start(_cb)
} else {
cb()
_cb()
}
}, cb)
} else {
Expand Down Expand Up @@ -206,13 +206,11 @@ class Node extends EventEmitter {
// detect which multiaddrs we don't have a transport for and remove them
const multiaddrs = this.peerInfo.multiaddrs.toArray()

this._transport.forEach((transport) => {
multiaddrs.forEach((multiaddr) => {
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
!this._transport.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()
},
Expand Down
21 changes: 6 additions & 15 deletions test/utils/bundle-browser.js
Expand Up @@ -8,7 +8,7 @@ const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const KadDHT = require('libp2p-kad-dht')
const SECIO = require('libp2p-secio')
const defaultsDeep = require('lodash.defaultsdeep')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../..')

function mapMuxers (list) {
Expand Down Expand Up @@ -43,18 +43,16 @@ class Node extends libp2p {
modules: {
transport: [
wrtcStar,
wsStar
wsStar,
new WS()
],
streamMuxer: getMuxers(_options.muxer),
connEncryption: [
SECIO
],
peerDiscovery: [
// NOTE: defaultsDeep clones these references making the listeners be
// attached to a clone and not the original. See the below how
// to attach instances.
// wrtcStar.discovery,
// wsStar.discovery,
wrtcStar.discovery,
wsStar.discovery,
Bootstrap
],
peerRouting: [],
Expand Down Expand Up @@ -89,14 +87,7 @@ class Node extends libp2p {
}
}

defaultsDeep(_options, defaults)

// NOTE: defaultsDeep clones instances and screws things up
_options.modules.transport.push(new WS()) // Test with transport instance
_options.modules.peerDiscovery.push(wrtcStar.discovery)
_options.modules.peerDiscovery.push(wsStar.discovery)

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

Expand Down
9 changes: 2 additions & 7 deletions test/utils/bundle-nodejs.js
Expand Up @@ -8,7 +8,7 @@ const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const defaultsDeep = require('lodash.defaultsdeep')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../..')

function mapMuxers (list) {
Expand Down Expand Up @@ -85,12 +85,7 @@ class Node extends libp2p {
}
}

defaultsDeep(_options, defaults)

// NOTE: defaultsDeep clones instances and screws things up
// _options.modules.transport.push(new TCP()) // Test with transport instance

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

Expand Down

0 comments on commit af3aeb2

Please sign in to comment.