Skip to content

Commit 5b4336c

Browse files
committed
Added checkIfPortIsUse
1 parent 9af0fb3 commit 5b4336c

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",

src/__tests__/Net.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
import { getNetworkInterfaces, stopNetworkUtilizationComputation } from '../net';
1+
import { checkIfPortIsInUse, getNetworkInterfaces, stopNetworkUtilizationComputation } from '../net';
2+
import net from 'net';
23

34
test('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+
830
afterAll(() => {
931
stopNetworkUtilizationComputation();
1032
});

src/net.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { execCommand } from './utils';
22
import fs from 'fs/promises';
3+
import net from 'net';
34
import os from 'os';
45

56
export 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
*

0 commit comments

Comments
 (0)