Skip to content

Commit

Permalink
chore: prefer net.isIPv4 over custom function (#443)
Browse files Browse the repository at this point in the history
This replaces our custom `isIpv4` function with [`net.isIPv4`][0], which
is built into Node.

[0]: https://nodejs.org/api/net.html#netisipv4input
  • Loading branch information
EvanHahn authored Jan 22, 2024
1 parent 5820d12 commit d8f4039
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/discovery/dns-sd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TypedEmitter } from 'tiny-typed-emitter'
import { Bonjour } from 'bonjour-service'
import pTimeout from 'p-timeout'
import { isIPv4 } from 'node:net'
import { randomBytes } from 'node:crypto'
import { once } from 'node:events'
import { Logger } from '../logger.js'
Expand Down Expand Up @@ -39,7 +40,7 @@ export class DnsSd extends TypedEmitter {
this.#log(`Ignoring service up from self`)
return
}
const address = service.addresses?.filter(isIpv4)[0]
const address = service.addresses?.filter(isIPv4)[0]
/* c8 ignore start */
if (!address) {
this.#log(`service up (${service.name}) with no ipv4 addresses`)
Expand All @@ -56,7 +57,7 @@ export class DnsSd extends TypedEmitter {
this.#log(`Ignoring service down from self`)
return
}
const address = service.addresses?.filter(isIpv4)[0]
const address = service.addresses?.filter(isIPv4)[0]
/* c8 ignore start */
if (!address) {
this.#log(`service down (${service.name}) with no ipv4 addresses`)
Expand Down Expand Up @@ -228,12 +229,3 @@ export class DnsSd extends TypedEmitter {
return this.#bonjour
}
}

/**
* Returns true if the given ip is an ipv4 address
* @param {string} ip
* @returns {boolean}
*/
function isIpv4(ip) {
return ip.split('.').length === 4
}

0 comments on commit d8f4039

Please sign in to comment.