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

Commit 15ab7a2

Browse files
richardschneiderdaviddias
authored andcommitted
fix: generate meaniful error when pubsub is called and not enabled
1 parent 1e9f260 commit 15ab7a2

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/core/components/no-floodsub.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const EventEmitter = require('events')
4+
5+
function fail() {
6+
throw new Error('The daemon must be run with \'--enable-pubsub-experiment\'')
7+
}
8+
9+
class NoFloodSub extends EventEmitter {
10+
constructor () {
11+
super()
12+
13+
this.peers = new Map()
14+
this.subscriptions = new Set()
15+
}
16+
17+
start (callback) { callback() }
18+
stop (callback) { callback() }
19+
publish () { fail() }
20+
subscribe () { fail() }
21+
unsubscribe () { fail() }
22+
23+
}
24+
25+
module.exports = NoFloodSub

src/core/components/start.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const series = require('async/series')
44
const Bitswap = require('ipfs-bitswap')
55
const FloodSub = require('libp2p-floodsub')
6+
const NoFloodSub = require('./no-floodsub')
67
const setImmediate = require('async/setImmediate')
78
const promisify = require('promisify-es6')
89

@@ -50,12 +51,10 @@ module.exports = (self) => {
5051
self._bitswap.start()
5152
self._blockService.setExchange(self._bitswap)
5253

53-
if (self._options.EXPERIMENTAL.pubsub) {
54-
self._pubsub = new FloodSub(self._libp2pNode)
55-
self._pubsub.start(done)
56-
} else {
57-
done()
58-
}
54+
self._pubsub = self._options.EXPERIMENTAL.pubsub
55+
? new FloodSub(self._libp2pNode)
56+
: new NoFloodSub()
57+
self._pubsub.start(done)
5958
})
6059
})
6160
}

src/core/components/stop.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ module.exports = (self) => {
3131
self._bitswap.stop()
3232

3333
series([
34-
(cb) => {
35-
if (self._options.EXPERIMENTAL.pubsub) {
36-
self._pubsub.stop(cb)
37-
} else {
38-
cb()
39-
}
40-
},
34+
(cb) => self._pubsub.stop(cb),
4135
(cb) => self.libp2p.stop(cb),
4236
(cb) => self._repo.close(cb)
4337
], done)

0 commit comments

Comments
 (0)