Socket
Socket
Sign inDemoInstall

libp2p-kad-dht

Package Overview
Dependencies
Maintainers
2
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libp2p-kad-dht - npm Package Compare versions

Comparing version 0.11.1 to 0.12.0

5

CHANGELOG.md

@@ -0,1 +1,6 @@

<a name="0.12.0"></a>
# [0.12.0](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.11.1...v0.12.0) (2018-11-22)
<a name="0.11.1"></a>

@@ -2,0 +7,0 @@ ## [0.11.1](https://github.com/libp2p/js-libp2p-kad-dht/compare/v0.11.0...v0.11.1) (2018-11-12)

3

package.json
{
"name": "libp2p-kad-dht",
"version": "0.11.1",
"version": "0.12.0",
"description": "JavaScript implementation of the Kad-DHT for libp2p",

@@ -44,2 +44,3 @@ "leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",

"debug": "^3.1.0",
"err-code": "^1.1.2",
"hashlru": "^2.2.1",

@@ -46,0 +47,0 @@ "heap": "~0.2.6",

@@ -12,2 +12,4 @@ 'use strict'

const errcode = require('err-code')
const RoutingTable = require('./routing')

@@ -18,3 +20,2 @@ const utils = require('./utils')

const Network = require('./network')
const errors = require('./errors')
const privateApi = require('./private')

@@ -271,4 +272,6 @@ const Providers = require('./providers')

if (rtp.length === 0) {
this._log.error('No peers from routing table!')
return cb(new Error('Failed to lookup key'))
const errMsg = 'Failed to lookup key! No peers from routing table!'
this._log.error(errMsg)
return cb(errcode(new Error(errMsg), 'ERR_NO_PEERS_IN_ROUTING_TABLE'))
}

@@ -281,3 +284,3 @@

// If we have an invalid record we just want to continue and fetch a new one.
if (!(err instanceof errors.InvalidRecordError)) {
if (!(err.code === 'ERR_INVALID_RECORD')) {
return cb(err)

@@ -289,4 +292,3 @@ }

if ((rec && rec.value) ||
err instanceof errors.InvalidRecordError) {
if ((rec && rec.value) || (err && err.code === 'ERR_INVALID_RECORD')) {
vals.push({

@@ -533,3 +535,3 @@ val: rec && rec.value,

if (peers.length === 0) {
return cb(new errors.LookupFailureError())
return cb(errcode(new Error('Peer lookup failed'), 'ERR_LOOKUP_FAILED'))
}

@@ -573,3 +575,3 @@

if (!result.peer) {
return cb(new errors.NotFoundError())
return cb(errcode(new Error('No peer found'), 'ERR_NOT_FOUND'))
}

@@ -576,0 +578,0 @@ cb(null, result.peer)

@@ -8,2 +8,4 @@ 'use strict'

const errcode = require('err-code')
const rpc = require('./rpc')

@@ -41,4 +43,4 @@ const c = require('./constants')

if (this.isOnline) {
return cb(new Error('Network is already running'))
if (this._running) {
return cb(errcode(new Error('Network is already running'), 'ERR_NETWORK_ALREADY_RUNNING'))
}

@@ -48,3 +50,3 @@

if (!this.dht.isStarted) {
return cb(new Error('Can not start network'))
return cb(errcode(new Error('Can not start network'), 'ERR_CANNOT_START_NETWORK'))
}

@@ -73,3 +75,3 @@

if (!this.dht.isStarted && !this.isStarted) {
return cb(new Error('Network is already stopped'))
return cb(errcode(new Error('Network is already stopped'), 'ERR_NETWORK_ALREADY_STOPPED'))
}

@@ -143,3 +145,3 @@ this._running = false

if (!this.isConnected) {
return callback(new Error('Network is offline'))
return callback(errcode(new Error('Network is offline'), 'ERR_NETWORK_OFFLINE'))
}

@@ -167,3 +169,3 @@

if (!this.isConnected) {
return setImmediate(() => callback(new Error('Network is offline')))
return setImmediate(() => callback(errcode(new Error('Network is offline'), 'ERR_NETWORK_OFFLINE')))
}

@@ -231,3 +233,3 @@

if (res.length === 0) {
return callback(new Error('No message received'))
return callback(errcode(new Error('No message received'), 'ERR_NO_MESSAGE_RECEIVED'))
}

@@ -239,3 +241,3 @@

} catch (err) {
return callback(new Error('failed to deserialize response: ' + err.message))
return callback(errcode(err, 'ERR_FAILED_DESERIALIZE_RESPONSE'))
}

@@ -242,0 +244,0 @@

@@ -10,4 +10,5 @@ 'use strict'

const errcode = require('err-code')
const utils = require('./utils')
const errors = require('./errors')
const Message = require('./message')

@@ -122,3 +123,3 @@ const c = require('./constants')

if (!record) {
return callback(new Error('Invalid record'))
return callback(errcode(new Error('Invalid record'), 'ERR_INVALID_RECORD'))
}

@@ -241,3 +242,3 @@

if (!resp.record.value.equals(Record.deserialize(rec).value)) {
return callback(new Error('value not put correctly'))
return callback(errcode(new Error('value not put correctly'), 'ERR_PUT_VALUE_INVALID'))
}

@@ -292,3 +293,3 @@

if (!best) {
return cb(new errors.NotFoundError())
return cb(errcode(new Error('best value was not found'), 'ERR_NOT_FOUND'))
}

@@ -385,4 +386,6 @@

if (err) {
dht._log('invalid record received, discarded')
return cb(new errors.InvalidRecordError())
const errMsg = 'invalid record received, discarded'
dht._log(errMsg)
return cb(errcode(new Error(errMsg), 'ERR_INVALID_RECORD'))
}

@@ -398,3 +401,3 @@

cb(new errors.NotFoundError('Not found'))
cb(errcode(new Error('Not found'), 'ERR_NOT_FOUND'))
}

@@ -445,3 +448,3 @@ ], callback)

if (!msg.record || !msg.record.value) {
return cb(new Error('Node not responding with its public key: ' + peer.toB58String()))
return cb(errcode(new Error(`Node not responding with its public key: ${peer.toB58String()}`), 'ERR_INVALID_RECORD'))
}

@@ -454,3 +457,3 @@

if (!recPeer.isEqual(peer)) {
return cb(new Error('public key does not match id'))
return cb(errcode(new Error('public key does not match id'), 'ERR_PUBLIC_KEY_DOES_NOT_MATCH_ID'))
}

@@ -457,0 +460,0 @@

@@ -10,5 +10,6 @@ 'use strict'

const assert = require('assert')
const errors = require('./errors')
const c = require('./constants')
const errcode = require('err-code')
class RandomWalk {

@@ -135,3 +136,3 @@ constructor (kadDHT) {

this._kadDHT.findPeer(id, (err, peer) => {
if (err instanceof errors.NotFoundError) {
if (err.code === 'ERR_NOT_FOUND') {
// expected case, we asked for random stuff after all

@@ -146,3 +147,3 @@ return callback()

// wait what, there was something found? Lucky day!
callback(new Error(`random-walk: ACTUALLY FOUND PEER: ${peer}, ${id.toB58String()}`))
callback(errcode(new Error(`random-walk: ACTUALLY FOUND PEER: ${peer}, ${id.toB58String()}`), 'ERR_FOUND_RANDOM_PEER'))
})

@@ -149,0 +150,0 @@ }

'use strict'
const CID = require('cids')
const utils = require('../../utils')
const errcode = require('err-code')

@@ -21,3 +21,3 @@ module.exports = (dht) => {

if (!msg.key || msg.key.length === 0) {
return callback(new Error('Missing key'))
return callback(errcode(new Error('Missing key'), 'ERR_MISSING_KEY'))
}

@@ -29,3 +29,5 @@

} catch (err) {
return callback(new Error('Invalid CID: ' + err.message))
const errMsg = `Invalid CID: ${err.message}`
return callback(errcode(new Error(errMsg), 'ERR_INVALID_CID'))
}

@@ -32,0 +34,0 @@

@@ -7,2 +7,4 @@ 'use strict'

const errcode = require('err-code')
const Message = require('../../message')

@@ -27,3 +29,3 @@ const utils = require('../../utils')

} catch (err) {
return callback(new Error('Invalid CID: ' + err.message))
return callback(errcode(new Error(`Invalid CID: ${err.message}`), 'ERR_INVALID_CID'))
}

@@ -30,0 +32,0 @@

@@ -6,2 +6,4 @@ 'use strict'

const errcode = require('err-code')
const Message = require('../../message')

@@ -27,3 +29,3 @@ const utils = require('../../utils')

if (!key || key.length === 0) {
return callback(new Error('Invalid key'))
return callback(errcode(new Error('Invalid key'), 'ERR_INVALID_KEY'))
}

@@ -30,0 +32,0 @@

'use strict'
const utils = require('../../utils')
const errcode = require('err-code')

@@ -23,4 +24,6 @@ module.exports = (dht) => {

if (!record) {
log.error('Got empty record from: %s', peer.id.toB58String())
return callback(new Error('Empty record'))
const errMsg = `Empty record from: ${peer.id.toB58String()}`
log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_EMPTY_RECORD'))
}

@@ -27,0 +30,0 @@

@@ -23,2 +23,4 @@ /* eslint-env mocha */

const errcode = require('err-code')
const KadDHT = require('../src')

@@ -74,3 +76,4 @@ const kadUtils = require('../src/utils')

dhts.forEach((dht) => {
dht.randomWalk._walk(3, 10000, () => {}) // don't need to know when it finishes
// dht.randomWalk._walk(3, 10000, () => {}) // don't need to know when it finishes
dht.randomWalk.start(1, 1000) // don't need to know when it finishes
})

@@ -203,2 +206,33 @@ }

it('should fail to start when already started', function (done) {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()
const dht = new KadDHT(sw, { enabledDiscovery: false })
series([
(cb) => dht.start(cb),
(cb) => dht.start(cb)
], (err) => {
expect(err).to.exist()
done()
})
})
it('should fail to stop when was not started', function (done) {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()
const dht = new KadDHT(sw, { enabledDiscovery: false })
series([
(cb) => dht.stop(cb)
], (err) => {
expect(err).to.exist()
done()
})
})
it('put - get', function (done) {

@@ -435,3 +469,3 @@ this.timeout(10 * 1000)

it.skip('findPeer', function (done) {
it('findPeer', function (done) {
this.timeout(40 * 1000)

@@ -489,4 +523,3 @@

// TODO fix this
it.skip('find peer query', function (done) {
it('find peer query', function (done) {
this.timeout(40 * 1000)

@@ -755,2 +788,107 @@

})
describe('errors', () => {
it('get many should fail if only has one peer', function (done) {
this.timeout(20 * 1000)
const nDHTs = 1
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
dhts[0].getMany('/v/hello', 5, (err) => {
expect(err).to.exist()
expect(err.code).to.be.eql('ERR_NO_PEERS_IN_ROUTING_TABLE')
tdht.teardown(done)
})
})
})
it('get should handle correctly an unexpected error', function (done) {
this.timeout(20 * 1000)
const errCode = 'ERR_INVALID_RECORD_FAKE'
const error = errcode(new Error('fake error'), errCode)
const nDHTs = 2
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]
const stub = sinon.stub(dhtA, '_getValueOrPeers').callsArgWithAsync(2, error)
waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.get(Buffer.from('/v/hello'), { maxTimeout: 1000 }, cb)
], (err) => {
expect(err).to.exist()
expect(err.code).to.be.eql(errCode)
stub.restore()
tdht.teardown(done)
})
})
})
it('get should handle correctly an invalid record error and return not found', function (done) {
this.timeout(20 * 1000)
const error = errcode(new Error('invalid record error'), 'ERR_INVALID_RECORD')
const nDHTs = 2
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]
const stub = sinon.stub(dhtA, '_getValueOrPeers').callsArgWithAsync(2, error)
waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.get(Buffer.from('/v/hello'), cb)
], (err) => {
expect(err).to.exist()
expect(err.code).to.be.eql('ERR_NOT_FOUND')
stub.restore()
tdht.teardown(done)
})
})
})
it('findPeer should fail if no closest peers available', function (done) {
this.timeout(40 * 1000)
const nDHTs = 4
const tdht = new TestDHT()
tdht.spawn(nDHTs, (err, dhts) => {
expect(err).to.not.exist()
const ids = dhts.map((d) => d.peerInfo.id)
waterfall([
(cb) => connect(dhts[0], dhts[1], cb),
(cb) => connect(dhts[1], dhts[2], cb),
(cb) => connect(dhts[2], dhts[3], cb)
], (err) => {
expect(err).to.not.exist()
const stub = sinon.stub(dhts[0].routingTable, 'closestPeers').returns([])
dhts[0].findPeer(ids[3], { maxTimeout: 1000 }, (err) => {
expect(err).to.exist()
expect(err.code).to.eql('ERR_LOOKUP_FAILED')
stub.restore()
tdht.teardown(done)
})
})
})
})
})
})

@@ -54,15 +54,15 @@ /* eslint-env mocha */

message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0),
error: /Missing key/
error: 'ERR_MISSING_KEY'
}, {
message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.alloc(0), 0),
error: /Missing key/
error: 'ERR_MISSING_KEY'
}, {
message: new Message(Message.TYPES.ADD_PROVIDER, Buffer.from('hello world'), 0),
error: /Invalid CID/
error: 'ERR_INVALID_CID'
}]
tests.forEach((t) => it(t.error.toString(), (done) => {
handler(dht)(peers[0], t.message, (err, res) => {
handler(dht)(peers[0], t.message, (err) => {
expect(err).to.exist()
expect(err.message).to.match(t.error)
expect(err.code).to.eql(t.error)
done()

@@ -69,0 +69,0 @@ })

@@ -56,3 +56,3 @@ /* eslint-env mocha */

handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Invalid CID/)
expect(err.code).to.eql('ERR_INVALID_CID')
expect(response).to.not.exist()

@@ -59,0 +59,0 @@ done()

@@ -49,3 +49,3 @@ /* eslint-env mocha */

handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Invalid key/)
expect(err.code).to.eql('ERR_INVALID_KEY')
expect(response).to.not.exist()

@@ -52,0 +52,0 @@ done()

@@ -49,4 +49,4 @@ /* eslint-env mocha */

const msg = new Message(T, Buffer.from('hello'), 5)
handler(dht)(peers[0], msg, (err, response) => {
expect(err).to.match(/Empty record/)
handler(dht)(peers[0], msg, (err) => {
expect(err.code).to.eql('ERR_EMPTY_RECORD')
done()

@@ -53,0 +53,0 @@ })

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc