Skip to content

Commit 8372433

Browse files
committed
refactor: use different defaultsDeep and clean up some code
1 parent c07ca62 commit 8372433

File tree

9 files changed

+32
-55
lines changed

9 files changed

+32
-55
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const MPLEX = require('libp2p-mplex')
115115
const SECIO = require('libp2p-secio')
116116
const MulticastDNS = require('libp2p-mdns')
117117
const DHT = require('libp2p-kad-dht')
118-
const defaultsDeep = require('lodash.defaultsdeep')
118+
const defaultsDeep = require('@nodeutils/defaults-deep')
119119

120120
class Node extends libp2p {
121121
constructor (_peerInfo, _peerBook, _options) {
@@ -174,10 +174,8 @@ class Node extends libp2p {
174174
},
175175
}
176176

177-
// overload any defaults of your bundle using https://lodash.com/docs/4.17.5#defaultsDeep
178-
defaultsDeep(_options, defaults)
179-
180-
super(options)
177+
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
178+
super(defaultsDeep(_options, defaults))
181179
}
182180
}
183181

examples/transports/1.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const libp2p = require('libp2p')
44
const TCP = require('libp2p-tcp')
55
const PeerInfo = require('peer-info')
66
const waterfall = require('async/waterfall')
7-
const defaultsDeep = require('lodash.defaultsdeep')
7+
const defaultsDeep = require('@nodeutils/defaults-deep')
88

99
class MyBundle extends libp2p {
1010
constructor (_options) {
@@ -16,8 +16,7 @@ class MyBundle extends libp2p {
1616
}
1717
}
1818

19-
defaultsDeep(_options, defaults)
20-
super(_options)
19+
super(defaultsDeep(_options, defaults))
2120
}
2221
}
2322

examples/transports/2.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const libp2p = require('libp2p')
44
const TCP = require('libp2p-tcp')
55
const PeerInfo = require('peer-info')
66
const waterfall = require('async/waterfall')
7-
const defaultsDeep = require('lodash.defaultsdeep')
7+
const defaultsDeep = require('@nodeutils/defaults-deep')
88
const parallel = require('async/parallel')
99
const pull = require('pull-stream')
1010

@@ -18,8 +18,7 @@ class MyBundle extends libp2p {
1818
}
1919
}
2020

21-
defaultsDeep(_options, defaults)
22-
super(_options)
21+
super(defaultsDeep(_options, defaults))
2322
}
2423
}
2524

examples/transports/3.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const TCP = require('libp2p-tcp')
55
const WebSockets = require('libp2p-websockets')
66
const PeerInfo = require('peer-info')
77
const waterfall = require('async/waterfall')
8-
const defaultsDeep = require('lodash.defaultsdeep')
8+
const defaultsDeep = require('@nodeutils/defaults-deep')
99
const parallel = require('async/parallel')
1010
const pull = require('pull-stream')
1111

@@ -20,8 +20,7 @@ class MyBundle extends libp2p {
2020
}
2121
}
2222

23-
defaultsDeep(_options, defaults)
24-
super(_options)
23+
super(defaultsDeep(_options, defaults))
2524
}
2625
}
2726

examples/transports/README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ A more complete definition of what is a transport can be found on the [interface
1010

1111
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).
1212

13-
You will need 4 deps total, so go ahead and install all of them with:
13+
You will need 5 deps total, so go ahead and install all of them with:
1414

1515
```bash
16-
> npm install libp2p libp2p-tcp peer-info async
16+
> npm install libp2p libp2p-tcp peer-info async @nodeutils/defaults-deep
1717
```
1818

1919
Then, on your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
@@ -27,7 +27,7 @@ const libp2p = require('libp2p')
2727
const TCP = require('libp2p-tcp')
2828
const PeerInfo = require('peer-info')
2929
const waterfall = require('async/waterfall')
30-
const defaultsDeep = require('lodash.defaultsdeep')
30+
const defaultsDeep = require('@nodeutils/defaults-deep')
3131

3232
// This MyBundle class is your libp2p bundle packed with TCP
3333
class MyBundle extends libp2p {
@@ -42,8 +42,7 @@ class MyBundle extends libp2p {
4242
}
4343
}
4444

45-
defaultsDeep(_options, defaults)
46-
super(_options)
45+
super(defaultsDeep(_options, defaults))
4746
}
4847
}
4948
```
@@ -140,7 +139,7 @@ Now we are going to use `async/parallel` to create two nodes, print their addres
140139
const parallel = require('async/parallel')
141140
```
142141

143-
Then,
142+
Then,
144143

145144
```js
146145
parallel([
@@ -213,8 +212,7 @@ class MyBundle extends libp2p {
213212
}
214213
}
215214

216-
defaultsDeep(_options, defaults)
217-
super(_options)
215+
super(defaultsDeep(_options, defaults))
218216
}
219217
}
220218
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"peer-info": "~0.14.1"
5151
},
5252
"devDependencies": {
53+
"@nodeutils/defaults-deep": "^1.1.0",
5354
"aegir": "^14.0.0",
5455
"chai": "^4.1.2",
5556
"cids": "~0.5.3",
@@ -66,7 +67,6 @@
6667
"libp2p-webrtc-star": "~0.15.3",
6768
"libp2p-websocket-star": "~0.8.1",
6869
"libp2p-websocket-star-rendezvous": "~0.2.3",
69-
"lodash.defaultsdeep": "^4.6.0",
7070
"lodash.times": "^4.3.2",
7171
"pull-goodbye": "0.0.2",
7272
"pull-serializer": "~0.3.2",

src/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Node extends EventEmitter {
159159

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

174174
d.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
175175
this._discovery.push(d)
176-
d.start(cb)
176+
d.start(_cb)
177177
} else {
178-
cb()
178+
_cb()
179179
}
180180
}, cb)
181181
} else {
@@ -206,13 +206,11 @@ class Node extends EventEmitter {
206206
// detect which multiaddrs we don't have a transport for and remove them
207207
const multiaddrs = this.peerInfo.multiaddrs.toArray()
208208

209-
this._transport.forEach((transport) => {
210-
multiaddrs.forEach((multiaddr) => {
211-
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
212-
!this._transport.find((transport) => transport.filter(multiaddr).length > 0)) {
213-
this.peerInfo.multiaddrs.delete(multiaddr)
214-
}
215-
})
209+
multiaddrs.forEach((multiaddr) => {
210+
if (!multiaddr.toString().match(/\/p2p-circuit($|\/)/) &&
211+
!this._transport.find((transport) => transport.filter(multiaddr).length > 0)) {
212+
this.peerInfo.multiaddrs.delete(multiaddr)
213+
}
216214
})
217215
cb()
218216
},

test/utils/bundle-browser.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const SPDY = require('libp2p-spdy')
88
const MPLEX = require('libp2p-mplex')
99
const KadDHT = require('libp2p-kad-dht')
1010
const SECIO = require('libp2p-secio')
11-
const defaultsDeep = require('lodash.defaultsdeep')
11+
const defaultsDeep = require('@nodeutils/defaults-deep')
1212
const libp2p = require('../..')
1313

1414
function mapMuxers (list) {
@@ -43,18 +43,16 @@ class Node extends libp2p {
4343
modules: {
4444
transport: [
4545
wrtcStar,
46-
wsStar
46+
wsStar,
47+
new WS()
4748
],
4849
streamMuxer: getMuxers(_options.muxer),
4950
connEncryption: [
5051
SECIO
5152
],
5253
peerDiscovery: [
53-
// NOTE: defaultsDeep clones these references making the listeners be
54-
// attached to a clone and not the original. See the below how
55-
// to attach instances.
56-
// wrtcStar.discovery,
57-
// wsStar.discovery,
54+
wrtcStar.discovery,
55+
wsStar.discovery,
5856
Bootstrap
5957
],
6058
peerRouting: [],
@@ -89,14 +87,7 @@ class Node extends libp2p {
8987
}
9088
}
9189

92-
defaultsDeep(_options, defaults)
93-
94-
// NOTE: defaultsDeep clones instances and screws things up
95-
_options.modules.transport.push(new WS()) // Test with transport instance
96-
_options.modules.peerDiscovery.push(wrtcStar.discovery)
97-
_options.modules.peerDiscovery.push(wsStar.discovery)
98-
99-
super(_options)
90+
super(defaultsDeep(_options, defaults))
10091
}
10192
}
10293

test/utils/bundle-nodejs.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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('lodash.defaultsdeep')
11+
const defaultsDeep = require('@nodeutils/defaults-deep')
1212
const libp2p = require('../..')
1313

1414
function mapMuxers (list) {
@@ -85,12 +85,7 @@ class Node extends libp2p {
8585
}
8686
}
8787

88-
defaultsDeep(_options, defaults)
89-
90-
// NOTE: defaultsDeep clones instances and screws things up
91-
// _options.modules.transport.push(new TCP()) // Test with transport instance
92-
93-
super(_options)
88+
super(defaultsDeep(_options, defaults))
9489
}
9590
}
9691

0 commit comments

Comments
 (0)