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

Commit fff1287

Browse files
committed
fix: make ipfs.ping() options optional
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
1 parent c26255d commit fff1287

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/core/components/ping.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const pull = require('pull-stream/pull')
55

66
module.exports = function ping (self) {
77
return promisify((peerId, opts, cb) => {
8+
if (typeof opts === 'function') {
9+
cb = opts
10+
opts = {}
11+
}
12+
813
pull(
914
self.pingPullStream(peerId, opts),
1015
pull.collect(cb)

test/core/ping.spec.js

+62-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const isNode = require('detect-node')
1313
const expect = chai.expect
1414
chai.use(dirtyChai)
1515
const df = DaemonFactory.create({ exec: 'src/cli/bin.js' })
16+
const dfProc = DaemonFactory.create({
17+
exec: require('../../'),
18+
type: 'proc'
19+
})
1620

1721
const config = {
1822
Bootstrap: [],
@@ -24,9 +28,10 @@ const config = {
2428
}
2529
}
2630

27-
function spawnNode ({ dht = false }, cb) {
31+
function spawnNode ({ dht = false, type = 'js' }, cb) {
2832
const args = dht ? ['--enable-dht-experiment'] : []
29-
df.spawn({
33+
const factory = type === 'js' ? df : dfProc
34+
factory.spawn({
3035
args,
3136
config,
3237
initOptions: { bits: 512 }
@@ -43,6 +48,61 @@ describe('ping', function () {
4348

4449
if (!isNode) return
4550

51+
describe('in-process daemon', function () {
52+
let ipfsdA
53+
let ipfsdB
54+
let bMultiaddr
55+
let ipfsdBId
56+
57+
// Spawn nodes
58+
before(function (done) {
59+
this.timeout(60 * 1000)
60+
61+
series([
62+
spawnNode.bind(null, { dht: false, type: 'proc' }),
63+
spawnNode.bind(null, { dht: false })
64+
], (err, ipfsd) => {
65+
expect(err).to.not.exist()
66+
ipfsdA = ipfsd[0]
67+
ipfsdB = ipfsd[1]
68+
done()
69+
})
70+
})
71+
72+
// Get the peer info object
73+
before(async function () {
74+
this.timeout(60 * 1000)
75+
76+
const peerInfo = await ipfsdB.api.id()
77+
78+
ipfsdBId = peerInfo.id
79+
bMultiaddr = peerInfo.addresses[0]
80+
})
81+
82+
// Connect the nodes
83+
before(async function () {
84+
this.timeout(60 * 1000)
85+
await ipfsdA.api.swarm.connect(bMultiaddr)
86+
})
87+
88+
after(async () => {
89+
if (!ipfsdA) return
90+
await ipfsdA.stop()
91+
})
92+
93+
after(async () => {
94+
if (!ipfsdB) return
95+
await ipfsdB.stop()
96+
})
97+
98+
it('can ping via a promise without options', async () => {
99+
const res = await ipfsdA.api.ping(ipfsdBId)
100+
101+
expect(res.length).to.be.ok()
102+
expect(res[0].success).to.be.true()
103+
})
104+
})
105+
46106
describe('DHT disabled', function () {
47107
// Without DHT nodes need to be previously connected
48108
let ipfsdA

0 commit comments

Comments
 (0)