|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const promisify = require('promisify-es6') |
| 4 | +const Readable = require('stream').Readable |
| 5 | +const _values = require('lodash.values') |
| 6 | + |
| 7 | +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR |
| 8 | + |
| 9 | +let subscriptions = {} |
| 10 | + |
| 11 | +const addSubscription = (topic, request, stream) => { |
| 12 | + subscriptions[topic] = { request: request, stream: stream } |
| 13 | +} |
| 14 | + |
| 15 | +const removeSubscription = promisify((topic, callback) => { |
| 16 | + if (!subscriptions[topic]) { |
| 17 | + return callback(new Error(`Not subscribed to ${topic}`)) |
| 18 | + } |
| 19 | + |
| 20 | + subscriptions[topic].stream.emit('end') |
| 21 | + delete subscriptions[topic] |
| 22 | + |
| 23 | + if (callback) { |
| 24 | + callback(null) |
| 25 | + } |
| 26 | +}) |
| 27 | + |
| 28 | +module.exports = function pubsub (self) { |
| 29 | + return { |
| 30 | + subscribe: promisify((topic, options, callback) => { |
| 31 | + if (!self.isOnline()) { |
| 32 | + throw OFFLINE_ERROR |
| 33 | + } |
| 34 | + |
| 35 | + if (typeof options === 'function') { |
| 36 | + callback = options |
| 37 | + options = {} |
| 38 | + } |
| 39 | + |
| 40 | + if (subscriptions[topic]) { |
| 41 | + return callback(`Error: Already subscribed to '${topic}'`) |
| 42 | + } |
| 43 | + |
| 44 | + const stream = new Readable({ objectMode: true }) |
| 45 | + |
| 46 | + stream._read = () => {} |
| 47 | + |
| 48 | + // There is no explicit unsubscribe; subscriptions have a cancel event |
| 49 | + stream.cancel = promisify((cb) => { |
| 50 | + self._pubsub.unsubscribe(topic) |
| 51 | + removeSubscription(topic, cb) |
| 52 | + }) |
| 53 | + |
| 54 | + self._pubsub.on(topic, (data) => { |
| 55 | + stream.emit('data', { |
| 56 | + data: data.toString(), |
| 57 | + topicIDs: [topic] |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + try { |
| 62 | + self._pubsub.subscribe(topic) |
| 63 | + } catch (err) { |
| 64 | + return callback(err) |
| 65 | + } |
| 66 | + |
| 67 | + // Add the request to the active subscriptions and return the stream |
| 68 | + addSubscription(topic, null, stream) |
| 69 | + callback(null, stream) |
| 70 | + }), |
| 71 | + |
| 72 | + publish: promisify((topic, data, callback) => { |
| 73 | + if (!self.isOnline()) { |
| 74 | + throw OFFLINE_ERROR |
| 75 | + } |
| 76 | + |
| 77 | + const buf = Buffer.isBuffer(data) ? data : new Buffer(data) |
| 78 | + |
| 79 | + try { |
| 80 | + self._pubsub.publish(topic, buf) |
| 81 | + } catch (err) { |
| 82 | + return callback(err) |
| 83 | + } |
| 84 | + |
| 85 | + callback(null) |
| 86 | + }), |
| 87 | + |
| 88 | + ls: promisify((callback) => { |
| 89 | + if (!self.isOnline()) { |
| 90 | + throw OFFLINE_ERROR |
| 91 | + } |
| 92 | + |
| 93 | + let subscriptions = [] |
| 94 | + |
| 95 | + try { |
| 96 | + subscriptions = self._pubsub.getSubscriptions() |
| 97 | + } catch (err) { |
| 98 | + return callback(err) |
| 99 | + } |
| 100 | + |
| 101 | + callback(null, subscriptions) |
| 102 | + }), |
| 103 | + |
| 104 | + peers: promisify((topic, callback) => { |
| 105 | + if (!self.isOnline()) { |
| 106 | + throw OFFLINE_ERROR |
| 107 | + } |
| 108 | + |
| 109 | + if (!subscriptions[topic]) { |
| 110 | + return callback(`Error: Not subscribed to '${topic}'`) |
| 111 | + } |
| 112 | + |
| 113 | + let peers = [] |
| 114 | + |
| 115 | + try { |
| 116 | + const peerSet = self._pubsub.getPeerSet() |
| 117 | + _values(peerSet).forEach((peer) => { |
| 118 | + const idB58Str = peer.peerInfo.id.toB58String() |
| 119 | + const index = peer.topics.indexOf(topic) |
| 120 | + if (index > -1) { |
| 121 | + peers.push(idB58Str) |
| 122 | + } |
| 123 | + }) |
| 124 | + } catch (err) { |
| 125 | + return callback(err) |
| 126 | + } |
| 127 | + |
| 128 | + callback(null, peers) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
0 commit comments