@enyo-energy/sunspec-sdk 1.0.0 → 1.0.1

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.
@@ -709,8 +709,21 @@ class SunspecModbusClient {
709
709
  const since = this.poisonedAtByUnit.get(unitId);
710
710
  throw new SunspecUnavailableError('socket-poisoned', unitId, since === undefined ? 0 : Date.now() - since);
711
711
  }
712
+ let op;
712
713
  try {
713
- const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, fn);
714
+ op = fn();
715
+ }
716
+ catch (error) {
717
+ // A transport throws synchronously (rather than rejecting) when it refuses the
718
+ // transaction before it ever reaches the wire — the socket was closed underneath us,
719
+ // e.g. a poll in flight racing a shutdown. The link is dead, so poison the unit the
720
+ // same way a timeout does; otherwise the client keeps believing the socket is open and
721
+ // every later read repeats the throw with nothing triggering a reconnect.
722
+ this.invalidateUnit(unitId, `transaction threw synchronously (${error})`);
723
+ throw error;
724
+ }
725
+ try {
726
+ const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, () => op);
714
727
  this.lastOkAtByUnit.set(unitId, Date.now());
715
728
  return result;
716
729
  }
@@ -725,13 +738,30 @@ class SunspecModbusClient {
725
738
  * Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
726
739
  * the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
727
740
  * timer is always cleared so a fast `fn` doesn't keep the event loop alive.
741
+ *
742
+ * `fn()` is invoked inside a try/catch rather than inline in the Promise.race argument list. A
743
+ * transport can throw synchronously (a transaction issued against an already-closed socket), and
744
+ * evaluating it while the array literal is built means the throw escapes before Promise.race
745
+ * exists: nothing ever consumes `timeout`, `.finally()` never runs, and the armed timer rejects
746
+ * `timeoutMs` later into an empty stack — an unhandled rejection that kills the process, blaming
747
+ * a timeout for a failure that actually happened a full timeout window earlier.
728
748
  */
729
749
  withTimeout(operation, unitId, timeoutMs, fn) {
730
750
  let timer;
731
751
  const timeout = new Promise((_, reject) => {
732
752
  timer = setTimeout(() => reject(new ModbusOperationTimeoutError(operation, unitId, timeoutMs)), timeoutMs);
733
753
  });
734
- return Promise.race([fn(), timeout]).finally(() => {
754
+ let op;
755
+ try {
756
+ op = fn();
757
+ }
758
+ catch (err) {
759
+ // Nothing will race `timeout`; disarm it so it can't reject unobserved.
760
+ if (timer !== undefined)
761
+ clearTimeout(timer);
762
+ return Promise.reject(err);
763
+ }
764
+ return Promise.race([op, timeout]).finally(() => {
735
765
  if (timer !== undefined)
736
766
  clearTimeout(timer);
737
767
  });
@@ -287,6 +287,13 @@ export declare class SunspecModbusClient {
287
287
  * Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
288
288
  * the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
289
289
  * timer is always cleared so a fast `fn` doesn't keep the event loop alive.
290
+ *
291
+ * `fn()` is invoked inside a try/catch rather than inline in the Promise.race argument list. A
292
+ * transport can throw synchronously (a transaction issued against an already-closed socket), and
293
+ * evaluating it while the array literal is built means the throw escapes before Promise.race
294
+ * exists: nothing ever consumes `timeout`, `.finally()` never runs, and the armed timer rejects
295
+ * `timeoutMs` later into an empty stack — an unhandled rejection that kills the process, blaming
296
+ * a timeout for a failure that actually happened a full timeout window earlier.
290
297
  */
291
298
  private withTimeout;
292
299
  /**
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
9
9
  /**
10
10
  * Current version of the enyo Energy App SDK.
11
11
  */
12
- exports.SDK_VERSION = '1.0.0';
12
+ exports.SDK_VERSION = '1.0.1';
13
13
  /**
14
14
  * Gets the current SDK version.
15
15
  * @returns The semantic version string of the SDK
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "1.0.0";
8
+ export declare const SDK_VERSION = "1.0.1";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
@@ -287,6 +287,13 @@ export declare class SunspecModbusClient {
287
287
  * Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
288
288
  * the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
289
289
  * timer is always cleared so a fast `fn` doesn't keep the event loop alive.
290
+ *
291
+ * `fn()` is invoked inside a try/catch rather than inline in the Promise.race argument list. A
292
+ * transport can throw synchronously (a transaction issued against an already-closed socket), and
293
+ * evaluating it while the array literal is built means the throw escapes before Promise.race
294
+ * exists: nothing ever consumes `timeout`, `.finally()` never runs, and the armed timer rejects
295
+ * `timeoutMs` later into an empty stack — an unhandled rejection that kills the process, blaming
296
+ * a timeout for a failure that actually happened a full timeout window earlier.
290
297
  */
291
298
  private withTimeout;
292
299
  /**
@@ -700,8 +700,21 @@ export class SunspecModbusClient {
700
700
  const since = this.poisonedAtByUnit.get(unitId);
701
701
  throw new SunspecUnavailableError('socket-poisoned', unitId, since === undefined ? 0 : Date.now() - since);
702
702
  }
703
+ let op;
703
704
  try {
704
- const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, fn);
705
+ op = fn();
706
+ }
707
+ catch (error) {
708
+ // A transport throws synchronously (rather than rejecting) when it refuses the
709
+ // transaction before it ever reaches the wire — the socket was closed underneath us,
710
+ // e.g. a poll in flight racing a shutdown. The link is dead, so poison the unit the
711
+ // same way a timeout does; otherwise the client keeps believing the socket is open and
712
+ // every later read repeats the throw with nothing triggering a reconnect.
713
+ this.invalidateUnit(unitId, `transaction threw synchronously (${error})`);
714
+ throw error;
715
+ }
716
+ try {
717
+ const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, () => op);
705
718
  this.lastOkAtByUnit.set(unitId, Date.now());
706
719
  return result;
707
720
  }
@@ -716,13 +729,30 @@ export class SunspecModbusClient {
716
729
  * Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
717
730
  * the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
718
731
  * timer is always cleared so a fast `fn` doesn't keep the event loop alive.
732
+ *
733
+ * `fn()` is invoked inside a try/catch rather than inline in the Promise.race argument list. A
734
+ * transport can throw synchronously (a transaction issued against an already-closed socket), and
735
+ * evaluating it while the array literal is built means the throw escapes before Promise.race
736
+ * exists: nothing ever consumes `timeout`, `.finally()` never runs, and the armed timer rejects
737
+ * `timeoutMs` later into an empty stack — an unhandled rejection that kills the process, blaming
738
+ * a timeout for a failure that actually happened a full timeout window earlier.
719
739
  */
720
740
  withTimeout(operation, unitId, timeoutMs, fn) {
721
741
  let timer;
722
742
  const timeout = new Promise((_, reject) => {
723
743
  timer = setTimeout(() => reject(new ModbusOperationTimeoutError(operation, unitId, timeoutMs)), timeoutMs);
724
744
  });
725
- return Promise.race([fn(), timeout]).finally(() => {
745
+ let op;
746
+ try {
747
+ op = fn();
748
+ }
749
+ catch (err) {
750
+ // Nothing will race `timeout`; disarm it so it can't reject unobserved.
751
+ if (timer !== undefined)
752
+ clearTimeout(timer);
753
+ return Promise.reject(err);
754
+ }
755
+ return Promise.race([op, timeout]).finally(() => {
726
756
  if (timer !== undefined)
727
757
  clearTimeout(timer);
728
758
  });
package/dist/version.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export declare const SDK_VERSION = "1.0.0";
8
+ export declare const SDK_VERSION = "1.0.1";
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/dist/version.js CHANGED
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Current version of the enyo Energy App SDK.
7
7
  */
8
- export const SDK_VERSION = '1.0.0';
8
+ export const SDK_VERSION = '1.0.1';
9
9
  /**
10
10
  * Gets the current SDK version.
11
11
  * @returns The semantic version string of the SDK
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enyo-energy/sunspec-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "enyo Energy Sunspec SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@enyo-energy/appliance-calibration": "0.0.3",
44
- "@enyo-energy/energy-app-sdk": "1.6.0"
44
+ "@enyo-energy/energy-app-sdk": "1.8.0"
45
45
  },
46
46
  "volta": {
47
47
  "node": "22.17.0"