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

Commit

Permalink
fix: code review
Browse files Browse the repository at this point in the history
Co-Authored-By: vasco-santos <vasco.santos@ua.pt>
  • Loading branch information
hugomrdias and vasco-santos committed Nov 30, 2018
1 parent 7f07aa2 commit 6f83aa1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 23 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"datastore-pubsub": "~0.1.1",
"debug": "^4.1.0",
"deep-extend": "~0.6.0",
"dlv": "^1.1.2",
"err-code": "^1.1.2",
"file-type": "^10.2.0",
"fnv1a": "^1.0.1",
Expand Down Expand Up @@ -120,7 +121,7 @@
"ipld-ethereum": "^2.0.1",
"ipld-git": "~0.2.2",
"ipld-zcash": "~0.1.6",
"ipns": "~0.4.2",
"ipns": "~0.4.3",
"is-ipfs": "~0.4.7",
"is-pull-stream": "~0.0.0",
"is-stream": "^1.1.0",
Expand Down
8 changes: 2 additions & 6 deletions src/core/components/name-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ log.error = debug('jsipfs:name-pubsub:error')

// Is pubsub enabled
const isNamePubsubEnabled = (node) => {
let pubsub
try {
pubsub = getPubsubRouting(node)
return Boolean(getPubsubRouting(node))
} catch (err) {
return false
}

return Boolean(pubsub)
}

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

log.error(errMsg)
throw errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED')
}

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

log.error(errMsg)
throw errcode(errMsg, 'ERR_PUBSUB_DATASTORE_NOT_FOUND')
}

return pubsub
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const get = require('lodash/get')
const get = require('dlv')
const setImmediate = require('async/setImmediate')
const promisify = require('promisify-es6')
const { TieredDatastore } = require('datastore-core')
Expand Down
2 changes: 1 addition & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const schema = Joi.object().keys({
}).allow(null),
EXPERIMENTAL: Joi.object().keys({
pubsub: Joi.boolean(),
namesysPubsub: Joi.boolean(),
ipnsPubsub: Joi.boolean(),
sharding: Joi.boolean(),
dht: Joi.boolean()
}).allow(null),
Expand Down
2 changes: 1 addition & 1 deletion src/core/ipns/routing/offline-datastore.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { Key } = require('interface-datastore')
const Record = require('libp2p-record').Record
const { Record } = require('libp2p-record')
const { encodeBase32 } = require('./utils')

const errcode = require('err-code')
Expand Down
22 changes: 9 additions & 13 deletions src/core/ipns/routing/pubsub-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const debug = require('debug')
const log = debug('jsipfs:ipns:pubsub')
log.error = debug('jsipfs:ipns:pubsub:error')

const ipnsNS = '/ipns/'
const ipnsNSLength = ipnsNS.length

// Pubsub aims to manage the pubsub subscriptions for IPNS
// Pubsub datastore aims to manage the pubsub subscriptions for IPNS
class IpnsPubsubDatastore {
constructor (pubsub, localDatastore, peerId) {
this._pubsub = pubsub
Expand Down Expand Up @@ -68,11 +65,11 @@ class IpnsPubsubDatastore {

this._pubsubDs.get(key, (err, res) => {
// Add topic subscribed
const ns = key.slice(0, ipnsNSLength)
const ns = key.slice(0, ipns.namespaceLength)

if (ns.toString() === ipnsNS) {
if (ns.toString() === ipns.namespace) {
const stringifiedTopic = key.toString()
const id = toB58String(key.slice(ipnsNSLength))
const id = toB58String(key.slice(ipns.namespaceLength))

this._subscriptions[stringifiedTopic] = id

Expand Down Expand Up @@ -116,9 +113,9 @@ class IpnsPubsubDatastore {
* @returns {void}
*/
getSubscriptions (callback) {
const subscriptions = Object.values(this._subscriptions)
const subscriptions = Object.values(this._subscriptions).filter(Boolean)

return callback(null, subscriptions.map((sub) => `/ipns/${sub}`))
return callback(null, subscriptions.map((sub) => `${ipns.namespace}${sub}`))
}

/**
Expand All @@ -136,8 +133,8 @@ class IpnsPubsubDatastore {
}

// Trim /ipns/ prefix from the name
if (name.startsWith(ipnsNS)) {
name = name.substring(ipnsNSLength)
if (name.startsWith(ipns.namespace)) {
name = name.substring(ipns.namespaceLength)
}

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

delete this._subscriptions[stringifiedTopic]
this._subscriptions[stringifiedTopic] = undefined
log(`unsubscribed pubsub ${stringifiedTopic}: ${name}`)

callback(null, {
Expand All @@ -167,5 +164,4 @@ class IpnsPubsubDatastore {
}
}

// exports = module.exports = IpnsPubsubDatastore
exports = module.exports = withIs(IpnsPubsubDatastore, { className: 'IpnsPubsubDatastore', symbolName: '@js-ipfs/ipns/IpnsPubsubDatastore' })
2 changes: 2 additions & 0 deletions test/core/name-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ describe('name-pubsub', function () {
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))

it('should publish and then resolve correctly', function (done) {
this.timeout(50 * 1000)

nodeB.name.resolve(idA.id, (err) => {
expect(err).to.exist()

Expand Down

0 comments on commit 6f83aa1

Please sign in to comment.