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

Add systemd-resolved mDNS advertiser support #965

Merged
merged 9 commits into from
Oct 31, 2022
Prev Previous commit
Next Next commit
Refactor duplicated D-Bus code
  • Loading branch information
elyscape committed Oct 3, 2022
commit dc8a2216dd9765da8ee2d02568ebb4cc1c0497c0
122 changes: 58 additions & 64 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,46 @@ export class BonjourHAPAdvertiser extends EventEmitter implements Advertiser {

}

function messageBusConnectionResult(bus: MessageBus): Promise<void> {
return new Promise((resolve, reject) => {
const errorHandler = (error: Error) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
bus.connection.removeListener("connect", connectHandler);
reject(error);
};
const connectHandler = () => {
bus.connection.removeListener("error", errorHandler);
resolve();
};

bus.connection.once("connect", connectHandler);
bus.connection.once("error", errorHandler);
});
}


// eslint-disable-next-line @typescript-eslint/no-explicit-any
function dbusInvoke(bus: MessageBus,
destination: string,
path: string,
interface: string,
member: string,
others?: any): Promise<Any> {
return new Promise((resolve, reject) => {
const command = {destination, path, interface, member, ...(others || {})};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
bus.invoke(command, (err: any, result: any) => {
if (err) {
reject(new Error(`dbusInvoke error: ${JSON.stringify(err)}`));
} else {
resolve(result);
}
});

});
}

/**
* Advertiser based on the Avahi D-Bus library.
* For (very crappy) docs on the interface, see the XML files at: https://github.com/lathiat/avahi/tree/master/avahi-daemon.
Expand Down Expand Up @@ -370,7 +410,7 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {

try {
try {
await this.messageBusConnectionResult(bus);
await messageBusConnectionResult(bus);
} catch (error) {
debug("Avahi/DBus classified unavailable due to missing dbus interface!");
return false;
Expand All @@ -390,36 +430,16 @@ export class AvahiAdvertiser extends EventEmitter implements Advertiser {
}
}

private static messageBusConnectionResult(bus: MessageBus): Promise<void> {
return new Promise((resolve, reject) => {
const errorHandler = (error: Error) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
bus.connection.removeListener("connect", connectHandler);
reject(error);
};
const connectHandler = () => {
bus.connection.removeListener("error", errorHandler);
resolve();
};

bus.connection.once("connect", connectHandler);
bus.connection.once("error", errorHandler);
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static avahiInvoke(bus: MessageBus, path: string, dbusInterface: string, member: string, others?: any): Promise<any> {
return new Promise((resolve, reject) => {
const command = { destination: "org.freedesktop.Avahi", path, interface: "org.freedesktop.Avahi." + dbusInterface, member, ...(others || {}) };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
bus.invoke(command, (err: any, result: any) => {
if (err) {
reject(new Error(`avahiInvoke error: ${JSON.stringify(err)}`));
} else {
resolve(result);
}
});
});
return dbusInvoke(
bus,
"org.freedesktop.Avahi",
path,
`org.freedesktop.Avahi.${dbusInterface}`,
member,
others,
);
}
}

Expand Down Expand Up @@ -529,7 +549,7 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {

try {
try {
await this.messageBusConnectionResult(bus);
await messageBusConnectionResult(bus);
} catch (error) {
debug("systemd-resolved/DBus classified unavailable due to missing dbus interface!");
return false;
Expand All @@ -553,41 +573,15 @@ export class ResolvedAdvertiser extends EventEmitter implements Advertiser {
}
}

private static messageBusConnectionResult(bus: MessageBus): Promise<void> {
return new Promise((resolve, reject) => {
const errorHandler = (error: Error) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
bus.connection.removeListener("connect", connectHandler);
reject(error);
};
const connectHandler = () => {
bus.connection.removeListener("error", errorHandler);
resolve();
};

bus.connection.once("connect", connectHandler);
bus.connection.once("error", errorHandler);
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static resolvedInvoke(bus: MessageBus, member: string, others?: any): Promise<any> {
return new Promise((resolve, reject) => {
const command = {
destination: "org.freedesktop.resolve1",
path: "/org/freedesktop/resolve1",
interface: "org.freedesktop.resolve1.Manager",
member,
...(others || {}),
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
bus.invoke(command, (err: any, result: any) => {
if (err) {
reject(new Error(`resolvedInvoke error: ${JSON.stringify(err)}`));
} else {
resolve(result);
}
});
});
return dbusInvoke(
bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
member,
others,
);
}
}