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

Commit a45f748

Browse files
committed
feat: ipns over pubsub
1 parent 231c4d7 commit a45f748

File tree

8 files changed

+192
-1
lines changed

8 files changed

+192
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ const ipfs = IpfsApi({
278278
- [name](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md)
279279
- [`ipfs.name.publish(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepublish)
280280
- [`ipfs.name.resolve(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#nameresolve)
281+
- [`ipfs.name.pubsub.cancel(arg, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubcancel)
282+
- [`ipfs.name.pubsub.state([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubstate)
283+
- [`ipfs.name.pubsub.subs([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubsubs)
281284

282285
#### Node Management
283286

src/name/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = (arg) => {
77

88
return {
99
publish: require('./publish')(send),
10-
resolve: require('./resolve')(send)
10+
resolve: require('./resolve')(send),
11+
pubsub: require('./pubsub')(send)
1112
}
1213
}

src/name/pubsub/cancel.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
const transform = function (res, callback) {
6+
callback(null, {
7+
canceled: res.Canceled
8+
})
9+
}
10+
11+
module.exports = (send) => {
12+
return promisify((args, opts, callback) => {
13+
if (typeof (opts) === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
18+
send.andTransform({
19+
path: 'name/pubsub/cancel',
20+
args: args,
21+
qs: opts
22+
}, transform, callback)
23+
})
24+
}

src/name/pubsub/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = (send) => ({
4+
cancel: require('./cancel')(send),
5+
state: require('./state')(send),
6+
subs: require('./subs')(send)
7+
})

src/name/pubsub/state.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
const transform = function (res, callback) {
6+
callback(null, {
7+
enabled: res.Enabled
8+
})
9+
}
10+
11+
module.exports = (send) => {
12+
return promisify((opts, callback) => {
13+
if (typeof (opts) === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
18+
send.andTransform({
19+
path: 'name/pubsub/state',
20+
qs: opts
21+
}, transform, callback)
22+
})
23+
}

src/name/pubsub/subs.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
const transform = function (res, callback) {
6+
callback(null, {
7+
strings: res.Strings
8+
})
9+
}
10+
11+
module.exports = (send) => {
12+
return promisify((opts, callback) => {
13+
if (typeof (opts) === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
18+
send.andTransform({
19+
path: 'name/pubsub/subs',
20+
qs: opts
21+
}, transform, callback)
22+
})
23+
}

test/interface.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ describe('interface-ipfs-core tests', () => {
178178
]
179179
})
180180

181+
/*
182+
TODO: uncomment after https://github.com/ipfs/interface-ipfs-core/pull/361 being merged and a new release
183+
tests.namePubsub(CommonFactory.create({
184+
spawnOptions: {
185+
args: ['--enable-namesys-pubsub'],
186+
initOptions: { bits: 1024 }
187+
}
188+
}), {
189+
skip: [
190+
// name.pubsub.cancel
191+
{
192+
name: 'should cancel a subscription correctly returning true',
193+
reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode'
194+
},
195+
// name.pubsub.subs
196+
{
197+
name: 'should get the list of subscriptions updated after a resolve',
198+
reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode'
199+
}
200+
]
201+
})
202+
*/
203+
181204
tests.object(defaultCommonFactory)
182205

183206
tests.pin(defaultCommonFactory)

test/name-pubsub.spec.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
9+
const parallel = require('async/parallel')
10+
const series = require('async/series')
11+
12+
const IPFSApi = require('../src')
13+
const f = require('./utils/factory')
14+
15+
describe('.name-pubsub', () => {
16+
let ipfs
17+
let ipfsd
18+
let otherd
19+
20+
before(function (done) {
21+
this.timeout(30 * 1000)
22+
23+
series([
24+
(cb) => {
25+
f.spawn({
26+
initOptions: { bits: 1024 },
27+
args: ['--enable-namesys-pubsub']
28+
}, (err, _ipfsd) => {
29+
expect(err).to.not.exist()
30+
ipfsd = _ipfsd
31+
ipfs = IPFSApi(_ipfsd.apiAddr)
32+
cb()
33+
})
34+
},
35+
(cb) => {
36+
f.spawn({ initOptions: { bits: 1024 } }, (err, node) => {
37+
expect(err).to.not.exist()
38+
otherd = node
39+
cb()
40+
})
41+
}
42+
], done)
43+
})
44+
45+
after(function (done) {
46+
this.timeout(10 * 1000)
47+
48+
parallel([
49+
(cb) => {
50+
if (!ipfsd) return cb()
51+
ipfsd.stop(cb)
52+
},
53+
(cb) => {
54+
if (!otherd) return cb()
55+
otherd.stop(cb)
56+
}
57+
], done)
58+
})
59+
60+
it('.name.pubsub.state', (done) => {
61+
ipfs.name.pubsub.state((err, res) => {
62+
expect(err).to.not.exist()
63+
expect(res).to.exist()
64+
expect(res).to.have.property('enabled')
65+
expect(res.enabled).to.be.eql(true)
66+
done()
67+
})
68+
})
69+
70+
it('.name.pubsub.subs', (done) => {
71+
ipfs.name.pubsub.subs((err, res) => {
72+
expect(err).to.not.exist()
73+
expect(res).to.exist()
74+
expect(res).to.have.property('strings')
75+
done()
76+
})
77+
})
78+
79+
it('.name.pubsub.cancel', (done) => {
80+
ipfs.name.pubsub.cancel('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', (err, res) => {
81+
expect(err).to.not.exist()
82+
expect(res).to.exist()
83+
expect(res).to.have.property('canceled')
84+
done()
85+
})
86+
})
87+
})

0 commit comments

Comments
 (0)