Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
feat: add protocol support to identify (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun authored Mar 20, 2019
1 parent f3ee2f0 commit 89ed15e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ module.exports = (conn, expectedPeerInfo, callback) => {
return callback(err)
}

// Copy the protocols
peerInfo.protocols = new Set(input.protocols)

callback(null, peerInfo, observedAddr)
})
})
Expand Down
3 changes: 2 additions & 1 deletion src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = (conn, pInfoSelf) => {
agentVersion: 'na',
publicKey: publicKey,
listenAddrs: pInfoSelf.multiaddrs.toArray().map((ma) => ma.buffer),
observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from('')
observedAddr: observedAddrs ? observedAddrs.buffer : Buffer.from(''),
protocols: Array.from(pInfoSelf.protocols)
})

pull(
Expand Down
44 changes: 43 additions & 1 deletion test/dialer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ describe('identify.dialer', () => {
it('works', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))
original.protocols.add('/echo/1.0.0')
original.protocols.add('/ping/1.0.0')

const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: Array.from(original.protocols)
})

pull(
Expand All @@ -58,6 +62,44 @@ describe('identify.dialer', () => {
expect(observedAddrs)
.to.eql([multiaddr('/ip4/127.0.0.1/tcp/5001')])

expect(info.protocols).to.eql(original.protocols)

done()
})
})

it('should handle missing protocols', (done) => {
const p = pair()
original.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/5002'))

const input = msg.encode({
protocolVersion: 'ipfs/0.1.0',
agentVersion: 'na',
publicKey: original.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: Array.from(original.protocols)
})

pull(
values([input]),
lp.encode(),
p[0]
)

identify.dialer(p[1], (err, info, observedAddrs) => {
expect(err).to.not.exist()
expect(info.id.pubKey.bytes)
.to.eql(original.id.pubKey.bytes)

expect(info.multiaddrs.toArray())
.to.eql(original.multiaddrs.toArray())

expect(observedAddrs)
.to.eql([multiaddr('/ip4/127.0.0.1/tcp/5001')])

expect(Array.from(info.protocols)).to.eql([])

done()
})
})
Expand Down
5 changes: 4 additions & 1 deletion test/listener.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe('identify.listener', () => {
return done(err)
}

_info.protocols.add('/echo/1.0.0')
_info.protocols.add('/chat/1.0.0')

info = _info
done()
})
Expand All @@ -51,7 +54,7 @@ describe('identify.listener', () => {
publicKey: info.id.pubKey.bytes,
listenAddrs: [multiaddr('/ip4/127.0.0.1/tcp/5002').buffer],
observedAddr: multiaddr('/ip4/127.0.0.1/tcp/5001').buffer,
protocols: []
protocols: ['/echo/1.0.0', '/chat/1.0.0']
})
done()
})
Expand Down

0 comments on commit 89ed15e

Please sign in to comment.