Skip to content

Commit

Permalink
mdns vs bonjour hopefully last iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Oct 1, 2024
1 parent 158a35d commit ac38428
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 89 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,5 @@ jobs:
node-version: 20
- name: Install Dependencies
run: make install
- name: Run Hub tests
run: make testhub
- name: Run Service tests
run: make testservice
- name: Run Client tests
run: make testclient
- name: Run tests
run: make test
8 changes: 7 additions & 1 deletion client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export class NestorClient {
}

start(): void {


// we use bonjour-service and not mdns
// as mdns has platform dependencies
// which makes it painful to embed in electron
// and it seems to be fine enough for a client
// despite seeming to be not as good as mdns

// now start the browser
const bonjour = new Bonjour()
this.browser = bonjour.find({ type: 'nestor' })
Expand Down
52 changes: 3 additions & 49 deletions hub/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dependencies": {
"@sindresorhus/slugify": "^2.2.1",
"body-parser": "^1.20.3",
"bonjour-service": "^1.2.1",
"commander": "^12.1.0",
"config": "^3.3.12",
"cors": "^2.8.5",
Expand Down
14 changes: 5 additions & 9 deletions hub/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import express, { Express } from 'express'
import { Service } from 'bonjour-service'
import bodyParser from 'body-parser'
import * as mdns from 'mdns'
import mdns from 'mdns'
import helmet from 'helmet'
import morgan from 'morgan'
import cors from 'cors'
Expand All @@ -20,11 +19,11 @@ import DiscoveryService from './services/discovery'
// start discovery immediately
const serviceDirectory = new ServiceDirectory()
const discoveryService = new DiscoveryService()
discoveryService.start((service: Service) => {
if (service.name && (service.subtypes?.includes('service') || service.txt.type === 'service')) {
serviceDirectory.add(service.name, service.host, service.port, service.txt.path)
discoveryService.start((service: mdns.Service) => {
if (service.name && (/*service.subtypes?.includes('service') || */service.txtRecord.type === 'service')) {
serviceDirectory.add(service.name, service.host, service.port, service.txtRecord.path)
}
}, (service: Service) => {
}, (service: mdns.Service) => {
if (service.name) {
serviceDirectory.remove(service.name)
}
Expand Down Expand Up @@ -94,9 +93,6 @@ process.on('SIGINT', async () => {
// publish
const publish = (name: string, port: number) => {

// we use mdns instead of bonjour-service here
// https://github.com/onlxltd/bonjour-service/issues/46

// subtype is not consistently supported so using txtRecord too
ad = mdns.createAdvertisement(mdns.tcp('nestor', 'hub'), port, {
name: name,
Expand Down
22 changes: 13 additions & 9 deletions hub/src/services/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@

import { Bonjour, Browser, Service } from 'bonjour-service'
import mdns from 'mdns'

export default class DiscoveryService {

bonjour!: Bonjour
browser?: Browser
browser!: mdns.Browser

start(onUp: CallableFunction, onDown: CallableFunction) {
this.bonjour = new Bonjour()
this.browser = this.bonjour.find({ type: 'nestor' })
this.browser.on('up', (service: Service) => {

// getaddr fails: https://stackoverflow.com/questions/29589543/raspberry-pi-mdns-getaddrinfo-3008-error
this.browser = mdns.createBrowser(mdns.tcp('nestor'), { resolverSequence: [
mdns.rst.DNSServiceResolve(),
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families:[4]}),
mdns.rst.makeAddressesUnique()
]})
this.browser.on('serviceUp', (service: mdns.Service) => {
onUp(service)
})
this.browser.on('down', (service: Service) => {
this.browser.on('serviceDown', (service: mdns.Service) => {
onDown(service)
})
this.browser.start()
});
this.browser.start();

}

Expand Down
14 changes: 5 additions & 9 deletions hub/tests/unit/discovery.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@

import { vi, test, expect } from 'vitest'
import ServiceDiscovery from '../../src/services/discovery'
import { Bonjour } from 'bonjour-service'
import mdns from 'mdns'

//const spyCreateBrowser = vi.spyOn(Bonjour(), 'find')
const spyCreateBrowser = vi.spyOn(mdns, 'createBrowser')

class ServiceMock {
ad: any

// subtype is not consistently supported so using txt.type too
start(port: number) {
const bonjour = new Bonjour()
this.ad = bonjour.publish({
this.ad = mdns.createAdvertisement(mdns.tcp('nestor', 'service'), port, {
name: 'service-test-1',
type: 'nestor',
subtypes: [ 'service' ],
port: port,
txt: {
txtRecord: {
type: 'service',
}
})
Expand All @@ -30,7 +26,7 @@ class ServiceMock {
test('creates browser', async () => {
const discovery = new ServiceDiscovery()
discovery.start(() => {}, () => {})
//expect(spyCreateBrowser).toHaveBeenCalled()
expect(spyCreateBrowser).toHaveBeenCalled()
})

test('calls callbacks', async () => {
Expand Down
5 changes: 1 addition & 4 deletions service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import * as mdns from 'mdns'
import mdns from 'mdns'

export interface NestorServiceOptions {
autostart?: boolean
Expand Down Expand Up @@ -43,9 +43,6 @@ export class NestorService {

start(): void {

// we use mdns instead of bonjour-service here
// https://github.com/onlxltd/bonjour-service/issues/46

// subtype is not consistently supported so using txtRecord too
this.advertise = mdns.createAdvertisement(mdns.tcp('nestor', 'service'), this.port, {
name: this.name,
Expand Down
2 changes: 1 addition & 1 deletion service/tests/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { vi, test, expect } from 'vitest'
import { NestorService } from '../src/index'
import * as mdns from 'mdns'
import mdns from 'mdns'

global.fetch = vi.fn((req) => {
if (req.includes('3000')) return { ok: true }
Expand Down

0 comments on commit ac38428

Please sign in to comment.