mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-05 03:05:07 +08:00
26 lines
580 B
JavaScript
26 lines
580 B
JavaScript
const net = require('net');
|
|
|
|
function checkout(port) {
|
|
return new Promise((resolve) => {
|
|
const server = net.createServer();
|
|
server.once('error', (err) => {
|
|
if (err.code === 'EADDRINUSE') {
|
|
resolve(checkout(port + 1));
|
|
}
|
|
});
|
|
|
|
server.once('listening', () => {
|
|
server.close(() => {
|
|
resolve(port);
|
|
});
|
|
});
|
|
|
|
server.listen(port);
|
|
});
|
|
}
|
|
|
|
module.exports = function getPort(basePort) {
|
|
basePort = basePort || 5000;
|
|
return checkout(basePort);
|
|
};
|