Transport disconnect method (#753)

* Prevent reads from piling up

* Implement disconnect() method for all transports
This commit is contained in:
Henri Bergius
2025-08-05 22:46:23 -04:00
committed by GitHub
parent 313c4dbb71
commit 0a7b653ec8
7 changed files with 62 additions and 6 deletions
+10 -2
View File
@@ -6,6 +6,7 @@ import { Utils } from "@meshtastic/core";
export class TransportNode implements Types.Transport {
private readonly _toDevice: WritableStream<Uint8Array>;
private readonly _fromDevice: ReadableStream<Types.DeviceOutput>;
private socket: Socket | undefined;
/**
* Creates and connects a new TransportNode instance.
@@ -36,7 +37,8 @@ export class TransportNode implements Types.Transport {
* @param connection - An active Node.js net.Socket connection.
*/
constructor(connection: Socket) {
connection.on("error", (err) => {
this.socket = connection;
this.socket.on("error", (err) => {
console.error("Socket connection error:", err);
});
@@ -56,7 +58,7 @@ export class TransportNode implements Types.Transport {
.pipeTo(Writable.toWeb(connection) as WritableStream<Uint8Array>)
.catch((err) => {
console.error("Error piping data to socket:", err);
connection.destroy(err as Error);
this.socket.destroy(err as Error);
});
}
@@ -73,4 +75,10 @@ export class TransportNode implements Types.Transport {
public get fromDevice(): ReadableStream<Types.DeviceOutput> {
return this._fromDevice;
}
disconnect() {
this.socket.destroy();
this.socket = undefined;
return Promise.resolve();
}
}