@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.
- package/dist/cjs/sunspec-modbus-client.cjs +32 -2
- package/dist/cjs/sunspec-modbus-client.d.cts +7 -0
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-modbus-client.d.ts +7 -0
- package/dist/sunspec-modbus-client.js +32 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
/**
|
package/dist/cjs/version.cjs
CHANGED
package/dist/cjs/version.d.cts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enyo-energy/sunspec-sdk",
|
|
3
|
-
"version": "1.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.
|
|
44
|
+
"@enyo-energy/energy-app-sdk": "1.8.0"
|
|
45
45
|
},
|
|
46
46
|
"volta": {
|
|
47
47
|
"node": "22.17.0"
|