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

Commit cbb7329

Browse files
committed
feat: ipns over pubsub
1 parent 1e86e7f commit cbb7329

File tree

6 files changed

+220
-2
lines changed

6 files changed

+220
-2
lines changed

SPEC/NAME.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ ipfs.name.pubsub.state(function (err, result) {
165165

166166
```JavaScript
167167
{
168-
strings: ['QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
168+
strings: ['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
169169
}
170170
```
171171

@@ -176,6 +176,6 @@ If no `callback` is passed, a promise is returned.
176176
```JavaScript
177177
ipfs.name.pubsub.subs(function (err, result) {
178178
console.log(result.strings)
179-
// ['QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
179+
// ['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
180180
})
181181
```

js/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exports.key = require('./key')
1111
exports.ls = require('./ls')
1212
exports.miscellaneous = require('./miscellaneous')
1313
exports.name = require('./name')
14+
exports.namePubsub = require('./name-pubsub')
1415
exports.object = require('./object')
1516
exports.pin = require('./pin')
1617
exports.ping = require('./ping')

js/src/name-pubsub/cancel.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const loadFixture = require('aegir/fixtures')
6+
7+
const { spawnNodeWithId } = require('../utils/spawn')
8+
const { getDescribe, getIt, expect } = require('../utils/mocha')
9+
10+
const fixture = Object.freeze({
11+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
12+
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
13+
})
14+
15+
module.exports = (createCommon, options) => {
16+
const describe = getDescribe(options)
17+
const it = getIt(options)
18+
const common = createCommon()
19+
20+
describe('.name.pubsub.cancel', function () {
21+
let ipfs
22+
let nodeId
23+
24+
before(function (done) {
25+
// CI takes longer to instantiate the daemon, so we need to increase the
26+
// timeout for the before step
27+
this.timeout(60 * 1000)
28+
29+
common.setup((err, factory) => {
30+
expect(err).to.not.exist()
31+
32+
spawnNodeWithId(factory, (err, node) => {
33+
expect(err).to.not.exist()
34+
35+
ipfs = node
36+
nodeId = node.peerId.id
37+
38+
ipfs.files.add(fixture.data, { pin: false }, done)
39+
})
40+
})
41+
})
42+
43+
after((done) => common.teardown(done))
44+
45+
it('should return false when the name that is intended to cancel is not subscribed', function (done) {
46+
this.timeout(60 * 1000)
47+
48+
ipfs.name.pubsub.cancel(nodeId, (err, res) => {
49+
expect(err).to.not.exist()
50+
expect(res).to.exist()
51+
expect(res).to.have.property('canceled')
52+
expect(res.canceled).to.eql(false)
53+
54+
done()
55+
})
56+
})
57+
58+
it('should cancel a subscription correctly returning true', function (done) {
59+
this.timeout(140 * 1000)
60+
const value = fixture.cid
61+
62+
ipfs.name.publish(value, { resolve: false }, (err, res) => {
63+
expect(err).to.not.exist()
64+
65+
ipfs.name.resolve(nodeId, (err) => {
66+
expect(err).to.not.exist()
67+
68+
ipfs.name.pubsub.cancel(nodeId, (err, res) => {
69+
expect(err).to.not.exist()
70+
expect(res).to.exist()
71+
expect(res).to.have.property('canceled')
72+
expect(res.canceled).to.eql(true)
73+
74+
done()
75+
})
76+
})
77+
})
78+
})
79+
})
80+
}

js/src/name-pubsub/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
cancel: require('./cancel'),
6+
state: require('./state'),
7+
subs: require('./subs')
8+
}
9+
10+
module.exports = createSuite(tests)

js/src/name-pubsub/state.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { spawnNodeWithId } = require('../utils/spawn')
5+
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
7+
module.exports = (createCommon, options) => {
8+
const describe = getDescribe(options)
9+
const it = getIt(options)
10+
const common = createCommon()
11+
12+
describe('.name.pubsub.state', function () {
13+
let ipfs
14+
15+
before(function (done) {
16+
// CI takes longer to instantiate the daemon, so we need to increase the
17+
// timeout for the before step
18+
this.timeout(60 * 1000)
19+
20+
common.setup((err, factory) => {
21+
expect(err).to.not.exist()
22+
23+
spawnNodeWithId(factory, (err, node) => {
24+
expect(err).to.not.exist()
25+
26+
ipfs = node
27+
done()
28+
})
29+
})
30+
})
31+
32+
after((done) => common.teardown(done))
33+
34+
it('should get the current state of pubsub', function (done) {
35+
this.timeout(50 * 1000)
36+
37+
ipfs.name.pubsub.state((err, res) => {
38+
expect(err).to.not.exist()
39+
expect(res).to.exist()
40+
expect(res).to.have.property('enabled')
41+
expect(res.enabled).to.be.eql(true)
42+
43+
done()
44+
})
45+
})
46+
})
47+
}

js/src/name-pubsub/subs.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const loadFixture = require('aegir/fixtures')
6+
7+
const { spawnNodeWithId } = require('../utils/spawn')
8+
const { getDescribe, getIt, expect } = require('../utils/mocha')
9+
10+
const fixture = Object.freeze({
11+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
12+
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
13+
})
14+
15+
module.exports = (createCommon, options) => {
16+
const describe = getDescribe(options)
17+
const it = getIt(options)
18+
const common = createCommon()
19+
20+
describe('.name.pubsub.subs', function () {
21+
let ipfs
22+
let nodeId
23+
24+
before(function (done) {
25+
// CI takes longer to instantiate the daemon, so we need to increase the
26+
// timeout for the before step
27+
this.timeout(60 * 1000)
28+
29+
common.setup((err, factory) => {
30+
expect(err).to.not.exist()
31+
32+
spawnNodeWithId(factory, (err, node) => {
33+
expect(err).to.not.exist()
34+
35+
ipfs = node
36+
nodeId = node.peerId.id
37+
38+
ipfs.files.add(fixture.data, { pin: false }, done)
39+
})
40+
})
41+
})
42+
43+
after((done) => common.teardown(done))
44+
45+
it('should get a null result of subscriptions before any resolve', function (done) {
46+
this.timeout(60 * 1000)
47+
48+
ipfs.name.pubsub.subs((err, res) => {
49+
expect(err).to.not.exist()
50+
expect(res).to.exist()
51+
expect(res).to.have.property('strings')
52+
expect(res.strings).to.eql(null)
53+
54+
done()
55+
})
56+
})
57+
58+
it('should get the list of subscriptions updated after a resolve', function (done) {
59+
this.timeout(140 * 1000)
60+
const value = fixture.cid
61+
62+
ipfs.name.publish(value, { resolve: false }, (err, res) => {
63+
expect(err).to.not.exist()
64+
65+
ipfs.name.resolve(nodeId, (err) => {
66+
expect(err).to.not.exist()
67+
68+
ipfs.name.pubsub.subs((err, res) => {
69+
expect(err).to.not.exist()
70+
expect(res).to.exist()
71+
expect(res).to.have.property('strings')
72+
expect(res.strings).to.be.an('array').that.does.include(`/ipns/${nodeId}`)
73+
74+
done()
75+
})
76+
})
77+
})
78+
})
79+
})
80+
}

0 commit comments

Comments
 (0)