UNPKG

axiodb

Version:

A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.

112 lines 4.56 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPortInUse = isPortInUse; exports.checkDockerPortMapping = checkDockerPortMapping; exports.default = checkPortAndDocker; /* eslint-disable @typescript-eslint/no-unused-vars */ const net_1 = __importDefault(require("net")); const child_process_1 = require("child_process"); const keys_1 = require("./keys"); /** * Checks whether a specific port is already in use. * * @param port - The port number to check * @param host - The host to check the port on, defaults to localhost * @returns A Promise that resolves to true if the port is in use, false otherwise * @throws {Error} If the port is already in use * * @example * // Check if port 3000 is available * try { * const inUse = await isPortInUse(3000); * if (!inUse) { * // Port is free, can use it * } * } catch (error) { * console.error(error.message); * } */ function isPortInUse(port, host = keys_1.ServerKeys.LOCALHOST) { return new Promise((resolve) => { const server = net_1.default .createServer() .once("error", (err) => { if (err.code === "EADDRINUSE") { resolve(true); // Port is in use throw new Error(`Port ${port} is already in use. Please Free the port to get the GUI.`); } else { resolve(false); // Other error } }) .once("listening", () => { server.close(); resolve(false); // Port is free }) .listen(port, String(host)); }); } /** * Checks if a specified port is currently mapped to any running Docker container. * * This function executes a Docker command to list all running containers and their port mappings, * then filters for containers that are using the specified port. * * @param port - The port number to check for Docker container mappings * @throws {Error} If the specified port is already mapped to a Docker container * @returns {Promise<void>} * * @example * ```typescript * try { * await checkDockerPortMapping(3000); * // Port 3000 is free for use * } catch (error) { * // Port 3000 is already in use by a Docker container * console.error(error.message); * } * ``` */ function checkDockerPortMapping(port) { return __awaiter(this, void 0, void 0, function* () { (0, child_process_1.exec)(`docker ps --format "{{.ID}} {{.Ports}}"`, (error, stdout, _stderr) => { if (error) { console.error(`Error executing docker: ${error.message}`); return; } const containers = stdout.trim().split("\n"); const matching = containers.filter((line) => line.includes(`:${port}->`)); if (matching.length > 0) { console.log(`Docker container(s) found using port ${port}:`); matching.forEach((c) => console.log(c)); throw new Error(`Port ${port} is already mapped to a Docker container. Please stop the container or Please Free the port to get the GUI.`); } }); }); } /** * Checks if a specified port is in use and if there's a Docker port mapping for that port. * * @param port - The port number to check * @returns A Promise that resolves when both port usage check and Docker port mapping check are complete * @throws May throw an error if the port is already in use or if there's a conflict with Docker port mappings */ function checkPortAndDocker(port) { return __awaiter(this, void 0, void 0, function* () { yield isPortInUse(port); yield checkDockerPortMapping(port); }); } //# sourceMappingURL=PortFreeChecker.js.map