From 29b6d1e2654598d34d1187ae130611fec9fbb2cc Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 31 Aug 2018 12:21:51 +0100 Subject: [PATCH 1/3] feat: ipns over pubsub --- README.md | 3 ++ src/name/index.js | 3 +- src/name/pubsub/cancel.js | 24 +++++++++++ src/name/pubsub/index.js | 7 ++++ src/name/pubsub/state.js | 23 +++++++++++ src/name/pubsub/subs.js | 23 +++++++++++ test/interface.spec.js | 23 +++++++++++ test/name-pubsub.spec.js | 87 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/name/pubsub/cancel.js create mode 100644 src/name/pubsub/index.js create mode 100644 src/name/pubsub/state.js create mode 100644 src/name/pubsub/subs.js create mode 100644 test/name-pubsub.spec.js diff --git a/README.md b/README.md index 914f9aac6..e082d01b6 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,9 @@ const ipfs = IpfsApi({ - [name](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md) - [`ipfs.name.publish(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepublish) - [`ipfs.name.resolve(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#nameresolve) + - [`ipfs.name.pubsub.cancel(arg, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubcancel) + - [`ipfs.name.pubsub.state([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubstate) + - [`ipfs.name.pubsub.subs([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubsubs) #### Node Management 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/interface.spec.js b/test/interface.spec.js index 30fe04a92..64d77a25d 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -183,6 +183,29 @@ describe('interface-ipfs-core tests', () => { ] }) + /* + TODO: uncomment after https://github.com/ipfs/interface-ipfs-core/pull/361 being merged and a new release + tests.namePubsub(CommonFactory.create({ + spawnOptions: { + args: ['--enable-namesys-pubsub'], + initOptions: { bits: 1024 } + } + }), { + skip: [ + // name.pubsub.cancel + { + name: 'should cancel a subscription correctly returning true', + reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' + }, + // name.pubsub.subs + { + name: 'should get the list of subscriptions updated after a resolve', + reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' + } + ] + }) + */ + tests.object(defaultCommonFactory) tests.pin(defaultCommonFactory) 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() + }) + }) +}) From 572b43fc77acb828856bd310e267145aa3557e92 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 17 Oct 2018 17:20:44 +0100 Subject: [PATCH 2/3] fix: code review --- README.md | 2 +- src/name/pubsub/cancel.js | 2 +- src/name/pubsub/subs.js | 4 +- test/interface.spec.js | 4 +- test/name-pubsub.spec.js | 87 --------------------------------------- 5 files changed, 5 insertions(+), 94 deletions(-) delete mode 100644 test/name-pubsub.spec.js diff --git a/README.md b/README.md index e082d01b6..b50218380 100644 --- a/README.md +++ b/README.md @@ -277,10 +277,10 @@ const ipfs = IpfsApi({ - [name](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md) - [`ipfs.name.publish(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepublish) - - [`ipfs.name.resolve(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#nameresolve) - [`ipfs.name.pubsub.cancel(arg, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubcancel) - [`ipfs.name.pubsub.state([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubstate) - [`ipfs.name.pubsub.subs([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#namepubsubsubs) + - [`ipfs.name.resolve(addr, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/NAME.md#nameresolve) #### Node Management diff --git a/src/name/pubsub/cancel.js b/src/name/pubsub/cancel.js index 941969b71..32dbbb3af 100644 --- a/src/name/pubsub/cancel.js +++ b/src/name/pubsub/cancel.js @@ -4,7 +4,7 @@ const promisify = require('promisify-es6') const transform = function (res, callback) { callback(null, { - canceled: res.Canceled + canceled: res.Canceled === undefined || res.Canceled === true }) } diff --git a/src/name/pubsub/subs.js b/src/name/pubsub/subs.js index eda39bb17..3a3a54c2a 100644 --- a/src/name/pubsub/subs.js +++ b/src/name/pubsub/subs.js @@ -3,9 +3,7 @@ const promisify = require('promisify-es6') const transform = function (res, callback) { - callback(null, { - strings: res.Strings - }) + callback(null, res.Strings || []) } module.exports = (send) => { diff --git a/test/interface.spec.js b/test/interface.spec.js index 64d77a25d..f2b56a070 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -195,12 +195,12 @@ describe('interface-ipfs-core tests', () => { // name.pubsub.cancel { name: 'should cancel a subscription correctly returning true', - reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' + reason: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' }, // name.pubsub.subs { name: 'should get the list of subscriptions updated after a resolve', - reasone: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' + reason: 'go-ipfs is really slow for publishing and resolving ipns records, unless in offline mode' } ] }) diff --git a/test/name-pubsub.spec.js b/test/name-pubsub.spec.js deleted file mode 100644 index b4b2d9a73..000000000 --- a/test/name-pubsub.spec.js +++ /dev/null @@ -1,87 +0,0 @@ -/* 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() - }) - }) -}) From 44c3c9b6caa4bcaa5660d99088b367b613d59ac5 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 18 Oct 2018 11:19:39 +0100 Subject: [PATCH 3/3] chore: upgraded interface-ipfs-core --- package.json | 2 +- test/interface.spec.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 77ec25ef2..0f3637d56 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "eslint-plugin-react": "^7.10.0", "go-ipfs-dep": "~0.4.17", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.78.0", + "interface-ipfs-core": "~0.80.0", "ipfsd-ctl": "~0.39.0", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", diff --git a/test/interface.spec.js b/test/interface.spec.js index f2b56a070..04e1dd303 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -183,8 +183,7 @@ describe('interface-ipfs-core tests', () => { ] }) - /* - TODO: uncomment after https://github.com/ipfs/interface-ipfs-core/pull/361 being merged and a new release + // TODO: uncomment after https://github.com/ipfs/interface-ipfs-core/pull/361 being merged and a new release tests.namePubsub(CommonFactory.create({ spawnOptions: { args: ['--enable-namesys-pubsub'], @@ -204,7 +203,6 @@ describe('interface-ipfs-core tests', () => { } ] }) - */ tests.object(defaultCommonFactory)