Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: dirkmc <[email protected]>
Co-Authored-By: Jacob Heun <[email protected]>
  • Loading branch information
3 people committed Nov 22, 2019
1 parent 89f85bf commit a10506e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"abort-controller": "^3.0.0",
"async": "^2.6.2",
"base32.js": "~0.1.0",
"chai-checkmark": "^1.0.1",
"cids": "~0.7.1",
"debug": "^4.1.1",
"err-code": "^2.0.0",
Expand Down Expand Up @@ -72,6 +71,7 @@
"aegir": "^20.4.1",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"datastore-level": "~0.12.1",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class KadDHT extends EventEmitter {
randomWalk = {}
}) {
super()
assert(dialer, 'libp2p-kad-dht requires a instance of Dialer')
assert(dialer, 'libp2p-kad-dht requires an instance of Dialer')

/**
* Local reference to the libp2p dialer instance
Expand All @@ -82,7 +82,7 @@ class KadDHT extends EventEmitter {
this.peerInfo = peerInfo

/**
* Local peer info
* Local PeerStore
* @type {PeerStore}
*/
this.peerStore = peerStore
Expand Down
21 changes: 10 additions & 11 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Network {
return
}

// TODO remove: add a way to check if switch has started or not
if (!this.dht.isStarted) {
throw errcode(new Error('Can not start network'), 'ERR_CANNOT_START_NETWORK')
}
Expand All @@ -52,7 +51,7 @@ class Network {

// register protocol with topology
const topology = new MulticodecTopology({
multicodecs: c.PROTOCOL_DHT,
multicodecs: [c.PROTOCOL_DHT],
handlers: {
onConnect: this._onPeerConnected,
onDisconnect: () => {}
Expand Down Expand Up @@ -152,40 +151,40 @@ class Network {
* If no response is received after the specified timeout
* this will error out.
*
* @param {Connection} conn - the connection to use
* @param {DuplexIterable} stream - the stream to use
* @param {Buffer} msg - the message to send
* @returns {Promise<Message>}
* @private
*/
async _writeReadMessage (conn, msg) { // eslint-disable-line require-await
async _writeReadMessage (stream, msg) { // eslint-disable-line require-await
return pTimeout(
writeReadMessage(conn, msg),
writeReadMessage(stream, msg),
this.readMessageTimeout
)
}

/**
* Write a message to the given connection.
* Write a message to the given stream.
*
* @param {Connection} conn - the connection to use
* @param {DuplexIterable} stream - the stream to use
* @param {Buffer} msg - the message to send
* @returns {Promise<void>}
* @private
*/
_writeMessage (conn, msg) {
_writeMessage (stream, msg) {
return pipe(
[msg],
lp.encode(),
conn
stream
)
}
}

async function writeReadMessage (conn, msg) {
async function writeReadMessage (stream, msg) {
const res = await pipe(
[msg],
lp.encode(),
conn,
stream,
utils.itFilter(
(msg) => msg.length < c.maxMessageSize
),
Expand Down
11 changes: 4 additions & 7 deletions src/rpc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ module.exports = (dht) => {
*
* @private
*/
async function handleMessage (peer, msg) { // eslint-disable-line
// get handler & exectue it
async function handleMessage (peer, msg) {
// get handler & execute it
const handler = getMessageHandler(msg.type)

try {
await dht._add(peer)
} catch (err) {
log.error('Failed to update the kbucket store')
log.error(err)
log.error('Failed to update the kbucket store', err)
}

if (!handler) {
Expand All @@ -44,14 +43,12 @@ module.exports = (dht) => {
/**
* Handle incoming streams on the dht protocol.
* @param {Object} props
* @param {string} props.protocol
* @param {DuplexStream} props.stream
* @param {Connection} props.connection connection
* @returns {Promise<void>}
*/
return async function onIncomingStream ({ protocol, stream, connection }) {
return async function onIncomingStream ({ stream, connection }) {
const peerInfo = await PeerInfo.create(connection.remotePeer)
peerInfo.protocols.add(protocol)

try {
await dht._add(peerInfo)
Expand Down
5 changes: 1 addition & 4 deletions test/query/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ describe('Query', () => {
before('create a dht', () => {
const peerStore = new PeerBook()
dht = new DHT({
dialer: {
_peerInfo: ourPeerInfo,
_peerBook: peerStore
},
dialer: {},
peerStore,
peerInfo: ourPeerInfo
})
Expand Down
4 changes: 2 additions & 2 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const delay = require('delay')
const pRetry = require('p-retry')
const pTimeout = require('p-timeout')
const DuplexPair = require('it-pair/duplex')
const duplexPair = require('it-pair/duplex')

const { sortClosestPeers } = require('../../src/utils')

Expand Down Expand Up @@ -35,7 +35,7 @@ const createMockRegistrar = (registrarRecord) => ({
exports.createMockRegistrar = createMockRegistrar

const ConnectionPair = () => {
const [d0, d1] = DuplexPair()
const [d0, d1] = duplexPair()

return [
{
Expand Down

0 comments on commit a10506e

Please sign in to comment.