@nx/module-federation
Version:
23 lines (22 loc) • 841 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPortInUse = isPortInUse;
const net_1 = require("net");
/**
* Check if a port is in use by attempting to bind to it. Binding is local and
* resolves immediately, so unlike a connection-based check it never stalls on
* the OS TCP connect timeout when `localhost` resolves to an unreachable address
* (e.g. IPv6 `::1`) -- the cause of the `nx serve` hang in #33909.
*/
function isPortInUse(port, host = 'localhost') {
return new Promise((resolve) => {
const server = (0, net_1.createServer)();
server.once('error', (err) => {
resolve(err.code === 'EADDRINUSE');
});
server.once('listening', () => {
server.close(() => resolve(false));
});
server.listen(port, host);
});
}