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

View file

@ -10,7 +10,7 @@ import { DeclarationError } from "./registry/types.js";
import { Dispatcher } from "./dispatcher.js";
import type { Timers } from "./dispatcher.js";
import * as topics from "./topics.js";
import { asError, MqttPublisher } from "./transport.js";
import { asError, MqttPublisher, send } from "./transport.js";
import type { Publisher } from "./transport.js";
/** Optional settings for the bridge (1.5.1). Everything has the 1.4.0 behaviour as its default. */
@ -176,9 +176,10 @@ 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); return; }
this.log(`Discovery payloads published to ${result.topic}`, deviceArray);
// 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 ${topic}`, deviceArray);
this.emit("discover", deviceArray.length);
}