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/cli.js CHANGED
@@ -8915,6 +8915,14 @@ class Connection {
8915
8915
  readMessage(expect, span) {
8916
8916
  return __classPrivateFieldGet(this, _Connection_lock, "f").runExclusive(() => message_1.Message.fromStream(__classPrivateFieldGet(this, _Connection_socket, "f"), expect, span));
8917
8917
  }
8918
+ /**
8919
+ * False once the device has closed its end of the connection or the socket
8920
+ * has failed. Every write after that fails with "write after end".
8921
+ */
8922
+ get isOpen() {
8923
+ const { stream } = __classPrivateFieldGet(this, _Connection_socket, "f");
8924
+ return stream.writable && !stream.closed && !stream.destroyed;
8925
+ }
8918
8926
  close() {
8919
8927
  __classPrivateFieldGet(this, _Connection_socket, "f").destroy();
8920
8928
  }
@@ -9046,7 +9054,14 @@ class RemoteDatabase {
9046
9054
  type: types_2.MessageType.Disconnect,
9047
9055
  args: [],
9048
9056
  });
9049
- await conn.writeMessage(goodbye, tx);
9057
+ // The goodbye is a courtesy. A device that has already closed the
9058
+ // connection can't receive it, and its socket still has to be released.
9059
+ try {
9060
+ await conn.writeMessage(goodbye, tx);
9061
+ }
9062
+ catch {
9063
+ // Ignore errors during disconnect
9064
+ }
9050
9065
  conn.close();
9051
9066
  __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(device.id);
9052
9067
  __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(device.id);
@@ -9085,6 +9100,15 @@ class RemoteDatabase {
9085
9100
  const releaseLock = await lock.acquire();
9086
9101
  try {
9087
9102
  let conn = __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").get(deviceId);
9103
+ // A device that closed its end of the connection (it restarted, or it
9104
+ // dropped off the network and came back) would fail every query on it
9105
+ // with "write after end". Open a new one instead.
9106
+ if (conn !== undefined && !conn.isOpen) {
9107
+ conn.close();
9108
+ __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(deviceId);
9109
+ __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(deviceId);
9110
+ conn = undefined;
9111
+ }
9088
9112
  if (conn === undefined) {
9089
9113
  await this.connectToDevice(device);
9090
9114
  }