Skip to content

Commit

Permalink
Update scala from 2.13.12 to 2.13.13
Browse files Browse the repository at this point in the history
  • Loading branch information
scholarsmate committed Oct 21, 2024
1 parent baa2261 commit c5886b0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/client/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,47 @@ export function delay(milliseconds: number): Promise<void> {
})
}

/**
* Find the first available port in a range, or null if no ports are available
* @param startPort start of the port range
* @param endPort end of the port range
* @returns first available port in the range, or null if no ports are available
*/
export async function findFirstAvailablePort(
startPort: number,
endPort: number
): Promise<number | null> {
const log = getLogger()
return new Promise((resolve) => {
let currentPort = startPort

const tryNextPort = () => {
if (currentPort > endPort) {
resolve(null) // No ports available in the range
return
}

const server = createServer()
server.listen(currentPort, '0.0.0.0', () => {
server.close((err) => {
if (err) {
log.error(`Error closing server on port ${currentPort}: ${err}`)
}
resolve(currentPort) // Found an available port
})
})

server.on('error', (err) => {
log.error(`Error when trying to listen on port ${currentPort}: ${err}`)
++currentPort
tryNextPort() // Try the next port
})
}

tryNextPort()
})
}

/**
* Wait for file to exist
* @param filePath path to file to wait for
Expand Down

0 comments on commit c5886b0

Please sign in to comment.