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

Commit

Permalink
fix: make ipfs.ping() options optional (#1627)
Browse files Browse the repository at this point in the history
Without this PR:

```console
$ npm test

...*snip*

     Uncaught TypeError: Cannot read property 'push' of null
      at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/collect.js:7:9
      at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/reduce.js:8:11
      at /Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-stream/sinks/drain.js:24:37
      at callback (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-pushable/index.js:84:5)
      at Function.push (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/pull-pushable/index.js:44:7)
      at libp2pNode.ping (src/core/components/ping-pull-stream.js:79:18)
      at _getPeerInfo (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/libp2p/src/index.js:339:7)
      at setImmediate (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/libp2p/src/get-peer-info.js:54:24)
      at Immediate.<anonymous> (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/async/internal/setImmediate.js:27:16)
```

Might resolve the second issue in #1616
  • Loading branch information
achingbrain authored and Alan Shaw committed Oct 16, 2018
1 parent a622668 commit 08f06b6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/core/components/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const pull = require('pull-stream/pull')

module.exports = function ping (self) {
return promisify((peerId, opts, cb) => {
if (typeof opts === 'function') {
cb = opts
opts = {}
}

pull(
self.pingPullStream(peerId, opts),
pull.collect(cb)
Expand Down
64 changes: 62 additions & 2 deletions test/core/ping.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const isNode = require('detect-node')
const expect = chai.expect
chai.use(dirtyChai)
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
const dfProc = DaemonFactory.create({
exec: require('../../'),
type: 'proc'
})

const config = {
Bootstrap: [],
Expand All @@ -24,9 +28,10 @@ const config = {
}
}

function spawnNode ({ dht = false }, cb) {
function spawnNode ({ dht = false, type = 'js' }, cb) {
const args = dht ? ['--enable-dht-experiment'] : []
df.spawn({
const factory = type === 'js' ? df : dfProc
factory.spawn({
args,
config,
initOptions: { bits: 512 }
Expand All @@ -43,6 +48,61 @@ describe('ping', function () {

if (!isNode) return

describe('in-process daemon', function () {
let ipfsdA
let ipfsdB
let bMultiaddr
let ipfsdBId

// Spawn nodes
before(function (done) {
this.timeout(60 * 1000)

series([
spawnNode.bind(null, { dht: false, type: 'proc' }),
spawnNode.bind(null, { dht: false })
], (err, ipfsd) => {
expect(err).to.not.exist()
ipfsdA = ipfsd[0]
ipfsdB = ipfsd[1]
done()
})
})

// Get the peer info object
before(async function () {
this.timeout(60 * 1000)

const peerInfo = await ipfsdB.api.id()

ipfsdBId = peerInfo.id
bMultiaddr = peerInfo.addresses[0]
})

// Connect the nodes
before(async function () {
this.timeout(60 * 1000)
await ipfsdA.api.swarm.connect(bMultiaddr)
})

after(async () => {
if (!ipfsdA) return
await ipfsdA.stop()
})

after(async () => {
if (!ipfsdB) return
await ipfsdB.stop()
})

it('can ping via a promise without options', async () => {
const res = await ipfsdA.api.ping(ipfsdBId)

expect(res.length).to.be.ok()
expect(res[0].success).to.be.true()
})
})

describe('DHT disabled', function () {
// Without DHT nodes need to be previously connected
let ipfsdA
Expand Down

0 comments on commit 08f06b6

Please sign in to comment.