Skip to content

Commit 6158abd

Browse files
committed
Renamed isPortInUse to isPortListendedOn, added new isPortInUse and canConnect
1 parent 6fb8af8 commit 6158abd

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
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.5.1",
3+
"version": "1.5.2",
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { isPortInUse, getNetworkInterfaces, stopNetworkUtilizationComputation } from '../net';
22
import net from 'net';
33

4+
45
test('getNetworkInterfaces', async () => {
56
const nics = await getNetworkInterfaces();
67
//console.log(nics); // TODO REMOVE
78
expect(nics).toBeDefined();
89
expect(nics.length).toBeGreaterThan(0);
910
}, 10000);
1011

12+
1113
test('isPortInUse(12345)', async () => {
1214
const port = 12345;
1315

@@ -27,6 +29,25 @@ test('isPortInUse(12345)', async () => {
2729
server.close();
2830
});
2931

32+
/*
33+
test('DEBUG', async () => {
34+
const ports = {
35+
5050: false,
36+
5432: false,
37+
6379: false,
38+
27017: false,
39+
};
40+
41+
await Promise.allSettled(Object.keys(ports).map(async p => {
42+
const inUse1 = await isPortInUse(parseInt(p), '0.0.0.0');
43+
const inUse2 = await isPortInUse(parseInt(p), '127.0.0.1');
44+
ports[p] = inUse1 || inUse2;
45+
}));
46+
47+
console.log(JSON.stringify(ports, null, ' '));
48+
});
49+
*/
50+
3051
afterAll(() => {
3152
stopNetworkUtilizationComputation();
3253
});

src/net.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,36 @@ export function stopNetworkUtilizationComputation() {
169169
}
170170

171171

172+
/**
173+
* Tries to connect to a given server over TCP.
174+
*
175+
* @param port Port number to connect to.
176+
* @param host Hostname or IP address of the server (default '127.0.0.1').
177+
* @returns True if the connection was successful, false otherwise.
178+
*/
179+
export async function canConnect(port: number, host: string = '127.0.0.1'): Promise<boolean> {
180+
return new Promise<boolean>((resolve) => {
181+
const socket = net.connect(port, host, () => {
182+
socket.end();
183+
resolve(true);
184+
});
185+
socket.on('error', () => {
186+
resolve(false);
187+
});
188+
});
189+
}
190+
172191
/**
173192
* Checks if a process is listening on a given port.
193+
*
194+
* @warning If a Docker proxy is involved multiple processes can bind to the same port.
195+
* Use canConnect() as an alternative to check if a port is already being listened on.
196+
*
174197
* @param port Port number to check.
175198
* @param bindAddress Address of the interface to bind to (default '0.0.0.0' which binds to all interfaces).
176199
* @returns Promise that resolves to true if the port is in use, false otherwise.
177200
*/
178-
export async function isPortInUse(port: number, bindAddress: string = '0.0.0.0'): Promise<boolean> {
201+
export async function isPortListendedOn(port: number, bindAddress: string = '0.0.0.0'): Promise<boolean> {
179202
const server = net.createServer();
180203
return new Promise((resolve) => {
181204
server.unref();
@@ -195,6 +218,22 @@ export async function isPortInUse(port: number, bindAddress: string = '0.0.0.0')
195218
}
196219

197220

221+
/**
222+
* Uses isPortListenedOn() and canConnect() to determine if a port is in use.
223+
* @param port Port number to check.
224+
* @param bindAddress Address of the interface to bind to (default '0.0.0.0').
225+
*/
226+
export async function isPortInUse(port: number, bindAddress: string = '0.0.0.0'): Promise<boolean> {
227+
let canConn = false;
228+
let isListened = false;
229+
await Promise.allSettled([
230+
canConnect(port, bindAddress).then(res => canConn = res),
231+
isPortListendedOn(port, bindAddress).then(res => isListened = res)
232+
])
233+
return canConn || isListened;
234+
}
235+
236+
198237
/**
199238
* Returns information about the network interfaces on the system.
200239
*

0 commit comments

Comments
 (0)