This repository was 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
/
Copy pathname-pubsub.js
92 lines (78 loc) · 2.09 KB
/
name-pubsub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const debug = require('debug')
const errcode = require('err-code')
const promisify = require('promisify-es6')
const IpnsPubsubDatastore = require('../ipns/routing/pubsub-datastore')
const log = debug('jsipfs:name-pubsub')
log.error = debug('jsipfs:name-pubsub:error')
// Is pubsub enabled
const isNamePubsubEnabled = (node) => {
try {
return Boolean(getPubsubRouting(node))
} catch (err) {
return false
}
}
// Get pubsub from IPNS routing
const getPubsubRouting = (node) => {
if (!node._ipns || !node._options.EXPERIMENTAL.ipnsPubsub) {
const errMsg = 'IPNS pubsub subsystem is not enabled'
throw errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED')
}
// Only one store and it is pubsub
if (IpnsPubsubDatastore.isIpnsPubsubDatastore(node._ipns.routing)) {
return node._ipns.routing
}
// Find in tiered
const pubsub = (node._ipns.routing.stores || []).find(s => IpnsPubsubDatastore.isIpnsPubsubDatastore(s))
if (!pubsub) {
const errMsg = 'IPNS pubsub datastore not found'
throw errcode(errMsg, 'ERR_PUBSUB_DATASTORE_NOT_FOUND')
}
return pubsub
}
module.exports = function namePubsub (self) {
return {
/**
* Query the state of IPNS pubsub.
*
* @returns {Promise|void}
*/
state: promisify((callback) => {
callback(null, {
enabled: isNamePubsubEnabled(self)
})
}),
/**
* Cancel a name subscription.
*
* @param {String} name subscription name.
* @param {function(Error)} [callback]
* @returns {Promise|void}
*/
cancel: promisify((name, callback) => {
let pubsub
try {
pubsub = getPubsubRouting(self)
} catch (err) {
return callback(err)
}
pubsub.cancel(name, callback)
}),
/**
* Show current name subscriptions.
*
* @param {function(Error)} [callback]
* @returns {Promise|void}
*/
subs: promisify((callback) => {
let pubsub
try {
pubsub = getPubsubRouting(self)
} catch (err) {
return callback(err)
}
pubsub.getSubscriptions(callback)
})
}
}