From 7ca80251946616b1d0338fd387376dd28db1da87 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 16:52:45 +0100 Subject: [PATCH 1/6] fix: make pubsub.unsubscribe async and alter pubsub.subscribe signature BREAKING CHANGE: pubsub.unsubscribe is now async and argument order for pubsub.subscribe has changed License: MIT Signed-off-by: Alan Shaw --- src/core/components/pubsub.js | 17 +++++++++++------ src/http/api/resources/pubsub.js | 7 ++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index d5825960e2..a5530226c4 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -4,10 +4,9 @@ const promisify = require('promisify-es6') module.exports = function pubsub (self) { return { - subscribe: (topic, options, handler, callback) => { + subscribe: (topic, handler, options, callback) => { if (typeof options === 'function') { - callback = handler - handler = options + callback = options options = {} } @@ -20,13 +19,19 @@ module.exports = function pubsub (self) { resolve() }) }) - } else { - self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) } + + self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) }, - unsubscribe: (topic, handler) => { + unsubscribe: (topic, handler, callback) => { self._libp2pNode.pubsub.unsubscribe(topic, handler) + + if (!callback) { + return Promise.resolve() + } + + process.nextTick(() => callback()) }, publish: promisify((topic, data, callback) => { diff --git a/src/http/api/resources/pubsub.js b/src/http/api/resources/pubsub.js index 065746c62e..8b88bd3dce 100644 --- a/src/http/api/resources/pubsub.js +++ b/src/http/api/resources/pubsub.js @@ -33,16 +33,13 @@ exports.subscribe = { res.write('{}\n') const unsubscribe = () => { - ipfs.pubsub.unsubscribe(topic, handler) - res.end() + ipfs.pubsub.unsubscribe(topic, handler, () => res.end()) } request.once('disconnect', unsubscribe) request.once('finish', unsubscribe) - ipfs.pubsub.subscribe(topic, { - discover: discover - }, handler, (err) => { + ipfs.pubsub.subscribe(topic, handler, { discover: discover }, (err) => { if (err) { return reply(err) } From e5125e9efdaee7e1e332aadf4f86bec2ef4b648b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 9 May 2018 16:52:45 +0100 Subject: [PATCH 2/6] fix: make pubsub.unsubscribe async and alter pubsub.subscribe signature BREAKING CHANGE: pubsub.unsubscribe is now async and argument order for pubsub.subscribe has changed License: MIT Signed-off-by: Alan Shaw --- src/core/components/pubsub.js | 17 +++++++++++------ src/http/api/resources/pubsub.js | 7 ++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index d5825960e2..a5530226c4 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -4,10 +4,9 @@ const promisify = require('promisify-es6') module.exports = function pubsub (self) { return { - subscribe: (topic, options, handler, callback) => { + subscribe: (topic, handler, options, callback) => { if (typeof options === 'function') { - callback = handler - handler = options + callback = options options = {} } @@ -20,13 +19,19 @@ module.exports = function pubsub (self) { resolve() }) }) - } else { - self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) } + + self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) }, - unsubscribe: (topic, handler) => { + unsubscribe: (topic, handler, callback) => { self._libp2pNode.pubsub.unsubscribe(topic, handler) + + if (!callback) { + return Promise.resolve() + } + + process.nextTick(() => callback()) }, publish: promisify((topic, data, callback) => { diff --git a/src/http/api/resources/pubsub.js b/src/http/api/resources/pubsub.js index 065746c62e..8b88bd3dce 100644 --- a/src/http/api/resources/pubsub.js +++ b/src/http/api/resources/pubsub.js @@ -33,16 +33,13 @@ exports.subscribe = { res.write('{}\n') const unsubscribe = () => { - ipfs.pubsub.unsubscribe(topic, handler) - res.end() + ipfs.pubsub.unsubscribe(topic, handler, () => res.end()) } request.once('disconnect', unsubscribe) request.once('finish', unsubscribe) - ipfs.pubsub.subscribe(topic, { - discover: discover - }, handler, (err) => { + ipfs.pubsub.subscribe(topic, handler, { discover: discover }, (err) => { if (err) { return reply(err) } From 23af010e79a54ab6c7049d18ae08e7b55e0d8102 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 12 May 2018 15:37:05 +0100 Subject: [PATCH 3/6] chore: update deps --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3d93c9f430..f742f1c627 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "expose-loader": "^0.7.5", "form-data": "^2.3.2", "hat": "0.0.3", - "interface-ipfs-core": "~0.64.3", + "interface-ipfs-core": "~0.65.5", "ipfsd-ctl": "~0.33.1", "lodash": "^4.17.10", "mocha": "^5.1.1", @@ -105,7 +105,7 @@ "hapi-set-header": "^1.0.2", "hoek": "^5.0.3", "human-to-milliseconds": "^1.0.0", - "ipfs-api": "^20.2.1", + "ipfs-api": "^21.0.0", "ipfs-bitswap": "~0.20.0", "ipfs-block": "~0.7.1", "ipfs-block-service": "~0.14.0", From bc5764ebe9216c3a95bdd6f1c51edfc7528288b4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 12 May 2018 15:40:05 +0100 Subject: [PATCH 4/6] fix: use async/setImmediate vs process.nextTick --- src/core/components/pubsub.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index a5530226c4..d125b6d2dc 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -1,6 +1,7 @@ 'use strict' const promisify = require('promisify-es6') +const setImmediate = require('async/setImmediate') module.exports = function pubsub (self) { return { @@ -31,7 +32,7 @@ module.exports = function pubsub (self) { return Promise.resolve() } - process.nextTick(() => callback()) + setImmediate(() => callback()) }, publish: promisify((topic, data, callback) => { From bb5e3ab21330f720487bc3a97cb437cbf02eb92d Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 12 May 2018 15:44:00 +0100 Subject: [PATCH 5/6] docs: update pubsub func signatures --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73133a7cb6..b20cff987b 100644 --- a/README.md +++ b/README.md @@ -413,8 +413,8 @@ The core API is grouped into several areas: - [dht (not implemented, yet!)](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/) - [pubsub](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md) - - [`ipfs.pubsub.subscribe(topic, options, handler, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe) - - [`ipfs.pubsub.unsubscribe(topic, handler)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubunsubscribe) + - [`ipfs.pubsub.subscribe(topic, handler, options, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubsubscribe) + - [`ipfs.pubsub.unsubscribe(topic, handler, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubunsubscribe) - [`ipfs.pubsub.publish(topic, data, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubpublish) - [`ipfs.pubsub.ls(topic, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubls) - [`ipfs.pubsub.peers(topic, callback)`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/PUBSUB.md#pubsubpeers) From 8d30fa437a58a8860f9dc882d3319eedcd46b0ad Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 14 May 2018 13:12:23 +0100 Subject: [PATCH 6/6] chore: upgrade ipfsd-ctl version License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f742f1c627..707ca981ea 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "form-data": "^2.3.2", "hat": "0.0.3", "interface-ipfs-core": "~0.65.5", - "ipfsd-ctl": "~0.33.1", + "ipfsd-ctl": "~0.34.0", "lodash": "^4.17.10", "mocha": "^5.1.1", "ncp": "^2.0.0",