Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 137eabb

Browse files
committed
Throw an error in pubsub if used in the browsers
1 parent 08ebb0b commit 137eabb

File tree

2 files changed

+207
-1
lines changed

2 files changed

+207
-1
lines changed

src/api/pubsub.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
const promisify = require('promisify-es6')
44
const EventEmitter = require('events')
55
const eos = require('end-of-stream')
6+
const isNode = require('detect-node')
67
const PubsubMessageStream = require('../pubsub-message-stream')
78
const stringlistToArray = require('../stringlist-to-array')
89

10+
const NotSupportedError = () => new Error('pubsub is currently not supported when run in the browser')
11+
912
/* Public API */
1013
module.exports = (send) => {
1114
/* Internal subscriptions state and functions */
@@ -28,6 +31,14 @@ module.exports = (send) => {
2831
options = defaultOptions
2932
}
3033

34+
// Throw an error if ran in the browsers
35+
if (!isNode) {
36+
if (!callback) {
37+
return Promise.reject(NotSupportedError())
38+
}
39+
return callback(NotSupportedError())
40+
}
41+
3142
// promisify doesn't work as we always pass a
3243
// function as last argument (`handler`)
3344
if (!callback) {
@@ -43,7 +54,11 @@ module.exports = (send) => {
4354

4455
subscribe(topic, options, handler, callback)
4556
},
46-
unsubscribe (topic, handler) {
57+
unsubscribe: (topic, handler) => {
58+
if (!isNode) {
59+
throw NotSupportedError()
60+
}
61+
4762
if (ps.listenerCount(topic) === 0 || !subscriptions[topic]) {
4863
throw new Error(`Not subscribed to '${topic}'`)
4964
}
@@ -57,6 +72,10 @@ module.exports = (send) => {
5772
}
5873
},
5974
publish: promisify((topic, data, callback) => {
75+
if (!isNode) {
76+
return callback(NotSupportedError())
77+
}
78+
6079
if (!Buffer.isBuffer(data)) {
6180
return callback(new Error('data must be a Buffer'))
6281
}
@@ -69,13 +88,21 @@ module.exports = (send) => {
6988
send(request, callback)
7089
}),
7190
ls: promisify((callback) => {
91+
if (!isNode) {
92+
return callback(NotSupportedError())
93+
}
94+
7295
const request = {
7396
path: 'pubsub/ls'
7497
}
7598

7699
send.andTransform(request, stringlistToArray, callback)
77100
}),
78101
peers: promisify((topic, callback) => {
102+
if (!isNode) {
103+
return callback(NotSupportedError())
104+
}
105+
79106
const request = {
80107
path: 'pubsub/peers',
81108
args: [topic]

test/pubsub-in-browser.spec.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
We currently don't support pubsub when run in the browser,
3+
and we test it with separate set of tests to make sure
4+
if it's being used in the browser, pubsub errors.
5+
6+
More info: https://github.com/ipfs/js-ipfs-api/issues/518
7+
8+
This means:
9+
- You can use pubsub from js-ipfs-api in Node.js
10+
- You can use pubsub from js-ipfs-api in Electron
11+
(when js-ipfs-api is ran in the main process of Electron)
12+
13+
- You can't use pubsub from js-ipfs-api in the browser
14+
- You can't use pubsub from js-ipfs-api in Electron's
15+
renderer process
16+
17+
- You can use pubsub from js-ipfs in the browsers
18+
- You can use pubsub from js-ipfs in Node.js
19+
- You can use pubsub from js-ipfs in Electron
20+
(in both the main process and the renderer process)
21+
- See https://github.com/ipfs/js-ipfs for details on
22+
pubsub in js-ipfs
23+
*/
24+
25+
/* eslint-env mocha */
26+
/* eslint max-nested-callbacks: ['error', 8] */
27+
'use strict'
28+
29+
const expect = require('chai').expect
30+
const series = require('async/series')
31+
const waterfall = require('async/waterfall')
32+
const isNode = require('detect-node')
33+
const FactoryClient = require('./ipfs-factory/client')
34+
35+
const expectedError = 'pubsub is currently not supported when run in the browser'
36+
37+
function spawnWithId (factory, callback) {
38+
waterfall([
39+
(cb) => factory.spawnNode(cb),
40+
(node, cb) => node.id((err, res) => {
41+
if (err) {
42+
return cb(err)
43+
}
44+
node.peerId = res
45+
cb(null, node)
46+
})
47+
], callback)
48+
}
49+
50+
if (!isNode) {
51+
describe('.pubsub-browser (pubsub not supported in the browsers currently)', () => {
52+
const topic = 'pubsub-tests'
53+
54+
let factory
55+
let ipfs1
56+
57+
before((done) => {
58+
factory = new FactoryClient()
59+
60+
series([
61+
(cb) => spawnWithId(factory, cb)
62+
], (err, nodes) => {
63+
if (err) {
64+
return done(err)
65+
}
66+
67+
ipfs1 = nodes[0]
68+
done()
69+
})
70+
})
71+
72+
after((done) => {
73+
factory.dismantle(done)
74+
})
75+
76+
describe('everything errors', () => {
77+
describe('Callback API', () => {
78+
describe('.publish', () => {
79+
it('throws an error', (done) => {
80+
ipfs1.pubsub.publish(topic, 'hello friend', (err, topics) => {
81+
expect(err).to.exist
82+
expect(err.message).to.equal(expectedError)
83+
done()
84+
})
85+
})
86+
})
87+
88+
describe('.subscribe', () => {
89+
const handler = () => {}
90+
it('throws an error', (done) => {
91+
ipfs1.pubsub.subscribe(topic, {}, handler, (err, topics) => {
92+
expect(err).to.exist
93+
expect(err.message).to.equal(expectedError)
94+
done()
95+
})
96+
})
97+
})
98+
99+
describe('.peers', () => {
100+
it('throws an error', (done) => {
101+
ipfs1.pubsub.peers(topic, (err, topics) => {
102+
expect(err).to.exist
103+
expect(err.message).to.equal(expectedError)
104+
done()
105+
})
106+
})
107+
})
108+
109+
describe('.ls', () => {
110+
it('throws an error', (done) => {
111+
ipfs1.pubsub.ls((err, topics) => {
112+
expect(err).to.exist
113+
expect(err.message).to.equal(expectedError)
114+
done()
115+
})
116+
})
117+
})
118+
})
119+
120+
describe('Promise API', () => {
121+
describe('.publish', () => {
122+
it('throws an error', () => {
123+
return ipfs1.pubsub.publish(topic, 'hello friend')
124+
.catch((e) => {
125+
expect(e).to.exist
126+
expect(e.message).to.equal(expectedError)
127+
})
128+
})
129+
})
130+
131+
describe('.subscribe', () => {
132+
const handler = () => {}
133+
it('throws an error', (done) => {
134+
ipfs1.pubsub.subscribe(topic, {}, handler)
135+
.catch((e) => {
136+
expect(e).to.exist
137+
expect(e.message).to.equal(expectedError)
138+
done()
139+
})
140+
})
141+
})
142+
143+
describe('.peers', () => {
144+
it('throws an error', (done) => {
145+
ipfs1.pubsub.peers(topic)
146+
.catch((e) => {
147+
expect(e).to.exist
148+
expect(e.message).to.equal(expectedError)
149+
done()
150+
})
151+
})
152+
})
153+
154+
describe('.ls', () => {
155+
it('throws an error', () => {
156+
return ipfs1.pubsub.ls()
157+
.catch((e) => {
158+
expect(e).to.exist
159+
expect(e.message).to.equal(expectedError)
160+
})
161+
})
162+
})
163+
})
164+
165+
describe('.unsubscribe', () => {
166+
it('throws an error', (done) => {
167+
try {
168+
ipfs1.pubsub.unsubscribe(topic)
169+
done('unsubscribe() didn\'t throw an error')
170+
} catch (e) {
171+
expect(e).to.exist
172+
expect(e.message).to.equal(expectedError)
173+
done()
174+
}
175+
})
176+
})
177+
})
178+
})
179+
}

0 commit comments

Comments
 (0)