diff --git a/src/name/index.js b/src/name/index.js index 811357b7c..8f823311a 100644 --- a/src/name/index.js +++ b/src/name/index.js @@ -7,6 +7,7 @@ module.exports = (arg) => { return { publish: require('./publish')(send), - resolve: require('./resolve')(send) + resolve: require('./resolve')(send), + pubsub: require('./pubsub')(send) } } diff --git a/src/name/pubsub/cancel.js b/src/name/pubsub/cancel.js new file mode 100644 index 000000000..941969b71 --- /dev/null +++ b/src/name/pubsub/cancel.js @@ -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) + }) +} diff --git a/src/name/pubsub/index.js b/src/name/pubsub/index.js new file mode 100644 index 000000000..aefc0f880 --- /dev/null +++ b/src/name/pubsub/index.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = (send) => ({ + cancel: require('./cancel')(send), + state: require('./state')(send), + subs: require('./subs')(send) +}) diff --git a/src/name/pubsub/state.js b/src/name/pubsub/state.js new file mode 100644 index 000000000..cc9b0b369 --- /dev/null +++ b/src/name/pubsub/state.js @@ -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) + }) +} diff --git a/src/name/pubsub/subs.js b/src/name/pubsub/subs.js new file mode 100644 index 000000000..eda39bb17 --- /dev/null +++ b/src/name/pubsub/subs.js @@ -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) + }) +} diff --git a/test/name-pubsub.spec.js b/test/name-pubsub.spec.js new file mode 100644 index 000000000..b4b2d9a73 --- /dev/null +++ b/test/name-pubsub.spec.js @@ -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() + }) + }) +})