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

Commit 99f0f87

Browse files
richardschneiderdaviddias
authored andcommitted
feat: PubSub Interop Tests and CLI+HTTP-API Implementation (#1081)
* test: enable pubsub tests * fix: generate meaniful error when pubsub is called and not enabled * test: enable pubsub for factory daemon * test: enable pubsub tests * fix: generate meaniful error when pubsub is called and not enabled * test: enable pubsub for factory daemon * fiix(pubsub-subscribe): stop HAPI gzip from buffering our streamed response * test: fix spec/pubsub * fix: lint errors * test: tests js/go pubsub interop * test: pubsub interop tests * test: enable pubsub tests * fix: generate meaniful error when pubsub is called and not enabled * test: enable pubsub for factory daemon * fiix(pubsub-subscribe): stop HAPI gzip from buffering our streamed response * test: fix spec/pubsub * fix: lint errors * test: tests js/go pubsub interop * test: pubsub interop tests * test: more tests with different data types Note that binary data from JS to GO fails * HTTP API server: parsing query string as binary in pubsub publish * HTTP API: pubsub: publish should fail gracefully when no argument is given * chore: update deps * chore: update deps * last pass
1 parent 71faa92 commit 99f0f87

File tree

17 files changed

+352
-76
lines changed

17 files changed

+352
-76
lines changed

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"test:unit:node:gateway": "aegir test -t node -f test/gateway/index.js",
3434
"test:unit:node:cli": "aegir test -t node -f test/cli/index.js",
3535
"test:unit:browser": "aegir test -t browser --no-cors",
36-
"test:interop": "IPFS_TEST=interop aegir test -t node -t browser -f test/interop",
36+
"test:interop": "IPFS_TEST=interop aegir test -t node -f test/interop",
3737
"test:interop:node": "IPFS_TEST=interop aegir test -t node -f test/interop/node.js",
3838
"test:interop:browser": "IPFS_TEST=interop aegir test -t browser -f test/interop/browser.js",
3939
"test:bootstrapers": "IPFS_TEST=bootstrapers aegir test -t browser -f test/bootstrapers.js",
@@ -63,7 +63,7 @@
6363
},
6464
"homepage": "https://github.com/ipfs/js-ipfs#readme",
6565
"devDependencies": {
66-
"aegir": "^12.1.3",
66+
"aegir": "^12.2.0",
6767
"buffer-loader": "0.0.1",
6868
"chai": "^4.1.2",
6969
"delay": "^2.0.0",
@@ -76,7 +76,7 @@
7676
"form-data": "^2.3.1",
7777
"hat": "0.0.3",
7878
"interface-ipfs-core": "~0.36.7",
79-
"ipfsd-ctl": "~0.24.1",
79+
"ipfsd-ctl": "~0.25.1",
8080
"left-pad": "^1.2.0",
8181
"lodash": "^4.17.4",
8282
"mocha": "^4.0.1",
@@ -92,6 +92,7 @@
9292
},
9393
"dependencies": {
9494
"async": "^2.6.0",
95+
"binary-querystring": "~0.1.2",
9596
"bl": "^1.2.1",
9697
"boom": "^7.1.1",
9798
"bs58": "^4.0.1",
@@ -106,7 +107,7 @@
106107
"hapi": "^16.6.2",
107108
"hapi-set-header": "^1.0.2",
108109
"hoek": "^5.0.2",
109-
"ipfs-api": "^17.1.0",
110+
"ipfs-api": "^17.1.1",
110111
"ipfs-bitswap": "~0.17.4",
111112
"ipfs-block": "~0.6.1",
112113
"ipfs-block-service": "~0.13.0",

src/core/components/no-floodsub.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
module.exports = NoFloodSub

src/core/components/start.js

+5-6
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

+1-7
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)

src/http/api/resources/pubsub.js

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

33
const PassThrough = require('stream').PassThrough
44
const bs58 = require('bs58')
5+
const binaryQueryString = require('binary-querystring')
56

67
exports = module.exports
78

@@ -48,6 +49,7 @@ exports.subscribe = {
4849

4950
reply(res)
5051
.header('X-Chunked-Output', '1')
52+
.header('content-encoding', 'identity') // stop gzip from buffering, see https://github.com/hapijs/hapi/issues/2975
5153
.header('content-type', 'application/json')
5254
})
5355
}
@@ -57,7 +59,9 @@ exports.publish = {
5759
handler: (request, reply) => {
5860
const arg = request.query.arg
5961
const topic = arg[0]
60-
const buf = arg[1]
62+
63+
const rawArgs = binaryQueryString(request.url.search)
64+
const buf = rawArgs.arg && rawArgs.arg[1]
6165

6266
const ipfs = request.server.app.ipfs
6367

@@ -69,7 +73,7 @@ exports.publish = {
6973
return reply(new Error('Missing buf'))
7074
}
7175

72-
ipfs.pubsub.publish(topic, Buffer.from(String(buf)), (err) => {
76+
ipfs.pubsub.publish(topic, buf, (err) => {
7377
if (err) {
7478
return reply(new Error(`Failed to publish to topic ${topic}: ${err}`))
7579
}

test/cli/pubsub.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const createTempNode = ''
1414
const repoPath = require('./index').repoPath
1515
const ipfs = require('../utils/ipfs-exec')(repoPath)
1616

17-
describe.skip('pubsub', () => {
17+
describe('pubsub', () => {
1818
const topicA = 'nonscentsA'
1919
const topicB = 'nonscentsB'
2020
const topicC = 'nonscentsC'

test/fixtures/go-ipfs-repo/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6
1+
6

test/http-api/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ describe('HTTP API', () => {
1919
let http = {}
2020

2121
before((done) => {
22-
http.api = new API(repoTests)
22+
const options = {
23+
enablePubsubExperiment: true
24+
}
25+
http.api = new API(repoTests, null, options)
2326

2427
ncp(repoExample, repoTests, (err) => {
2528
expect(err).to.not.exist()

test/http-api/interface/pubsub.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
'use strict'
44

5-
// TODO needs: https://github.com/ipfs/js-ipfs-api/pull/493
6-
/*
75
const test = require('interface-ipfs-core')
86
const FactoryClient = require('./../../utils/ipfs-factory-daemon')
97

@@ -20,4 +18,3 @@ const common = {
2018
}
2119

2220
test.pubsub(common)
23-
*/

test/http-api/spec/pubsub.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,17 @@ const chai = require('chai')
66
const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
9-
const createTempNode = ''
109

11-
// TODO migrate to use ipfs-factory-daemon
1210
module.exports = (http) => {
13-
describe.skip('/pubsub', () => {
11+
describe('/pubsub', () => {
1412
let api
15-
let tmpNode
1613

1714
const buf = Buffer.from('some message')
1815
const topic = 'nonScents'
1916
const topicNotSubscribed = 'somethingRandom'
2017

21-
before((done) => {
18+
before(() => {
2219
api = http.api.server.select('API')
23-
24-
createTempNode(47, (err, _ipfs) => {
25-
expect(err).to.not.exist()
26-
tmpNode = _ipfs
27-
tmpNode.goOnline((err) => {
28-
expect(err).to.not.exist()
29-
done()
30-
})
31-
})
32-
})
33-
34-
after((done) => {
35-
setTimeout(() => {
36-
tmpNode.goOffline(done)
37-
}, 1000)
3820
})
3921

4022
describe('/sub', () => {

test/interop/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ require('./exchange-files')
66
require('./circuit-relay')
77
require('./kad-dht')
88
require('./pubsub')
9+
require('./pubsub-go')

0 commit comments

Comments
 (0)