From 03c24e1e63efb2ee8e42585b6e8506c07b51825e Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 22 Nov 2018 12:15:57 +0000 Subject: [PATCH] fix: remove libp2p-record for pubsub --- package.json | 2 +- src/core/components/pubsub.js | 14 ++++++++------ src/core/ipns/publisher.js | 20 ++++---------------- src/core/ipns/resolver.js | 4 +--- src/core/ipns/routing/pubsub-datastore.js | 1 - 5 files changed, 14 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index ea8164a85b..af4703c630 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "cids": "~0.5.5", "class-is": "^1.1.0", "datastore-core": "~0.6.0", - "datastore-pubsub": "ipfs/js-datastore-pubsub#feat/encode-record-store-keys", + "datastore-pubsub": "~0.1.1", "debug": "^4.1.0", "deep-extend": "~0.6.0", "err-code": "^1.1.2", diff --git a/src/core/components/pubsub.js b/src/core/components/pubsub.js index f22f34cf34..8bb15910bc 100644 --- a/src/core/components/pubsub.js +++ b/src/core/components/pubsub.js @@ -8,6 +8,8 @@ const errPubsubDisabled = () => { return errCode(new Error('pubsub experiment is not enabled'), 'ERR_PUBSUB_DISABLED') } +const pubsubEnabled = (options) => options.EXPERIMENTAL.pubsub || options.EXPERIMENTAL.ipnsPubsub + module.exports = function pubsub (self) { return { subscribe: (topic, handler, options, callback) => { @@ -16,7 +18,7 @@ module.exports = function pubsub (self) { options = {} } - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { return callback ? setImmediate(() => callback(errPubsubDisabled())) : Promise.reject(errPubsubDisabled()) @@ -37,7 +39,7 @@ module.exports = function pubsub (self) { }, unsubscribe: (topic, handler, callback) => { - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { return callback ? setImmediate(() => callback(errPubsubDisabled())) : Promise.reject(errPubsubDisabled()) @@ -53,28 +55,28 @@ module.exports = function pubsub (self) { }, publish: promisify((topic, data, callback) => { - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { return setImmediate(() => callback(errPubsubDisabled())) } self._libp2pNode.pubsub.publish(topic, data, callback) }), ls: promisify((callback) => { - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { return setImmediate(() => callback(errPubsubDisabled())) } self._libp2pNode.pubsub.ls(callback) }), peers: promisify((topic, callback) => { - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { return setImmediate(() => callback(errPubsubDisabled())) } self._libp2pNode.pubsub.peers(topic, callback) }), setMaxListeners (n) { - if (!self._options.EXPERIMENTAL.pubsub) { + if (!pubsubEnabled(self._options)) { throw errPubsubDisabled() } self._libp2pNode.pubsub.setMaxListeners(n) diff --git a/src/core/ipns/publisher.js b/src/core/ipns/publisher.js index 9e8fcdf3f3..4be4fc41bb 100644 --- a/src/core/ipns/publisher.js +++ b/src/core/ipns/publisher.js @@ -1,7 +1,6 @@ 'use strict' const PeerId = require('peer-id') -const Record = require('libp2p-record').Record const { Key } = require('interface-datastore') const series = require('async/series') const errcode = require('err-code') @@ -97,19 +96,17 @@ class IpnsPublisher { return callback(errcode(new Error(errMsg), 'ERR_INVALID_DATASTORE_KEY')) } - let rec + let entryData try { // Marshal record - const entryData = ipns.marshal(entry) - // Marshal to libp2p record - rec = new Record(key.toBuffer(), entryData) + entryData = ipns.marshal(entry) } catch (err) { log.error(err) return callback(err) } // Add record to routing (buffer key) - this._routing.put(key.toBuffer(), rec.serialize(), (err, res) => { + this._routing.put(key.toBuffer(), entryData, (err, res) => { if (err) { const errMsg = `ipns record for ${key.toString()} could not be stored in the routing` @@ -137,17 +134,8 @@ class IpnsPublisher { return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER')) } - let rec - try { - // Marshal to libp2p record - rec = new Record(key.toBuffer(), publicKey.bytes) - } catch (err) { - log.error(err) - return callback(err) - } - // Add public key to routing (buffer key) - this._routing.put(key.toBuffer(), rec.serialize(), (err, res) => { + this._routing.put(key.toBuffer(), publicKey.bytes, (err, res) => { if (err) { const errMsg = `public key for ${key.toString()} could not be stored in the routing` diff --git a/src/core/ipns/resolver.js b/src/core/ipns/resolver.js index f9fb81ccd5..8ff9c38ab3 100644 --- a/src/core/ipns/resolver.js +++ b/src/core/ipns/resolver.js @@ -1,7 +1,6 @@ 'use strict' const ipns = require('ipns') -const Record = require('libp2p-record').Record const PeerId = require('peer-id') const errcode = require('err-code') @@ -119,8 +118,7 @@ class IpnsResolver { let ipnsEntry try { - const record = Record.deserialize(res) - ipnsEntry = ipns.unmarshal(record.value) + ipnsEntry = ipns.unmarshal(res) } catch (err) { const errMsg = `found ipns record that we couldn't convert to a value` diff --git a/src/core/ipns/routing/pubsub-datastore.js b/src/core/ipns/routing/pubsub-datastore.js index 0556698c4b..17d4b718e9 100644 --- a/src/core/ipns/routing/pubsub-datastore.js +++ b/src/core/ipns/routing/pubsub-datastore.js @@ -89,7 +89,6 @@ class IpnsPubsubDatastore { } // Modify subscription key to have a proper encoding - // Without this, the utf-8 encoding gets the key broken _handleSubscriptionKey (key, callback) { const subscriber = this._subscriptions[key]