|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const promisify = require('promisify-es6') |
| 4 | +const EventEmitter = require('events') |
| 5 | +const eos = require('end-of-stream') |
| 6 | +const isNode = require('detect-node') |
| 7 | +const PubsubMessageStream = require('../pubsub-message-stream') |
| 8 | +const stringlistToArray = require('../stringlist-to-array') |
| 9 | + |
| 10 | +const NotSupportedError = () => new Error('pubsub is currently not supported when run in the browser') |
| 11 | + |
| 12 | +/* Public API */ |
| 13 | +module.exports = (send) => { |
| 14 | + /* Internal subscriptions state and functions */ |
| 15 | + const ps = new EventEmitter() |
| 16 | + const subscriptions = {} |
| 17 | + ps.id = Math.random() |
| 18 | + return { |
| 19 | + subscribe: (topic, options, handler, callback) => { |
| 20 | + const defaultOptions = { |
| 21 | + discover: false |
| 22 | + } |
| 23 | + |
| 24 | + if (typeof options === 'function') { |
| 25 | + callback = handler |
| 26 | + handler = options |
| 27 | + options = defaultOptions |
| 28 | + } |
| 29 | + |
| 30 | + if (!options) { |
| 31 | + options = defaultOptions |
| 32 | + } |
| 33 | + |
| 34 | + // Throw an error if ran in the browsers |
| 35 | + if (!isNode) { |
| 36 | + if (!callback) { |
| 37 | + return Promise.reject(NotSupportedError()) |
| 38 | + } |
| 39 | + return callback(NotSupportedError()) |
| 40 | + } |
| 41 | + |
| 42 | + // promisify doesn't work as we always pass a |
| 43 | + // function as last argument (`handler`) |
| 44 | + if (!callback) { |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + subscribe(topic, options, handler, (err) => { |
| 47 | + if (err) { |
| 48 | + return reject(err) |
| 49 | + } |
| 50 | + resolve() |
| 51 | + }) |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + subscribe(topic, options, handler, callback) |
| 56 | + }, |
| 57 | + unsubscribe: (topic, handler) => { |
| 58 | + if (!isNode) { |
| 59 | + throw NotSupportedError() |
| 60 | + } |
| 61 | + |
| 62 | + if (ps.listenerCount(topic) === 0 || !subscriptions[topic]) { |
| 63 | + throw new Error(`Not subscribed to '${topic}'`) |
| 64 | + } |
| 65 | + |
| 66 | + ps.removeListener(topic, handler) |
| 67 | + |
| 68 | + // Drop the request once we are actualy done |
| 69 | + if (ps.listenerCount(topic) === 0) { |
| 70 | + subscriptions[topic].abort() |
| 71 | + subscriptions[topic] = null |
| 72 | + } |
| 73 | + }, |
| 74 | + publish: promisify((topic, data, callback) => { |
| 75 | + if (!isNode) { |
| 76 | + return callback(NotSupportedError()) |
| 77 | + } |
| 78 | + |
| 79 | + if (!Buffer.isBuffer(data)) { |
| 80 | + return callback(new Error('data must be a Buffer')) |
| 81 | + } |
| 82 | + |
| 83 | + const request = { |
| 84 | + path: 'pubsub/pub', |
| 85 | + args: [topic, data] |
| 86 | + } |
| 87 | + |
| 88 | + send(request, callback) |
| 89 | + }), |
| 90 | + ls: promisify((callback) => { |
| 91 | + if (!isNode) { |
| 92 | + return callback(NotSupportedError()) |
| 93 | + } |
| 94 | + |
| 95 | + const request = { |
| 96 | + path: 'pubsub/ls' |
| 97 | + } |
| 98 | + |
| 99 | + send.andTransform(request, stringlistToArray, callback) |
| 100 | + }), |
| 101 | + peers: promisify((topic, callback) => { |
| 102 | + if (!isNode) { |
| 103 | + return callback(NotSupportedError()) |
| 104 | + } |
| 105 | + |
| 106 | + const request = { |
| 107 | + path: 'pubsub/peers', |
| 108 | + args: [topic] |
| 109 | + } |
| 110 | + |
| 111 | + send.andTransform(request, stringlistToArray, callback) |
| 112 | + }), |
| 113 | + setMaxListeners (n) { |
| 114 | + return ps.setMaxListeners(n) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + function subscribe (topic, options, handler, callback) { |
| 119 | + ps.on(topic, handler) |
| 120 | + if (subscriptions[topic]) { |
| 121 | + return callback() |
| 122 | + } |
| 123 | + |
| 124 | + // Request params |
| 125 | + const request = { |
| 126 | + path: 'pubsub/sub', |
| 127 | + args: [topic], |
| 128 | + qs: { |
| 129 | + discover: options.discover |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Start the request and transform the response |
| 134 | + // stream to Pubsub messages stream |
| 135 | + subscriptions[topic] = send.andTransform(request, PubsubMessageStream.from, (err, stream) => { |
| 136 | + if (err) { |
| 137 | + subscriptions[topic] = null |
| 138 | + ps.removeListener(topic, handler) |
| 139 | + return callback(err) |
| 140 | + } |
| 141 | + |
| 142 | + stream.on('data', (msg) => { |
| 143 | + ps.emit(topic, msg) |
| 144 | + }) |
| 145 | + |
| 146 | + stream.on('error', (err) => { |
| 147 | + ps.emit('error', err) |
| 148 | + }) |
| 149 | + |
| 150 | + eos(stream, (err) => { |
| 151 | + if (err) { |
| 152 | + ps.emit('error', err) |
| 153 | + } |
| 154 | + |
| 155 | + subscriptions[topic] = null |
| 156 | + ps.removeListener(topic, handler) |
| 157 | + }) |
| 158 | + |
| 159 | + callback() |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments