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 pathpubsub-datastore.js
146 lines (117 loc) · 4.22 KB
/
pubsub-datastore.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const ipns = require('ipns')
const { fromB58String, toB58String } = require('multihashes')
const PubsubDatastore = require('datastore-pubsub')
const withIs = require('class-is')
const errcode = require('err-code')
const debug = require('debug')
const log = debug('jsipfs:ipns:pubsub')
log.error = debug('jsipfs:ipns:pubsub:error')
// Pubsub datastore aims to manage the pubsub subscriptions for IPNS
class IpnsPubsubDatastore {
constructor (pubsub, localDatastore, peerId) {
this._pubsub = pubsub
this._subscriptions = {}
// Bind _handleSubscriptionKey function, which is called by PubsubDatastore.
this._handleSubscriptionKey = this._handleSubscriptionKey.bind(this)
this._pubsubDs = new PubsubDatastore(pubsub, localDatastore, peerId, ipns.validator, this._handleSubscriptionKey)
}
/**
* Put a value to the pubsub datastore indexed by the received key properly encoded.
* @param {Buffer} key identifier of the value.
* @param {Buffer} value value to be stored.
* @param {function(Error)} callback
* @returns {void}
*/
put (key, value, callback) {
this._pubsubDs.put(key, value, callback)
}
/**
* Get a value from the pubsub datastore indexed by the received key properly encoded.
* Moreover, the identifier topic is subscribed and the pubsub datastore records will be
* updated once new publishes occur.
* @param {Buffer} key identifier of the value to be obtained.
* @param {function(Error, Buffer)} callback
* @returns {void}
*/
get (key, callback) {
this._pubsubDs.get(key, (err, res) => {
// Add topic subscribed
const ns = key.slice(0, ipns.namespaceLength)
if (ns.toString() === ipns.namespace) {
const stringifiedTopic = key.toString()
const id = toB58String(key.slice(ipns.namespaceLength))
this._subscriptions[stringifiedTopic] = id
log(`subscribed pubsub ${stringifiedTopic}: ${id}`)
}
// If no data was obtained, after storing the subscription, return the error.
if (err) {
return callback(err)
}
callback(null, res)
})
}
// Modify subscription key to have a proper encoding
_handleSubscriptionKey (key, callback) {
const subscriber = this._subscriptions[key]
if (!subscriber) {
const errMsg = `key ${key} does not correspond to a subscription`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
}
let keys
try {
keys = ipns.getIdKeys(fromB58String(subscriber))
} catch (err) {
log.error(err)
return callback(err)
}
callback(null, keys.routingKey.toBuffer())
}
/**
* Get pubsub subscriptions related to ipns.
* @param {function(Error, Object)} callback
* @returns {void}
*/
getSubscriptions (callback) {
const subscriptions = Object.values(this._subscriptions).filter(Boolean)
return callback(null, subscriptions.map((sub) => `${ipns.namespace}${sub}`))
}
/**
* Cancel pubsub subscriptions related to ipns.
* @param {String} name ipns path to cancel the pubsub subscription.
* @param {function(Error, Object)} callback
* @returns {void}
*/
cancel (name, callback) {
if (typeof name !== 'string') {
const errMsg = `received subscription name is not valid`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_SUBSCRIPTION_NAME'))
}
// Trim /ipns/ prefix from the name
if (name.startsWith(ipns.namespace)) {
name = name.substring(ipns.namespaceLength)
}
const stringifiedTopic = Object.keys(this._subscriptions).find((key) => this._subscriptions[key] === name)
// Not found topic
if (!stringifiedTopic) {
return callback(null, {
canceled: false
})
}
// Unsubscribe topic
try {
const bufTopic = Buffer.from(stringifiedTopic)
this._pubsubDs.unsubscribe(bufTopic)
} catch (err) {
return callback(err)
}
this._subscriptions[stringifiedTopic] = undefined
log(`unsubscribed pubsub ${stringifiedTopic}: ${name}`)
callback(null, {
canceled: true
})
}
}
exports = module.exports = withIs(IpnsPubsubDatastore, { className: 'IpnsPubsubDatastore', symbolName: '@js-ipfs/ipns/IpnsPubsubDatastore' })