mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
85 lines (84 loc) • 3.79 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.supportLocalHost = exports.isLocalHost = exports.getMyIp = exports.isPortInUse = exports.detectPort = void 0;
const net_1 = __importDefault(require("net"));
const os_1 = __importDefault(require("os"));
const color_cc_1 = __importDefault(require("color-cc"));
const print_1 = require("./print");
function detectPort(port, noLogPrint) {
return __awaiter(this, void 0, void 0, function* () {
const result = (yield new Promise(res => {
const server = net_1.default.createServer().listen(port);
server.on('listening', function () {
server.close();
res({ isInUse: false, err: null });
});
server.on('error', (err) => {
const ERR_CODE = err.code.toUpperCase();
if (ERR_CODE === 'EADDRINUSE') {
!noLogPrint && print_1.Printer.error(color_cc_1.default.yellow(`[ERROR] ${port} 端口占用中!`));
}
if (ERR_CODE === 'EACCES') {
!noLogPrint && print_1.Printer.error(color_cc_1.default.yellow(`[ERROR] ${port} 端口访问受限制!`));
}
res({ isInUse: true, err: err });
});
}));
return result;
});
}
exports.detectPort = detectPort;
function isPortInUse(port, options) {
return __awaiter(this, void 0, void 0, function* () {
const { timeout = 1000, noLogPrint = false } = options || {};
let isInUse = false;
try {
const detectd = yield Promise.race([
detectPort(port, !!noLogPrint),
new Promise(res => setTimeout(() => res({ err: `Timeout with ${timeout}` }), parseInt(timeout) || 1000)),
]);
isInUse = !!(detectd === null || detectd === void 0 ? void 0 : detectd.isInUse);
}
catch (error) {
print_1.Printer.warn(color_cc_1.default.warn(`Detec isPortInUse(${port}) occur errors:`), error);
isInUse = false;
}
return isInUse;
});
}
exports.isPortInUse = isPortInUse;
function getMyIp(ipv6 = false) {
const LOCAL_HOST = '127.0.0.1';
const PROTOCOL = ipv6 ? 'ipv6' : 'ipv4';
const interfaces = os_1.default.networkInterfaces();
for (const infoList of Object.values(interfaces)) {
for (const info of infoList) {
const { address, family, internal, } = info || {};
if (address !== LOCAL_HOST && !internal && family.toLowerCase() === PROTOCOL) {
return address;
}
}
}
return LOCAL_HOST;
}
exports.getMyIp = getMyIp;
function isLocalHost(host) {
return host === 'localhost' || host === '127.0.0.1' || host === '::1';
}
exports.isLocalHost = isLocalHost;
function supportLocalHost(host) {
return isLocalHost(host) || '0.0.0.0' === host;
}
exports.supportLocalHost = supportLocalHost;