Skip to content

Commit

Permalink
pkg: add type lints using tsc.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jul 6, 2023
1 parent 7b662e7 commit 1f889c8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ 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

- name: Lint
run: npm run lint

- name: Lint types
run: npm run lint-types

cover:
name: Coverage test
runs-on: ubuntu-latest
Expand Down
34 changes: 22 additions & 12 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/onion.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ onion.is = function is(raw) {

/**
* Encode onion address.
* @param {Buffer} key
* @param {Buffer} raw
* @param {Function} sha3
* @returns {String}
*/
Expand Down Expand Up @@ -116,7 +116,7 @@ onion.isLegacy = function isLegacy(raw) {

/**
* Encode onion address.
* @param {Buffer} key
* @param {Buffer} raw
* @returns {String}
*/

Expand Down
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit 1f889c8

Please sign in to comment.