njs-modbus 3.4.0 → 4.0.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/dist/utils.d.ts DELETED
@@ -1,164 +0,0 @@
1
- /**
2
- * RTU timing parameter — accepts either:
3
- * - a bare `number` in milliseconds (`0` to disable the timer entirely)
4
- * - `{ unit: 'ms', value: N }` — explicit milliseconds (equivalent to bare `N`)
5
- * - `{ unit: 'bit', value: N }` — bit-time approximation, derived from `baudRate`
6
- *
7
- * The bare-number form is the recommended default; the object form exists for
8
- * specs that quote bit-time. Pass `0` (or `{ unit: 'ms', value: 0 }`) to disable
9
- * the timer; either form short-circuits the baudRate-derived fallback.
10
- */
11
- type RtuTimingValue = number | {
12
- unit: 'bit' | 'ms';
13
- value: number;
14
- };
15
- /** User-facing RTU protocol options (supports both bit and ms units). */
16
- interface RtuProtocolOptions {
17
- /**
18
- * Inter-frame silence (Modbus RTU t3.5).
19
- *
20
- * - `20` or `{ unit: 'ms', value: 20 }` — 20 ms
21
- * - `{ unit: 'bit', value: 38.5 }` — spec bit-time approximation (default when `baudRate` is provided)
22
- * - `0` — disable t3.5 timing (immediate parse on every chunk; useful for
23
- * lossless transports such as RTU-over-TCP or PTY-based tests where the
24
- * wire's silence semantics do not apply)
25
- *
26
- * Per Modbus V1.02 §2.5.1.1, at baud rates > 19200 a fixed 1.75 ms is used
27
- * regardless of the bit value.
28
- */
29
- intervalBetweenFrames?: RtuTimingValue;
30
- /**
31
- * Inter-character timeout (Modbus RTU t1.5). Opt-in; **disabled** by default.
32
- *
33
- * - `1` or `{ unit: 'ms', value: 1 }` — 1 ms
34
- * - `{ unit: 'bit', value: 21 }` — bit-time approximation (~1.5 char times)
35
- * - `0` — disable explicitly
36
- *
37
- * Per Modbus V1.02 §2.5.1.1, at baud rates > 19200 a fixed 0.75 ms is used
38
- * regardless of the bit value.
39
- */
40
- interCharTimeout?: RtuTimingValue;
41
- /**
42
- * Enforces strict Modbus RTU timing. When true, any frame containing a t1.5
43
- * inter-character timeout event will be discarded immediately, even if the
44
- * CRC16 is valid.
45
- * @default false
46
- */
47
- strictTiming?: boolean;
48
- }
49
- /** Resolved RTU timing values in milliseconds. */
50
- interface ResolvedRtuTiming {
51
- intervalBetweenFrames: number;
52
- interCharTimeout: number;
53
- }
54
- /**
55
- * Resolve Modbus RTU timing parameters from user options into milliseconds.
56
- *
57
- * - `intervalBetweenFrames` (t3.5): if omitted and `baudRate` is present,
58
- * defaults to 38.5 bits per Modbus V1.02 §2.5.1.1. Pass `0` to disable.
59
- * - `interCharTimeout` (t1.5): opt-in; only resolved when explicitly provided.
60
- *
61
- * Per the spec, at baud rates > 19200 fixed values are used
62
- * (1.75 ms for t3.5, 0.75 ms for t1.5) regardless of the bit value.
63
- */
64
- declare function resolveRtuTiming(opts?: RtuProtocolOptions, baudRate?: number): ResolvedRtuTiming;
65
-
66
- /**
67
- * Convert a number of bits to milliseconds at a given baud rate.
68
- *
69
- * Used to derive Modbus RTU timing intervals from bit counts — e.g. 38.5 bits
70
- * = 3.5 character times at 11 bits/char (t3.5 inter-frame silence), or 16.5
71
- * bits = 1.5 character times (t1.5 inter-character timeout), per Modbus V1.02
72
- * §2.5.1.1.
73
- *
74
- * @param baudRate Serial port baud rate.
75
- * @param bits Number of bits to convert.
76
- * @returns Duration in milliseconds.
77
- */
78
- declare function bitsToMs(baudRate: number, bits: number): number;
79
-
80
- /**
81
- * Drain a pending-callback array: invoke each callback with the given error (or null).
82
- *
83
- * Handles `null`/`undefined` entries (from optional `cb?` parameters) gracefully.
84
- * Used by physical layers and connections to resolve queued
85
- * open / close / destroy callbacks.
86
- */
87
- declare function drainCbs(cbs: (((err?: Error | null) => void) | undefined)[] | null, err?: Error | null): void;
88
-
89
- declare function checkRange(value: number | number[], range?: [number, number] | [number, number][]): boolean;
90
-
91
- declare const CRC_TABLE: Uint16Array<ArrayBuffer>;
92
- /** CRC-16 (Modbus) over a single contiguous buffer. */
93
- declare function crcFixed(data: Uint8Array, start: number, end: number): number;
94
- /**
95
- * CRC-16 (Modbus) over two contiguous buffer segments.
96
- * Computes CRC(head[headOff:headOff+headLen]) followed by CRC(tail[tailOff:tailOff+tailLen]).
97
- */
98
- declare function crcDual(head: Uint8Array, headOff: number, headLen: number, tail: Uint8Array, tailOff: number, tailLen: number): number;
99
-
100
- declare function lrc(data: Uint8Array, start: number, end: number): number;
101
-
102
- declare const PREDICT_NEED_MORE = 0;
103
- declare const PREDICT_UNKNOWN = -1;
104
- declare const REQ_TABLE: Int32Array<ArrayBuffer>;
105
- declare const RES_TABLE: Int32Array<ArrayBuffer>;
106
- declare function predictRtuFrameLength(residual: Buffer, data: Buffer, residualLen: number, start: number, end: number, isResponse: boolean): number;
107
-
108
- /**
109
- * Convert a callback-style `(cb: (err?) => void) => void` call into a Promise.
110
- */
111
- declare function promisifyCb(fn: (cb: (err?: Error | null) => void) => void): Promise<void>;
112
-
113
- /**
114
- * Hybrid timer manager: uses native `setTimeout` for low concurrency
115
- * and switches to a binary min-heap when concurrency exceeds the threshold.
116
- *
117
- * Benchmarks (add + clear throughput, Node 24, x64):
118
- * 1 concurrent: setTimeout ~1.7× faster than heap
119
- * 2 concurrent: setTimeout ~1.6× faster than heap
120
- * 5 concurrent: setTimeout ~1.5-1.9× faster than heap
121
- * 10 concurrent: roughly equal
122
- * 20 concurrent: heap ~1.3× faster than setTimeout[]
123
- * 50 concurrent: heap ~1.4-1.7× faster than setTimeout[]
124
- *
125
- * The crossover point is around 10 concurrent timers, so the default
126
- * `concurrentThreshold = 2` keeps the common 1-2 request case on the
127
- * fast direct path while delegating to the heap for larger batches.
128
- */
129
- declare class TimerHeap {
130
- private _deadlines;
131
- private _ids;
132
- private _seqs;
133
- private _counter;
134
- private _timer;
135
- private _onFire;
136
- private _boundTick;
137
- private _threshold;
138
- private _mode;
139
- private _directTimers;
140
- /**
141
- * @param onFire Callback invoked with the timer id when it expires.
142
- * @param concurrentThreshold Maximum number of timers kept as individual
143
- * native `setTimeout` handles. Once exceeded, all timers migrate to
144
- * the internal heap and share a single native timer. Default is 2.
145
- */
146
- constructor(onFire: (id: number) => void, concurrentThreshold?: number);
147
- get size(): number;
148
- add(id: number, ms: number): void;
149
- clear(): void;
150
- private _heapAdd;
151
- private _refresh;
152
- private _onTick;
153
- private _pop;
154
- }
155
-
156
- /**
157
- * Check whether a remote address is allowed by the given whitelist.
158
- * IPv4-mapped IPv6 addresses are normalized before comparison.
159
- * Returns `true` when whitelist is absent or empty.
160
- */
161
- declare function isWhitelisted(address: string | undefined, whitelist: string[] | undefined): boolean;
162
-
163
- export { CRC_TABLE, PREDICT_NEED_MORE, PREDICT_UNKNOWN, REQ_TABLE, RES_TABLE, TimerHeap, bitsToMs, checkRange, crcDual, crcFixed, drainCbs, isWhitelisted, lrc, predictRtuFrameLength, promisifyCb, resolveRtuTiming };
164
- export type { ResolvedRtuTiming, RtuProtocolOptions };