Skip to content

Commit d16b7eb

Browse files
jacobheundaviddias
authored andcommitted
refactor: use different defaultsDeep and clean up some code
1 parent d59ef62 commit d16b7eb

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
@@ -113,7 +113,7 @@ const MPLEX = require('libp2p-mplex')
113113
const SECIO = require('libp2p-secio')
114114
const MulticastDNS = require('libp2p-mdns')
115115
const DHT = require('libp2p-kad-dht')
116-
const defaultsDeep = require('lodash.defaultsdeep')
116+
const defaultsDeep = require('@nodeutils/defaults-deep')
117117

118118
class Node extends libp2p {
119119
constructor (_peerInfo, _peerBook, _options) {
@@ -172,10 +172,8 @@ class Node extends libp2p {
172172
},
173173
}
174174

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

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
@@ -49,6 +49,7 @@
4949
"peer-info": "~0.14.1"
5050
},
5151
"devDependencies": {
52+
"@nodeutils/defaults-deep": "^1.1.0",
5253
"aegir": "^14.0.0",
5354
"chai": "^4.1.2",
5455
"cids": "~0.5.3",
@@ -65,7 +66,6 @@
6566
"libp2p-webrtc-star": "~0.15.3",
6667
"libp2p-websocket-star": "~0.8.1",
6768
"libp2p-websocket-star-rendezvous": "~0.2.3",
68-
"lodash.defaultsdeep": "^4.6.0",
6969
"lodash.times": "^4.3.2",
7070
"pull-goodbye": "0.0.2",
7171
"pull-serializer": "~0.3.2",

src/index.js

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

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

172172
d.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
173173
this._discovery.push(d)
174-
d.start(cb)
174+
d.start(_cb)
175175
} else {
176-
cb()
176+
_cb()
177177
}
178178
}, cb)
179179
} else {
@@ -204,13 +204,11 @@ class Node extends EventEmitter {
204204
// detect which multiaddrs we don't have a transport for and remove them
205205
const multiaddrs = this.peerInfo.multiaddrs.toArray()
206206

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

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)