Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit b2755a0

Browse files
fix: code review
Co-Authored-By: vasco-santos <[email protected]>
1 parent 62f25a6 commit b2755a0

File tree

7 files changed

+18
-23
lines changed

7 files changed

+18
-23
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"datastore-pubsub": "~0.1.1",
9494
"debug": "^4.1.0",
9595
"deep-extend": "~0.6.0",
96+
"dlv": "^1.1.2",
9697
"err-code": "^1.1.2",
9798
"file-type": "^10.2.0",
9899
"fnv1a": "^1.0.1",
@@ -120,7 +121,7 @@
120121
"ipld-ethereum": "^2.0.1",
121122
"ipld-git": "~0.2.2",
122123
"ipld-zcash": "~0.1.6",
123-
"ipns": "~0.4.2",
124+
"ipns": "~0.4.3",
124125
"is-ipfs": "~0.4.7",
125126
"is-pull-stream": "~0.0.0",
126127
"is-stream": "^1.1.0",

src/core/components/name-pubsub.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@ log.error = debug('jsipfs:name-pubsub:error')
1111

1212
// Is pubsub enabled
1313
const isNamePubsubEnabled = (node) => {
14-
let pubsub
1514
try {
16-
pubsub = getPubsubRouting(node)
15+
return Boolean(getPubsubRouting(node))
1716
} catch (err) {
1817
return false
1918
}
20-
21-
return Boolean(pubsub)
2219
}
2320

2421
// Get pubsub from IPNS routing
2522
const getPubsubRouting = (node) => {
2623
if (!node._ipns || !node._options.EXPERIMENTAL.ipnsPubsub) {
2724
const errMsg = 'IPNS pubsub subsystem is not enabled'
2825

29-
log.error(errMsg)
3026
throw errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED')
3127
}
3228

@@ -41,9 +37,9 @@ const getPubsubRouting = (node) => {
4137
if (!pubsub) {
4238
const errMsg = 'IPNS pubsub datastore not found'
4339

44-
log.error(errMsg)
4540
throw errcode(errMsg, 'ERR_PUBSUB_DATASTORE_NOT_FOUND')
4641
}
42+
4743
return pubsub
4844
}
4945

src/core/components/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const series = require('async/series')
44
const Bitswap = require('ipfs-bitswap')
5-
const get = require('lodash/get')
5+
const get = require('dlv')
66
const setImmediate = require('async/setImmediate')
77
const promisify = require('promisify-es6')
88
const { TieredDatastore } = require('datastore-core')

src/core/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const schema = Joi.object().keys({
2929
}).allow(null),
3030
EXPERIMENTAL: Joi.object().keys({
3131
pubsub: Joi.boolean(),
32-
namesysPubsub: Joi.boolean(),
32+
ipnsPubsub: Joi.boolean(),
3333
sharding: Joi.boolean(),
3434
dht: Joi.boolean()
3535
}).allow(null),

src/core/ipns/routing/offline-datastore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { Key } = require('interface-datastore')
4-
const Record = require('libp2p-record').Record
4+
const { Record } = require('libp2p-record')
55
const { encodeBase32 } = require('./utils')
66

77
const errcode = require('err-code')

src/core/ipns/routing/pubsub-datastore.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ const debug = require('debug')
1111
const log = debug('jsipfs:ipns:pubsub')
1212
log.error = debug('jsipfs:ipns:pubsub:error')
1313

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
1815
class IpnsPubsubDatastore {
1916
constructor (pubsub, localDatastore, peerId) {
2017
this._pubsub = pubsub
@@ -68,11 +65,11 @@ class IpnsPubsubDatastore {
6865

6966
this._pubsubDs.get(key, (err, res) => {
7067
// Add topic subscribed
71-
const ns = key.slice(0, ipnsNSLength)
68+
const ns = key.slice(0, ipns.namespaceLength)
7269

73-
if (ns.toString() === ipnsNS) {
70+
if (ns.toString() === ipns.namespace) {
7471
const stringifiedTopic = key.toString()
75-
const id = toB58String(key.slice(ipnsNSLength))
72+
const id = toB58String(key.slice(ipns.namespaceLength))
7673

7774
this._subscriptions[stringifiedTopic] = id
7875

@@ -116,9 +113,9 @@ class IpnsPubsubDatastore {
116113
* @returns {void}
117114
*/
118115
getSubscriptions (callback) {
119-
const subscriptions = Object.values(this._subscriptions)
116+
const subscriptions = Object.values(this._subscriptions).filter(Boolean)
120117

121-
return callback(null, subscriptions.map((sub) => `/ipns/${sub}`))
118+
return callback(null, subscriptions.map((sub) => `${ipns.namespace}${sub}`))
122119
}
123120

124121
/**
@@ -136,8 +133,8 @@ class IpnsPubsubDatastore {
136133
}
137134

138135
// 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)
141138
}
142139

143140
const stringifiedTopic = Object.keys(this._subscriptions).find((key) => this._subscriptions[key] === name)
@@ -158,7 +155,7 @@ class IpnsPubsubDatastore {
158155
return callback(err)
159156
}
160157

161-
delete this._subscriptions[stringifiedTopic]
158+
this._subscriptions[stringifiedTopic] = undefined
162159
log(`unsubscribed pubsub ${stringifiedTopic}: ${name}`)
163160

164161
callback(null, {
@@ -167,5 +164,4 @@ class IpnsPubsubDatastore {
167164
}
168165
}
169166

170-
// exports = module.exports = IpnsPubsubDatastore
171167
exports = module.exports = withIs(IpnsPubsubDatastore, { className: 'IpnsPubsubDatastore', symbolName: '@js-ipfs/ipns/IpnsPubsubDatastore' })

test/core/name-pubsub.js

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ describe('name-pubsub', function () {
6464
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
6565

6666
it('should publish and then resolve correctly', function (done) {
67+
this.timeout(50 * 1000)
68+
6769
nodeB.name.resolve(idA.id, (err) => {
6870
expect(err).to.exist()
6971

0 commit comments

Comments
 (0)