diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index f4ed5a0..9120d3f 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -17,7 +17,7 @@ jobs: node-version: 20.x - name: Install tools - run: npm install --location=global bslint + run: npm install --location=global bslint typescript - name: Install dependencies run: npm install @@ -25,6 +25,9 @@ jobs: - name: Lint run: npm run lint + - name: Lint types + run: npm run lint-types + cover: name: Coverage test runs-on: ubuntu-latest diff --git a/lib/ip.js b/lib/ip.js index 3845f34..0afc82e 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -29,6 +29,10 @@ const inet = require('./inet'); const onion = require('./onion'); const binet = exports; +/** @typedef {import('bufio').BufferWriter} BufferWriter */ +/** @typedef {import('bufio').StaticWriter} StaticWriter */ +/** @typedef {import('bufio').BufferReader} BufferReader */ + /* * Constants */ @@ -225,19 +229,21 @@ binet.write = function write(dst, str, off, size) { /** * Write an IP string to a buffer writer. - * @param {BufferWriter} bw + * @param {BufferWriter|StaticWriter} bw * @param {String} str * @param {Number} [size=16] - * @returns {BufferWriter} + * @returns {BufferWriter|StaticWriter} */ binet.writeBW = function writeBW(bw, str, size) { assert(bw && typeof bw === 'object'); // StaticWriter + // @ts-ignore if (bw.data) { - bw.offset = binet.write(bw.data, str, bw.offset, size); - return bw; + const sw = /** @type {StaticWriter} */(bw); + sw.offset = binet.write(sw.data, str, sw.offset, size); + return sw; } const buf = Buffer.alloc(size); @@ -696,7 +702,7 @@ binet.isEqualString = function isEqualString(a, b) { * Apply a network mask to IP. * @param {String} str * @param {String} mask - * @returns {Buffer} + * @returns {String} */ binet.maskString = function maskString(str, mask) { @@ -715,7 +721,7 @@ binet.maskString = function maskString(str, mask) { * to IP from CIDR bits. * @param {String} str * @param {Number} bits - * @returns {Buffer} + * @returns {String} */ binet.cidrString = function cidrString(str, bits) { @@ -1382,7 +1388,7 @@ binet.getReachability = function getReachability(src, dest) { * @private * @param {Number} filter * @param {Number} af - * @returns {String} + * @returns {String[]} */ binet._interfaces = function _interfaces(filter, af) { @@ -1472,7 +1478,7 @@ binet._interfaces = function _interfaces(filter, af) { /** * Get local IP from network interfaces. * @param {String?} family - IP family name. - * @returns {String} + * @returns {String[]} */ binet.getInterfaces = function getInterfaces(family) { @@ -1482,7 +1488,7 @@ binet.getInterfaces = function getInterfaces(family) { /** * Get local IP from network interfaces. * @param {String?} family - IP family name. - * @returns {String} + * @returns {String[]} */ binet.getLocal = function getLocal(family) { @@ -1492,7 +1498,7 @@ binet.getLocal = function getLocal(family) { /** * Get non-local IP from network interfaces. * @param {String?} family - IP family name. - * @returns {String} + * @returns {String[]} */ binet.getNonlocal = function getNonlocal(family) { @@ -1502,7 +1508,7 @@ binet.getNonlocal = function getNonlocal(family) { /** * Get private IP from network interfaces. * @param {String?} family - IP family name. - * @returns {String} + * @returns {String[]} */ binet.getPrivate = function getPrivate(family) { @@ -1512,7 +1518,7 @@ binet.getPrivate = function getPrivate(family) { /** * Get public IP from network interfaces. * @param {String?} family - IP family name. - * @returns {String} + * @returns {String[]} */ binet.getPublic = function getPublic(family) { @@ -1588,9 +1594,13 @@ binet.equal = binet.isEqualString; * Compat (deprecated) */ +// @ts-ignore types.NAME = 0; +// @ts-ignore types.DNS = 0; +// @ts-ignore types.IPV4 = 4; +// @ts-ignore types.IPV6 = 6; binet.toString = binet.encode; diff --git a/lib/onion.js b/lib/onion.js index 41e9c0b..76d5d49 100644 --- a/lib/onion.js +++ b/lib/onion.js @@ -39,7 +39,7 @@ onion.is = function is(raw) { /** * Encode onion address. - * @param {Buffer} key + * @param {Buffer} raw * @param {Function} sha3 * @returns {String} */ @@ -116,7 +116,7 @@ onion.isLegacy = function isLegacy(raw) { /** * Encode onion address. - * @param {Buffer} key + * @param {Buffer} raw * @returns {String} */ diff --git a/package-lock.json b/package-lock.json index 6285210..7b3fe61 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,13 @@ "version": "0.3.7", "license": "MIT", "dependencies": { - "bs32": "~0.1.5", - "bsert": "~0.0.10" + "bs32": "https://github.com/nodech/bs32#ts-lint", + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0", - "bufio": "^1.0.7" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3", + "bufio": "https://github.com/nodech/bufio#ts-lint" }, "engines": { "node": ">=8.0.0" @@ -35,8 +36,8 @@ }, "node_modules/bs32": { "version": "0.1.6", - "resolved": "https://registry.npmjs.org/bs32/-/bs32-0.1.6.tgz", - "integrity": "sha512-usjDesQqZ8ihHXOnOEQuAdymBHnJEfSd+aELFSg1jN/V3iAf12HrylHlRJwIt6DTMmXpBDQ+YBg3Q3DIYdhRgQ==", + "resolved": "git+ssh://git@github.com/nodech/bs32.git#6a625986ddf8f8290298f1970226bce76f6c389e", + "license": "MIT", "dependencies": { "bsert": "~0.0.10" }, @@ -52,13 +53,19 @@ "node": ">=8.0.0" } }, + "node_modules/bts-type-deps": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/bts-type-deps/-/bts-type-deps-0.0.3.tgz", + "integrity": "sha512-OQHGWhX5amae6Vj6ShlGaQu0sNCICgJ5YspNZPRzfR5RobrD+wjm5vkZK/J3EH5b/ymxqSWo9VkiFNpCxjaG2Q==", + "dev": true + }, "node_modules/bufio": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/bufio/-/bufio-1.2.0.tgz", - "integrity": "sha512-UlFk8z/PwdhYQTXSQQagwGAdtRI83gib2n4uy4rQnenxUM2yQi8lBDzF230BNk+3wAoZDxYRoBwVVUPgHa9MCA==", + "resolved": "git+ssh://git@github.com/nodech/bufio.git#59135453e7ca5190df32f216239e2c18a019f5bc", "dev": true, + "license": "MIT", "engines": { - "node": ">=8.0.0" + "node": ">=14.0.0" } } } diff --git a/package.json b/package.json index 30c9801..a91af55 100644 --- a/package.json +++ b/package.json @@ -18,16 +18,18 @@ "main": "./lib/binet.js", "scripts": { "lint": "eslint lib/ test/", + "lint-types": "tsc -p .", "test": "bmocha --reporter spec test/*-test.js", "test-ci": "nyc --reporter lcov bmocha -- --reporter spec test/*.js" }, "dependencies": { "bs32": "~0.1.5", - "bsert": "~0.0.10" + "bsert": "~0.0.12" }, "devDependencies": { - "bmocha": "^2.1.0", - "bufio": "^1.0.7" + "bmocha": "^2.1.8", + "bts-type-deps": "^0.0.3", + "bufio": "^1.2.0" }, "engines": { "node": ">=8.0.0" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..56c78ac --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,31 @@ +{ + "include": [ + "lib/**/*.js" + ], + "compilerOptions": { + "rootDir": ".", + "target": "ES2016", + "lib": [ + "ES2016" + ], + + "noEmit": true, + + "allowJs": true, + "checkJs": true, + "maxNodeModuleJsDepth": 2, + + "module": "commonjs", + "moduleResolution": "node", + "resolveJsonModule": true, + + "stripInternal": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": false, + + "typeRoots": [ + "node_modules/bts-type-deps/types" + ] + } +}