This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pubsub
- Loading branch information
Showing
24 changed files
with
804 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
command: 'pubsub', | ||
|
||
description: 'pubsub commands', | ||
|
||
builder (yargs) { | ||
return yargs | ||
.commandDir('pubsub') | ||
}, | ||
|
||
handler (argv) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pubsub') | ||
log.error = debug('cli:pubsub:error') | ||
|
||
module.exports = { | ||
command: 'ls', | ||
|
||
describe: 'Get your list of subscriptions', | ||
|
||
builder: {}, | ||
|
||
handler (argv) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
ipfs.pubsub.ls((err, subscriptions) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
subscriptions.forEach((sub) => { | ||
console.log(sub) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pubsub') | ||
log.error = debug('cli:pubsub:error') | ||
|
||
module.exports = { | ||
command: 'peers <topic>', | ||
|
||
describe: 'Get all peers subscribed to a topic', | ||
|
||
builder: {}, | ||
|
||
handler (argv) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
ipfs.pubsub.peers(argv.topic, (err, peers) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
peers.forEach((peer) => { | ||
console.log(peer) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pubsub') | ||
log.error = debug('cli:pubsub:error') | ||
|
||
module.exports = { | ||
command: 'pub <topic> <data>', | ||
|
||
describe: 'Publish data to a topic', | ||
|
||
builder: {}, | ||
|
||
handler (argv) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
const data = new Buffer(String(argv.data)) | ||
|
||
ipfs.pubsub.publish(argv.topic, data, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const debug = require('debug') | ||
const log = debug('cli:pubsub') | ||
log.error = debug('cli:pubsub:error') | ||
|
||
module.exports = { | ||
command: 'sub <topic>', | ||
|
||
describe: 'Subscribe to a topic', | ||
|
||
builder: {}, | ||
|
||
handler (argv) { | ||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
const handler = (msg) => { | ||
console.log(msg.data.toString()) | ||
} | ||
|
||
ipfs.pubsub.subscribe(argv.topic, handler, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
'use strict' | ||
|
||
module.exports = function goOffline (self) { | ||
return (cb) => { | ||
module.exports = (self) => { | ||
return (callback) => { | ||
self._blockService.goOffline() | ||
self._bitswap.stop() | ||
self.libp2p.stop(cb) | ||
self._pubsub.stop((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
self.libp2p.stop(callback) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
'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 OFFLINE_ERROR | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = handler | ||
handler = options | ||
options = {} | ||
} | ||
|
||
if (!callback) { | ||
return new Promise((resolve, reject) => { | ||
subscribe(topic, options, handler, (err) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
subscribe(topic, options, handler, callback) | ||
}, | ||
|
||
unsubscribe: (topic, handler) => { | ||
const ps = self._pubsub | ||
|
||
ps.removeListener(topic, handler) | ||
|
||
if (ps.listenerCount(topic) === 0) { | ||
ps.unsubscribe(topic) | ||
} | ||
}, | ||
|
||
publish: promisify((topic, data, callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(OFFLINE_ERROR)) | ||
} | ||
|
||
if (!Buffer.isBuffer(data)) { | ||
return setImmediate(() => callback(new Error('data must be a Buffer'))) | ||
} | ||
|
||
self._pubsub.publish(topic, data) | ||
setImmediate(() => callback()) | ||
}), | ||
|
||
ls: promisify((callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(OFFLINE_ERROR)) | ||
} | ||
|
||
const subscriptions = Array.from( | ||
self._pubsub.subscriptions | ||
) | ||
|
||
setImmediate(() => callback(null, subscriptions)) | ||
}), | ||
|
||
peers: promisify((topic, callback) => { | ||
if (!self.isOnline()) { | ||
return setImmediate(() => callback(OFFLINE_ERROR)) | ||
} | ||
|
||
const peers = Array.from(self._pubsub.peers.values()) | ||
.filter((peer) => peer.topics.has(topic)) | ||
.map((peer) => peer.info.id.toB58String()) | ||
|
||
setImmediate(() => callback(null, peers)) | ||
}), | ||
|
||
setMaxListeners (n) { | ||
return self._pubsub.setMaxListeners(n) | ||
} | ||
} | ||
|
||
function subscribe (topic, options, handler, callback) { | ||
const ps = self._pubsub | ||
|
||
if (ps.listenerCount(topic) === 0) { | ||
ps.subscribe(topic) | ||
} | ||
|
||
ps.on(topic, handler) | ||
setImmediate(() => callback()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.