The Port Client
class offers a killer feature: fast operations.
The Port Client
class allows users to interact with network ports, providing functionalities to check whether a port is active, kill a port, and verify the existence of ports. It supports both TCP and UDP protocols, works on multiple platforms (Windows and Unix-like systems), and includes a dryRun
mode for testing.
The --fast
flag for the CLI or the speed: 'fast'
option for the script provides fast operations. This option makes port operations significantly faster by bypassing slower checks like listing all active ports. It's particularly useful when you need to check or kill ports quickly.
When using the --fast
flag or speed: 'fast'
, the script will skip certain checks and perform actions directly on the specified ports.
ports
: The port(s) to operate on (either a number, an array, or a range).options
: Configuration options for the port operation, such as action (check
,kill
), protocol (tcp
orudp
), speed (safe
orfast
), and interactive mode.
You can also use the Port Client
class via a command-line interface by calling it from a cli.js
script. This allows you to interact with the ports directly from your terminal.
- To check port(s):
npx port-client 3000
- To kill a port(s):
npx port-client 3000 --kill
This script accepts a list of ports followed by an action (check
or kill
). The ports will be parsed, and the specified action will be executed.
You can enable fast operations in the CLI by using the --fast
flag:
npx port-client 3000 check --fast
This will perform the operation 100 times faster by skipping extra checks.
The constructor of the Port Client
class initializes an instance with the ports and options for the port operation. Below are the parameters available for the constructor.
ports
: The port(s) to operate on (either a number, an array, or a range).options
: Configuration options for the port operation. The available options are:action
: Action to perform on the port(s) (check
,kill
,isExist
). Default ischeck
.method
: The protocol method to use (tcp
orudp
). Default istcp
.interactive
: Whether to enable interactive mode for selecting ports. Default isfalse
.dryRun
: Iftrue
, no actual changes are made (dry run). Default isfalse
.verbose
: Iftrue
, enables verbose logging. Default isfalse
.graceful
: Iftrue
, uses graceful termination for killing ports. Default isfalse
.filter
: A filter for limiting results. Default isnull
.range
: A range of ports to check. Default isnull
.speed
: The speed mode to use (safe
orfast
). Default issafe
.
import port from 'port-client';
const ports = [8080, 3000];
const options = {
action: 'check', // Action to perform
method: 'tcp', // Protocol method
interactive: true, // Enable interactive mode
dryRun: false, // Dry run (no actual changes)
verbose: true, // Enable verbose logging
graceful: true, // Use graceful kill
speed: 'safe', // Safe speed mode (default)
};
const port = new port(ports, options);
port.execute()
.then(() => console.log('Port operations complete.'))
.catch((error) => console.error('Error during port operations:', error));
The Port Client
class offers a flexible and interactive way to manage ports, whether you're checking if they're active, killing processes associated with them, or performing dry runs to preview actions. It can be used in a Node.js script or directly in the shell using the provided CLI script.