UNPKG

port-client

Version:

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.

2,899 lines (744 loc) 30.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Class: PortClient</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Class: PortClient</h1> <section> <header> <h2><span class="attribs"><span class="type-signature"></span></span>PortClient<span class="signature">(ports, options)</span><span class="type-signature"></span></h2> <div class="class-description">A class to manage port operations (kill, check, or verify existence).</div> </header> <article> <div class="container-overview"> <h2>Constructor</h2> <h4 class="name" id="PortClient"><span class="type-signature"></span>new PortClient<span class="signature">(ports, options)</span><span class="type-signature"></span></h4> <div class="description"> Creates an instance of PortClient </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>ports</code></td> <td class="type"> <span class="param-type">string</span> | <span class="param-type">number</span> | <span class="param-type">Array.&lt;number></span> </td> <td class="description last">Ports to be checked or killed.</td> </tr> <tr> <td class="name"><code>options</code></td> <td class="type"> <span class="param-type">Object</span> </td> <td class="description last">Options for the operation.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line9">line 9</a> </li></ul></dd> </dl> </div> <h3 class="subsection-title">Methods</h3> <h4 class="name" id="buildBaseCommand"><span class="type-signature"></span>buildBaseCommand<span class="signature">(method, port)</span><span class="type-signature"> &rarr; {string}</span></h4> <div class="description"> Builds the base command for listing the process associated with a port and method. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>method</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="description last">The method (protocol) for the command (e.g., 'tcp' or 'udp').</td> </tr> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">number</span> </td> <td class="description last">The port number where the process is running.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line319">line 319</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The base command to find the process ID (PID) of the process running on the specified port and protocol. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">string</span> </dd> </dl> <h4 class="name" id="checkIfProcessExists"><span class="type-signature">(async) </span>checkIfProcessExists<span class="signature">(port)</span><span class="type-signature"> &rarr; {Promise.&lt;boolean>}</span></h4> <div class="description"> Checks if a process is running on the specified port by using either a fast or normal check based on the current speed setting. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">number</span> </td> <td class="description last">The port number where the process is running.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line329">line 329</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> A boolean indicating whether a process is running on the specified port. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;boolean></span> </dd> </dl> <h4 class="name" id="checkPortStatus"><span class="type-signature">(async) </span>checkPortStatus<span class="signature">(port)</span><span class="type-signature"> &rarr; {Promise.&lt;boolean>}</span></h4> <div class="description"> Checks whether a given port is active based on the current speed setting. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">number</span> </td> <td class="description last">The port number to check.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line235">line 235</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> A promise that resolves to a boolean indicating whether the port is active. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;boolean></span> </dd> </dl> <h4 class="name" id="error"><span class="type-signature"></span>error<span class="signature">(message)</span><span class="type-signature"> &rarr; {void}</span></h4> <div class="description"> Logs an error message in red. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>message</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="description last">The error message to log.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line208">line 208</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">void</span> </dd> </dl> <h4 class="name" id="execute"><span class="type-signature">(async) </span>execute<span class="signature">()</span><span class="type-signature"> &rarr; {Promise.&lt;void>}</span></h4> <div class="description"> Executes the port operation based on the action flag. </div> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line69">line 69</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;void></span> </dd> </dl> <h4 class="name" id="getUnixKillCommand"><span class="type-signature">(async) </span>getUnixKillCommand<span class="signature">(port, method<span class="signature-attributes">opt</span>, graceful<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {Promise.&lt;string>}</span></h4> <div class="description"> Constructs the Unix command to kill a process running on a specified port. The command will either gracefully or forcefully terminate the process, depending on the `graceful` parameter. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th>Default</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">number</span> </td> <td class="attributes"> </td> <td class="default"> </td> <td class="description last">The port number where the process is running.</td> </tr> <tr> <td class="name"><code>method</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> 'tcp' </td> <td class="description last">The method (protocol) for the command (e.g., 'tcp' or 'udp'). Defaults to 'tcp'.</td> </tr> <tr> <td class="name"><code>graceful</code></td> <td class="type"> <span class="param-type">boolean</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="default"> false </td> <td class="description last">Whether to send a graceful kill signal (`true`) or a forceful one (`false`). Defaults to `false` (forceful kill).</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line295">line 295</a> </li></ul></dd> </dl> <h5>Throws:</h5> <dl> <dt> <div class="param-desc"> Throws an error if no process is found running on the specified port. </div> </dt> <dd></dd> <dt> <dl> <dt> Type </dt> <dd> <span class="param-type">Error</span> </dd> </dl> </dt> <dd></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The full Unix command string to kill the process. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;string></span> </dd> </dl> <h4 class="name" id="getWindowsKillCommand"><span class="type-signature">(async) </span>getWindowsKillCommand<span class="signature">(port)</span><span class="type-signature"> &rarr; {string}</span></h4> <div class="description"> Gets the Windows command to kill a port. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>port</code></td> <td class="type"> <span class="param-type">number</span> </td> <td class="description last">The port to kill.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line266">line 266</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The Windows kill command. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">string</span> </dd> </dl> <h4 class="name" id="handlePorts"><span class="type-signature">(async) </span>handlePorts<span class="signature">(ports)</span><span class="type-signature"> &rarr; {Promise}</span></h4> <div class="description"> Handles the action on a list of ports based on the specified action type. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>ports</code></td> <td class="type"> <span class="param-type">Array.&lt;number></span> </td> <td class="description last">The list of port numbers to act upon.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line166">line 166</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The result of the action performed. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise</span> </dd> </dl> <h4 class="name" id="killPorts"><span class="type-signature">(async) </span>killPorts<span class="signature">(ports)</span><span class="type-signature"> &rarr; {Promise.&lt;void>}</span></h4> <div class="description"> Kills the specified ports. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>ports</code></td> <td class="type"> <span class="param-type">Array.&lt;number></span> </td> <td class="description last">The ports to kill.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line245">line 245</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;void></span> </dd> </dl> <h4 class="name" id="listActivePorts"><span class="type-signature">(async) </span>listActivePorts<span class="signature">()</span><span class="type-signature"> &rarr; {Promise.&lt;Array.&lt;string>>}</span></h4> <div class="description"> Lists active ports based on the platform and speed flag. </div> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line93">line 93</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> List of active ports. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;Array.&lt;string>></span> </dd> </dl> <h4 class="name" id="log"><span class="type-signature"></span>log<span class="signature">(message)</span><span class="type-signature"></span></h4> <div class="description"> Logs messages if verbose mode is enabled. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>message</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="description last">The message to log.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line43">line 43</a> </li></ul></dd> </dl> <h4 class="name" id="parsePorts"><span class="type-signature"></span>parsePorts<span class="signature">()</span><span class="type-signature"> &rarr; {Array.&lt;number>}</span></h4> <div class="description"> Parses the provided ports into an array of valid ports. </div> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line53">line 53</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The parsed ports. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Array.&lt;number></span> </dd> </dl> <h4 class="name" id="parseUnixPorts"><span class="type-signature"></span>parseUnixPorts<span class="signature">(lines)</span><span class="type-signature"> &rarr; {Array.&lt;string>}</span></h4> <div class="description"> Parses active ports for Unix-like systems. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>lines</code></td> <td class="type"> <span class="param-type">Array.&lt;string></span> </td> <td class="description last">The output lines from the lsof command.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line130">line 130</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The parsed active ports. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Array.&lt;string></span> </dd> </dl> <h4 class="name" id="parseWindowsPorts"><span class="type-signature"></span>parseWindowsPorts<span class="signature">(lines)</span><span class="type-signature"> &rarr; {Array.&lt;string>}</span></h4> <div class="description"> Parses active ports for Windows. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>lines</code></td> <td class="type"> <span class="param-type">Array.&lt;string></span> </td> <td class="description last">The output lines from the netstat command.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line114">line 114</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The parsed active ports. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Array.&lt;string></span> </dd> </dl> <h4 class="name" id="promptUserToSelectPorts"><span class="type-signature"></span>promptUserToSelectPorts<span class="signature">(activePorts)</span><span class="type-signature"> &rarr; {Promise.&lt;Array.&lt;number>>}</span></h4> <div class="description"> Prompts the user to select ports interactively. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>activePorts</code></td> <td class="type"> <span class="param-type">Array.&lt;string></span> </td> <td class="description last">List of active ports to choose from.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line143">line 143</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> The selected ports. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">Promise.&lt;Array.&lt;number>></span> </dd> </dl> <h4 class="name" id="showPortInfo"><span class="type-signature">(async) </span>showPortInfo<span class="signature">(ports)</span><span class="type-signature"></span></h4> <div class="description"> Checks and logs the status of each port, indicating whether it is active or not. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>ports</code></td> <td class="type"> <span class="param-type">Array.&lt;number></span> </td> <td class="description last">An array of port numbers to check for activity.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line217">line 217</a> </li></ul></dd> </dl> <h4 class="name" id="success"><span class="type-signature"></span>success<span class="signature">(message)</span><span class="type-signature"> &rarr; {void}</span></h4> <div class="description"> Logs a success message in green. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>message</code></td> <td class="type"> <span class="param-type">string</span> </td> <td class="description last">The success message to log.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="index.js.html">index.js</a>, <a href="index.js.html#line198">line 198</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">void</span> </dd> </dl> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="PortClient.html">PortClient</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Sun Dec 08 2024 12:53:40 GMT+0000 (Greenwich Mean Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>