Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit a88483c

Browse files
achingbrainjacobheun
authored andcommittedJan 17, 2020
fix: do not emit empty peer info objects (#85)
I noticed stack traces in the logs from trying to access properties of `undefined`. It seems this module emits `undefined` `peerInfo` values, for example when recieving a response to your own mDNS query or a response with an invalid PeerID. This PR guards on empty PeerInfo objects from `query.gotResponse`
1 parent de2c897 commit a88483c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
 

‎src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class MulticastDNS extends EventEmitter {
5656
async _onMdnsResponse (event) {
5757
try {
5858
const foundPeer = await query.gotResponse(event, this.peerInfo, this.serviceTag)
59-
this.emit('peer', foundPeer)
59+
60+
if (foundPeer) {
61+
this.emit('peer', foundPeer)
62+
}
6063
} catch (err) {
6164
log('Error processing peer response', err)
6265
}

‎test/multicast-dns.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,35 @@ describe('MulticastDNS', () => {
160160
await mdns.start()
161161
await mdns.stop()
162162
})
163+
164+
it('should not emit undefined peer ids', async () => {
165+
const mdns = new MulticastDNS({ peerInfo: pA, port: 50004 })
166+
await mdns.start()
167+
168+
return new Promise((resolve, reject) => {
169+
mdns.on('peer', (peerInfo) => {
170+
if (!peerInfo) {
171+
reject(new Error('peerInfo was not set'))
172+
}
173+
})
174+
175+
mdns.mdns.on('response', () => {
176+
// query.gotResponse is async - we'll bail from that method when
177+
// comparing the senders PeerId to our own but it'll happen later
178+
// so allow enough time for the test to have failed if we emit
179+
// empty PeerInfo objects
180+
setTimeout(() => {
181+
resolve()
182+
}, 100)
183+
})
184+
185+
// this will cause us to respond to ourselves
186+
mdns.mdns.query({
187+
questions: [{
188+
name: 'localhost',
189+
type: 'A'
190+
}]
191+
})
192+
})
193+
})
163194
})

0 commit comments

Comments
 (0)
This repository has been archived.