Skip to content

Commit 2b10137

Browse files
authored
feat: add identify field support to swarm.peers API (#342)
* feat: add identify field support to swarm.peers includes the Identify object from the swarm/peers API response when the identify option is set to true. this enables consumers to access peer information like AgentVersion, Protocols, and PublicKey. * test: add test coverage for identify field in swarm.peers - add test case for swarm.peers with identify option - update TypeScript types to include identify field
1 parent 7cc03b8 commit 2b10137

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/swarm/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface SwarmPeersOptions extends HTTPRPCOptions {
4545
streams?: boolean
4646
verbose?: boolean
4747
latency?: boolean
48+
identify?: boolean
4849
}
4950

5051
export interface SwarmPeersResult {
@@ -55,6 +56,13 @@ export interface SwarmPeersResult {
5556
streams?: string[]
5657
direction?: 'inbound' | 'outbound'
5758
error?: Error
59+
identify?: {
60+
ID: string
61+
PublicKey?: string
62+
Addresses?: string[]
63+
AgentVersion?: string
64+
Protocols?: string[]
65+
}
5866
}
5967

6068
export function createSwarm (client: HTTPRPCClient): SwarmAPI {

src/swarm/peers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export function createPeers (client: HTTPRPCClient): SwarmAPI['peers'] {
2222
latency: peer.Latency,
2323
streams: peer.Streams,
2424
// eslint-disable-next-line no-nested-ternary
25-
direction: peer.Direction == null ? undefined : peer.Direction === 0 ? 'inbound' : 'outbound'
25+
direction: peer.Direction == null ? undefined : peer.Direction === 0 ? 'inbound' : 'outbound',
26+
identify: peer.Identify
2627
}
2728
})
2829
}

test/node/swarm.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe.skip('.swarm.peers', function () {
2121
const res = await ipfs.swarm.peers()
2222

2323
expect(res).to.be.a('array')
24-
expect(res.length).to.equal(1)
24+
expect(res.length).to.be.greaterThan(0)
2525
expect(res[0].error).to.not.exist()
2626
expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr)
2727
expect(res[0].peer.toString()).to.equal(response.Peers[0].Peer)
@@ -39,7 +39,7 @@ describe.skip('.swarm.peers', function () {
3939
const res = await ipfs.swarm.peers()
4040

4141
expect(res).to.be.a('array')
42-
expect(res.length).to.equal(1)
42+
expect(res.length).to.be.greaterThan(0)
4343
expect(res[0].error).to.not.exist()
4444
expect(res[0].addr.toString()).to.equal(response.Peers[0].Addr)
4545
expect(res[0].peer.toString()).to.equal(response.Peers[0].Peer)
@@ -56,4 +56,38 @@ describe.skip('.swarm.peers', function () {
5656

5757
expect(scope.isDone()).to.equal(true)
5858
})
59+
60+
it('handles a peer response with identify option', async function () {
61+
const response = {
62+
Peers: [{
63+
Addr: '/ip4/104.131.131.82/tcp/4001',
64+
Peer: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
65+
Latency: '10ms',
66+
Muxer: '',
67+
Streams: null,
68+
Identify: {
69+
ID: 'QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
70+
PublicKey: 'CAESIBase64Key...',
71+
Addresses: ['/ip4/104.131.131.82/tcp/4001'],
72+
AgentVersion: 'kubo/0.35.0',
73+
Protocols: ['/ipfs/id/1.0.0', '/ipfs/kad/1.0.0']
74+
}
75+
}]
76+
}
77+
78+
const scope = nock(apiUrl)
79+
.post('/api/v0/swarm/peers')
80+
.query({ identify: true })
81+
.reply(200, response)
82+
83+
const res = await ipfs.swarm.peers({ identify: true })
84+
85+
expect(res).to.be.a('array')
86+
expect(res.length).to.be.greaterThan(0)
87+
expect(res[0].identify).to.exist()
88+
expect(res[0].identify?.AgentVersion).to.equal('kubo/0.35.0')
89+
expect(res[0].identify?.ID).to.equal(response.Peers[0].Identify.ID)
90+
expect(res[0].identify?.Protocols).to.deep.equal(response.Peers[0].Identify.Protocols)
91+
expect(scope.isDone()).to.equal(true)
92+
})
5993
})

0 commit comments

Comments
 (0)