UNPKG

balena-cli

Version:

The official balena Command Line Interface

52 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.discoverLocalBalenaOsDevices = discoverLocalBalenaOsDevices; const bonjour_service_1 = require("bonjour-service"); const os = require("os"); const avahiBalenaSshConfig = { type: 'ssh', name: '_resin-device._sub', protocol: 'tcp', }; const avahiBalenaSshSubtype = 'resin-device'; async function discoverLocalBalenaOsDevices(timeout = 4000) { const networks = os.networkInterfaces(); const validNics = []; for (const networkName of Object.keys(networks)) { for (const iface of networks[networkName]) { if (isIPv4(iface.family) && !iface.internal) { validNics.push(iface); } } } const allServices = await Promise.all(validNics.map((iface) => searchBalenaDevicesOnInterface(iface, timeout))); const services = Array.from(new Map(allServices.flat().map((item) => [item.fqdn, item])).values()); return services .filter(({ subtypes, referer }) => (subtypes === null || subtypes === void 0 ? void 0 : subtypes.includes(avahiBalenaSshSubtype)) && referer != null) .map(({ referer, host, port }) => ({ address: referer.address, host, port, })); } async function searchBalenaDevicesOnInterface(iface, timeout) { return await new Promise((resolve) => { const bonjour = new bonjour_service_1.default({ interface: iface.address, bind: '0.0.0.0', }, async (err) => { await (await Promise.resolve().then(() => require('../errors'))).handleError(err); }); const resinSshServices = []; const browser = bonjour.find(avahiBalenaSshConfig, (service) => resinSshServices.push(service)); setTimeout(() => { browser.stop(); bonjour.destroy(); resolve(resinSshServices); }, timeout); }); } function isIPv4(family) { return family === 4 || family === 'IPv4'; } //# sourceMappingURL=discover.js.map