feat(port-management): add findFreePort method for automatic port discovery within a range
Some checks failed
Default (tags) / security (push) Successful in 46s
Default (tags) / test (push) Failing after 6m10s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped

This commit is contained in:
Juergen Kunz
2025-07-31 22:04:20 +00:00
parent 9cf4e433bf
commit d1ab85cbb3
5 changed files with 329 additions and 82 deletions

View File

@@ -143,6 +143,33 @@ export class SmartNetwork {
return result;
}
/**
* Find the first available port within a given range
* @param startPort The start of the port range (inclusive)
* @param endPort The end of the port range (inclusive)
* @returns The first available port number, or null if no ports are available
*/
public async findFreePort(startPort: number, endPort: number): Promise<number | null> {
// Validate port range
if (startPort < 1 || startPort > 65535 || endPort < 1 || endPort > 65535) {
throw new NetworkError('Port numbers must be between 1 and 65535', 'EINVAL');
}
if (startPort > endPort) {
throw new NetworkError('Start port must be less than or equal to end port', 'EINVAL');
}
// Check each port in the range
for (let port = startPort; port <= endPort; port++) {
const isUnused = await this.isLocalPortUnused(port);
if (isUnused) {
return port;
}
}
// No free port found in the range
return null;
}
/**
* checks wether a remote port is available
* @param domainArg