@ya-modbus/transport 0.7.3 → 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 +6 -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 +2 -2
package/dist/esm/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { createRTUTransport } from './rtu-transport.js';
|
|
|
9
9
|
export { createTCPTransport } from './tcp-transport.js';
|
|
10
10
|
export { createModbusTransport } from './create-modbus-transport.js';
|
|
11
11
|
export { withRetry, MAX_RETRIES, RETRY_DELAY_MS } from './retry.js';
|
|
12
|
+
export { normalizeModbusError } from './normalize-error.js';
|
|
12
13
|
export { TransportManager } 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.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAwB,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAkB,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAkB,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAoB,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,gBAAgB,EAAuB,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAwB,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAkB,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,kBAAkB,EAAkB,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAoB,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,oBAAoB,EAAoB,MAAM,sBAAsB,CAAA;AAC7E,OAAO,EAAE,gBAAgB,EAAuB,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA"}
|
|
@@ -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,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error normalization for modbus-serial failures
|
|
3
|
+
*/
|
|
4
|
+
function nonEmptyString(value) {
|
|
5
|
+
return typeof value === 'string' && value !== '' ? value : undefined;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Convert anything thrown by modbus-serial into an Error
|
|
9
|
+
*
|
|
10
|
+
* modbus-serial rejects with plain objects that do not extend Error
|
|
11
|
+
* (e.g., `{ name: 'TransactionTimedOutError', message: 'Timed out', errno: 'ETIMEDOUT' }`).
|
|
12
|
+
* Converting them with `String()` gives "[object Object]" and loses errno.
|
|
13
|
+
* This keeps message, name, errno, code and modbusCode, and sets the original
|
|
14
|
+
* value as `cause`. Error instances are returned unchanged.
|
|
15
|
+
*
|
|
16
|
+
* @param error - Value that was thrown or used to reject a promise
|
|
17
|
+
* @returns Error instance with the diagnostic properties of the original
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeModbusError(error) {
|
|
20
|
+
if (error instanceof Error) {
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
if (typeof error !== 'object' || error === null) {
|
|
24
|
+
return new Error(String(error), { cause: error });
|
|
25
|
+
}
|
|
26
|
+
const fields = error;
|
|
27
|
+
const name = nonEmptyString(fields.name);
|
|
28
|
+
const code = nonEmptyString(fields.code);
|
|
29
|
+
const errno = typeof fields.errno === 'string' || typeof fields.errno === 'number' ? fields.errno : undefined;
|
|
30
|
+
const modbusCode = typeof fields.modbusCode === 'number' ? fields.modbusCode : undefined;
|
|
31
|
+
const normalized = new Error(nonEmptyString(fields.message) ??
|
|
32
|
+
code ??
|
|
33
|
+
(errno === undefined ? 'Unknown error' : String(errno)), { cause: error });
|
|
34
|
+
if (name !== undefined)
|
|
35
|
+
normalized.name = name;
|
|
36
|
+
if (code !== undefined)
|
|
37
|
+
normalized.code = code;
|
|
38
|
+
if (errno !== undefined)
|
|
39
|
+
normalized.errno = errno;
|
|
40
|
+
if (modbusCode !== undefined)
|
|
41
|
+
normalized.modbusCode = modbusCode;
|
|
42
|
+
return normalized;
|
|
43
|
+
}
|
|
44
|
+
//# 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;AAcH,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,MAAM,UAAU,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/esm/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/esm/src/retry.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Retry configuration and utilities for Modbus transport operations
|
|
3
3
|
*/
|
|
4
|
+
import { normalizeModbusError } from './normalize-error.js';
|
|
4
5
|
/** Maximum number of retry attempts for failed operations */
|
|
5
6
|
export const MAX_RETRIES = 3;
|
|
6
7
|
/** Delay between retry attempts in milliseconds */
|
|
@@ -21,7 +22,8 @@ function sleep(ms) {
|
|
|
21
22
|
* @param maxRetries - Maximum number of attempts (default: MAX_RETRIES)
|
|
22
23
|
* @param logger - Optional callback to log retry attempts
|
|
23
24
|
* @returns Promise resolving to the function's return value
|
|
24
|
-
* @throws The last error encountered if all retries are exhausted
|
|
25
|
+
* @throws The last error encountered if all retries are exhausted, as an Error
|
|
26
|
+
* with the diagnostic properties of the original (see normalizeModbusError)
|
|
25
27
|
*/
|
|
26
28
|
export async function withRetry(fn, maxRetries = MAX_RETRIES, logger) {
|
|
27
29
|
let lastError = new Error('No retry attempts were made');
|
|
@@ -30,7 +32,7 @@ export async function withRetry(fn, maxRetries = MAX_RETRIES, logger) {
|
|
|
30
32
|
return await fn();
|
|
31
33
|
}
|
|
32
34
|
catch (error) {
|
|
33
|
-
lastError =
|
|
35
|
+
lastError = normalizeModbusError(error);
|
|
34
36
|
// Don't retry on the last attempt
|
|
35
37
|
if (attempt < maxRetries) {
|
|
36
38
|
// 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;AAEH,6DAA6D;AAC7D,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAA;AAE5B,mDAAmD;AACnD,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAA;AAEjC;;;;;GAKG;AACH,SAAS,KAAK,CAAC,EAAU;I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