alphatheta-connect 0.28.0 → 0.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -11834,6 +11834,14 @@ class Connection {
11834
11834
  readMessage(expect, span) {
11835
11835
  return __classPrivateFieldGet(this, _Connection_lock, "f").runExclusive(() => message_1.Message.fromStream(__classPrivateFieldGet(this, _Connection_socket, "f"), expect, span));
11836
11836
  }
11837
+ /**
11838
+ * False once the device has closed its end of the connection or the socket
11839
+ * has failed. Every write after that fails with "write after end".
11840
+ */
11841
+ get isOpen() {
11842
+ const { stream } = __classPrivateFieldGet(this, _Connection_socket, "f");
11843
+ return stream.writable && !stream.closed && !stream.destroyed;
11844
+ }
11837
11845
  close() {
11838
11846
  __classPrivateFieldGet(this, _Connection_socket, "f").destroy();
11839
11847
  }
@@ -11965,7 +11973,14 @@ class RemoteDatabase {
11965
11973
  type: types_2.MessageType.Disconnect,
11966
11974
  args: [],
11967
11975
  });
11968
- await conn.writeMessage(goodbye, tx);
11976
+ // The goodbye is a courtesy. A device that has already closed the
11977
+ // connection can't receive it, and its socket still has to be released.
11978
+ try {
11979
+ await conn.writeMessage(goodbye, tx);
11980
+ }
11981
+ catch {
11982
+ // Ignore errors during disconnect
11983
+ }
11969
11984
  conn.close();
11970
11985
  __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(device.id);
11971
11986
  __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(device.id);
@@ -12004,6 +12019,15 @@ class RemoteDatabase {
12004
12019
  const releaseLock = await lock.acquire();
12005
12020
  try {
12006
12021
  let conn = __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").get(deviceId);
12022
+ // A device that closed its end of the connection (it restarted, or it
12023
+ // dropped off the network and came back) would fail every query on it
12024
+ // with "write after end". Open a new one instead.
12025
+ if (conn !== undefined && !conn.isOpen) {
12026
+ conn.close();
12027
+ __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(deviceId);
12028
+ __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(deviceId);
12029
+ conn = undefined;
12030
+ }
12007
12031
  if (conn === undefined) {
12008
12032
  await this.connectToDevice(device);
12009
12033
  }