@oxog/port-finder
Version:
Zero-dependency port finder for Node.js applications with plugin support
94 lines (93 loc) • 5.32 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanPortsParallel = exports.scanPorts = exports.checkPort = exports.validatePortRange = exports.validatePort = exports.builtInValidators = exports.applyValidators = exports.getValidator = exports.removeValidator = exports.addValidator = void 0;
exports.isPortAvailable = isPortAvailable;
exports.findPort = findPort;
exports.findPorts = findPorts;
const types_1 = require("./types");
const core_1 = require("./core");
const validators_1 = require("./validators");
const DEFAULT_START_PORT = 3000;
const DEFAULT_END_PORT = 65535;
const DEFAULT_HOST = '0.0.0.0';
/**
* Checks if a specific port is available
* @param port - The port number to check
* @param host - The host to bind to (default: '0.0.0.0')
* @returns Promise resolving to true if port is available, false otherwise
*/
async function isPortAvailable(port, host = DEFAULT_HOST) {
(0, core_1.validatePort)(port);
return (0, core_1.checkPort)(port, host);
}
/**
* Finds a single available port
* @param options - Port finder options
* @returns Promise resolving to an available port number
* @throws {PortFinderError} If no available port is found
*/
async function findPort(options = {}) {
const { start = DEFAULT_START_PORT, end = DEFAULT_END_PORT, exclude = [], host = DEFAULT_HOST, validators = [] } = options;
(0, core_1.validatePortRange)(start, end);
const excludeSet = new Set(exclude);
const validator = validators.length > 0
? (port) => (0, validators_1.applyValidators)(port, validators)
: undefined;
const results = await (0, core_1.scanPortsParallel)(start, end, host, excludeSet, validator, 1);
if (results.length === 0) {
throw new types_1.PortFinderError(`No available port found between ${start} and ${end}`, 'NO_AVAILABLE_PORT', { start, end, exclude, validators });
}
return results[0];
}
/**
* Finds multiple available ports
* @param count - Number of ports to find
* @param options - Port finder options
* @returns Promise resolving to an array of available port numbers
* @throws {PortFinderError} If not enough available ports are found
*/
async function findPorts(count, options = {}) {
if (!Number.isInteger(count) || count <= 0) {
throw new types_1.PortFinderError('Count must be a positive integer', 'INVALID_COUNT', { count });
}
const { start = DEFAULT_START_PORT, end = DEFAULT_END_PORT, exclude = [], host = DEFAULT_HOST, validators = [], consecutive = false } = options;
(0, core_1.validatePortRange)(start, end);
const excludeSet = new Set(exclude);
const validator = validators.length > 0
? (port) => (0, validators_1.applyValidators)(port, validators)
: undefined;
const results = consecutive
? await (0, core_1.scanPorts)(start, end, host, excludeSet, validator, count, true)
: await (0, core_1.scanPortsParallel)(start, end, host, excludeSet, validator, count);
if (results.length < count) {
throw new types_1.PortFinderError(`Could only find ${results.length} available ports out of ${count} requested`, 'INSUFFICIENT_PORTS', { requested: count, found: results.length, ports: results });
}
return results;
}
var validators_2 = require("./validators");
Object.defineProperty(exports, "addValidator", { enumerable: true, get: function () { return validators_2.addValidator; } });
Object.defineProperty(exports, "removeValidator", { enumerable: true, get: function () { return validators_2.removeValidator; } });
Object.defineProperty(exports, "getValidator", { enumerable: true, get: function () { return validators_2.getValidator; } });
Object.defineProperty(exports, "applyValidators", { enumerable: true, get: function () { return validators_2.applyValidators; } });
Object.defineProperty(exports, "builtInValidators", { enumerable: true, get: function () { return validators_2.builtInValidators; } });
var core_2 = require("./core");
Object.defineProperty(exports, "validatePort", { enumerable: true, get: function () { return core_2.validatePort; } });
Object.defineProperty(exports, "validatePortRange", { enumerable: true, get: function () { return core_2.validatePortRange; } });
Object.defineProperty(exports, "checkPort", { enumerable: true, get: function () { return core_2.checkPort; } });
Object.defineProperty(exports, "scanPorts", { enumerable: true, get: function () { return core_2.scanPorts; } });
Object.defineProperty(exports, "scanPortsParallel", { enumerable: true, get: function () { return core_2.scanPortsParallel; } });
__exportStar(require("./types"), exports);