trm-core
Version:
TRM (Transport Request Manager) Core
690 lines (689 loc) • 27.3 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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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.RFCClient = void 0;
const commons_1 = require("../commons");
const trm_commons_1 = require("trm-commons");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const _1 = require(".");
const xml = __importStar(require("xml-js"));
const nodeRfcLib = 'node-rfc';
class RFCClient {
constructor(_rfcClientArgs, _cLangu, traceDir, _globalNodeModulesPath) {
this._rfcClientArgs = _rfcClientArgs;
this._cLangu = _cLangu;
this._globalNodeModulesPath = _globalNodeModulesPath;
this._aliveCheck = false;
try {
process.env["RFC_TRACE_DIR"] = traceDir || process.cwd();
}
catch (e) {
trm_commons_1.Logger.warning(`Couldn't set RFC trace!`, true);
trm_commons_1.Logger.error(e.toString(), true);
}
trm_commons_1.Logger.log(`RFC_TRACE_DIR: ${process.env["RFC_TRACE_DIR"]}`, true);
}
getRfcClient() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._rfcClient) {
const libPath = path_1.default.join(this._globalNodeModulesPath || (0, trm_commons_1.getGlobalNodeModules)(), nodeRfcLib);
trm_commons_1.Logger.log(`Node RFC lib path: ${libPath}`, true);
if (!(0, fs_1.existsSync)(libPath)) {
throw new _1.RFCClientError("ZRFC_LIB_NOT_FOUND", null, null, `${nodeRfcLib} not found. Run command "npm install ${nodeRfcLib} -g" to continue.`);
}
this._rfcClient = new (yield Promise.resolve(`${libPath}`).then(s => __importStar(require(s)))).Client(this._rfcClientArgs);
}
return this._rfcClient;
});
}
open() {
return __awaiter(this, void 0, void 0, function* () {
try {
trm_commons_1.Logger.loading(`Opening RFC connection`, true);
yield (yield this.getRfcClient()).open();
trm_commons_1.Logger.success(`RFC open`, true);
}
catch (e) {
throw new _1.RFCClientError("ZNO_CONN", null, e, e.message);
}
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
try {
trm_commons_1.Logger.loading(`Closing RFC connection`, true);
yield (yield this.getRfcClient()).close();
trm_commons_1.Logger.success(`RFC closed`, true);
}
catch (e) {
throw new _1.RFCClientError("ZNO_CLOSE", null, e, e.message);
}
});
}
checkConnection() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._aliveCheck) {
if ((yield this.getRfcClient()).alive) {
trm_commons_1.Logger.success(`RFC open`, true);
}
else {
trm_commons_1.Logger.warning(`RFC closed`, true);
}
this._aliveCheck = true;
}
return (yield this.getRfcClient()).alive;
});
}
_call(fm_1, arg_1, timeout_1, noErrorParsing_1) {
return __awaiter(this, arguments, void 0, function* (fm, arg, timeout, noErrorParsing, retryCount = 0) {
var argNormalized;
if (arg) {
var emptyKeys = [];
argNormalized = (0, commons_1.normalize)(arg, 'upper');
Object.keys(argNormalized).forEach(key => {
if (argNormalized[key] === undefined || argNormalized === null) {
emptyKeys.push(key);
}
});
emptyKeys.forEach(key => {
delete argNormalized[key];
});
}
else {
argNormalized = {};
}
var callOptions = undefined;
if (timeout) {
callOptions = {
timeout
};
}
try {
trm_commons_1.Logger.loading(`Executing RFC, FM ${fm}, args ${JSON.stringify(argNormalized)}, opts ${JSON.stringify(callOptions)}`, true);
const response = yield (yield this.getRfcClient()).call(fm, argNormalized, callOptions);
const responseNormalized = (0, commons_1.normalize)(response);
trm_commons_1.Logger.success(`RFC resonse: ${JSON.stringify(responseNormalized)}`, true);
return responseNormalized;
}
catch (e) {
if (e.message === 'device or resource busy: device or resource busy' && retryCount <= 10) {
trm_commons_1.Logger.log('device or resource busy, retrying', true);
yield new Promise(res => {
setTimeout(res, 1000);
});
return this._call(fm, arg, timeout, noErrorParsing, retryCount + 1);
}
if (noErrorParsing) {
throw e;
}
else {
var message;
var messageError;
const sapMessage = {
no: `${e.abapMsgNumber}`,
class: e.abapMsgClass,
v1: e.abapMsgV1,
v2: e.abapMsgV2,
v3: e.abapMsgV3,
v4: e.abapMsgV4
};
if (sapMessage.no && sapMessage.class) {
try {
message = yield this._getMessage(true, sapMessage);
}
catch (k) {
messageError = k;
message = `Couldn't read error message ${e.abapMsgClass} ${e.abapMsgNumber} ${e.abapMsgV1} ${e.abapMsgV2} ${e.abapMsgV3} ${e.abapMsgV4}`;
}
}
else {
message = e.message;
}
var rfcClientError = new _1.RFCClientError(e.key, sapMessage, e, message);
if (messageError) {
rfcClientError.messageError = messageError;
}
trm_commons_1.Logger.error(rfcClientError.toString(), true);
throw rfcClientError;
}
}
});
}
_getMessage(noErrorParsing, data) {
return __awaiter(this, void 0, void 0, function* () {
var msgnr = data.no;
while (msgnr.length < 3) {
msgnr = `0${msgnr}`;
}
const aT100 = yield this._readTable(noErrorParsing, 'T100', [{ fieldName: 'SPRSL' }, { fieldName: 'ARBGB' }, { fieldName: 'MSGNR' }, { fieldName: 'TEXT' }], `SPRSL EQ '${this._cLangu}' AND ARBGB EQ '${data.class}' AND MSGNR EQ '${msgnr}'`);
if (aT100.length === 1 && aT100[0].text) {
var msg = aT100[0].text;
var counter = 1;
do {
if (msg.includes(`&${counter}`)) {
msg = msg.replace(new RegExp(`&${counter}`, 'gmi'), data[`v${counter}`] || '');
msg = msg.replace(new RegExp(`&${counter}&`, 'gmi'), data[`v${counter}`] || '');
}
else {
msg = msg.replace("&", data[`v${counter}`] || '');
}
counter++;
} while (counter <= 4);
msg = msg.replace(new RegExp(`&\\d*`, 'gmi'), '');
msg = msg.replace(new RegExp(`&`, 'gmi'), '');
return msg.trim();
}
else {
throw new _1.RFCClientError("ZMSG_NOT_FOUND", null, null, `Message ${msgnr}, class ${data.class}, lang ${this._cLangu} not found.`);
}
});
}
getMessage(data) {
return __awaiter(this, void 0, void 0, function* () {
return this._getMessage(false, data);
});
}
_readTable(noErrorParsing, tableName, fields, options) {
return __awaiter(this, void 0, void 0, function* () {
var sqlOutput = [];
const delimiter = '|';
var aOptions = [];
if (options) {
const aSplit = options.split(/\s+AND\s+/);
if (aSplit.length > 1) {
aSplit.forEach((s, i) => {
var sText = s.trim();
if (i !== 0) {
sText = `AND ${sText}`;
}
aOptions.push({ text: sText });
});
}
else {
aOptions = aSplit.map(s => {
return {
text: s
};
});
}
}
try {
const result = yield this._call("RFC_READ_TABLE", {
query_table: tableName.toUpperCase(),
delimiter,
options: aOptions,
fields: fields
}, undefined, noErrorParsing);
const data = result['data'];
data.forEach(tab512 => {
var sqlLine = {};
const waSplit = tab512.wa.split(delimiter);
fields.forEach((field, index) => {
sqlLine[field['FIELDNAME']] = waSplit[index].trim();
});
sqlOutput.push(sqlLine);
});
return (0, commons_1.normalize)(sqlOutput);
}
catch (e) {
if (e.exceptionType === 'TABLE_WITHOUT_DATA') {
return [];
}
else {
throw e;
}
}
});
}
readTable(tableName, fields, options) {
return __awaiter(this, void 0, void 0, function* () {
return this._readTable(false, tableName, fields, options);
});
}
getFileSystem() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_FILE_SYS", {});
return result['evFileSys'];
});
}
getDirTrans() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_DIR_TRANS", {});
return result['evDirTrans'];
});
}
getBinaryFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_BINARY_FILE", {
iv_file_path: filePath
});
return result['evFile'];
});
}
writeBinaryFile(filePath, binary) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_WRITE_BINARY_FILE", {
iv_file_path: filePath,
iv_file: binary
});
});
}
createTocTransport(text, target) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_CREATE_TOC", {
iv_text: text,
iv_target: target.trim().toUpperCase()
});
return result['evTrkorr'];
});
}
createWbTransport(text, target) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_CREATE_IMPORT_TR", {
iv_text: text,
iv_target: target.trim().toUpperCase()
});
return result['evTrkorr'];
});
}
setTransportDoc(trkorr, doc) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_SET_TRANSPORT_DOC", {
iv_trkorr: trkorr.trim().toUpperCase(),
it_doc: doc
});
});
}
getDevclassObjects(devclass) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_DEVCLASS_OBJS", {
iv_devclass: devclass.trim().toUpperCase()
});
return result['etTadir'];
});
}
removeComments(trkorr, object) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_REMOVE_TR_COMMENTS", {
iv_trkorr: trkorr.trim().toUpperCase(),
iv_object: object.trim().toUpperCase()
});
});
}
addToTransportRequest(trkorr, content, lock) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_ADD_OBJS_TR", {
iv_lock: lock ? 'X' : ' ',
iv_trkorr: trkorr.trim().toUpperCase(),
it_e071: content.map(o => {
return {
PGMID: o.pgmid,
OBJECT: o.object,
OBJ_NAME: o.objName
};
})
});
});
}
repositoryEnvironment(objectType, objectName) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("REPOSITORY_ENVIRONMENT_RFC", {
obj_type: objectType.trim().toUpperCase(),
object_name: objectName.trim().toUpperCase()
});
return result['environmentTab'];
});
}
deleteTrkorr(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_DELETE_TRANSPORT", {
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
releaseTrkorr(trkorr, lock, timeout) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_RELEASE_TR", {
iv_trkorr: trkorr.trim().toUpperCase(),
iv_lock: lock ? 'X' : ' '
}, timeout);
});
}
addSkipTrkorr(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_ADD_SKIP_TRKORR", {
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
removeSkipTrkorr(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_REMOVE_SKIP_TRKORR", {
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
addSrcTrkorr(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_ADD_SRC_TRKORR", {
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
readTmsQueue(target) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_READ_TMS_QUEUE", {
iv_target: target
});
return result['etRequests'];
});
}
createPackage(scompkdtln) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_CREATE_PACKAGE", {
is_data: scompkdtln
});
});
}
tdevcInterface(devclass, parentcl, rmParentCl, devlayer) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_TDEVC_INTERFACE", {
iv_devclass: devclass.trim().toUpperCase(),
iv_parentcl: parentcl ? parentcl.trim().toUpperCase() : '',
iv_rm_parentcl: rmParentCl ? 'X' : ' ',
iv_devlayer: devlayer ? devlayer.trim().toUpperCase() : ''
});
});
}
getDefaultTransportLayer() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_TRANSPORT_LAYER");
return result['evLayer'];
});
}
tadirInterface(tadir) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_TADIR_INTERFACE", {
iv_pgmid: tadir.pgmid,
iv_object: tadir.object,
iv_obj_name: tadir.objName,
iv_devclass: tadir.devclass,
iv_set_genflag: tadir.genflag ? 'X' : ' ',
iv_srcsystem: tadir.srcsystem
});
});
}
dequeueTransport(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_DEQUEUE_TR", {
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
forwardTransport(trkorr_1, target_1, source_1) {
return __awaiter(this, arguments, void 0, function* (trkorr, target, source, importAgain = true) {
yield this._call("ZTRM_FORWARD_TR", {
iv_trkorr: trkorr.trim().toUpperCase(),
iv_target: target.trim().toUpperCase(),
iv_source: source.trim().toUpperCase(),
iv_import_again: importAgain ? 'X' : ' '
});
});
}
importTransport(trkorr, system) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_IMPORT_TR", {
iv_system: system.trim().toUpperCase(),
iv_trkorr: trkorr.trim().toUpperCase()
});
});
}
setInstallDevc(installDevc) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_SET_INSTALL_DEVC", {
it_installdevc: installDevc
});
});
}
getObjectsList() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_LIST_OBJECT_TYPES");
return result['etObjectText'];
});
}
getTrmServerVersion() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_VERSION");
return result['evVersion'];
});
}
getTrmRestVersion() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_VERSION");
return result['evRest'];
});
}
trmServerPing() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_PING");
return result['evReturn'];
});
}
renameTransportRequest(trkorr, as4text) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_RENAME_TRANSPORT_REQUEST", {
iv_trkorr: trkorr.trim().toUpperCase(),
iv_as4text: as4text
});
});
}
setPackageIntegrity(integrity) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_SET_INTEGRITY", {
is_integrity: integrity
});
});
}
addTranslationToTr(trkorr, devclassFilter) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_ADD_LANG_TR", {
iv_trkorr: trkorr,
it_devclass: devclassFilter
});
});
}
trCopy(from_1, to_1) {
return __awaiter(this, arguments, void 0, function* (from, to, doc = false) {
yield this._call("ZTRM_TR_COPY", {
iv_from: from,
iv_to: to,
iv_doc: doc ? 'X' : ' '
});
});
}
addNamespace(namespace, replicense, texts) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_ADD_NAMESPACE", {
iv_namespace: namespace,
iv_replicense: replicense,
it_texts: texts
});
});
}
getR3transInfo() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_R3TRANS_INFO");
return result['evLog'];
});
}
migrateTransport(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_MIGRATE_TRANSPORT", {
iv_trkorr: trkorr
});
return result['evTrmTrkorr'];
});
}
deleteTmsTransport(trkorr, system) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_DEL_TRANSPORT_TMS", {
iv_trkorr: trkorr,
iv_system: system
});
});
}
refreshTransportTmsTxt(trkorr) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_REFRESH_TR_TMS_TXT", {
iv_trkorr: trkorr
});
});
}
getDotAbapgit(devclass) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_DOT_ABAPGIT", {
iv_devclass: devclass
});
return result['evDotAbapgit'];
});
}
getAbapgitSource(devclass) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_ABAPGIT_SOURCE", {
iv_devclass: devclass
});
const sXml = result['evObjects'].toString().replace(/&/g, "&").replace(/-/g, "-");
const oAbapXml = xml.xml2js(sXml, { compact: true });
const objects = oAbapXml['asx:abap']['asx:values']['OBJECTS'].item.map(o => {
return {
pgmid: o['PGMID']['_text'],
object: o['OBJECT']['_text'],
objName: o['OBJ_NAME']['_text'],
fullPath: o['FULL_PATH']['_text']
};
});
return {
zip: result['evZip'],
objects
};
});
}
executePostActivity(data, pre) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_EXECUTE_POST_ACTIVITY", {
iv_data: data,
iv_pre: pre ? 'X' : ''
});
return {
messages: result['etMessages'],
execute: result['evExecute'] === 'X'
};
});
}
getInstalledPackagesBackend() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this._call("ZTRM_GET_INSTALLED_PACKAGES");
const sXml = result['evPackages'].toString().replace(/&/g, "&").replace(/-/g, "-");
const oAbapXml = xml.xml2js(sXml, { compact: true });
return oAbapXml['asx:abap']['asx:values']['PACKAGES'].item.map(o => {
var flatTdevc = [];
if (o['TDEVC'] && o['TDEVC']['TDEVC']) {
if (!Array.isArray(o['TDEVC']['TDEVC'])) {
o['TDEVC']['TDEVC'] = [o['TDEVC']['TDEVC']];
}
flatTdevc = o['TDEVC']['TDEVC'].map((item) => {
const flattened = {};
for (const [key, value] of Object.entries(item)) {
if (typeof value === 'object' && value !== null && '_text' in value) {
flattened[key] = value._text;
}
}
return flattened;
});
}
return {
name: o['NAME']['_text'],
version: o['VERSION']['_text'],
registry: o['REGISTRY']['_text'],
manifest: o['XMANIFEST']['_text'] ? Buffer.from(o['XMANIFEST']['_text'], 'base64').toString('utf8') : undefined,
transport: {
trkorr: o['TRANSPORT']['TRKORR']['_text'],
migration: o['TRANSPORT']['MIGRATION']['_text'] === 'X',
},
tdevc: (0, commons_1.normalize)(flatTdevc),
trkorr: o['TRKORR']['_text']
};
});
});
}
isServerApisAllowed() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this._call("ZTRM_CHECK_AUTH");
return true;
}
catch (e) {
if (e.exceptionType !== 'CALL_FUNCTION_NOT_REMOTE') {
return e;
}
else {
return true;
}
}
});
}
changeTrOwner(trkorr, owner) {
return __awaiter(this, void 0, void 0, function* () {
yield this._call("ZTRM_CHANGE_TR_OWNER", {
iv_trkorr: trkorr,
iv_new_owner: owner
});
});
}
}
exports.RFCClient = RFCClient;
;