diff --git a/src/swarm/index.ts b/src/swarm/index.ts index 5d2a79b78..2d1ee04db 100644 --- a/src/swarm/index.ts +++ b/src/swarm/index.ts @@ -45,6 +45,7 @@ export interface SwarmPeersOptions extends HTTPRPCOptions { streams?: boolean verbose?: boolean latency?: boolean + identify?: boolean } export interface SwarmPeersResult { @@ -55,6 +56,13 @@ export interface SwarmPeersResult { streams?: string[] direction?: 'inbound' | 'outbound' error?: Error + identify?: { + ID: string + PublicKey?: string + Addresses?: string[] + AgentVersion?: string + Protocols?: string[] + } } export function createSwarm (client: HTTPRPCClient): SwarmAPI { diff --git a/src/swarm/peers.ts b/src/swarm/peers.ts index cb6e63e80..7a026c954 100644 --- a/src/swarm/peers.ts +++ b/src/swarm/peers.ts @@ -22,7 +22,8 @@ export function createPeers (client: HTTPRPCClient): SwarmAPI['peers'] { latency: peer.Latency, streams: peer.Streams, // eslint-disable-next-line no-nested-ternary - direction: peer.Direction == null ? undefined : peer.Direction === 0 ? 'inbound' : 'outbound' + direction: peer.Direction == null ? undefined : peer.Direction === 0 ? 'inbound' : 'outbound', + identify: peer.Identify } }) } diff --git a/test/node/swarm.ts b/test/node/swarm.ts index e64c4c5ee..d0665c38c 100644 --- a/test/node/swarm.ts +++ b/test/node/swarm.ts @@ -21,7 +21,7 @@ describe.skip('.swarm.peers', function () { const res = await ipfs.swarm.peers() expect(res).to.be.a('array') - expect(res.length).to.equal(1) + expect(res.length).to.be.greaterThan(0) expect(res[0].error).to.not.exist() expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) expect(res[0].peer.toString()).to.equal(response.Peers[0].Peer) @@ -39,7 +39,7 @@ describe.skip('.swarm.peers', function () { const res = await ipfs.swarm.peers() expect(res).to.be.a('array') - expect(res.length).to.equal(1) + expect(res.length).to.be.greaterThan(0) expect(res[0].error).to.not.exist() expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr) expect(res[0].peer.toString()).to.equal(response.Peers[0].Peer) @@ -56,4 +56,38 @@ describe.skip('.swarm.peers', function () { expect(scope.isDone()).to.equal(true) }) + + it('handles a peer response with identify option', async function () { + const response = { + Peers: [{ + Addr: '/ip4/104.131.131.82/tcp/4001', + Peer: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', + Latency: '10ms', + Muxer: '', + Streams: null, + Identify: { + ID: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', + PublicKey: 'CAESIBase64Key...', + Addresses: ['/ip4/104.131.131.82/tcp/4001'], + AgentVersion: 'kubo/0.35.0', + Protocols: ['/ipfs/id/1.0.0', '/ipfs/kad/1.0.0'] + } + }] + } + + const scope = nock(apiUrl) + .post('/api/v0/swarm/peers') + .query({ identify: true }) + .reply(200, response) + + const res = await ipfs.swarm.peers({ identify: true }) + + expect(res).to.be.a('array') + expect(res.length).to.be.greaterThan(0) + expect(res[0].identify).to.exist() + expect(res[0].identify?.AgentVersion).to.equal('kubo/0.35.0') + expect(res[0].identify?.ID).to.equal(response.Peers[0].Identify.ID) + expect(res[0].identify?.Protocols).to.deep.equal(response.Peers[0].Identify.Protocols) + expect(scope.isDone()).to.equal(true) + }) })