Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/swarm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface SwarmPeersOptions extends HTTPRPCOptions {
streams?: boolean
verbose?: boolean
latency?: boolean
identify?: boolean
}

export interface SwarmPeersResult {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/swarm/peers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
}
Expand Down
38 changes: 36 additions & 2 deletions test/node/swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
})
})
Loading