axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
71 lines • 2.87 kB
JavaScript
;
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.default = checkPortAndDocker;
/* eslint-disable @typescript-eslint/no-unused-vars */
const net_1 = __importDefault(require("net"));
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 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);
});
}
//# sourceMappingURL=PortFreeChecker.js.map