File tree Expand file tree Collapse file tree 3 files changed +53
-3
lines changed
Expand file tree Collapse file tree 3 files changed +53
-3
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " lup-system" ,
3- "version" : " 1.4 .0" ,
3+ "version" : " 1.5 .0" ,
44 "description" : " NodeJS library to retrieve system information and utilization." ,
55 "main" : " ./lib/index" ,
66 "types" : " ./lib/index.d.ts" ,
Original file line number Diff line number Diff line change 1- import { getNetworkInterfaces , stopNetworkUtilizationComputation } from '../net' ;
1+ import { checkIfPortIsInUse , getNetworkInterfaces , stopNetworkUtilizationComputation } from '../net' ;
2+ import net from 'net' ;
23
34test ( 'getNetworkInterfaces' , async ( ) => {
45 const nics = await getNetworkInterfaces ( ) ;
5- console . log ( nics ) ; // TODO REMOVE
6+ //console.log(nics); // TODO REMOVE
7+ expect ( nics ) . toBeDefined ( ) ;
8+ expect ( nics . length ) . toBeGreaterThan ( 0 ) ;
69} , 10000 ) ;
710
11+ test ( 'checkIfPortIsInUse(12345)' , async ( ) => {
12+ const port = 12345 ;
13+
14+ // port should be free
15+ const isInUse1 = await checkIfPortIsInUse ( port ) ;
16+ expect ( isInUse1 ) . toBe ( false ) ;
17+
18+ // start a server
19+ const server = net . createServer ( ) ;
20+ server . unref ( ) ;
21+ await new Promise < void > ( ( resolve ) => server . listen ( port , '0.0.0.0' , resolve ) ) ;
22+
23+ // port should now be in use
24+ const isInUse2 = await checkIfPortIsInUse ( port ) ;
25+ expect ( isInUse2 ) . toBe ( true ) ;
26+
27+ server . close ( ) ;
28+ } ) ;
29+
830afterAll ( ( ) => {
931 stopNetworkUtilizationComputation ( ) ;
1032} ) ;
Original file line number Diff line number Diff line change 11import { execCommand } from './utils' ;
22import fs from 'fs/promises' ;
3+ import net from 'net' ;
34import os from 'os' ;
45
56export type NICUtilization = {
@@ -167,6 +168,33 @@ export function stopNetworkUtilizationComputation() {
167168 NET_COMPUTE_RUNNING = false ;
168169}
169170
171+
172+ /**
173+ * Checks if a process is listening on a given port.
174+ * @param port Port number to check.
175+ * @param bindAddress Address of the interface to bind to (default '0.0.0.0' which binds to all interfaces).
176+ * @returns Promise that resolves to true if the port is in use, false otherwise.
177+ */
178+ export async function checkIfPortIsInUse ( port : number , bindAddress : string = '0.0.0.0' ) : Promise < boolean > {
179+ const server = net . createServer ( ) ;
180+ return new Promise ( ( resolve ) => {
181+ server . unref ( ) ;
182+ server . once ( 'error' , ( err ) => {
183+ server . close ( ) ;
184+ if ( ( err as any ) . code === 'EADDRINUSE' ) {
185+ resolve ( true ) ;
186+ } else {
187+ resolve ( false ) ;
188+ }
189+ } ) ;
190+ server . listen ( port , bindAddress , ( ) => {
191+ server . close ( ) ;
192+ resolve ( false ) ;
193+ } ) ;
194+ } ) ;
195+ }
196+
197+
170198/**
171199 * Returns information about the network interfaces on the system.
172200 *
You can’t perform that action at this time.
0 commit comments