bridge: the discovery answer is published through send()

The answer on <root>/discover_r called the publisher directly; a publisher
that threw or rejected was only caught by the handler meant for listeners
of "discover". It now takes the path of every device message: send()
resolves, and a failure goes to the "error" event when somebody listens.
A test covers a publish that fails, throws and rejects, with and without
an "error" listener: receive() resolves each time and "discover" is not
emitted.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 21:15:47 +00:00
parent 94b13f733a
commit d12767f9a7
4 changed files with 40 additions and 15 deletions

11
dist/esm/Alex2Node.js vendored
View file

@ -7,7 +7,7 @@ import { DisplayCategory } from "./compat/enums.js";
import { DeclarationError } from "./registry/types.js";
import { Dispatcher } from "./dispatcher.js";
import * as topics from "./topics.js";
import { asError, MqttPublisher } from "./transport.js";
import { asError, MqttPublisher, send } from "./transport.js";
export const DEFAULT_HOST = "mqtt://Alex2MQTT.stormysdream.club:1883";
// What addDevice() copies from the definition to the device
const DESCRIBED = [
@ -135,12 +135,11 @@ class Alex2MQTT extends EventEmitter {
this.log("Discovery request received, getting device json...");
const deviceArray = this.describeDevices();
this.lastDiscoveryAt = new Date().toISOString();
const result = await this.publisher.publish(topics.discoverReply(this.rootTopic), deviceArray);
if (!result.ok) {
this.fail(result.error);
// send(), as for every message of a device: a publisher that throws or rejects is reported like one that fails
const topic = await send(this.publisher, topics.discoverReply(this.rootTopic), () => deviceArray, (err) => this.fail(err));
if (topic === "")
return;
}
this.log(`Discovery payloads published to ${result.topic}`, deviceArray);
this.log(`Discovery payloads published to ${topic}`, deviceArray);
this.emit("discover", deviceArray.length);
}
// The endpoint objects of a discovery answer. What check() says about a device is logged, each line once. A device