Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add srv-update event #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ Emitted every time a new service is found that matches the browser.

Emitted every time an existing service emmits a goodbye message.

#### `Event: srv-update`

Emitted every time an existing service does a new announcement with an updated SRV record.

#### `Event: txt-update`

Emitted every time an existing service does a new announcement with an updated TXT record.
Expand Down
35 changes: 29 additions & 6 deletions src/lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ export class Browser extends EventEmitter {
if (matches.length === 0) return

matches.forEach((service: Service) => {
if (self.serviceMap[service.fqdn]) {
self.updateService(service)
const existingService = self._services.find((s) => dnsEqual(s.fqdn, service.fqdn))
if (existingService) {
self.updateServiceSrv(existingService, service)
self.updateServiceTxt(existingService, service)
return
}
self.addService(service)
Expand Down Expand Up @@ -140,20 +142,41 @@ export class Browser extends EventEmitter {
this.emit('up', service)
}

private updateService(service: Service) {
private updateServiceSrv(existingService: Service, newService: Service) {
// check if any properties derived from SRV are updated
if (existingService.name !== newService.name
|| existingService.host !== newService.host
|| existingService.port !== newService.port
|| existingService.type !== newService.type
|| existingService.protocol !== newService.protocol
){
// replace service
this.replaceService(newService)

this.emit('srv-update', newService, existingService);
}
}

private updateServiceTxt(existingService: Service, service: Service) {
// check if txt updated
if (equalTxt(service.txt, this._services.find((s) => dnsEqual(s.fqdn, service.fqdn))?.txt || {})) return
if (equalTxt(service.txt, existingService?.txt || {})) return
// if the new service is not allowed by the txt query, remove it
if(!filterService(service, this.txtQuery)) {
this.removeService(service.fqdn)
return
}

// replace service
this._services = this._services.map(function (s) {
this.replaceService(service)

this.emit('txt-update', service, existingService);
}

private replaceService(service: Service) {
this._services = this._services.map((s) =>{
if (!dnsEqual(s.fqdn, service.fqdn)) return s
return service
})
this.emit('txt-update', service);
}

private removeService(fqdn: string) {
Expand Down
Loading