Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: ipns over pubsub #1559

Merged
merged 9 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"datastore-pubsub": "~0.1.1",
"debug": "^4.1.0",
"deep-extend": "~0.6.0",
"dlv": "^1.1.2",
"err-code": "^1.1.2",
"file-type": "^10.2.0",
"fnv1a": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function libp2p (self) {
},
EXPERIMENTAL: {
dht: get(opts.options, 'EXPERIMENTAL.dht', false),
pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false) || get(opts.options, 'EXPERIMENTAL.ipnsPubsub', false)
pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
}
},
connectionManager: get(opts.options, 'connectionManager',
Expand Down
14 changes: 6 additions & 8 deletions src/core/components/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ 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) => {
Expand All @@ -18,7 +16,7 @@ module.exports = function pubsub (self) {
options = {}
}

if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
return callback
? setImmediate(() => callback(errPubsubDisabled()))
: Promise.reject(errPubsubDisabled())
Expand All @@ -39,7 +37,7 @@ module.exports = function pubsub (self) {
},

unsubscribe: (topic, handler, callback) => {
if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
return callback
? setImmediate(() => callback(errPubsubDisabled()))
: Promise.reject(errPubsubDisabled())
Expand All @@ -55,28 +53,28 @@ module.exports = function pubsub (self) {
},

publish: promisify((topic, data, callback) => {
if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self._libp2pNode.pubsub.publish(topic, data, callback)
}),

ls: promisify((callback) => {
if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self._libp2pNode.pubsub.ls(callback)
}),

peers: promisify((topic, callback) => {
if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
return setImmediate(() => callback(errPubsubDisabled()))
}
self._libp2pNode.pubsub.peers(topic, callback)
}),

setMaxListeners (n) {
if (!pubsubEnabled(self._options)) {
if (!self._options.EXPERIMENTAL.pubsub) {
throw errPubsubDisabled()
}
self._libp2pNode.pubsub.setMaxListeners(n)
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const get = require('dlv')
const get = require('lodash/get')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use smaller package
https://bundlephobia.com/result?p=lodash.get@4.4.2
vs
https://bundlephobia.com/result?p=dlv@1.1.2

Suggested change
const get = require('lodash/get')
const get = require('dlv')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks! Will add dlv to package.json as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with using dlv over loadash.get but all other occurances of lodash.get need to be replaced as well otherwise we're just adding more to our bundle.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you agree, I can create other PR with all those occurrences changed to dlv.

const setImmediate = require('async/setImmediate')
const promisify = require('promisify-es6')
const { TieredDatastore } = require('datastore-core')
Expand Down
5 changes: 5 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ class IPFS extends EventEmitter {
this.log('EXPERIMENTAL pubsub is enabled')
}
if (this._options.EXPERIMENTAL.ipnsPubsub) {
if (!this._options.EXPERIMENTAL.pubsub) {
this.log('EXPERIMENTAL pubsub is enabled to use IPNS pubsub')
this._options.EXPERIMENTAL.pubsub = true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does go-ipfs do IPNS over PubSub as an experimental flag? Why not just one flag that enables all of PubSub

Copy link
Member Author

@vasco-santos vasco-santos Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, go-ipfs uses an experimental flag for IPNS over Pubsub: --enable-namesys-pubsub is the CLI flag

Copy link
Member

@daviddias daviddias Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Experimental features on Alpha software", how much more bleeding edge can you go 😅


this.log('EXPERIMENTAL IPNS pubsub is enabled')
}
if (this._options.EXPERIMENTAL.sharding) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/ipns/routing/offline-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class OfflineDatastore {
}

// Marshal to libp2p record as the DHT does
let record = new Record(key, value)
const record = new Record(key, value)

this._repo.datastore.put(routingKey, record.serialize(), callback)
}
Expand Down
21 changes: 0 additions & 21 deletions src/core/ipns/routing/pubsub-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ class IpnsPubsubDatastore {
* @returns {void}
*/
put (key, value, callback) {
if (!Buffer.isBuffer(key)) {
const errMsg = `key ${key} does not have a valid format`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
}

if (!Buffer.isBuffer(value)) {
const errMsg = `received value ${value} is not a buffer`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_VALUE_RECEIVED'))
}

this._pubsubDs.put(key, value, callback)
}

Expand All @@ -56,13 +42,6 @@ class IpnsPubsubDatastore {
* @returns {void}
*/
get (key, callback) {
if (!Buffer.isBuffer(key)) {
const errMsg = `key ${key} does not have a valid format`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
}

this._pubsubDs.get(key, (err, res) => {
// Add topic subscribed
const ns = key.slice(0, ipns.namespaceLength)
Expand Down
13 changes: 7 additions & 6 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function uriToMultiaddr (uri) {
}

function HttpApi (repo, config, cliArgs) {
cliArgs = cliArgs || {}
this.node = undefined
this.server = undefined

Expand Down Expand Up @@ -71,13 +72,13 @@ function HttpApi (repo, config, cliArgs) {
init: init,
start: true,
config: config,
local: cliArgs && cliArgs.local,
pass: cliArgs && cliArgs.pass,
local: cliArgs.local,
pass: cliArgs.pass,
EXPERIMENTAL: {
pubsub: cliArgs && cliArgs.enablePubsubExperiment,
ipnsPubsub: cliArgs && cliArgs.enableNamesysPubsub,
dht: cliArgs && cliArgs.enableDhtExperiment,
sharding: cliArgs && cliArgs.enableShardingExperiment
pubsub: cliArgs.enablePubsubExperiment,
ipnsPubsub: cliArgs.enableNamesysPubsub,
dht: cliArgs.enableDhtExperiment,
sharding: cliArgs.enableShardingExperiment
},
libp2p: libp2p
})
Expand Down