diff --git a/ModbusRTU.d.ts b/ModbusRTU.d.ts index 8b60bc1..26a1404 100644 --- a/ModbusRTU.d.ts +++ b/ModbusRTU.d.ts @@ -1,12 +1,26 @@ import { Socket, SocketConstructorOpts, TcpSocketConnectOpts } from 'net'; import { TestPort } from "./TestPort"; -import { PortInfo } from '@serialport/bindings-cpp'; + +/** + * Subset of `serialport` `SerialPort.list()` entries — defined here so typings do not require + * `@serialport/bindings-cpp` at install time when using `npm install --no-optional`. Values from + * `getPorts()` are compatible with `PortInfo` when `serialport` is installed. + */ +export interface ModbusSerialPortListEntry { + path: string; + manufacturer?: string; + serialNumber?: string; + pnpId?: string; + locationId?: string; + productId?: string; + vendorId?: string; +} export class ModbusRTU { constructor(port?: any); static TestPort: typeof TestPort - static getPorts(): Promise + static getPorts(): Promise open(callback?: Function): void; close(callback?: Function): void; diff --git a/README.md b/README.md index e8d61b3..ecaac6a 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,27 @@ try these options on npm install to build, if you have problems to install --unsafe-perm --build-from-source +##### Optional `serialport` dependency + +[`serialport`](https://www.npmjs.com/package/serialport) is an **optional** dependency: npm tries to install it, but if the install fails (or you skip optionals), `modbus-serial` still installs and TCP/UDP usage works. + +**Skip installing optional dependencies** (no native serial build, smaller tree): + + npm install modbus-serial --no-optional + +Or in `.npmrc`: `optional=false` + +If you skipped or dropped `serialport` and later need serial RTU/ASCII, install it explicitly: + + npm install serialport + +Without `serialport`, serial-specific APIs fail at **runtime** when used, for example: + +- `ModbusRTU.getPorts()`, `connectRTU`, `connectRTUBuffered`, `connectAsciiSerial` +- `ModbusRTU.RTUBufferedPort`, `ModbusRTU.ServerSerial` (not exported if `serialport` cannot be loaded) + +TypeScript: `ServerSerial.d.ts` imports `serialport` types. If you use `--no-optional` and have no `serialport` in the tree, use **`skipLibCheck`** or add **`serialport` as a devDependency** for type-checking. + #### Development This repo uses **npm** and a committed **`package-lock.json`**. After cloning: diff --git a/ServerSerial.d.ts b/ServerSerial.d.ts index b733646..d11ca6f 100644 --- a/ServerSerial.d.ts +++ b/ServerSerial.d.ts @@ -1,3 +1,7 @@ +/** + * References `serialport` / `@serialport/stream`. `serialport` is optional; add it (or `skipLibCheck`) + * if this file must type-check when optional deps were skipped. + */ import * as events from 'events'; import * as stream from 'stream'; import { SerialPortOpenOptions } from 'serialport'; diff --git a/index.d.ts b/index.d.ts index f88a711..71632b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,9 @@ -import {ModbusRTU} from "./ModbusRTU"; +/** + * `serialport` is an optional dependency. Some declarations import `serialport` modules; if it is + * not installed (`npm install --no-optional`), TypeScript may not resolve those modules — use + * `skipLibCheck` or add `serialport` as a devDependency. Runtime serial APIs are optional (README). + */ +import { ModbusRTU } from "./ModbusRTU"; export * from "./ServerTCP"; export * from "./ServerSerial"; diff --git a/package-lock.json b/package-lock.json index 1750981..139cf4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,7 @@ "version": "8.0.23", "license": "ISC", "dependencies": { - "debug": "^4.3.1", - "serialport": "^12.0.0" + "debug": "^4.3.1" }, "devDependencies": { "@eslint/js": "^9.39.1", @@ -26,6 +25,9 @@ "sinon": "^21.0.3", "web-bluetooth-mock": "^1.2.0", "webbluetooth": "^3.6.0" + }, + "optionalDependencies": { + "serialport": "^12.0.0" } }, "node_modules/@babel/code-frame": { @@ -769,6 +771,7 @@ "resolved": "https://registry.npmjs.org/@serialport/binding-mock/-/binding-mock-10.2.2.tgz", "integrity": "sha512-HAFzGhk9OuFMpuor7aT5G1ChPgn5qSsklTFOTUX72Rl6p0xwcSVsRtG/xaGp6bxpN7fI9D/S8THLBWbBgS6ldw==", "license": "MIT", + "optional": true, "dependencies": { "@serialport/bindings-interface": "^1.2.1", "debug": "^4.3.3" @@ -783,6 +786,7 @@ "integrity": "sha512-r2XOwY2dDvbW7dKqSPIk2gzsr6M6Qpe9+/Ngs94fNaNlcTRCV02PfaoDmRgcubpNVVcLATlxSxPTIDw12dbKOg==", "hasInstallScript": true, "license": "MIT", + "optional": true, "dependencies": { "@serialport/bindings-interface": "1.2.2", "@serialport/parser-readline": "11.0.0", @@ -802,6 +806,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-delimiter/-/parser-delimiter-11.0.0.tgz", "integrity": "sha512-aZLJhlRTjSmEwllLG7S4J8s8ctRAS0cbvCpO87smLvl3e4BgzbVgF6Z6zaJd3Aji2uSiYgfedCdNc4L6W+1E2g==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -814,6 +819,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-readline/-/parser-readline-11.0.0.tgz", "integrity": "sha512-rRAivhRkT3YO28WjmmG4FQX6L+KMb5/ikhyylRfzWPw0nSXy97+u07peS9CbHqaNvJkMhH1locp2H36aGMOEIA==", "license": "MIT", + "optional": true, "dependencies": { "@serialport/parser-delimiter": "11.0.0" }, @@ -829,6 +835,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", "license": "MIT", + "optional": true, "dependencies": { "ms": "2.1.2" }, @@ -845,13 +852,15 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/@serialport/bindings-interface": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@serialport/bindings-interface/-/bindings-interface-1.2.2.tgz", "integrity": "sha512-CJaUd5bLvtM9c5dmO9rPBHPXTa9R2UwpkJ0wdh9JCYcbrPWsKz+ErvR0hBLeo7NPeiFdjFO4sonRljiw4d2XiA==", "license": "MIT", + "optional": true, "engines": { "node": "^12.22 || ^14.13 || >=16" } @@ -861,6 +870,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-byte-length/-/parser-byte-length-12.0.0.tgz", "integrity": "sha512-0ei0txFAj+s6FTiCJFBJ1T2hpKkX8Md0Pu6dqMrYoirjPskDLJRgZGLqoy3/lnU1bkvHpnJO+9oJ3PB9v8rNlg==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -873,6 +883,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-cctalk/-/parser-cctalk-12.0.0.tgz", "integrity": "sha512-0PfLzO9t2X5ufKuBO34DQKLXrCCqS9xz2D0pfuaLNeTkyGUBv426zxoMf3rsMRodDOZNbFblu3Ae84MOQXjnZw==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -885,6 +896,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-delimiter/-/parser-delimiter-12.0.0.tgz", "integrity": "sha512-gu26tVt5lQoybhorLTPsH2j2LnX3AOP2x/34+DUSTNaUTzu2fBXw+isVjQJpUBFWu6aeQRZw5bJol5X9Gxjblw==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -897,6 +909,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-inter-byte-timeout/-/parser-inter-byte-timeout-12.0.0.tgz", "integrity": "sha512-GnCh8K0NAESfhCuXAt+FfBRz1Cf9CzIgXfp7SdMgXwrtuUnCC/yuRTUFWRvuzhYKoAo1TL0hhUo77SFHUH1T/w==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -909,6 +922,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-packet-length/-/parser-packet-length-12.0.0.tgz", "integrity": "sha512-p1hiCRqvGHHLCN/8ZiPUY/G0zrxd7gtZs251n+cfNTn+87rwcdUeu9Dps3Aadx30/sOGGFL6brIRGK4l/t7MuQ==", "license": "MIT", + "optional": true, "engines": { "node": ">=8.6.0" } @@ -918,6 +932,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-readline/-/parser-readline-12.0.0.tgz", "integrity": "sha512-O7cywCWC8PiOMvo/gglEBfAkLjp/SENEML46BXDykfKP5mTPM46XMaX1L0waWU6DXJpBgjaL7+yX6VriVPbN4w==", "license": "MIT", + "optional": true, "dependencies": { "@serialport/parser-delimiter": "12.0.0" }, @@ -933,6 +948,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-ready/-/parser-ready-12.0.0.tgz", "integrity": "sha512-ygDwj3O4SDpZlbrRUraoXIoIqb8sM7aMKryGjYTIF0JRnKeB1ys8+wIp0RFMdFbO62YriUDextHB5Um5cKFSWg==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -945,6 +961,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-regex/-/parser-regex-12.0.0.tgz", "integrity": "sha512-dCAVh4P/pZrLcPv9NJ2mvPRBg64L5jXuiRxIlyxxdZGH4WubwXVXY/kBTihQmiAMPxbT3yshSX8f2+feqWsxqA==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -957,6 +974,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-slip-encoder/-/parser-slip-encoder-12.0.0.tgz", "integrity": "sha512-0APxDGR9YvJXTRfY+uRGhzOhTpU5akSH183RUcwzN7QXh8/1jwFsFLCu0grmAUfi+fItCkR+Xr1TcNJLR13VNA==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -969,6 +987,7 @@ "resolved": "https://registry.npmjs.org/@serialport/parser-spacepacket/-/parser-spacepacket-12.0.0.tgz", "integrity": "sha512-dozONxhPC/78pntuxpz/NOtVps8qIc/UZzdc/LuPvVsqCoJXiRxOg6ZtCP/W58iibJDKPZPAWPGYeZt9DJxI+Q==", "license": "MIT", + "optional": true, "engines": { "node": ">=12.0.0" }, @@ -981,6 +1000,7 @@ "resolved": "https://registry.npmjs.org/@serialport/stream/-/stream-12.0.0.tgz", "integrity": "sha512-9On64rhzuqKdOQyiYLYv2lQOh3TZU/D3+IWCR5gk0alPel2nwpp4YwDEGiUBfrQZEdQ6xww0PWkzqth4wqwX3Q==", "license": "MIT", + "optional": true, "dependencies": { "@serialport/bindings-interface": "1.2.2", "debug": "4.3.4" @@ -997,6 +1017,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", "license": "MIT", + "optional": true, "dependencies": { "ms": "2.1.2" }, @@ -1013,7 +1034,8 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/@sinonjs/commons": { "version": "3.0.1", @@ -3378,7 +3400,8 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.0.0.tgz", "integrity": "sha512-vgbBJTS4m5/KkE16t5Ly0WW9hz46swAstv0hYYwMtbG7AznRhNyfLRe8HZAiWIpcHzoO7HxhLuBQj9rJ/Ho0ZA==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/node-api-headers": { "version": "1.8.0", @@ -3392,6 +3415,7 @@ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.0.tgz", "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==", "license": "MIT", + "optional": true, "bin": { "node-gyp-build": "bin.js", "node-gyp-build-optional": "optional.js", @@ -4249,6 +4273,7 @@ "resolved": "https://registry.npmjs.org/serialport/-/serialport-12.0.0.tgz", "integrity": "sha512-AmH3D9hHPFmnF/oq/rvigfiAouAKyK/TjnrkwZRYSFZxNggJxwvbAbfYrLeuvq7ktUdhuHdVdSjj852Z55R+uA==", "license": "MIT", + "optional": true, "dependencies": { "@serialport/binding-mock": "10.2.2", "@serialport/bindings-cpp": "12.0.1", @@ -4277,6 +4302,7 @@ "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", "license": "MIT", + "optional": true, "dependencies": { "ms": "2.1.2" }, @@ -4293,7 +4319,8 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/set-blocking": { "version": "2.0.0", diff --git a/package.json b/package.json index d2dbf26..3ab0f33 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,23 @@ { "name": "modbus-serial", "version": "8.0.23", - "description": "A pure JavaScript implementation of MODBUS-RTU (Serial and TCP) for NodeJS.", + "description": "A pure JavaScript implementation of MODBUS-RTU (Serial and TCP) for NodeJS. The serialport package is an optional dependency (use npm install --no-optional to skip).", "main": "index.js", + "types": "index.d.ts", + "files": [ + "index.js", + "apis", + "ports", + "servers", + "utils", + "index.d.ts", + "ModbusRTU.d.ts", + "ServerTCP.d.ts", + "ServerSerial.d.ts", + "TestPort.d.ts", + "README.md", + "LICENSE" + ], "scripts": { "test": "nyc --reporter=lcov --reporter=text mocha --recursive", "lint": "eslint .", @@ -48,7 +63,9 @@ "webbluetooth": "^3.6.0" }, "dependencies": { - "debug": "^4.3.1", + "debug": "^4.3.1" + }, + "optionalDependencies": { "serialport": "^12.0.0" } }