Skip to content

Commit

Permalink
pkg: add type defs for type lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Sep 19, 2023
1 parent 4e61ba4 commit 2d475f3
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,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
run: npm run lint-types

test:
name: Test
runs-on: ${{ matrix.os }}
Expand Down
109 changes: 98 additions & 11 deletions lib/socks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ const net = require('net');
const {format} = require('util');
const IP = require('binet');

/**
* @typedef {Object} ServerOptions
* @property {string} host
* @property {number} port
* @property {string} [username]
* @property {string} [password]
*/

/**
* @typedef {Object} NameOption
* @property {string} [name]
*/

/**
* @typedef {Object} DestinationOptions
* @property {string} destHost
* @property {number} destPort
*/

/** @typedef {ServerOptions & NameOption} ResolveOptions */
/** @typedef {ResolveOptions & DestinationOptions} ProxyOptions */

/**
* SOCKS State Machine
* @extends EventEmitter
Expand Down Expand Up @@ -91,6 +113,11 @@ class SOCKS extends EventEmitter {
}
}

/**
* @param {number} port
* @param {string} host
*/

connect(port, host) {
assert(typeof port === 'number');
assert(typeof host === 'string');
Expand Down Expand Up @@ -123,6 +150,10 @@ class SOCKS extends EventEmitter {
});
}

/**
* @param {ResolveOptions} options
*/

open(options) {
assert(this.state === SOCKS.states.INIT);

Expand All @@ -144,6 +175,10 @@ class SOCKS extends EventEmitter {
this.connect(options.port, options.host);
}

/**
* @param {ProxyOptions} options
*/

proxy(options) {
assert(options);
assert(typeof options.destHost === 'string');
Expand All @@ -156,6 +191,10 @@ class SOCKS extends EventEmitter {
this.open(options);
}

/**
* @param {ResolveOptions} options
*/

resolve(options) {
assert(options);
assert(typeof options.name === 'string');
Expand Down Expand Up @@ -336,13 +375,13 @@ class SOCKS extends EventEmitter {
let ip, len, type, name;

switch (IP.getStringType(host)) {
case IP.types.IPV4:
case IP.types.INET4:
ip = IP.toBuffer(host);
type = 0x01;
name = ip.slice(12, 16);
len = 4;
break;
case IP.types.IPV6:
case IP.types.INET6:
ip = IP.toBuffer(host);
type = 0x04;
name = ip;
Expand Down Expand Up @@ -471,6 +510,11 @@ class SOCKS extends EventEmitter {
this.emit('resolve', [addr.host]);
}

/**
* @param {ResolveOptions} options
* @returns {Promise<string[]>}
*/

static resolve(options) {
const socks = new SOCKS();
return new Promise((resolve, reject) => {
Expand All @@ -480,6 +524,11 @@ class SOCKS extends EventEmitter {
});
}

/**
* @param {ProxyOptions} options
* @returns {Promise<net.Socket>}
*/

static proxy(options) {
const socks = new SOCKS();
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -531,13 +580,16 @@ SOCKS.errors = [
*/

class ProxySocket extends EventEmitter {
/** @type {net.Socket} */
socket;

/**
* Create a proxy socket.
* @constructor
* @param {String} host
* @param {Number} port
* @param {String?} user
* @param {String?} pass
* @param {string} host
* @param {number} port
* @param {string} [user]
* @param {string} [pass]
*/

constructor(host, port, user, pass) {
Expand All @@ -557,10 +609,8 @@ class ProxySocket extends EventEmitter {
this.ops = [];
}

get encrypted() {
if (!this.socket)
return false;
return this.socket.encrypted || false;
sad() {
this.host;
}

get readable() {
Expand Down Expand Up @@ -617,6 +667,12 @@ class ProxySocket extends EventEmitter {
return this.socket.localPort;
}

/**
* @param {number} port
* @param {string} host
* @returns {Promise}
*/

async connect(port, host) {
assert(!this.socket, 'Already connected.');

Expand All @@ -635,7 +691,7 @@ class ProxySocket extends EventEmitter {
const type = IP.getStringType(host);

this.remoteAddress = host;
this.remoteFamily = type === IP.types.IPV6 ? 'IPv6' : 'IPv4';
this.remoteFamily = type === IP.types.INET6 ? 'IPv6' : 'IPv4';
this.remotePort = port;

let socket;
Expand Down Expand Up @@ -801,6 +857,11 @@ class ProxySocket extends EventEmitter {
* Helpers
*/

/**
* @param {string} host
* @returns {ServerOptions}
*/

function parseProxy(host) {
const index = host.indexOf('@');

Expand Down Expand Up @@ -828,6 +889,19 @@ function parseProxy(host) {
};
}

/**
* @typedef {Object} AddrInfo
* @property {string} host
* @property {number} port
* @property {number} type
*/

/**
* @param {Buffer} data
* @param {number} off
* @returns {AddrInfo}
*/

function parseAddr(data, off) {
if (data.length - off < 2)
throw new Error('Bad SOCKS address length.');
Expand Down Expand Up @@ -885,6 +959,13 @@ function parseAddr(data, off) {

exports.unsupported = false;

/**
* @param {string} proxy
* @param {number} destPort
* @param {string} destHost
* @returns {ProxySocket}
*/

exports.connect = function connect(proxy, destPort, destHost) {
const addr = parseProxy(proxy);
const host = addr.host;
Expand All @@ -898,6 +979,12 @@ exports.connect = function connect(proxy, destPort, destHost) {
return socket;
};

/**
* @param {string} proxy
* @param {string} name
* @returns {Promise<string[]>}
*/

exports.resolve = function resolve(proxy, name) {
const addr = parseProxy(proxy);
return SOCKS.resolve({
Expand Down
27 changes: 22 additions & 5 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
"main": "./lib/bsocks.js",
"scripts": {
"lint": "eslint lib/ test/",
"lint-types": "tsc -p .",
"test": "bmocha --reporter spec test/*-test.js"
},
"dependencies": {
"binet": "~0.3.8",
"binet": "~0.3.9",
"bsert": "~0.0.12"
},
"devDependencies": {
"bmocha": "^2.1.8"
"bmocha": "^2.1.8",
"bts-type-deps": "^0.0.3",
"bufio": "^1.2.1"
},
"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": "ES2020",
"lib": [
"ES2020"
],

"noEmit": true,

"allowJs": true,
"checkJs": true,
"maxNodeModuleJsDepth": 10,

"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 2d475f3

Please sign in to comment.