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

Commit

Permalink
fix: second pass of code review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 5, 2018
1 parent e3201da commit 1ba697e
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 106 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"dependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"async": "^2.6.1",
"base32.js": "~0.1.0",
"big.js": "^5.2.2",
"binary-querystring": "~0.1.2",
"bl": "^2.1.2",
Expand All @@ -86,6 +87,8 @@
"byteman": "^1.3.5",
"cid-tool": "~0.1.0",
"cids": "~0.5.5",
"class-is": "^1.1.0",
"datastore-core": "~0.4.0",
"datastore-pubsub": "~0.0.2",
"debug": "^4.1.0",
"err-code": "^1.1.2",
Expand Down
68 changes: 51 additions & 17 deletions src/core/components/name-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,48 @@ const debug = require('debug')
const errcode = require('err-code')
const promisify = require('promisify-es6')

const IpnsPubsubDatastore = require('../ipns/routing/pubsub-datastore')

const log = debug('jsipfs:name-pubsub')
log.error = debug('jsipfs:name-pubsub:error')

const isNamePubsubEnabled = (node) => (
Boolean(node._options.EXPERIMENTAL.ipnsPubsub &&
node._libp2pNode &&
node._libp2pNode._floodSub)
)
// Is pubsub enabled
const isNamePubsubEnabled = (node) => {
let pubsub
try {
pubsub = 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')
}

// Only one store and it is pubsub
if (IpnsPubsubDatastore.isIpnsPubsubDatastore(node._ipns.routing)) {
return node._ipns.routing
}

// Find in tiered
const pubsub = (node._ipns.routing.stores || []).find(s => IpnsPubsubDatastore.isIpnsPubsubDatastore(s))

if (!pubsub) {
const errMsg = 'IPNS pubsub datastore not found'

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

module.exports = function namePubsub (self) {
return {
Expand All @@ -33,14 +67,14 @@ module.exports = function namePubsub (self) {
* @returns {Promise|void}
*/
cancel: promisify((name, callback) => {
if (!isNamePubsubEnabled(self)) {
const errMsg = 'IPNS pubsub subsystem is not enabled'

log.error(errMsg)
return callback(errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED'))
let pubsub
try {
pubsub = getPubsubRouting(self)
} catch (err) {
return callback(err)
}

self._ipns.pubsub.cancel(name, callback)
pubsub.cancel(name, callback)
}),
/**
* Show current name subscriptions.
Expand All @@ -49,14 +83,14 @@ module.exports = function namePubsub (self) {
* @returns {Promise|void}
*/
subs: promisify((callback) => {
if (!isNamePubsubEnabled(self)) {
const errMsg = 'IPNS pubsub subsystem is not enabled'

log.error(errMsg)
return callback(errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED'))
let pubsub
try {
pubsub = getPubsubRouting(self)
} catch (err) {
return callback(err)
}

self._ipns.pubsub.getSubscriptions(callback)
pubsub.getSubscriptions(callback)
})
}
}
11 changes: 11 additions & 0 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const waterfall = require('async/waterfall')
const Keychain = require('libp2p-keychain')
const extend = require('deep-extend')
const NoKeychain = require('./no-keychain')

const IPNS = require('../ipns')
const OfflineDatastore = require('../ipns/routing/offline-datastore')

/*
* Load stuff from Repo into memory
*/
Expand Down Expand Up @@ -95,6 +99,13 @@ module.exports = function preStart (self) {

cb()
},
// Setup offline routing for IPNS.
(cb) => {
const offlineDatastore = new OfflineDatastore(self._repo)

self._ipns = new IPNS(offlineDatastore, self)
cb()
},
(cb) => self.pin._load(cb)
], callback)
}
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 @@ -60,7 +60,7 @@ module.exports = (self) => {
ipnsStores.push(offlineDatastore)

const routing = new TieredDatastore(ipnsStores)
self._ipns = new IPNS(routing, self, pubsubDs)
self._ipns = new IPNS(routing, self)

self._bitswap = new Bitswap(
self._libp2pNode,
Expand Down
6 changes: 3 additions & 3 deletions src/core/ipns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const path = require('./path')
const defaultRecordTtl = 60 * 1000

class IPNS {
constructor (routing, ipfs, pubsub) {
constructor (routing, ipfs) {
this.publisher = new IpnsPublisher(routing, ipfs._repo)
this.republisher = new IpnsRepublisher(this.publisher, ipfs)
this.resolver = new IpnsResolver(routing, ipfs._repo)
this.resolver = new IpnsResolver(routing)
this.cache = new Receptacle({ max: 1000 }) // Create an LRU cache with max 1000 items
this.pubsub = pubsub
this.routing = routing
}

// Publish
Expand Down
8 changes: 1 addition & 7 deletions src/core/ipns/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ const resolvePath = (ipfsNode, name, callback) => {
if (isIPFS.ipnsPath(name)) {
log(`resolve ipns path ${name}`)

const local = ipfsNode._options.local

const options = {
local: local
}

return ipfsNode._ipns.resolve(name, options, callback)
return ipfsNode._ipns.resolve(name, callback)
}

// ipfs path
Expand Down
3 changes: 1 addition & 2 deletions src/core/ipns/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ log.error = debug('jsipfs:ipns:resolver:error')
const defaultMaximumRecursiveDepth = 32

class IpnsResolver {
constructor (routing, repo) {
constructor (routing) {
this._routing = routing
this._repo = repo
}

resolve (name, options, callback) {
Expand Down
5 changes: 0 additions & 5 deletions src/core/ipns/routing/offline-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
const { Key } = require('interface-datastore')
const { encodeBase32 } = require('./utils')

const errcode = require('err-code')
const debug = require('debug')
const log = debug('jsipfs:ipns:offline-datastore')
log.error = debug('jsipfs:ipns:offline-datastore:error')

// Offline datastore aims to mimic the same encoding as routing when storing records
// to the local datastore
class OfflineDatastore {
Expand Down
6 changes: 4 additions & 2 deletions src/core/ipns/routing/pubsub-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const ipns = require('ipns')
const { fromB58String, toB58String } = require('multihashes')
const PeerId = require('peer-id')
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')
Expand Down Expand Up @@ -167,4 +168,5 @@ class IpnsPubsubDatastore {
}
}

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

0 comments on commit 1ba697e

Please sign in to comment.