From aa2eb36851ccd3648217de34811df3c32f638a2e Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 28 Aug 2019 14:25:46 +0200 Subject: [PATCH] chore: address review --- .aegir.js | 2 +- README.md | 9 ++++++++- doc/config.md | 2 +- package.json | 3 ++- src/core/components/libp2p.js | 15 +++++++++++++-- src/core/runtime/config-browser.js | 3 --- src/core/runtime/libp2p-pubsub-routers-browser.js | 5 +++++ src/core/runtime/libp2p-pubsub-routers-nodejs.js | 6 ++++++ 8 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 src/core/runtime/libp2p-pubsub-routers-browser.js create mode 100644 src/core/runtime/libp2p-pubsub-routers-nodejs.js diff --git a/.aegir.js b/.aegir.js index 2e535ada4b..6d0ed4b2dd 100644 --- a/.aegir.js +++ b/.aegir.js @@ -8,7 +8,7 @@ const ipfsdServer = IPFSFactory.createServer() const preloadNode = MockPreloadNode.createNode() module.exports = { - bundlesize: { maxSize: '694kB' }, + bundlesize: { maxSize: '689kB' }, webpack: { resolve: { mainFields: ['browser', 'main'], diff --git a/README.md b/README.md index 706eff4073..6f5ae82034 100644 --- a/README.md +++ b/README.md @@ -324,7 +324,7 @@ Configure remote preload nodes. The remote will preload content added on this no | Type | Default | |------|---------| -| object | `{ pubsub: false, ipnsPubsub: false, sharding: false }` | +| object | `{ ipnsPubsub: false, sharding: false }` | Enable and configure experimental features. @@ -495,6 +495,8 @@ You can see the bundle in action in the [custom libp2p example](examples/custom- - `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. - `peerDiscovery` (Array<[libp2p.PeerDiscovery](https://github.com/libp2p/interface-peer-discovery)>): An array of Libp2p peer discovery classes/instances to use _instead_ of the defaults. 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) + - `dht` (object): a DHT implementation that enables PeerRouting and ContentRouting. Example [libp2p/js-libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht) + - `pubsub` (object): a Pubsub implementation on top of [libp2p/js-libp2p-pubsub](https://github.com/libp2p/js-libp2p-pubsub) - `config` (object): - `peerDiscovery` (object): - `autoDial` (boolean): Dial to discovered peers when under the Connection Manager min peer count watermark. (default `true`) @@ -506,6 +508,11 @@ You can see the bundle in action in the [custom libp2p example](examples/custom- - `kBucketSize` (number): bucket size (default `20`) - `randomWalk` (object): configuration for random walk - `enabled` (boolean): whether random DHT walking is enabled (default `false`) + - `pubsub` (object): Configuration options for Pubsub + - `enabled` (boolean): if pubbsub subsystem should be enabled (default: `true`) + - `emitSelf` (boolean): whether the node should emit to self on publish, in the event of the topic being subscribed (default: `true`) + - `signMessages` (boolean): if messages should be signed (default: `true`) + - `strictSigning` (boolean): if message signing should be required (default: `true`) ##### `options.connectionManager` diff --git a/doc/config.md b/doc/config.md index fa331c4d77..08222f4a10 100644 --- a/doc/config.md +++ b/doc/config.md @@ -172,7 +172,7 @@ You can check the [parameter choice for pbkdf2](https://cryptosense.com/paramete ## `Pubsub` -Options for configuring the pubsub subsystem. +Options for configuring the pubsub subsystem. It is important pointing out that this is not supported in the browser. If you want to configure a different pubsub router in the browser you must configure `libp2p.modules.pubsub` options instead. ### `Router` diff --git a/package.json b/package.json index debf3f7ffe..aa4f1d7755 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js", "./src/core/runtime/fetch-nodejs.js": "./src/core/runtime/fetch-browser.js", "./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js", + "./src/core/runtime/libp2p-pubsub-routers-nodejs.js": "./src/core/runtime/libp2p-pubsub-routers-browser.js", "./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js", "./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js", "./src/core/runtime/ipld-nodejs.js": "./src/core/runtime/ipld-browser.js", @@ -193,7 +194,7 @@ "execa": "^2.0.4", "form-data": "^2.5.0", "hat": "0.0.3", - "interface-ipfs-core": "^0.110.0", + "interface-ipfs-core": "^0.111.0", "ipfsd-ctl": "^0.43.0", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 4a5f7fd31f..ef261f8c1c 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -2,10 +2,12 @@ const get = require('dlv') const mergeOptions = require('merge-options') +const errCode = require('err-code') const ipnsUtils = require('../ipns/routing/utils') const multiaddr = require('multiaddr') const DelegatedPeerRouter = require('libp2p-delegated-peer-routing') const DelegatedContentRouter = require('libp2p-delegated-content-routing') +const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs') module.exports = function libp2p (self, config) { const options = self._options || {} @@ -58,6 +60,16 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { peerRouting = [new DelegatedPeerRouter(delegatedApiOptions)] } + const getPubsubRouter = () => { + const router = get(config, 'Pubsub.Router', 'gossipsub') + + if (!PubsubRouters[router]) { + throw errCode(new Error(`Router unavailable. Configure libp2p.modules.pubsub to use the ${router} router.`), 'ERR_NOT_SUPPORTED') + } + + return PubsubRouters[router] + } + const libp2pDefaults = { datastore, peerInfo, @@ -65,8 +77,7 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { modules: { contentRouting, peerRouting, - pubsub: get(config, 'Pubsub.Router', 'gossipsub') === 'floodsub' - ? require('libp2p-floodsub') : require('libp2p-gossipsub') + pubsub: getPubsubRouter() }, config: { peerDiscovery: { diff --git a/src/core/runtime/config-browser.js b/src/core/runtime/config-browser.js index 13c803b011..537316d431 100644 --- a/src/core/runtime/config-browser.js +++ b/src/core/runtime/config-browser.js @@ -27,9 +27,6 @@ module.exports = () => ({ '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6' ], - Pubsub: { - Router: 'gossipsub' - }, Swarm: { ConnMgr: { LowWater: 200, diff --git a/src/core/runtime/libp2p-pubsub-routers-browser.js b/src/core/runtime/libp2p-pubsub-routers-browser.js new file mode 100644 index 0000000000..5b6aaedc0a --- /dev/null +++ b/src/core/runtime/libp2p-pubsub-routers-browser.js @@ -0,0 +1,5 @@ +'use strict' + +module.exports = { + gossipsub: require('libp2p-gossipsub') +} diff --git a/src/core/runtime/libp2p-pubsub-routers-nodejs.js b/src/core/runtime/libp2p-pubsub-routers-nodejs.js new file mode 100644 index 0000000000..0af111e881 --- /dev/null +++ b/src/core/runtime/libp2p-pubsub-routers-nodejs.js @@ -0,0 +1,6 @@ +'use strict' + +module.exports = { + gossipsub: require('libp2p-gossipsub'), + floodsub: require('libp2p-floodsub') +}