From 289b12ed57a17b1956924abdd879292d3a61fa1b Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Thu, 9 Aug 2018 19:49:39 +0200 Subject: [PATCH] refactor: dont expose new getters, use options refactor: generator to factory --- README.md | 6 ++-- examples/custom-libp2p/index.js | 20 +++++++++---- src/core/components/libp2p.js | 53 ++++++++++++++++++--------------- src/core/index.js | 14 --------- test/core/libp2p.spec.js | 12 ++++---- 5 files changed, 52 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index c4d62b7080..af5ae24b88 100644 --- a/README.md +++ b/README.md @@ -301,11 +301,11 @@ Modify the default IPFS node config. This object will be *merged* with the defau | Type | Default | |------|---------| | 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 | -| function | [`libp2p generator`](examples/custom-libp2p) | +| function | [`libp2p factory`](examples/custom-libp2p) | -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. +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. -You can see the generator in action in the [custom libp2p example](examples/custom-libp2p). +You can see the factory in action in the [custom libp2p example](examples/custom-libp2p). - `modules` (object): - `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. diff --git a/examples/custom-libp2p/index.js b/examples/custom-libp2p/index.js index 4bd8330d5b..f777e3b093 100644 --- a/examples/custom-libp2p/index.js +++ b/examples/custom-libp2p/index.js @@ -12,18 +12,26 @@ const MPLEX = require('libp2p-mplex') const SECIO = require('libp2p-secio') const assert = require('assert') +/** + * Options for the libp2p factory + * @typedef {Object} libp2pFactory~options + * @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node + * @property {PeerBook} peerBook - The PeerBook of the IPFS node + * @property {Object} config - The config of the IPFS node + * @property {Object} options - The options given to the IPFS node + */ + /** * This is the factory we will use to create our fully customized libp2p node. * - * @param {*} _ipfsNode The ipfs node. This houses the PeerInfo and PeerBook that modules may need - * @param {*} _ipfsConfig The config that is fetched from the ipfs-repo + * @param {libp2pFactory~options} opts The options to use when generating the libp2p node * @returns {Libp2p} Our new libp2p node */ -const libp2pFactory = (_ipfsNode, _ipfsConfig) => { +const libp2pFactory = (opts) => { // Set convenience variables to clearly showcase some of the useful things that are available - const peerInfo = _ipfsNode.peerInfo - const peerBook = _ipfsNode.peerBook - const bootstrapList = _ipfsConfig.Bootstrap + const peerInfo = opts.peerInfo + const peerBook = opts.peerBook + const bootstrapList = opts.config.Bootstrap // Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node const wsstar = new WebSocketStar({ diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 6080da25d3..29e5271358 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -16,42 +16,42 @@ module.exports = function libp2p (self) { return callback(err) } - const defaultGenerator = (_ipfs, _config) => { + const defaultFactory = (opts) => { const libp2pDefaults = { - peerInfo: _ipfs._peerInfo, - peerBook: _ipfs._peerInfoBook, + peerInfo: opts.peerInfo, + peerBook: opts.peerBook, config: { peerDiscovery: { mdns: { - enabled: get(_ipfs._options, 'config.Discovery.MDNS.Enabled', - get(_config, 'Discovery.MDNS.Enabled', true)) + enabled: get(opts.options, 'config.Discovery.MDNS.Enabled', + get(opts.config, 'Discovery.MDNS.Enabled', true)) }, webRTCStar: { - enabled: get(_ipfs._options, 'config.Discovery.webRTCStar.Enabled', - get(_config, 'Discovery.webRTCStar.Enabled', true)) + enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled', + get(opts.config, 'Discovery.webRTCStar.Enabled', true)) }, bootstrap: { - list: get(_ipfs._options, 'config.Bootstrap', - get(_config, 'Bootstrap', [])) + list: get(opts.options, 'config.Bootstrap', + get(opts.config, 'Bootstrap', [])) } }, relay: { - enabled: get(_ipfs._options, 'relay.enabled', - get(_config, 'relay.enabled', false)), + enabled: get(opts.options, 'relay.enabled', + get(opts.config, 'relay.enabled', false)), hop: { - enabled: get(_ipfs._options, 'relay.hop.enabled', - get(_config, 'relay.hop.enabled', false)), - active: get(_ipfs._options, 'relay.hop.active', - get(_config, 'relay.hop.active', false)) + enabled: get(opts.options, 'relay.hop.enabled', + get(opts.config, 'relay.hop.enabled', false)), + active: get(opts.options, 'relay.hop.active', + get(opts.config, 'relay.hop.active', false)) } }, EXPERIMENTAL: { - dht: get(_ipfs._options, 'EXPERIMENTAL.dht', false), - pubsub: get(_ipfs._options, 'EXPERIMENTAL.pubsub', false) + dht: get(opts.options, 'EXPERIMENTAL.dht', false), + pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false) } }, - connectionManager: get(_ipfs._options, 'connectionManager', - get(_config, 'connectionManager', {})) + connectionManager: get(opts.options, 'connectionManager', + get(opts.config, 'connectionManager', {})) } const libp2pOptions = defaultsDeep( @@ -62,13 +62,18 @@ module.exports = function libp2p (self) { return new Node(libp2pOptions) } - // Always create libp2p via a generator - let libp2pGenerator = get(self._options, 'libp2p', null) - if (typeof libp2pGenerator !== 'function') { - libp2pGenerator = defaultGenerator + // Always create libp2p via a factory + let libp2pFactory = get(self._options, 'libp2p', null) + if (typeof libp2pFactory !== 'function') { + libp2pFactory = defaultFactory } - self._libp2pNode = libp2pGenerator(self, config) + self._libp2pNode = libp2pFactory({ + options: self._options, + config: config, + peerInfo: self._peerInfo, + peerBook: self._peerInfoBook + }) self._libp2pNode.on('peer:discovery', (peerInfo) => { const dial = () => { diff --git a/src/core/index.js b/src/core/index.js index 89f90f9589..52266b7b41 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -153,20 +153,6 @@ class IPFS extends EventEmitter { boot(this) } - - /** - * @type {PeerBook} - */ - get peerBook () { - return this._peerInfoBook - } - - /** - * @type {PeerInfo} - */ - get peerInfo () { - return this._peerInfo - } } exports = module.exports = IPFS diff --git a/test/core/libp2p.spec.js b/test/core/libp2p.spec.js index 0b503c7387..9dfbc82f7c 100644 --- a/test/core/libp2p.spec.js +++ b/test/core/libp2p.spec.js @@ -65,19 +65,19 @@ describe('libp2p customization', function () { }) }) - describe('generator', () => { - it('should allow for using a libp2p generator', (done) => { + describe('factory', () => { + it('should allow for using a libp2p factory', (done) => { const ipfs = { _peerInfo: peerInfo, _peerBook: peerBook, config: mockConfig, _options: { - libp2p: (_ipfs, _ipfsConfig) => { - const wsstar = new WebSocketStar({id: _ipfs._peerInfo.id}) + libp2p: (opts) => { + const wsstar = new WebSocketStar({id: opts.peerInfo.id}) return new Libp2p({ - peerInfo: _ipfs._peerInfo, - peerBook: _ipfs._peerBook, + peerInfo: opts.peerInfo, + peerBook: opts.peerBook, modules: { transport: [ wsstar