@@ -11,10 +11,7 @@ const debug = require('debug')
11
11
const log = debug ( 'jsipfs:ipns:pubsub' )
12
12
log . error = debug ( 'jsipfs:ipns:pubsub:error' )
13
13
14
- const ipnsNS = '/ipns/'
15
- const ipnsNSLength = ipnsNS . length
16
-
17
- // Pubsub aims to manage the pubsub subscriptions for IPNS
14
+ // Pubsub datastore aims to manage the pubsub subscriptions for IPNS
18
15
class IpnsPubsubDatastore {
19
16
constructor ( pubsub , localDatastore , peerId ) {
20
17
this . _pubsub = pubsub
@@ -68,11 +65,11 @@ class IpnsPubsubDatastore {
68
65
69
66
this . _pubsubDs . get ( key , ( err , res ) => {
70
67
// Add topic subscribed
71
- const ns = key . slice ( 0 , ipnsNSLength )
68
+ const ns = key . slice ( 0 , ipns . namespaceLength )
72
69
73
- if ( ns . toString ( ) === ipnsNS ) {
70
+ if ( ns . toString ( ) === ipns . namespace ) {
74
71
const stringifiedTopic = key . toString ( )
75
- const id = toB58String ( key . slice ( ipnsNSLength ) )
72
+ const id = toB58String ( key . slice ( ipns . namespaceLength ) )
76
73
77
74
this . _subscriptions [ stringifiedTopic ] = id
78
75
@@ -116,9 +113,9 @@ class IpnsPubsubDatastore {
116
113
* @returns {void }
117
114
*/
118
115
getSubscriptions ( callback ) {
119
- const subscriptions = Object . values ( this . _subscriptions )
116
+ const subscriptions = Object . values ( this . _subscriptions ) . filter ( Boolean )
120
117
121
- return callback ( null , subscriptions . map ( ( sub ) => `/ ipns/ ${ sub } ` ) )
118
+ return callback ( null , subscriptions . map ( ( sub ) => `${ ipns . namespace } ${ sub } ` ) )
122
119
}
123
120
124
121
/**
@@ -136,8 +133,8 @@ class IpnsPubsubDatastore {
136
133
}
137
134
138
135
// Trim /ipns/ prefix from the name
139
- if ( name . startsWith ( ipnsNS ) ) {
140
- name = name . substring ( ipnsNSLength )
136
+ if ( name . startsWith ( ipns . namespace ) ) {
137
+ name = name . substring ( ipns . namespaceLength )
141
138
}
142
139
143
140
const stringifiedTopic = Object . keys ( this . _subscriptions ) . find ( ( key ) => this . _subscriptions [ key ] === name )
@@ -158,7 +155,7 @@ class IpnsPubsubDatastore {
158
155
return callback ( err )
159
156
}
160
157
161
- delete this . _subscriptions [ stringifiedTopic ]
158
+ this . _subscriptions [ stringifiedTopic ] = undefined
162
159
log ( `unsubscribed pubsub ${ stringifiedTopic } : ${ name } ` )
163
160
164
161
callback ( null , {
@@ -167,5 +164,4 @@ class IpnsPubsubDatastore {
167
164
}
168
165
}
169
166
170
- // exports = module.exports = IpnsPubsubDatastore
171
167
exports = module . exports = withIs ( IpnsPubsubDatastore , { className : 'IpnsPubsubDatastore' , symbolName : '@js-ipfs/ipns/IpnsPubsubDatastore' } )
0 commit comments