From 71883e3feda70f8691c98eeca033e99ac3ccd05a Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 28 Jun 2018 16:47:27 +0100 Subject: [PATCH] feat: pass libp2p peer discovery config BREAKING CHANGE: the configuration key for adding your own libp2p peer discovery modules has changed in the config passed to the IPFS constructor. Previously this key was `libp2p.modules.discovery` and has changed to `libp2p.modules.peerDiscovery`. License: MIT Signed-off-by: Alan Shaw --- README.md | 9 +++++++-- src/core/components/libp2p.js | 9 +++++++-- src/core/index.js | 1 - 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index af89084433..4b028c7b89 100644 --- a/README.md +++ b/README.md @@ -238,8 +238,13 @@ Creates and returns an instance of an IPFS node. Use the `options` argument to s - `libp2p` (object) add custom modules to the libp2p stack of your node - `modules` (object): - - `transport` (Array<[libp2p.Transport](https://github.com/libp2p/interface-transport)>): An array of additional Libp2p transport instances to use. See [libp2p/interface-transport](https://github.com/libp2p/interface-transport) for details. - - `discovery` (Array<[libp2p.PeerDiscovery](https://github.com/libp2p/interface-peer-discovery)>): An array of additional Libp2p peer discovery instances to use. See [libp2p/peer-discovery](https://github.com/libp2p/interface-peer-discovery) for details. + - `transport` (Array<[libp2p.Transport](https://github.com/libp2p/interface-transport)>): An array of additional Libp2p transport classes/instances to use. See [libp2p/interface-transport](https://github.com/libp2p/interface-transport) for details. + - `peerDiscovery` (Array<[libp2p.PeerDiscovery](https://github.com/libp2p/interface-peer-discovery)>): An array of additional Libp2p peer discovery classes/instances to use. See [libp2p/peer-discovery](https://github.com/libp2p/interface-peer-discovery) for details. If passing a class, configuration can be passed using the config section below under the key corresponding to you module's unique `tag` (a static property on the class) + - `config` (object): + - `peerDiscovery` (object): + - `[PeerDiscovery.tag]` (object): configuration for a peer discovery module + - `enabled` (boolean): whether this module is enabled or disabled + - `[custom config]` (any): other keys are specific to the module #### Events diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 52030a4a05..7bc16fc819 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -4,6 +4,7 @@ const Node = require('../runtime/libp2p-nodejs') const promisify = require('promisify-es6') const get = require('lodash.get') +const defaultsDeep = require('lodash.defaultsdeep') module.exports = function libp2p (self) { return { @@ -15,10 +16,9 @@ module.exports = function libp2p (self) { return callback(err) } - const libp2pOptions = { + const libp2pDefaults = { peerInfo: self._peerInfo, peerBook: self._peerInfoBook, - modules: self._libp2pModules, config: { peerDiscovery: { mdns: { @@ -51,6 +51,11 @@ module.exports = function libp2p (self) { } } + const libp2pOptions = defaultsDeep( + get(self._options, 'libp2p', {}), + libp2pDefaults + ) + self._libp2pNode = new Node(libp2pOptions) self._libp2pNode.on('peer:discovery', (peerInfo) => { diff --git a/src/core/index.js b/src/core/index.js index 57efa58385..77f23b5456 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -34,7 +34,6 @@ class IPFS extends EventEmitter { } options = config.validate(options || {}) - this._libp2pModules = options.libp2p && options.libp2p.modules extend(this._options, options)