UNPKG

redis-smq-common

Version:

Provides essential components and utilities shared across RedisSMQ packages.

36 lines 1.03 kB
import { createServer } from 'net'; async function isPortInUse(port) { return new Promise((resolve) => { const server = createServer() .once('error', (err) => { if (err.code === 'EADDRINUSE') { resolve(true); } else { resolve(false); } }) .once('listening', () => { server.close(); resolve(false); }) .listen(port, '127.0.0.1'); }); } async function getRandomPort() { const MIN_PORT = 1024; const MAX_PORT = 65535; const MAX_ATTEMPTS = 100; for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { const port = Math.floor(Math.random() * (MAX_PORT - MIN_PORT + 1)) + MIN_PORT; if (!(await isPortInUse(port))) { return port; } } throw new Error('Unable to find an available port after multiple attempts'); } export const net = { isPortInUse, getRandomPort, }; //# sourceMappingURL=index.js.map