@ya-modbus/transport 0.7.2 → 0.8.0
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/CHANGELOG.md +10 -0
- package/README.md +27 -0
- package/dist/cjs/src/index.d.ts +1 -0
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js +3 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/normalize-error.d.ts +28 -0
- package/dist/cjs/src/normalize-error.d.ts.map +1 -0
- package/dist/cjs/src/normalize-error.js +47 -0
- package/dist/cjs/src/normalize-error.js.map +1 -0
- package/dist/cjs/src/retry.d.ts +4 -2
- package/dist/cjs/src/retry.d.ts.map +1 -1
- package/dist/cjs/src/retry.js +4 -2
- package/dist/cjs/src/retry.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/index.d.ts +1 -0
- package/dist/esm/src/index.d.ts.map +1 -1
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/normalize-error.d.ts +28 -0
- package/dist/esm/src/normalize-error.d.ts.map +1 -0
- package/dist/esm/src/normalize-error.js +44 -0
- package/dist/esm/src/normalize-error.js.map +1 -0
- package/dist/esm/src/retry.d.ts +4 -2
- package/dist/esm/src/retry.d.ts.map +1 -1
- package/dist/esm/src/retry.js +4 -2
- package/dist/esm/src/retry.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.8.0](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.7.3...@ya-modbus/transport@0.8.0) (2026-10-12)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **transport:** keep details of modbus-serial errors instead of "[object Object]" ([#512](https://github.com/groupsky/ya-modbus/issues/512)) ([343dd50](https://github.com/groupsky/ya-modbus/commit/343dd50d011c7e06e86e31701e25a8d5b9e861dc)), closes [#177](https://github.com/groupsky/ya-modbus/issues/177)
|
|
11
|
+
|
|
12
|
+
## [0.7.3](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.7.2...@ya-modbus/transport@0.7.3) (2026-10-12)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @ya-modbus/transport
|
|
15
|
+
|
|
6
16
|
## [0.7.2](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/transport@0.7.1...@ya-modbus/transport@0.7.2) (2026-02-11)
|
|
7
17
|
|
|
8
18
|
### Bug Fixes
|
package/README.md
CHANGED
|
@@ -42,6 +42,33 @@ const tcpTransport = await createTransport({
|
|
|
42
42
|
await rtuTransport.readHoldingRegisters(0, 10)
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## Error handling
|
|
46
|
+
|
|
47
|
+
Some `modbus-serial` failures (response timeouts, closed port) are plain objects instead of `Error`
|
|
48
|
+
instances. Transport read and write methods reject with an `Error` that keeps the diagnostic
|
|
49
|
+
properties of the original (the `ModbusError` type). The original value is available as `cause`:
|
|
50
|
+
|
|
51
|
+
| Property | Example | Meaning |
|
|
52
|
+
| ------------ | -------------------------- | --------------------------------------------- |
|
|
53
|
+
| `name` | `TransactionTimedOutError` | Error type reported by `modbus-serial` |
|
|
54
|
+
| `errno` | `ETIMEDOUT`, `-111` | Error name or number (timeout, refused, etc.) |
|
|
55
|
+
| `code` | `ECONNREFUSED` | Node.js error code (TCP socket errors) |
|
|
56
|
+
| `modbusCode` | `2` | Exception code returned by the device |
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import type { ModbusError } from '@ya-modbus/transport'
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
await transport.readHoldingRegisters(0, 10)
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if ((error as ModbusError).errno === 'ETIMEDOUT') {
|
|
65
|
+
// device did not answer
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`normalizeModbusError(error)` applies the same conversion to any caught value.
|
|
71
|
+
|
|
45
72
|
## License
|
|
46
73
|
|
|
47
74
|
GPL-3.0-or-later
|
package/dist/cjs/src/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { createRTUTransport, type RTUConfig } from './rtu-transport.js';
|
|
|
9
9
|
export { createTCPTransport, type TCPConfig } from './tcp-transport.js';
|
|
10
10
|
export { createModbusTransport } from './create-modbus-transport.js';
|
|
11
11
|
export { withRetry, MAX_RETRIES, RETRY_DELAY_MS, type RetryLogger } from './retry.js';
|
|
12
|
+
export { normalizeModbusError, type ModbusError } from './normalize-error.js';
|
|
12
13
|
export { TransportManager, type TransportStats } from './manager.js';
|
|
13
14
|
export { MutexTransport } from './mutex-transport.js';
|
|
14
15
|
export { SlaveTransport } from './slave-transport.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,gBAAgB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,oBAAoB,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAC7E,OAAO,EAAE,gBAAgB,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA"}
|
package/dist/cjs/src/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* and transport pooling with mutex-based RTU bus serialization
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.SlaveTransport = exports.MutexTransport = exports.TransportManager = exports.RETRY_DELAY_MS = exports.MAX_RETRIES = exports.withRetry = exports.createModbusTransport = exports.createTCPTransport = exports.createRTUTransport = exports.createTransport = void 0;
|
|
9
|
+
exports.SlaveTransport = exports.MutexTransport = exports.TransportManager = exports.normalizeModbusError = exports.RETRY_DELAY_MS = exports.MAX_RETRIES = exports.withRetry = exports.createModbusTransport = exports.createTCPTransport = exports.createRTUTransport = exports.createTransport = void 0;
|
|
10
10
|
var factory_js_1 = require("./factory.js");
|
|
11
11
|
Object.defineProperty(exports, "createTransport", { enumerable: true, get: function () { return factory_js_1.createTransport; } });
|
|
12
12
|
var rtu_transport_js_1 = require("./rtu-transport.js");
|
|
@@ -19,6 +19,8 @@ var retry_js_1 = require("./retry.js");
|
|
|
19
19
|
Object.defineProperty(exports, "withRetry", { enumerable: true, get: function () { return retry_js_1.withRetry; } });
|
|
20
20
|
Object.defineProperty(exports, "MAX_RETRIES", { enumerable: true, get: function () { return retry_js_1.MAX_RETRIES; } });
|
|
21
21
|
Object.defineProperty(exports, "RETRY_DELAY_MS", { enumerable: true, get: function () { return retry_js_1.RETRY_DELAY_MS; } });
|
|
22
|
+
var normalize_error_js_1 = require("./normalize-error.js");
|
|
23
|
+
Object.defineProperty(exports, "normalizeModbusError", { enumerable: true, get: function () { return normalize_error_js_1.normalizeModbusError; } });
|
|
22
24
|
var manager_js_1 = require("./manager.js");
|
|
23
25
|
Object.defineProperty(exports, "TransportManager", { enumerable: true, get: function () { return manager_js_1.TransportManager; } });
|
|
24
26
|
var mutex_transport_js_1 = require("./mutex-transport.js");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,2CAAoE;AAA3D,6GAAA,eAAe,OAAA;AACxB,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,2EAAoE;AAA3D,mIAAA,qBAAqB,OAAA;AAC9B,uCAAqF;AAA5E,qGAAA,SAAS,OAAA;AAAE,uGAAA,WAAW,OAAA;AAAE,0GAAA,cAAc,OAAA;AAC/C,2CAAoE;AAA3D,8GAAA,gBAAgB,OAAA;AACzB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA;AACvB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,2CAAoE;AAA3D,6GAAA,eAAe,OAAA;AACxB,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,uDAAuE;AAA9D,sHAAA,kBAAkB,OAAA;AAC3B,2EAAoE;AAA3D,mIAAA,qBAAqB,OAAA;AAC9B,uCAAqF;AAA5E,qGAAA,SAAS,OAAA;AAAE,uGAAA,WAAW,OAAA;AAAE,0GAAA,cAAc,OAAA;AAC/C,2DAA6E;AAApE,0HAAA,oBAAoB,OAAA;AAC7B,2CAAoE;AAA3D,8GAAA,gBAAgB,OAAA;AACzB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA;AACvB,2DAAqD;AAA5C,oHAAA,cAAc,OAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error normalization for modbus-serial failures
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Error with the diagnostic properties that modbus-serial attaches to failures
|
|
6
|
+
*/
|
|
7
|
+
export interface ModbusError extends Error {
|
|
8
|
+
/** Node.js error code (e.g., 'ECONNREFUSED' from a TCP socket) */
|
|
9
|
+
code?: string | undefined;
|
|
10
|
+
/** Error number or name (e.g., 'ETIMEDOUT' for a response timeout, -111 from a TCP socket) */
|
|
11
|
+
errno?: string | number | undefined;
|
|
12
|
+
/** Modbus exception code returned by the device (e.g., 2 = illegal data address) */
|
|
13
|
+
modbusCode?: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Convert anything thrown by modbus-serial into an Error
|
|
17
|
+
*
|
|
18
|
+
* modbus-serial rejects with plain objects that do not extend Error
|
|
19
|
+
* (e.g., `{ name: 'TransactionTimedOutError', message: 'Timed out', errno: 'ETIMEDOUT' }`).
|
|
20
|
+
* Converting them with `String()` gives "[object Object]" and loses errno.
|
|
21
|
+
* This keeps message, name, errno, code and modbusCode, and sets the original
|
|
22
|
+
* value as `cause`. Error instances are returned unchanged.
|
|
23
|
+
*
|
|
24
|
+
* @param error - Value that was thrown or used to reject a promise
|
|
25
|
+
* @returns Error instance with the diagnostic properties of the original
|
|
26
|
+
*/
|
|
27
|
+
export declare function normalizeModbusError(error: unknown): ModbusError;
|
|
28
|
+
//# sourceMappingURL=normalize-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize-error.d.ts","sourceRoot":"","sources":["../../../src/normalize-error.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,kEAAkE;IAClE,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,8FAA8F;IAC9F,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;IACnC,oFAAoF;IACpF,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAChC;AAMD;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAkChE"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error normalization for modbus-serial failures
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalizeModbusError = normalizeModbusError;
|
|
7
|
+
function nonEmptyString(value) {
|
|
8
|
+
return typeof value === 'string' && value !== '' ? value : undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Convert anything thrown by modbus-serial into an Error
|
|
12
|
+
*
|
|
13
|
+
* modbus-serial rejects with plain objects that do not extend Error
|
|
14
|
+
* (e.g., `{ name: 'TransactionTimedOutError', message: 'Timed out', errno: 'ETIMEDOUT' }`).
|
|
15
|
+
* Converting them with `String()` gives "[object Object]" and loses errno.
|
|
16
|
+
* This keeps message, name, errno, code and modbusCode, and sets the original
|
|
17
|
+
* value as `cause`. Error instances are returned unchanged.
|
|
18
|
+
*
|
|
19
|
+
* @param error - Value that was thrown or used to reject a promise
|
|
20
|
+
* @returns Error instance with the diagnostic properties of the original
|
|
21
|
+
*/
|
|
22
|
+
function normalizeModbusError(error) {
|
|
23
|
+
if (error instanceof Error) {
|
|
24
|
+
return error;
|
|
25
|
+
}
|
|
26
|
+
if (typeof error !== 'object' || error === null) {
|
|
27
|
+
return new Error(String(error), { cause: error });
|
|
28
|
+
}
|
|
29
|
+
const fields = error;
|
|
30
|
+
const name = nonEmptyString(fields.name);
|
|
31
|
+
const code = nonEmptyString(fields.code);
|
|
32
|
+
const errno = typeof fields.errno === 'string' || typeof fields.errno === 'number' ? fields.errno : undefined;
|
|
33
|
+
const modbusCode = typeof fields.modbusCode === 'number' ? fields.modbusCode : undefined;
|
|
34
|
+
const normalized = new Error(nonEmptyString(fields.message) ??
|
|
35
|
+
code ??
|
|
36
|
+
(errno === undefined ? 'Unknown error' : String(errno)), { cause: error });
|
|
37
|
+
if (name !== undefined)
|
|
38
|
+
normalized.name = name;
|
|
39
|
+
if (code !== undefined)
|
|
40
|
+
normalized.code = code;
|
|
41
|
+
if (errno !== undefined)
|
|
42
|
+
normalized.errno = errno;
|
|
43
|
+
if (modbusCode !== undefined)
|
|
44
|
+
normalized.modbusCode = modbusCode;
|
|
45
|
+
return normalized;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=normalize-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize-error.js","sourceRoot":"","sources":["../../../src/normalize-error.ts"],"names":[],"mappings":";AAAA;;GAEG;;AA8BH,oDAkCC;AAlDD,SAAS,cAAc,CAAC,KAAc;IACpC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AACtE,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,oBAAoB,CAAC,KAAc;IACjD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;IACnD,CAAC;IAED,MAAM,MAAM,GAAG,KAMd,CAAA;IACD,MAAM,IAAI,GAAG,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACxC,MAAM,IAAI,GAAG,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACxC,MAAM,KAAK,GACT,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IACjG,MAAM,UAAU,GAAG,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAA;IAExF,MAAM,UAAU,GAAgB,IAAI,KAAK,CACvC,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC;QAC5B,IAAI;QACJ,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EACzD,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAA;IACD,IAAI,IAAI,KAAK,SAAS;QAAE,UAAU,CAAC,IAAI,GAAG,IAAI,CAAA;IAC9C,IAAI,IAAI,KAAK,SAAS;QAAE,UAAU,CAAC,IAAI,GAAG,IAAI,CAAA;IAC9C,IAAI,KAAK,KAAK,SAAS;QAAE,UAAU,CAAC,KAAK,GAAG,KAAK,CAAA;IACjD,IAAI,UAAU,KAAK,SAAS;QAAE,UAAU,CAAC,UAAU,GAAG,UAAU,CAAA;IAEhE,OAAO,UAAU,CAAA;AACnB,CAAC"}
|
package/dist/cjs/src/retry.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Retry configuration and utilities for Modbus transport operations
|
|
3
3
|
*/
|
|
4
|
+
import { type ModbusError } from './normalize-error.js';
|
|
4
5
|
/** Maximum number of retry attempts for failed operations */
|
|
5
6
|
export declare const MAX_RETRIES = 3;
|
|
6
7
|
/** Delay between retry attempts in milliseconds */
|
|
@@ -11,7 +12,7 @@ export declare const RETRY_DELAY_MS = 100;
|
|
|
11
12
|
* @param attempt - The attempt number (1-indexed)
|
|
12
13
|
* @param error - The error that caused the retry
|
|
13
14
|
*/
|
|
14
|
-
export type RetryLogger = (attempt: number, error:
|
|
15
|
+
export type RetryLogger = (attempt: number, error: ModbusError) => void;
|
|
15
16
|
/**
|
|
16
17
|
* Retry a function with fixed delay between attempts
|
|
17
18
|
*
|
|
@@ -19,7 +20,8 @@ export type RetryLogger = (attempt: number, error: Error) => void;
|
|
|
19
20
|
* @param maxRetries - Maximum number of attempts (default: MAX_RETRIES)
|
|
20
21
|
* @param logger - Optional callback to log retry attempts
|
|
21
22
|
* @returns Promise resolving to the function's return value
|
|
22
|
-
* @throws The last error encountered if all retries are exhausted
|
|
23
|
+
* @throws The last error encountered if all retries are exhausted, as an Error
|
|
24
|
+
* with the diagnostic properties of the original (see normalizeModbusError)
|
|
23
25
|
*/
|
|
24
26
|
export declare function withRetry<T>(fn: () => Promise<T>, maxRetries?: number, logger?: RetryLogger): Promise<T>;
|
|
25
27
|
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../../src/retry.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,6DAA6D;AAC7D,eAAO,MAAM,WAAW,IAAI,CAAA;AAE5B,mDAAmD;AACnD,eAAO,MAAM,cAAc,MAAM,CAAA;AAYjC;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../../src/retry.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAwB,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAE7E,6DAA6D;AAC7D,eAAO,MAAM,WAAW,IAAI,CAAA;AAE5B,mDAAmD;AACnD,eAAO,MAAM,cAAc,MAAM,CAAA;AAYjC;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,KAAK,IAAI,CAAA;AAEvE;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,UAAU,GAAE,MAAoB,EAChC,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,CAAC,CAAC,CAqBZ"}
|
package/dist/cjs/src/retry.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.RETRY_DELAY_MS = exports.MAX_RETRIES = void 0;
|
|
7
7
|
exports.withRetry = withRetry;
|
|
8
|
+
const normalize_error_js_1 = require("./normalize-error.js");
|
|
8
9
|
/** Maximum number of retry attempts for failed operations */
|
|
9
10
|
exports.MAX_RETRIES = 3;
|
|
10
11
|
/** Delay between retry attempts in milliseconds */
|
|
@@ -25,7 +26,8 @@ function sleep(ms) {
|
|
|
25
26
|
* @param maxRetries - Maximum number of attempts (default: MAX_RETRIES)
|
|
26
27
|
* @param logger - Optional callback to log retry attempts
|
|
27
28
|
* @returns Promise resolving to the function's return value
|
|
28
|
-
* @throws The last error encountered if all retries are exhausted
|
|
29
|
+
* @throws The last error encountered if all retries are exhausted, as an Error
|
|
30
|
+
* with the diagnostic properties of the original (see normalizeModbusError)
|
|
29
31
|
*/
|
|
30
32
|
async function withRetry(fn, maxRetries = exports.MAX_RETRIES, logger) {
|
|
31
33
|
let lastError = new Error('No retry attempts were made');
|
|
@@ -34,7 +36,7 @@ async function withRetry(fn, maxRetries = exports.MAX_RETRIES, logger) {
|
|
|
34
36
|
return await fn();
|
|
35
37
|
}
|
|
36
38
|
catch (error) {
|
|
37
|
-
lastError =
|
|
39
|
+
lastError = (0, normalize_error_js_1.normalizeModbusError)(error);
|
|
38
40
|
// Don't retry on the last attempt
|
|
39
41
|
if (attempt < maxRetries) {
|
|
40
42
|
// Log the retry attempt if logger is provided
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../../src/retry.ts"],"names":[],"mappings":";AAAA;;GAEG;;;
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../../src/retry.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAsCH,8BAyBC;AA7DD,6DAA6E;AAE7E,6DAA6D;AAChD,QAAA,WAAW,GAAG,CAAC,CAAA;AAE5B,mDAAmD;AACtC,QAAA,cAAc,GAAG,GAAG,CAAA;AAEjC;;;;;GAKG;AACH,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAC1D,CAAC;AAUD;;;;;;;;;GASG;AACI,KAAK,UAAU,SAAS,CAC7B,EAAoB,EACpB,aAAqB,mBAAW,EAChC,MAAoB;IAEpB,IAAI,SAAS,GAAgB,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;IAErE,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC;QACvD,IAAI,CAAC;YACH,OAAO,MAAM,EAAE,EAAE,CAAA;QACnB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,IAAA,yCAAoB,EAAC,KAAK,CAAC,CAAA;YAEvC,kCAAkC;YAClC,IAAI,OAAO,GAAG,UAAU,EAAE,CAAC;gBACzB,8CAA8C;gBAC9C,IAAI,MAAM,EAAE,CAAC;oBACX,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;gBAC5B,CAAC;gBACD,MAAM,KAAK,CAAC,sBAAc,CAAC,CAAA;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,SAAS,CAAA;AACjB,CAAC"}
|