Connection robustness improvements (#813)

* Clear heartbeat and queue when disconnected

* Give clearer error in case configure fails due to a lost connection. Used to throw 'Packet does not exist'

* If the queue processing error is due to a lost connection, throw it instead of looping endlessly

* In case we send a disconnection event we don't need to also throw

* Catch heartbeat errors

* Also handle invalid state errors

* Handle socket timeouts

* Log heartbeat failures

* Make linter happy

* Transform stream being a singleton prevented reconnection attempts

* Adapt tests to not using singleton

* Aborting already ends the connection
This commit is contained in:
Henri Bergius
2025-09-04 13:08:05 -04:00
committed by GitHub
parent dcb44d27fe
commit bcdda8b751
11 changed files with 124 additions and 54 deletions
@@ -58,7 +58,8 @@ export class TransportWebSerial implements Types.Transport {
const abortController = this.abortController;
// Set up the pipe with abort signal for clean cancellation
this.pipePromise = Utils.toDeviceStream.readable
const toDeviceTransform = Utils.toDeviceStream();
this.pipePromise = toDeviceTransform.readable
.pipeTo(connection.writable, { signal: this.abortController.signal })
.catch((err) => {
// Ignore expected rejection when we cancel it via the AbortController.
@@ -73,7 +74,7 @@ export class TransportWebSerial implements Types.Transport {
);
});
this._toDevice = Utils.toDeviceStream.writable;
this._toDevice = toDeviceTransform.writable;
// Wrap + capture controller to inject status packets
this._fromDevice = new ReadableStream<Types.DeviceOutput>({
@@ -199,7 +200,8 @@ export class TransportWebSerial implements Types.Transport {
const abortController = this.abortController;
// Re-establish the pipe connection
this.pipePromise = Utils.toDeviceStream.readable
const toDeviceTransform = Utils.toDeviceStream();
this.pipePromise = toDeviceTransform.readable
.pipeTo(this.connection.writable, {
signal: this.abortController.signal,
})