simple-mdns-announce
Version:
A NodeJS library to announce services using mdns.
87 lines • 2.83 kB
JavaScript
import { debug } from "./debug.js";
// @ts-ignore
import { spawn } from "node:child_process";
import { composeTypeAndSubtype } from "./composeTypeAndSubtype.js";
import { dnsSdExecutableMacOs, dnsSdExecutableWin32, } from "./dnsSdExecutable.js";
// @ts-ignore
import { platform } from "./platform.js";
import { unsupportedPlatformError } from "./unsupportedPlatformError.js";
const log = debug("simple-mdns-announce");
function appendTxtRecords(args, service) {
if (service.txtRecords) {
for (const name in service.txtRecords) {
args.push(`${name}=${service.txtRecords[name]}`);
}
}
}
function getCommandLine(service) {
let cmd;
let args;
switch (platform) {
case "darwin": {
cmd = dnsSdExecutableMacOs;
args = [
"-R",
service.name,
composeTypeAndSubtype(service.type, service.subtype),
"local",
service.port.toFixed(0),
];
appendTxtRecords(args, service);
break;
}
case "win32":
cmd = dnsSdExecutableWin32;
args = [
"-R",
service.name,
composeTypeAndSubtype(service.type, service.subtype),
"local",
service.port.toFixed(0),
];
appendTxtRecords(args, service);
break;
case "linux":
cmd = "avahi-publish";
args = ["-s"];
if (service.subtype) {
args.push("--subtype", `${service.subtype}._sub.${service.type}`);
}
args.push(service.name, service.type, service.port.toFixed(0));
appendTxtRecords(args, service);
break;
default:
throw unsupportedPlatformError();
}
return [cmd, args];
}
/**
* This function announces a service over mDNS. You pass an IService object with
* the service details. The function returns a cleanup handle that you can call
* to stop the service announcement and kill the underlying dns-sd process.
*
* @param service
* The service to announce.
* @param onError
* An optional error handler. It is used as event handler for the 'error' event
* on the spawned child process.
* @returns
*/
export function announceService(service, onError) {
const [executable, args] = getCommandLine(service);
if (!onError)
onError = (err) => {
log("Error: ", err);
};
log("spawn %s %o", executable, args);
const process = spawn(executable, args, {
windowsHide: true,
stdio: "ignore",
});
process.on("error", onError);
return () => {
log("kill %s %o", executable, args);
process.kill();
};
}
//# sourceMappingURL=announceService.js.map