-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: use PubSub API directly from libp2p #1215
Changes from all commits
c6caddb
f46957c
cdbee33
f7328a9
4ac081d
fce326c
1976974
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ module.exports = function libp2p (self) { | |
bootstrap: get(config, 'Bootstrap'), | ||
modules: self._libp2pModules, | ||
// EXPERIMENTAL | ||
pubsub: get(self._options, 'EXPERIMENTAL.pubsub', false), | ||
dht: get(self._options, 'EXPERIMENTAL.dht', false), | ||
relay: { | ||
enabled: get(config, 'EXPERIMENTAL.relay.enabled', false), | ||
|
@@ -50,9 +51,7 @@ module.exports = function libp2p (self) { | |
}) | ||
|
||
self._libp2pNode.start((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (err) { return callback(err) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I prefer not having unrelated formatting changes together with real code changes. |
||
|
||
self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => { | ||
console.log('Swarm listening on', ma.toString()) | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,48 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const setImmediate = require('async/setImmediate') | ||
|
||
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR | ||
|
||
module.exports = function pubsub (self) { | ||
return { | ||
subscribe: (topic, options, handler, callback) => { | ||
if (!self.isOnline()) { | ||
throw new Error(OFFLINE_ERROR) | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = handler | ||
handler = options | ||
options = {} | ||
} | ||
|
||
function subscribe (cb) { | ||
if (self._pubsub.listenerCount(topic) === 0) { | ||
self._pubsub.subscribe(topic) | ||
} | ||
|
||
self._pubsub.on(topic, handler) | ||
setImmediate(cb) | ||
} | ||
|
||
if (!callback) { | ||
return new Promise((resolve, reject) => { | ||
subscribe((err) => { | ||
self._libp2pNode.pubsub.subscribe(topic, options, handler, (err) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} else { | ||
subscribe(callback) | ||
self._libp2pNode.pubsub.subscribe(topic, options, handler, callback) | ||
} | ||
}, | ||
|
||
unsubscribe: (topic, handler) => { | ||
self._pubsub.removeListener(topic, handler) | ||
|
||
if (self._pubsub.listenerCount(topic) === 0) { | ||
self._pubsub.unsubscribe(topic) | ||
} | ||
self._libp2pNode.pubsub.unsubscribe(topic, handler) | ||
}, | ||
|
||
publish: promisify((topic, data, callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(new Error(OFFLINE_ERROR))) | ||
} | ||
|
||
if (!Buffer.isBuffer(data)) { | ||
return setImmediate(() => callback(new Error('data must be a Buffer'))) | ||
} | ||
|
||
self._pubsub.publish(topic, data) | ||
setImmediate(() => callback()) | ||
self._libp2pNode.pubsub.publish(topic, data, callback) | ||
}), | ||
|
||
ls: promisify((callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(new Error(OFFLINE_ERROR))) | ||
} | ||
|
||
const subscriptions = Array.from(self._pubsub.subscriptions) | ||
|
||
setImmediate(() => callback(null, subscriptions)) | ||
self._libp2pNode.pubsub.ls(callback) | ||
}), | ||
|
||
peers: promisify((topic, callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(new Error(OFFLINE_ERROR))) | ||
} | ||
|
||
if (typeof topic === 'function') { | ||
callback = topic | ||
topic = null | ||
} | ||
|
||
const peers = Array.from(self._pubsub.peers.values()) | ||
.filter((peer) => topic ? peer.topics.has(topic) : true) | ||
.map((peer) => peer.info.id.toB58String()) | ||
|
||
setImmediate(() => callback(null, peers)) | ||
self._libp2pNode.pubsub.peers(topic, callback) | ||
}), | ||
|
||
setMaxListeners (n) { | ||
return self._pubsub.setMaxListeners(n) | ||
self._libp2pNode.pubsub.setMaxListeners(n) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,9 @@ const isNode = require('detect-node') | |
const multihashing = require('multihashing-async') | ||
const CID = require('cids') | ||
|
||
const DaemonFactory = require('ipfsd-ctl') | ||
const df = DaemonFactory.create({ type: 'js' }) | ||
|
||
const dfProc = DaemonFactory.create({ type: 'proc' }) | ||
const IPFSFactory = require('ipfsd-ctl') | ||
const fDaemon = IPFSFactory.create({ type: 'js' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a Factory. I could call it factoryDaemon if that makes more sense for you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it matter that's a factory? If you really want to know, I think it's apparent from |
||
const fInProc = IPFSFactory.create({ type: 'proc' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #1215 (comment). I can use the full word if that works better :) |
||
|
||
// This gets replaced by '../utils/create-repo-browser.js' in the browser | ||
const createTempRepo = require('../utils/create-repo-nodejs.js') | ||
|
@@ -69,7 +68,7 @@ function connectNodes (remoteNode, inProcNode, callback) { | |
let nodes = [] | ||
|
||
function addNode (inProcNode, callback) { | ||
df.spawn({ | ||
fDaemon.spawn({ | ||
exec: './src/cli/bin.js', | ||
config: { | ||
Addresses: { | ||
|
@@ -119,7 +118,7 @@ describe('bitswap', function () { | |
}) | ||
} | ||
|
||
dfProc.spawn({ exec: IPFS, config }, (err, _ipfsd) => { | ||
fInProc.spawn({ exec: IPFS, config: config }, (err, _ipfsd) => { | ||
expect(err).to.not.exist() | ||
nodes.push(_ipfsd) | ||
inProcNode = _ipfsd.api | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This idea comes from type checking discussion. It would be cool if
isValid()
functions would returnfalse
if invalid and return a valid object whatever got checked (in this case aMultiaddr
).That way you could also use
isValid()
to make sure you always get a validMultiaddr
object, no matter if it was already one or of if it was a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This got upgraded in #1227