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

Commit 5237dd9

Browse files
author
Alan Shaw
authored
fix: better error message when pubsub is not enabled (#1729)
If pubsub is disabled there's no `pubsub` property on the libp2p node and we get the error "Cannot read property 'subscribe' of undefined". This is really confusing. This PR detects disabled pubsub and throws an error with a better message. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 008a14d commit 5237dd9

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

src/core/components/pubsub.js

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
const promisify = require('promisify-es6')
44
const setImmediate = require('async/setImmediate')
5+
const errCode = require('err-code')
6+
7+
const errPubsubDisabled = () => {
8+
return errCode(new Error('pubsub experiment is not enabled'), 'ERR_PUBSUB_DISABLED')
9+
}
510

611
module.exports = function pubsub (self) {
712
return {
@@ -11,6 +16,12 @@ module.exports = function pubsub (self) {
1116
options = {}
1217
}
1318

19+
if (!self._options.EXPERIMENTAL.pubsub) {
20+
return callback
21+
? setImmediate(() => callback(errPubsubDisabled()))
22+
: Promise.reject(errPubsubDisabled())
23+
}
24+
1425
if (!callback) {
1526
return new Promise((resolve, reject) => {
1627
self._libp2pNode.pubsub.subscribe(topic, options, handler, (err) => {
@@ -26,6 +37,12 @@ module.exports = function pubsub (self) {
2637
},
2738

2839
unsubscribe: (topic, handler, callback) => {
40+
if (!self._options.EXPERIMENTAL.pubsub) {
41+
return callback
42+
? setImmediate(() => callback(errPubsubDisabled()))
43+
: Promise.reject(errPubsubDisabled())
44+
}
45+
2946
self._libp2pNode.pubsub.unsubscribe(topic, handler)
3047

3148
if (!callback) {
@@ -36,18 +53,30 @@ module.exports = function pubsub (self) {
3653
},
3754

3855
publish: promisify((topic, data, callback) => {
56+
if (!self._options.EXPERIMENTAL.pubsub) {
57+
return setImmediate(() => callback(errPubsubDisabled()))
58+
}
3959
self._libp2pNode.pubsub.publish(topic, data, callback)
4060
}),
4161

4262
ls: promisify((callback) => {
63+
if (!self._options.EXPERIMENTAL.pubsub) {
64+
return setImmediate(() => callback(errPubsubDisabled()))
65+
}
4366
self._libp2pNode.pubsub.ls(callback)
4467
}),
4568

4669
peers: promisify((topic, callback) => {
70+
if (!self._options.EXPERIMENTAL.pubsub) {
71+
return setImmediate(() => callback(errPubsubDisabled()))
72+
}
4773
self._libp2pNode.pubsub.peers(topic, callback)
4874
}),
4975

5076
setMaxListeners (n) {
77+
if (!self._options.EXPERIMENTAL.pubsub) {
78+
throw errPubsubDisabled()
79+
}
5180
self._libp2pNode.pubsub.setMaxListeners(n)
5281
}
5382
}

test/core/pubsub.spec.js

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const hat = require('hat')
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
11+
const IPFS = require('../../src')
12+
const createTempRepo = require('../utils/create-repo-nodejs')
13+
14+
describe('pubsub disabled', () => {
15+
let ipfs
16+
let repo
17+
18+
before(function (done) {
19+
this.timeout(20 * 1000)
20+
21+
repo = createTempRepo()
22+
ipfs = new IPFS({
23+
repo,
24+
config: {
25+
Addresses: {
26+
Swarm: []
27+
}
28+
},
29+
preload: {
30+
enabled: false
31+
},
32+
EXPERIMENTAL: {
33+
pubsub: false
34+
}
35+
})
36+
37+
ipfs.on('ready', done)
38+
})
39+
40+
after((done) => ipfs.stop(done))
41+
42+
after((done) => repo.teardown(done))
43+
44+
it('should not allow subscribe if disabled', done => {
45+
const topic = hat()
46+
const handler = () => done(new Error('unexpected message'))
47+
ipfs.pubsub.subscribe(topic, handler, (err) => {
48+
expect(err).to.exist()
49+
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
50+
done()
51+
})
52+
})
53+
54+
it('should not allow subscribe if disabled (promised)', async () => {
55+
try {
56+
const topic = hat()
57+
const handler = () => { throw new Error('unexpected message') }
58+
await ipfs.pubsub.subscribe(topic, handler)
59+
} catch (err) {
60+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
61+
}
62+
throw new Error('expected error to be thrown')
63+
})
64+
65+
it('should not allow unsubscribe if disabled', done => {
66+
const topic = hat()
67+
const handler = () => done(new Error('unexpected message'))
68+
ipfs.pubsub.unsubscribe(topic, handler, (err) => {
69+
expect(err).to.exist()
70+
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
71+
done()
72+
})
73+
})
74+
75+
it('should not allow unsubscribe if disabled (promised)', async () => {
76+
try {
77+
const topic = hat()
78+
const handler = () => { throw new Error('unexpected message') }
79+
await ipfs.pubsub.unsubscribe(topic, handler)
80+
} catch (err) {
81+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
82+
}
83+
throw new Error('expected error to be thrown')
84+
})
85+
86+
it('should not allow publish if disabled', done => {
87+
const topic = hat()
88+
const msg = Buffer.from(hat())
89+
ipfs.pubsub.publish(topic, msg, (err) => {
90+
expect(err).to.exist()
91+
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
92+
done()
93+
})
94+
})
95+
96+
it('should not allow publish if disabled (promised)', async () => {
97+
try {
98+
const topic = hat()
99+
const msg = Buffer.from(hat())
100+
await ipfs.pubsub.publish(topic, msg)
101+
} catch (err) {
102+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
103+
}
104+
throw new Error('expected error to be thrown')
105+
})
106+
107+
it('should not allow ls if disabled', done => {
108+
ipfs.pubsub.ls((err) => {
109+
expect(err).to.exist()
110+
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
111+
done()
112+
})
113+
})
114+
115+
it('should not allow ls if disabled (promised)', async () => {
116+
try {
117+
await ipfs.pubsub.ls()
118+
} catch (err) {
119+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
120+
}
121+
throw new Error('expected error to be thrown')
122+
})
123+
124+
it('should not allow peers if disabled', done => {
125+
const topic = hat()
126+
ipfs.pubsub.peers(topic, (err) => {
127+
expect(err).to.exist()
128+
expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
129+
done()
130+
})
131+
})
132+
133+
it('should not allow peers if disabled (promised)', async () => {
134+
try {
135+
const topic = hat()
136+
await ipfs.pubsub.peers(topic)
137+
} catch (err) {
138+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
139+
}
140+
throw new Error('expected error to be thrown')
141+
})
142+
143+
it('should not allow setMaxListeners if disabled', async () => {
144+
try {
145+
await ipfs.pubsub.setMaxListeners(100)
146+
} catch (err) {
147+
return expect(err.code).to.equal('ERR_PUBSUB_DISABLED')
148+
}
149+
throw new Error('expected error to be thrown')
150+
})
151+
})

0 commit comments

Comments
 (0)