balena-cli/lib/utils/discover.ts
Otavio Jacobi 7273656d07 Replace resin-discoverable-services with bonjour-service
Instead of using the more generic resin-discoverable-services lib which is unmantained
and currently has several vulnerabilities and forks for fixing issues (that were later on fixed upstream)
we directly talk with mDNS using standard (and currently mantained) bonjour-service.

Change-type: patch
2024-07-10 16:06:19 -03:00

49 lines
1.2 KiB
TypeScript

import Bonjour from 'bonjour-service';
import type { Service } from 'bonjour-service';
interface LocalBalenaOsDevice {
address: string;
host: string;
osVariant?: string;
port: number;
}
const avahiBalenaSshConfig = {
type: 'ssh',
name: '_resin-device._sub',
protocol: 'tcp' as const,
};
const avahiBalenaSshSubtype = 'resin-device';
export async function discoverLocalBalenaOsDevices(
timeout = 4000,
): Promise<LocalBalenaOsDevice[]> {
const services = await new Promise<Service[]>((resolve) => {
const bonjour = new Bonjour({}, async (err: string | Error) => {
await (await import('../errors')).handleError(err);
});
const resinSshServices: Service[] = [];
const browser = bonjour.find(avahiBalenaSshConfig, (service) =>
resinSshServices.push(service),
);
setTimeout(() => {
browser.stop();
bonjour.destroy();
resolve(resinSshServices);
}, timeout);
});
return services
.filter(
({ subtypes, referer }) =>
subtypes?.includes(avahiBalenaSshSubtype) && referer != null,
)
.map(({ referer, host, port }) => ({
// We ensure referer is not null on the filter above
address: referer!.address,
host,
port,
}));
}