Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 289b12e

Browse files
jacobheunalanshaw
authored andcommitted
refactor: dont expose new getters, use options
refactor: generator to factory
1 parent 3417b6f commit 289b12e

File tree

5 files changed

+52
-53
lines changed

5 files changed

+52
-53
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ Modify the default IPFS node config. This object will be *merged* with the defau
301301
| Type | Default |
302302
|------|---------|
303303
| object | [`libp2p-nodejs.js`](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-nodejs.js) in Node.js, [`libp2p-browser.js`](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-browser.js) in browsers |
304-
| function | [`libp2p generator`](examples/custom-libp2p) |
304+
| function | [`libp2p factory`](examples/custom-libp2p) |
305305

306-
The libp2p option allows you to build your libp2p node by configuration, or via a generator. If you are looking to just modify the below options, using the object format is the quickest way to get the default features of libp2p. If you need to create a more customized libp2p node, such as with custom transports or peer/content routers that need some of the ipfs data on startup, a generator is a great way to achieve this.
306+
The libp2p option allows you to build your libp2p node by configuration, or via a factory. If you are looking to just modify the below options, using the object format is the quickest way to get the default features of libp2p. If you need to create a more customized libp2p node, such as with custom transports or peer/content routers that need some of the ipfs data on startup, a factory is a great way to achieve this.
307307

308-
You can see the generator in action in the [custom libp2p example](examples/custom-libp2p).
308+
You can see the factory in action in the [custom libp2p example](examples/custom-libp2p).
309309

310310
- `modules` (object):
311311
- `transport` (Array<[libp2p.Transport](https://github.com/libp2p/interface-transport)>): An array of Libp2p transport classes/instances to use _instead_ of the defaults. See [libp2p/interface-transport](https://github.com/libp2p/interface-transport) for details.

examples/custom-libp2p/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@ const MPLEX = require('libp2p-mplex')
1212
const SECIO = require('libp2p-secio')
1313
const assert = require('assert')
1414

15+
/**
16+
* Options for the libp2p factory
17+
* @typedef {Object} libp2pFactory~options
18+
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
19+
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
20+
* @property {Object} config - The config of the IPFS node
21+
* @property {Object} options - The options given to the IPFS node
22+
*/
23+
1524
/**
1625
* This is the factory we will use to create our fully customized libp2p node.
1726
*
18-
* @param {*} _ipfsNode The ipfs node. This houses the PeerInfo and PeerBook that modules may need
19-
* @param {*} _ipfsConfig The config that is fetched from the ipfs-repo
27+
* @param {libp2pFactory~options} opts The options to use when generating the libp2p node
2028
* @returns {Libp2p} Our new libp2p node
2129
*/
22-
const libp2pFactory = (_ipfsNode, _ipfsConfig) => {
30+
const libp2pFactory = (opts) => {
2331
// Set convenience variables to clearly showcase some of the useful things that are available
24-
const peerInfo = _ipfsNode.peerInfo
25-
const peerBook = _ipfsNode.peerBook
26-
const bootstrapList = _ipfsConfig.Bootstrap
32+
const peerInfo = opts.peerInfo
33+
const peerBook = opts.peerBook
34+
const bootstrapList = opts.config.Bootstrap
2735

2836
// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
2937
const wsstar = new WebSocketStar({

src/core/components/libp2p.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,42 @@ module.exports = function libp2p (self) {
1616
return callback(err)
1717
}
1818

19-
const defaultGenerator = (_ipfs, _config) => {
19+
const defaultFactory = (opts) => {
2020
const libp2pDefaults = {
21-
peerInfo: _ipfs._peerInfo,
22-
peerBook: _ipfs._peerInfoBook,
21+
peerInfo: opts.peerInfo,
22+
peerBook: opts.peerBook,
2323
config: {
2424
peerDiscovery: {
2525
mdns: {
26-
enabled: get(_ipfs._options, 'config.Discovery.MDNS.Enabled',
27-
get(_config, 'Discovery.MDNS.Enabled', true))
26+
enabled: get(opts.options, 'config.Discovery.MDNS.Enabled',
27+
get(opts.config, 'Discovery.MDNS.Enabled', true))
2828
},
2929
webRTCStar: {
30-
enabled: get(_ipfs._options, 'config.Discovery.webRTCStar.Enabled',
31-
get(_config, 'Discovery.webRTCStar.Enabled', true))
30+
enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled',
31+
get(opts.config, 'Discovery.webRTCStar.Enabled', true))
3232
},
3333
bootstrap: {
34-
list: get(_ipfs._options, 'config.Bootstrap',
35-
get(_config, 'Bootstrap', []))
34+
list: get(opts.options, 'config.Bootstrap',
35+
get(opts.config, 'Bootstrap', []))
3636
}
3737
},
3838
relay: {
39-
enabled: get(_ipfs._options, 'relay.enabled',
40-
get(_config, 'relay.enabled', false)),
39+
enabled: get(opts.options, 'relay.enabled',
40+
get(opts.config, 'relay.enabled', false)),
4141
hop: {
42-
enabled: get(_ipfs._options, 'relay.hop.enabled',
43-
get(_config, 'relay.hop.enabled', false)),
44-
active: get(_ipfs._options, 'relay.hop.active',
45-
get(_config, 'relay.hop.active', false))
42+
enabled: get(opts.options, 'relay.hop.enabled',
43+
get(opts.config, 'relay.hop.enabled', false)),
44+
active: get(opts.options, 'relay.hop.active',
45+
get(opts.config, 'relay.hop.active', false))
4646
}
4747
},
4848
EXPERIMENTAL: {
49-
dht: get(_ipfs._options, 'EXPERIMENTAL.dht', false),
50-
pubsub: get(_ipfs._options, 'EXPERIMENTAL.pubsub', false)
49+
dht: get(opts.options, 'EXPERIMENTAL.dht', false),
50+
pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
5151
}
5252
},
53-
connectionManager: get(_ipfs._options, 'connectionManager',
54-
get(_config, 'connectionManager', {}))
53+
connectionManager: get(opts.options, 'connectionManager',
54+
get(opts.config, 'connectionManager', {}))
5555
}
5656

5757
const libp2pOptions = defaultsDeep(
@@ -62,13 +62,18 @@ module.exports = function libp2p (self) {
6262
return new Node(libp2pOptions)
6363
}
6464

65-
// Always create libp2p via a generator
66-
let libp2pGenerator = get(self._options, 'libp2p', null)
67-
if (typeof libp2pGenerator !== 'function') {
68-
libp2pGenerator = defaultGenerator
65+
// Always create libp2p via a factory
66+
let libp2pFactory = get(self._options, 'libp2p', null)
67+
if (typeof libp2pFactory !== 'function') {
68+
libp2pFactory = defaultFactory
6969
}
7070

71-
self._libp2pNode = libp2pGenerator(self, config)
71+
self._libp2pNode = libp2pFactory({
72+
options: self._options,
73+
config: config,
74+
peerInfo: self._peerInfo,
75+
peerBook: self._peerInfoBook
76+
})
7277

7378
self._libp2pNode.on('peer:discovery', (peerInfo) => {
7479
const dial = () => {

src/core/index.js

-14
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,6 @@ class IPFS extends EventEmitter {
153153

154154
boot(this)
155155
}
156-
157-
/**
158-
* @type {PeerBook}
159-
*/
160-
get peerBook () {
161-
return this._peerInfoBook
162-
}
163-
164-
/**
165-
* @type {PeerInfo}
166-
*/
167-
get peerInfo () {
168-
return this._peerInfo
169-
}
170156
}
171157

172158
exports = module.exports = IPFS

test/core/libp2p.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ describe('libp2p customization', function () {
6565
})
6666
})
6767

68-
describe('generator', () => {
69-
it('should allow for using a libp2p generator', (done) => {
68+
describe('factory', () => {
69+
it('should allow for using a libp2p factory', (done) => {
7070
const ipfs = {
7171
_peerInfo: peerInfo,
7272
_peerBook: peerBook,
7373
config: mockConfig,
7474
_options: {
75-
libp2p: (_ipfs, _ipfsConfig) => {
76-
const wsstar = new WebSocketStar({id: _ipfs._peerInfo.id})
75+
libp2p: (opts) => {
76+
const wsstar = new WebSocketStar({id: opts.peerInfo.id})
7777

7878
return new Libp2p({
79-
peerInfo: _ipfs._peerInfo,
80-
peerBook: _ipfs._peerBook,
79+
peerInfo: opts.peerInfo,
80+
peerBook: opts.peerBook,
8181
modules: {
8282
transport: [
8383
wsstar

0 commit comments

Comments
 (0)