UNPKG

can-bind-to-host

Version:

Utility package to see if the node process can bind to the host or listen on a port. Can be used for checking if a host resolves to localhost.

31 lines (30 loc) 1.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.canBindToHost = void 0; var net_1 = __importDefault(require("net")); var canBindToHost = function (host, port) { if (host === void 0) { host = '0.0.0.0'; } if (port === void 0) { port = 0; } return new Promise(function (res) { if (port >= 0 && port <= 65535) { var server_1 = net_1.default .createServer() .listen({ host: host, port: port, }) .addListener('error', function () { return res(false); }) .addListener('listening', function () { server_1.close(); res(true); }); } else res(false); }); }; exports.canBindToHost = canBindToHost; exports.default = canBindToHost;