|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug') |
| 4 | +const errcode = require('err-code') |
| 5 | +const promisify = require('promisify-es6') |
| 6 | + |
| 7 | +const log = debug('jsipfs:name-pubsub') |
| 8 | +log.error = debug('jsipfs:name-pubsub:error') |
| 9 | + |
| 10 | +const isNamePubsubEnabled = (node) => ( |
| 11 | + node._options.EXPERIMENTAL.ipnsPubsub && node._libp2pNode._floodSub |
| 12 | +) |
| 13 | + |
| 14 | +module.exports = function namePubsub (self) { |
| 15 | + return { |
| 16 | + /** |
| 17 | + * Query the state of IPNS pubsub. |
| 18 | + * |
| 19 | + * @returns {Promise|void} |
| 20 | + */ |
| 21 | + state: promisify((callback) => { |
| 22 | + callback(null, { |
| 23 | + enabled: Boolean(isNamePubsubEnabled(self)) |
| 24 | + }) |
| 25 | + }), |
| 26 | + /** |
| 27 | + * Cancel a name subscription. |
| 28 | + * |
| 29 | + * @param {String} name subscription name. |
| 30 | + * @param {function(Error)} [callback] |
| 31 | + * @returns {Promise|void} |
| 32 | + */ |
| 33 | + cancel: promisify((name, callback) => { |
| 34 | + if (!isNamePubsubEnabled(self)) { |
| 35 | + const errMsg = 'IPNS pubsub subsystem is not enabled' |
| 36 | + |
| 37 | + log.error(errMsg) |
| 38 | + return callback(errcode(errMsg, 'ERR_IPNS_PS_NOT_ENABLED')) |
| 39 | + } |
| 40 | + |
| 41 | + self._ipns.pubsub.cancel(name, callback) |
| 42 | + }), |
| 43 | + /** |
| 44 | + * Show current name subscriptions. |
| 45 | + * |
| 46 | + * @param {function(Error)} [callback] |
| 47 | + * @returns {Promise|void} |
| 48 | + */ |
| 49 | + subs: promisify((callback) => { |
| 50 | + if (!isNamePubsubEnabled(self)) { |
| 51 | + const errMsg = 'IPNS pubsub subsystem is not enabled' |
| 52 | + |
| 53 | + log.error(errMsg) |
| 54 | + return callback(errcode(errMsg, 'ERR_IPNS_PS_NOT_ENABLED')) |
| 55 | + } |
| 56 | + |
| 57 | + self._ipns.pubsub.getSubscriptions(callback) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
0 commit comments