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

Commit

Permalink
feat: ipns over pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 31, 2018
1 parent 231c4d7 commit 963288e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/name/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = (arg) => {

return {
publish: require('./publish')(send),
resolve: require('./resolve')(send)
resolve: require('./resolve')(send),
pubsub: require('./pubsub')(send)
}
}
24 changes: 24 additions & 0 deletions src/name/pubsub/cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
canceled: res.Canceled
})
}

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

send.andTransform({
path: 'name/pubsub/cancel',
args: args,
qs: opts
}, transform, callback)
})
}
7 changes: 7 additions & 0 deletions src/name/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = (send) => ({
cancel: require('./cancel')(send),
state: require('./state')(send),
subs: require('./subs')(send)
})
23 changes: 23 additions & 0 deletions src/name/pubsub/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
enabled: res.Enabled
})
}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

send.andTransform({
path: 'name/pubsub/state',
qs: opts
}, transform, callback)
})
}
23 changes: 23 additions & 0 deletions src/name/pubsub/subs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, {
strings: res.Strings
})
}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

send.andTransform({
path: 'name/pubsub/subs',
qs: opts
}, transform, callback)
})
}
87 changes: 87 additions & 0 deletions test/name-pubsub.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const parallel = require('async/parallel')
const series = require('async/series')

const IPFSApi = require('../src')
const f = require('./utils/factory')

describe('.name-pubsub', () => {
let ipfs
let ipfsd
let otherd

before(function (done) {
this.timeout(30 * 1000)

series([
(cb) => {
f.spawn({
initOptions: { bits: 1024 },
args: ['--enable-namesys-pubsub']
}, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = IPFSApi(_ipfsd.apiAddr)
cb()
})
},
(cb) => {
f.spawn({ initOptions: { bits: 1024 } }, (err, node) => {
expect(err).to.not.exist()
otherd = node
cb()
})
}
], done)
})

after(function (done) {
this.timeout(10 * 1000)

parallel([
(cb) => {
if (!ipfsd) return cb()
ipfsd.stop(cb)
},
(cb) => {
if (!otherd) return cb()
otherd.stop(cb)
}
], done)
})

it('.name.pubsub.state', (done) => {
ipfs.name.pubsub.state((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('enabled')
expect(res.enabled).to.be.eql(true)
done()
})
})

it('.name.pubsub.subs', (done) => {
ipfs.name.pubsub.subs((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('strings')
done()
})
})

it('.name.pubsub.cancel', (done) => {
ipfs.name.pubsub.cancel('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC', (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('canceled')
done()
})
})
})

0 comments on commit 963288e

Please sign in to comment.