jka-core
Version:
Library for working with Jedi Academy servers
61 lines (60 loc) • 2.5 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rconBasicRequest = void 0;
const basic_request_1 = require("../basic.request");
const ERRORS = {
INCORRECT_PASSWORD: 'Incorrect rconpassword!',
INVALID_PASSWORD_FORMAT: 'Invalid rconpassword format! It must be a string without any whitespaces within!',
INVALID_CMD_FORMAT: 'Invalid rcon command format! It must be a string!',
};
function rconBasicRequest(params) {
return __awaiter(this, void 0, void 0, function* () {
// TODO: Add input validation
const { server, rconpassword, cmd, timeout } = parseAndValidate(params);
let response = '';
try {
response = yield (0, basic_request_1.basicRequest)({ request: `rcon ${rconpassword} ${cmd}`, server, timeout });
}
catch (error) {
console.error('Rcon Request failed:');
throw error;
}
response = response.replaceAll(/����print\n/g, '');
if (response.trim() === 'Bad rconpassword.')
throw new Error(ERRORS.INCORRECT_PASSWORD);
return response;
});
}
exports.rconBasicRequest = rconBasicRequest;
function parseAndValidate(params) {
let { cmd, rconpassword, server, timeout } = params;
if (typeof rconpassword === 'string') {
rconpassword = rconpassword.trim();
if (/\s/g.test(rconpassword)) {
// Should not have any whitespaces within
throw new Error(ERRORS.INVALID_PASSWORD_FORMAT);
}
}
else
throw new Error(ERRORS.INVALID_PASSWORD_FORMAT);
if (typeof cmd === 'string') {
cmd = cmd.trim();
}
else
throw new Error(ERRORS.INVALID_PASSWORD_FORMAT);
return {
server,
timeout,
rconpassword,
cmd,
};
}