This repository has been 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
/
pubsub-datastore.js
147 lines (117 loc) · 4.1 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
147
'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('ipfs:ipns:pubsub')
log.error = debug('ipfs: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}
*/
async put (key, value) { // eslint-disable-line require-await
return this._pubsubDs.put(key, value)
}
/**
* Get a value from the pubsub datastore indexed by the received key properly encoded.
* Also, the identifier topic is subscribed to 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}
*/
async get (key) {
let res
let err
try {
res = await this._pubsubDs.get(key)
} catch (e) {
err = e
}
// Add topic subscribed
const ns = key.slice(0, ipns.namespaceLength)
if (ns.toString() === ipns.namespace) {
const stringifiedTopic = toB58String(key)
const id = toB58String(key.slice(ipns.namespaceLength))
this._subscriptions[stringifiedTopic] = id
log(`subscribed to pubsub topic ${stringifiedTopic}, id ${id}`)
}
// If no data was obtained, after storing the subscription, return the error.
if (err) {
throw err
}
return res
}
// Modify subscription key to have a proper encoding
_handleSubscriptionKey (key) {
if (Buffer.isBuffer(key)) {
key = toB58String(key)
}
const subscriber = this._subscriptions[key]
if (!subscriber) {
throw errcode(new Error(`key ${key} does not correspond to a subscription`), 'ERR_INVALID_KEY')
}
let keys
try {
keys = ipns.getIdKeys(fromB58String(subscriber))
} catch (err) {
log.error(err)
throw err
}
return keys.routingKey.toBuffer()
}
/**
* Get pubsub subscriptions related to ipns.
* @param {function(Error, Object)} callback
* @returns {Array<Object>}
*/
getSubscriptions () {
const subscriptions = Object.values(this._subscriptions).filter(Boolean)
return 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}
*/
async cancel (name) { // eslint-disable-line require-await
if (typeof name !== 'string') {
throw errcode(new Error('invalid subscription name'), '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 {
canceled: false
}
}
// Unsubscribe topic
const bufTopic = Buffer.from(stringifiedTopic)
this._pubsubDs.unsubscribe(bufTopic)
this._subscriptions[stringifiedTopic] = undefined
log(`unsubscribed pubsub ${stringifiedTopic}: ${name}`)
return {
canceled: true
}
}
}
exports = module.exports = withIs(IpnsPubsubDatastore, { className: 'IpnsPubsubDatastore', symbolName: '@js-ipfs/ipns/IpnsPubsubDatastore' })