npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

port-client

v1.4.5

Published

A powerful utility for managing processes on specified ports, including options for checking port status, killing processes, handling multiple ports, enabling interactive mode, and supporting graceful termination.

Downloads

31,463

Readme

alt text

Port Client

The Port Client class offers a killer feature: fast operations.

Table of Contents

  1. Overview
  2. Command-Line Interface (CLI) Usage
  3. Class Constructor
  4. Usage Example

Overview

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.

Key Feature: Fast Operations

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.

Parameters:

  • 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 or udp), speed (safe or fast), and interactive mode.

Command-Line Interface (CLI) Usage

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.

Running the CLI

  1. To check port(s):
npx port-client 3000
  1. 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.

Fast Operation Flag

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.

Class Constructor

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.

Constructor Parameters:

  • 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 is check.
    • method: The protocol method to use (tcp or udp). Default is tcp.
    • interactive: Whether to enable interactive mode for selecting ports. Default is false.
    • dryRun: If true, no actual changes are made (dry run). Default is false.
    • verbose: If true, enables verbose logging. Default is false.
    • graceful: If true, uses graceful termination for killing ports. Default is false.
    • filter: A filter for limiting results. Default is null.
    • range: A range of ports to check. Default is null.
    • speed: The speed mode to use (safe or fast). Default is safe.

Example Usage:

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));

Conclusion

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.