Skip to content

refactor: use different defaultsDeep and clean up some code #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great find!


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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -65,7 +66,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
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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 @@ -171,9 +171,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 @@ -204,13 +204,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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