alphatheta-connect 0.28.1 → 0.28.4

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.
@@ -1,5 +1,10 @@
1
1
  import { Device } from "../types";
2
2
  /**
3
3
  * Converts a announce packet to a device object.
4
+ *
5
+ * Returns null for any packet that is not a stage-3 Pro DJ Link announce. The
6
+ * announce socket receives every UDP broadcast on its port, so foreign traffic
7
+ * (other apps, other protocols) is normal and must be ignored, not treated as
8
+ * an error.
4
9
  */
5
10
  export declare function deviceFromPacket(packet: Buffer): Device | null;
package/lib/index.js CHANGED
@@ -2127,7 +2127,16 @@ class DeviceManager {
2127
2127
  this.off = __classPrivateFieldGet(this, _DeviceManager_emitter, "f").removeListener.bind(__classPrivateFieldGet(this, _DeviceManager_emitter, "f"));
2128
2128
  this.once = __classPrivateFieldGet(this, _DeviceManager_emitter, "f").once.bind(__classPrivateFieldGet(this, _DeviceManager_emitter, "f"));
2129
2129
  _DeviceManager_handleAnnounce.set(this, (message) => {
2130
- const device = (0, utils_1.deviceFromPacket)(message);
2130
+ // This runs on every UDP datagram the announce socket receives, so a parse
2131
+ // failure here would surface as an uncaught exception on the socket and
2132
+ // crash the process. Foreign or truncated packets are ignored instead.
2133
+ let device;
2134
+ try {
2135
+ device = (0, utils_1.deviceFromPacket)(message);
2136
+ }
2137
+ catch {
2138
+ return;
2139
+ }
2131
2140
  if (device === null) {
2132
2141
  return;
2133
2142
  }
@@ -2243,10 +2252,15 @@ const ip = __importStar(__webpack_require__(/*! ip-address */ "ip-address"));
2243
2252
  const constants_1 = __webpack_require__(/*! src/constants */ "./src/constants.ts");
2244
2253
  /**
2245
2254
  * Converts a announce packet to a device object.
2255
+ *
2256
+ * Returns null for any packet that is not a stage-3 Pro DJ Link announce. The
2257
+ * announce socket receives every UDP broadcast on its port, so foreign traffic
2258
+ * (other apps, other protocols) is normal and must be ignored, not treated as
2259
+ * an error.
2246
2260
  */
2247
2261
  function deviceFromPacket(packet) {
2248
2262
  if (packet.indexOf(constants_1.PROLINK_HEADER) !== 0) {
2249
- throw new Error('Announce packet does not start with expected header');
2263
+ return null;
2250
2264
  }
2251
2265
  if (packet[0x0a] !== 0x06) {
2252
2266
  return null;
@@ -11834,6 +11848,14 @@ class Connection {
11834
11848
  readMessage(expect, span) {
11835
11849
  return __classPrivateFieldGet(this, _Connection_lock, "f").runExclusive(() => message_1.Message.fromStream(__classPrivateFieldGet(this, _Connection_socket, "f"), expect, span));
11836
11850
  }
11851
+ /**
11852
+ * False once the device has closed its end of the connection or the socket
11853
+ * has failed. Every write after that fails with "write after end".
11854
+ */
11855
+ get isOpen() {
11856
+ const { stream } = __classPrivateFieldGet(this, _Connection_socket, "f");
11857
+ return stream.writable && !stream.closed && !stream.destroyed;
11858
+ }
11837
11859
  close() {
11838
11860
  __classPrivateFieldGet(this, _Connection_socket, "f").destroy();
11839
11861
  }
@@ -11965,7 +11987,14 @@ class RemoteDatabase {
11965
11987
  type: types_2.MessageType.Disconnect,
11966
11988
  args: [],
11967
11989
  });
11968
- await conn.writeMessage(goodbye, tx);
11990
+ // The goodbye is a courtesy. A device that has already closed the
11991
+ // connection can't receive it, and its socket still has to be released.
11992
+ try {
11993
+ await conn.writeMessage(goodbye, tx);
11994
+ }
11995
+ catch {
11996
+ // Ignore errors during disconnect
11997
+ }
11969
11998
  conn.close();
11970
11999
  __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(device.id);
11971
12000
  __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(device.id);
@@ -12004,6 +12033,15 @@ class RemoteDatabase {
12004
12033
  const releaseLock = await lock.acquire();
12005
12034
  try {
12006
12035
  let conn = __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").get(deviceId);
12036
+ // A device that closed its end of the connection (it restarted, or it
12037
+ // dropped off the network and came back) would fail every query on it
12038
+ // with "write after end". Open a new one instead.
12039
+ if (conn !== undefined && !conn.isOpen) {
12040
+ conn.close();
12041
+ __classPrivateFieldGet(this, _RemoteDatabase_connections, "f").delete(deviceId);
12042
+ __classPrivateFieldGet(this, _RemoteDatabase_queryIds, "f").delete(deviceId);
12043
+ conn = undefined;
12044
+ }
12007
12045
  if (conn === undefined) {
12008
12046
  await this.connectToDevice(device);
12009
12047
  }