-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Feature: PubSub 🌟 #644
Feature: PubSub 🌟 #644
Changes from 41 commits
d6c6d12
641fe1c
d3c4f02
9026a88
9b7da97
adeccec
94bbc43
4ee7b5b
5031470
8084cb5
3cdfcb0
3b365f7
faa6e33
6ba150c
7d17a86
ba12388
39db7df
d2e6f6e
deb5fd6
a89301b
9c82402
7f1eca4
4233e53
186c5d2
093897b
d4e9efa
580313a
ca9c0ea
43045f5
467a9a9
a302b25
2d829f8
5a5a810
82aba1e
8d855c2
b32af31
c457bf7
65fe21c
ea458ee
3e29f95
350a854
b5b4739
6465bb5
d19864a
966b597
c206800
249bca1
9371b67
50ae06d
9b1aea7
aa29598
4495b3a
3082c2f
0208504
738c55c
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 'use strict' | ||
|
|
||
| module.exports = { | ||
| command: 'pubsub', | ||
|
|
||
| description: 'pubsub commands', | ||
|
|
||
| builder (yargs) { | ||
| return yargs | ||
| .commandDir('pubsub') | ||
| }, | ||
|
|
||
| handler (argv) {} | ||
| } |
| 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) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| 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) | ||
|
Member
Author
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 above |
||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| '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 | ||
| } | ||
|
|
||
| ipfs.pubsub.publish(argv.topic, argv.data, (err) => { | ||
| if (err) { | ||
| throw err | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| } |
| 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()) | ||
|
Member
Author
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 above |
||
| } | ||
|
|
||
| ipfs.pubsub.subscribe(argv.topic, handler, (err) => { | ||
| if (err) { | ||
| throw err | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| '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()) { | ||
| throw OFFLINE_ERROR | ||
| } | ||
|
|
||
| if (typeof data === 'string') { | ||
| data = new Buffer(data) | ||
| } | ||
|
|
||
| self._pubsub.publish(topic, data) | ||
|
|
||
| setImmediate(callback) | ||
| }), | ||
|
|
||
| ls: promisify((callback) => { | ||
| if (!self.isOnline()) { | ||
| throw OFFLINE_ERROR | ||
| } | ||
|
|
||
| const subscriptions = Array.from( | ||
| self._pubsub.subscriptions | ||
| ) | ||
|
|
||
| setImmediate(() => callback(null, subscriptions)) | ||
| }), | ||
|
|
||
| peers: promisify((topic, callback) => { | ||
| if (!self.isOnline()) { | ||
| throw 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) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use strict' | ||
|
|
||
| const Hoek = require('hoek') | ||
|
|
||
| module.exports = (server) => { | ||
|
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. @dignifiedquire - thanks for adding/overhauling this...I'll add myself a chore to start dropping swapping this in throughout.
Member
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. yeah I need to test this a bit more, but the goal is that
Member
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. which is the same format go-ipfs produces for errors |
||
| server.ext('onRequest', (request, reply) => { | ||
| request.handleError = handleError | ||
| reply.continue() | ||
| }) | ||
|
|
||
| server.ext('onPreResponse', (request, reply) => { | ||
| const res = request.response | ||
| const req = request.raw.req | ||
|
|
||
| let statusCode = 200 | ||
| let msg = 'Sorry, something went wrong, please retrace your steps.' | ||
|
|
||
| if (res.isBoom) { | ||
| statusCode = res.output.payload.statusCode | ||
|
|
||
| if (res.message && res.isDeveloperError) { | ||
| // we caught it! | ||
|
Member
Author
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. \o/ can the comment be more descriptive? Or, do we need the comment?
Member
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. Need to clean up this file and add some better tests for it, this is mostly ported from another module atm, which is why the comments are a bit odd |
||
| msg = res.message.replace('Uncaught error: ', '') | ||
| } | ||
|
|
||
| const debug = { | ||
| method: req.method, | ||
| url: request.url.path, | ||
| headers: request.raw.req.headers, | ||
| info: request.info, | ||
| payload: request.payload, | ||
| response: res.output.payload | ||
| } | ||
| // ALWAYS Log the error | ||
|
Member
Author
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. Tone might sound off |
||
| server.log('error', debug) | ||
|
|
||
| reply({ | ||
| Message: msg, | ||
| Code: 0 | ||
| }).code(statusCode) | ||
| return | ||
| } | ||
|
|
||
| reply.continue() | ||
| }) | ||
| } | ||
|
|
||
| function handleError (error, errorMessage) { | ||
| if (errorMessage) { | ||
| return Hoek.assert(!error, errorMessage) | ||
| } | ||
|
|
||
| return Hoek.assert(!error, error) | ||
| } | ||
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.
Change to the
loggerthing @victorbjelkholm added, so that we save time in refactoring.#495
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.
I can't find the logger, @victorbjelkholm can you remind me where/how to use it?